123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 |
- <?php
- namespace App\Http\Controllers\Channel\Book;
- use App\Http\Controllers\Channel\BaseController;
- use App\Http\Controllers\Channel\Book\Transformers\BookTransformer;
- use App\Http\Controllers\Channel\Book\Transformers\BookV2Transformer;
- use App\Http\Controllers\Channel\Book\Transformers\ChapterListTransformer;
- use App\Modules\Book\Services\BookCategoryService;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Book\Services\BookSpecialChannelService;
- use App\Modules\Book\Services\BookSubscribleChapterService;
- use App\Modules\Book\Services\ChapterService;
- use App\Modules\Book\Services\SuperiorHistoryBookService;
- use App\Modules\Book\Services\SuperiorNewBookService;
- use App\Modules\Channel\Services\ChannelService;
- use App\Modules\SendOrder\Services\SendOrderService;
- use App\Modules\Channel\Services\CompanySpecialBookService;
- use App\Modules\Channel\Services\ChannelUserService;
- use App\Modules\Channel\Services\CompanyService;
- use App\Modules\Book\Models\BookOrderStatistical;
- use Hashids;
- use Illuminate\Http\Request;
- use Log;
- use Storage;
- use Illuminate\Support\Facades\Cache;
- use Redis;
- use DB;
- class BooksController extends BaseController
- {
-
-
- function getBookInfo(Request $request)
- {
- $bid = $request->has('bid') ? $request->input('bid') : '';
- if (empty($bid)) return response()->error("PARAM_EMPTY");
- $bid = Hashids::decode($bid)[0];
- $hidden_book = env('HIDE_BOOKS');
- if ($hidden_book) {
- $hidden_book_array = explode(',', $hidden_book);
- if (in_array($bid, $hidden_book_array)) {
-
- }
- }
- $data = BookConfigService::getBookById($bid);
- $subscribe_chapter_seq = BookSubscribleChapterService::getSubcribleChapter($bid, $this->getChannelId());
- if ($subscribe_chapter_seq && $subscribe_chapter_seq->subscribe_chapter_id > 0) {
- $data->force_subscribe_chapter_seq = $subscribe_chapter_seq->subscribe_chapter_id;
- }
- $chapter = ChapterService::getChapterInfoByBidAndSeq($bid, $data->force_subscribe_chapter_seq);
- if ($chapter) {
- $data->force_subscribe_cid = $chapter->id;
- $data->force_subscribe_chapter_name = $chapter->name;
- }
- return response()->item(new BookTransformer(), $data);
- }
-
- function getBookDirectory(Request $request)
- {
- $bid = $request->has('bid') ? $request->input('bid') : '';
- if (empty($bid)) return response()->error("PARAM_EMPTY");
- $bid = Hashids::decode($bid)[0];
- $pageSize = $request->has('pageSize') ? (int)$request->input('pageSize') : 15;
- $data = ChapterService::getChapterListsPage($bid, $pageSize);
- $book = BookConfigService::getBookById($bid);
- $subscribe_chapter_seq = BookSubscribleChapterService::getSubcribleChapter($bid, $this->getChannelId());
- $default_subscribe = $book->force_subscribe_chapter_seq;
- $channel_subscribe = 0;
- if ($subscribe_chapter_seq && $subscribe_chapter_seq->subscribe_chapter_id > 0) {
- $channel_subscribe = $subscribe_chapter_seq->subscribe_chapter_id;
- $book->force_subscribe_chapter_seq = $subscribe_chapter_seq->subscribe_chapter_id;
- }
- foreach ($data as &$item) {
- $item->dafault_subscribe = 0;
- $item->channel_subscribe = 0;
- if ($item->sequence == $default_subscribe) {
- $item->dafault_subscribe = 1;
- }
- if ($channel_subscribe && $item->sequence == $channel_subscribe) {
- $item->channel_subscribe = 1;
- }
- if ($item->sequence >= ($book->force_subscribe_chapter_seq)) {
- $item->is_need_subscirbe = 1;
- } else {
- $item->is_need_subscirbe = 0;
- }
- if ($item->id == $book->recommend_cid) {
- $item->is_recommend = 1;
- if (Storage::exists('RecommendChapterPositionWord.txt')) {
- $item->recommend_text = Storage::get('RecommendChapterPositionWord.txt');
- } else {
- $item->recommend_text = '建议此章节生成推广文案(原文转化率好)';
- }
- }
- }
- return response()->pagination(new ChapterListTransformer(), $data);
- }
-
- function getBookCategories(Request $request)
- {
- $res = BookCategoryService::getSecondCategories();
- $male = [];
- $female = [];
- foreach ($res as $v) {
- if ($v->channel_name == '男频') {
- $male[] = ['id' => $v->id, 'name' => $v->category_name];
- }
- if ($v->channel_name == '女频') {
- $female[] = ['id' => $v->id, 'name' => $v->category_name];
- }
- }
- $data = [
- ['id' => 1, 'name' => '男频', 'children' => $male],
- ['id' => 2, 'name' => '女频', 'children' => $female],
- ];
-
- return response()->success($data);
- }
- function getBookCategoriesV2(Request $request)
- {
-
- $female = [['id'=>13,'name'=>'豪门虐情'], ['id'=> 14,'name'=>'婚恋生活'] ,['id'=>18,'name'=>'穿越重生'], ['id'=>22,'name'=>'女生灵异']];
- $male = [
- ['id'=>4,'name'=>'都市艳遇'],
- ['id'=>5,'name'=>'兵王保镖'],
- ['id'=>8,'name'=>'风云职场'],
- ['id'=>12,'name'=>'悬疑灵异']
- ];
- $data = [
- ['id' => 1, 'name' => '男频', 'children' => $male],
- ['id' => 2, 'name' => '女频', 'children' => $female],
- ];
-
- return response()->success($data);
- }
-
- public function getPromotionBookList(Request $request)
- {
- $channel_id = $this->getChannelId();
- $channel_user = ChannelService::getUserIdById($channel_id);
- $channel_user_id = $channel_user->channel_user_id;
- $channel_user_info = ChannelUserService::getById($channel_user_id);
- $bids = [];
- if ($channel_user_info && !empty($channel_user_info->company_id)) {
- $bids = CompanySpecialBookService::getBidByCompany($channel_user_info->company_id);
- }
- $hidden_book = env('HIDE_BOOKS');
- if ($hidden_book) {
- $hidden_book_array = explode(',', $hidden_book);
- $where['hidden_books'] = $hidden_book_array;
- if ($bids) {
- $bids = array_diff($bids, $hidden_book_array);
- }
- }
-
- $where['is_on_shelf'] = [2];
- $category_id = $request->input('category_id');
- if ($request->has('key') && trim($request->input('key'))) $where['key'] = trim($request->input('key'));
- if ($category_id) {
- if ($category_id == 1) {
- $where['channel_name'] = '男频';
- } elseif ($category_id == 2) {
- $where['channel_name'] = '女频';
- } else {
- $where['category_id'] = $category_id;
- }
- }
- $order_field = $request->input('order_field');
- $order_seq = $request->input('order_seq', 'desc');
- if ($order_field) {
- $order = ['book_configs.recommend_index', $order_seq];
- } else {
- $order = ['book_configs.id', $order_seq];
- }
- $data = BookConfigService::getPromotionBooks($where, $bids, $order);
- if (!empty($data)) {
- foreach ($data as $item) {
- $item->promotion_count = SendOrderService::getPromotionCountByBid($item->bid, $channel_id);
- }
- }
- return response()->pagination(new BookTransformer(), $data);
- }
-
- public function getPromotionBookListV2(Request $request)
- {
- $data = [];
- $where = [];
- $orwhere = [];
- $channel_id = $this->getChannelId();
- $data['distribution_channel_id'] = $channel_id;
- $channel_user = ChannelService::getUserIdById($channel_id);
- $channel_user_id = $channel_user->channel_user_id;
- $channel_user_info = ChannelUserService::getById($channel_user_id);
-
- $bids = [];
- if ($channel_user_info && !empty($channel_user_info->company_id)) {
- $bids = CompanySpecialBookService::getBidByCompany($channel_user_info->company_id);
- }
-
- $hidden_book = env('HIDE_BOOKS');
- $whereIn = [];
- if ($hidden_book) {
- $hidden_book_array = explode(',', $hidden_book);
- $whereIn = $hidden_book_array;
- if ($bids) {
- $bids = array_diff($bids, $hidden_book_array);
- }
- }
-
- $sex = $request->get('sex', 0);
- if ($sex && in_array($sex, [1, 2])) {
- $sex_str = ['', '男频', '女频'];
- $where[] = ['book_categories.channel_name', '=', $sex_str[$sex]];
-
- }
-
- $own_score = $request->get('own_score');
- if ($own_score && in_array($own_score, range('A', 'Z'))) {
- $where[] = ['book_channel_scores.score', '=', $own_score];
-
- }
-
- $score = $request->get('recommend_index');
- if ($score && in_array($score, ['A', 'B', 'C', 'S', 'D', 'SS', 'A+']) && !$own_score) {
- $score_number = ['SS' => [100, 110], 'S' => [95, 99], 'A+' => [90, 94], 'A' => [85, 89], 'B' => [80, 84], 'C' => [70, 79], 'D' => [0, 69]];
- $where[] = ['book_configs.recommend_index', '>=', $score_number[$score][0]];
-
- $where[] = ['book_configs.recommend_index', '<=', $score_number[$score][1]];
-
- }
-
- $onshelf = $request->get('onshelf');
- if ($onshelf && in_array($onshelf, [7, 30, 90])) {
- $where[] = ['book_configs.created_at', '>=', date('Y-m-d H:i:s', time() - $onshelf * 86400)];
-
- }
-
- $category_id = $request->get('category_id');
- if ($category_id) {
- $where[] = ['books.category_id', '=', $category_id];
-
- }
-
- $size = $request->get('size');
- if ($size && in_array($size, [1, 2, 3, 4, 5, 6])) {
- $size_arr = [[], [200, -1], [100, 200], [20, 100], [0, 20], [20, -1], [0, 20]];
- $where[] = ['books.size', '>=', $size_arr[$size][0] * 10000];
-
- if ($size_arr[$size][1] != -1) {
- $where[] = ['books.size', '<=', $size_arr[$size][1] * 10000];
-
- }
- }
-
- $status = $request->get('status', -1);
- if (in_array($status, [0, 1])) {
- $where[] = ['books.status', '=', $status];
-
- }
- $key = $request->get('key','');
- if ($key) {
- $key = trim($key);
-
-
- }
-
-
-
- $Superior = $request->input('superior',0);
-
-
- $whereDeedIn = [];
- $superior_lib = '';
-
- $order_field = $request->get('order_field','');
- $order_type = $request->get('order_type', 'desc');
-
-
- if ($order_field && in_array($order_field, ['order_index','recommend_index', 'total_send_order_sum', 'week_send_order_sum', 'read_deep', 'channel_send_order_sum', 'own_score'])) {
- $data['order_field'] = $order_field;
- if (!in_array($order_type, ['asc', 'desc'])) {
- $order_type = 'desc';
- }
- $data['order_type'] = $order_type;
- }
- if($Superior==1){
- $history_lib = SuperiorHistoryBookService::getSupriorBidList();
-
- $whereDeedIn=count($history_lib)>0?$history_lib:[0];
- $superior_lib = 'superior_history_book';
- $data['order_field'] = 'order_index';
-
- }
- if($Superior==2){
- $new_lib = SuperiorNewBookService::getSupriorBidList();
-
- $whereDeedIn=count($new_lib)>0?$new_lib:['-1'];
- $superior_lib = 'superior_new_book';
- $data['order_field'] = 'superior_new_book.order_index';
- $data['order_type'] = 'desc';
-
- }
- if($Superior==3){
- $where[]=['book_configs.is_current_week_promotion','=',1];
- $data['order_field'] = 'recommend_index';
- $data['order_type'] = 'desc';
- }
- if($Superior ==1) $data['order_type'] = 'asc';
- $result = BookConfigService::getPromotionBooksV2($where, $whereIn, $data, $bids, $whereDeedIn,$superior_lib,$key);
- if ($result) {
- $total_send_order = (int)DB::table('send_orders')->max('id');
- $week_send_order = (int)DB::table('send_orders')->where('created_at', '>=', date('Y-m-d', strtotime('-7 day')))->count();
- foreach ($result as &$v) {
- if ($total_send_order > 0) {
- $rate = (round($v->total_send_order_sum / $total_send_order, 4)) * 10000;
- $v->total_send_order_sum = $this->sendOrderIndex($rate);
- }
- $v->week_send_order_bid = $v->week_send_order_sum;
- if ($week_send_order > 0) {
- $rate = (round($v->week_send_order_sum / $week_send_order, 4)) * 10000;
- $v->week_send_order_sum = $this->sendOrderIndex($rate);
- }
- $v->week_send_order = $week_send_order;
- }
- }
- return response()->pagination(new BookV2Transformer(), $result);
- }
- private function sendOrderIndex($rate)
- {
- if ($rate < 1) {
- return 60;
- }
- if ($rate >= 200) {
- return 100;
- } elseif ($rate >= 100) {
- return 98;
- } elseif ($rate >= 60) {
- return 96;
- } else {
- if ($rate >= 10) {
- return 96 - ceil((60 - $rate) / 10) * 2;
- } else {
- return 96 - 12 - ceil((10 - $rate)) * 2;
- }
- }
- }
-
- function getRanks(Request $request)
- {
- $type = $request->input('type');
- if (!$type) {
- return response()->error("PARAM_EMPTY");
- }
- $flush = $request->input('flush');
- $book = [];
- $channel_user_id = $this->getChannelUserId();
- $channel_user_info = ChannelUserService::getById($channel_user_id);
- $company = CompanyService::getCompany($channel_user_info->company_id);
- $on_shelf = [2];
- $important = 'unimportant';
- $important_books = null;
- if ($company && $company->is_important == 1) {
- $bids = [];
- $bids = CompanySpecialBookService::getBidByCompany($channel_user_info->company_id);
-
- $hidden_book = env('HIDE_BOOKS');
- if ($hidden_book) {
- $hidden_book_array = explode(',', $hidden_book);
- if ($bids) {
- $bids = array_diff($bids, $hidden_book_array);
- }
- }
- if ($bids) {
- $important_books = BookConfigService::getBooksByIds($bids, ['book_configs.created_at', 'desc']);
- }
- $important = 'important';
- }
- $key = 'channel_book_rank_' . $type . '_' . $important;
- if ($flush == 1) {
- Cache::forget($key);
- }
- $cache = Cache::get($key, []);
- if ($cache) {
- $cache = json_decode($cache);
- return response()->collection(new BookTransformer(), $cache);
- }
- switch ($type) {
- case 'new':
- $order = [['book_configs.created_at', 'desc'], ['book_configs.recommend_index', 'desc']];
- $book = BookConfigService::getBooksNoPage([], $order, $on_shelf, 20);
- $book = $this->sortBooks($important_books, $book);
- break;
- case 'sale_total';
- $start = date('Y-m') . '-01';
- $end = date('Y-m-d');
- $bids = BookOrderStatistical::getTopSaleBooks($start, $end, $on_shelf, 20);
- $id = [];
- foreach ($bids as $v) {
- $id[] = $v->bid;
- }
- if ($id) {
- $book = BookConfigService::getBooksByIds($id);
- }
- break;
- case 'sale_half_month':
- $start = date('Y-m-d', strtotime('-14 day'));
- $end = date('Y-m-d');
- $bids = BookOrderStatistical::getTopSaleBooks($start, $end, $on_shelf, 10);
- $id = [];
- foreach ($bids as $v) {
- $id[] = $v->bid;
- }
- if ($id) {
- $book = BookConfigService::getBooksByIds($id);
- }
- break;
- case 'recommend':
- $id_arr = env('CHANNEL_RECOMMEND_BIDS', '');
- $redis_field = 'unimportant';
- if ($important == 'important') {
- $redis_field = 'important';
- $id_arr = env('CHANNEL_RECOMMEND_IMPORTANT_BIDS', '');
- }
- $ids = Redis::hget('channel_recomm_books', $redis_field);
- if ($ids) $id_arr = $ids;
- if ($id_arr) {
- $book = BookConfigService::getBooksByIds(explode(',', $id_arr));
- }
- break;
- }
- if ($book) {
- Cache::put($key, json_encode($book), 60);
- }
- return response()->collection(new BookTransformer(), $book);
- }
- private function sortBooks($book1, $book2)
- {
- if (!$book1)
- return $book2;
- $book1 = $book1->filter(function ($value) {
- return $value->is_on_shelf = 1;
- });
- return $book2->concat($book1)->sortByDesc('created_at')->slice(0, 20);
- }
- }
|