BookConfigService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/4
  6. * Time: 下午1:43
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\BookCombinationConfig;
  10. use App\Modules\Book\Models\BookConfig;
  11. use App\Modules\Book\Models\BookCopyright;
  12. use App\Modules\Product\Services\ProductService;
  13. use App\Modules\Book\Models\Book;
  14. use App\Modules\Book\Models\BookKeyword;
  15. use App\Modules\Book\Models\FreeBook;
  16. use App\Modules\Book\Models\FreeBookConfig;
  17. use App\Modules\Book\Models\QappUserSearchBookLog;
  18. use App\Modules\Trade\Models\Order;
  19. use Illuminate\Support\Facades\DB;
  20. use phpDocumentor\Reflection\Types\This;
  21. use Redis;
  22. use Vinkla\Hashids\Facades\Hashids;
  23. class BookConfigService
  24. {
  25. /**
  26. * 根据id获取图书
  27. * @param $bid
  28. * @return mixed
  29. */
  30. public static function getBookById($bid)
  31. {
  32. return BookConfig::getBookById($bid);
  33. }
  34. /**
  35. * 获取图书组合信息
  36. * @param $bid
  37. * @return array
  38. */
  39. public static function getCombinationInfo($bid)
  40. {
  41. $info = BookCombinationConfig::where('bid', $bid)->where('is_enable', 1)->select('bid_group')->first();
  42. if ($info) return explode(',', $info->bid_group);
  43. return [];
  44. }
  45. /**
  46. * 根据bid数组获取多本图书
  47. * @param $where
  48. * @param null $order
  49. * @return mixed
  50. */
  51. public static function getBooksByIds(array $where, $order = [], $is_external_shelf = true)
  52. {
  53. if (empty($where)) {
  54. return (object)array();
  55. }
  56. if ($order)
  57. $res = BookConfig::getBooksByIds($where, $order, $is_external_shelf);
  58. else
  59. $res = BookConfig::getBooksByIds($where, [], $is_external_shelf);
  60. return $res;
  61. }
  62. public static function getBookLists(array $where, $order = [], $is_external_shelf = true)
  63. {
  64. if (empty($where)) {
  65. return (object)array();
  66. }
  67. if ($order) {
  68. $res = BookConfig::getBookLists($where, $order, $is_external_shelf);
  69. } else {
  70. $res = BookConfig::getBookLists($where, [], $is_external_shelf);
  71. }
  72. return $res;
  73. }
  74. /**
  75. *
  76. * 根据条件获取图书
  77. * @param array $where ['key'=>'根据关键词查询','category_id'=>'根据分类id查询','is_on_shelf'=>上下架查询,'channel_name'=>'频道查询(男频女频)']
  78. * @param array $order 排序 默认是bid排序
  79. * @param int $page_size
  80. * @return mixed
  81. */
  82. public static function getBooks(array $where, array $order = [], $page_size = 15)
  83. {
  84. return BookConfig::getBooks($where, $order, $page_size);
  85. }
  86. /**
  87. * 获取阅读完的推荐
  88. * @param $category_id
  89. * @param int $num
  90. * @return mixed
  91. */
  92. public static function getRecommendBooks($bid, $category_id, $num = 4)
  93. {
  94. return BookConfig::getRecommendBooks($bid, $category_id, $num);
  95. }
  96. /**
  97. * 获取阅读完的推荐(快应用)
  98. * @param $category_id
  99. * @param int $num
  100. * @return mixed
  101. */
  102. public static function getQuickAppRecommendBooks($bid, $category_id, $num = 4)
  103. {
  104. return BookConfig::getQuickAppRecommendBooks($bid, $category_id, $num);
  105. }
  106. public static function getSimpleBooksByIds(array $ids)
  107. {
  108. $str = implode(',', $ids);
  109. $field = 'bid,' . $str;
  110. return BookConfig::whereIn('bid', $ids)->select('bid', 'book_name')->orderBy(DB::raw('field(' . $field . ')'))->get();
  111. }
  112. public static function findBookKeywords(bool $is_all = false)
  113. {
  114. $sql = BookKeyword::where('status', 1)->orderBy('sequence');
  115. if ($is_all) {
  116. return $sql->get();
  117. } else {
  118. return $sql->paginate(10);
  119. }
  120. }
  121. public static function saveUserSearchLog(string $words, int $uid)
  122. {
  123. QappUserSearchBookLog::create([
  124. 'uid' => $uid,
  125. 'words' => $words,
  126. ]);
  127. }
  128. /**
  129. * @return FreeBookConfig
  130. */
  131. public static function findFreeBookConfig(int $sex)
  132. {
  133. return FreeBookConfig::where(['sex' => $sex, 'is_enabled' => 1])
  134. ->where('end_time', '>=', now())
  135. ->orderBy('end_time')
  136. ->first();
  137. }
  138. /**
  139. * 查找限免书籍
  140. * @return array
  141. */
  142. public static function findFreeBooks(int $sex)
  143. {
  144. $config = self::findFreeBookConfig($sex);
  145. \Log::info('return_empty_free_books:' . $sex);
  146. \Log::info($config);
  147. if ($config) {
  148. $free_books = FreeBook::where('config_id', $config->id)
  149. ->where('is_enabled', 1)
  150. ->get();
  151. $bids = $free_books->pluck('bid')->all();
  152. $book_configs = BookConfig::whereIn('bid', $bids)
  153. ->where('is_on_shelf', 2)
  154. ->select('bid', 'book_name', 'cover')
  155. ->get();
  156. $books = Book::whereIn('id', $bids)->select('id', 'intro')->get();
  157. \Log::info('return_empty_data:');
  158. \Log::info($books);
  159. $book_list = $book_configs->transform(function ($item) use ($books) {
  160. $book = $books->where('id', $item->bid)->first();
  161. return [
  162. 'book_id' => Hashids::encode($item->bid),
  163. 'cover_url' => $item->cover,
  164. 'book_name' => $item->book_name,
  165. 'intro' => $book->intro,
  166. ];
  167. })->all();
  168. return [
  169. 'title' => $config->name,
  170. 'end_time' => $config->end_time,
  171. 'list' => $book_list,
  172. ];
  173. }
  174. return [];
  175. }
  176. /**
  177. * 判断书籍是否限免
  178. * @return bool
  179. */
  180. public static function judgeBookIsFree(int $bid)
  181. {
  182. $ids = [];
  183. foreach ([1, 2] as $sex) {
  184. $config = self::findFreeBookConfig($sex);
  185. if ($config) {
  186. $ids[] = $config->id;
  187. }
  188. }
  189. return FreeBook::where('bid', $bid)
  190. ->whereIn('config_id', $ids)
  191. ->where('is_enabled', 1)->select('id')->first();
  192. }
  193. public static function getByBidNoFilter($bid)
  194. {
  195. return FreeBook::join('free_book_config', 'free_book_config.id', '=', 'free_books.config_id')
  196. ->where('bid', $bid)
  197. ->select('free_books.id', 'end_time')
  198. ->where('end_time', '<', date('Y-m-d H:i:s'))
  199. ->orderBy('free_book_config.end_time', 'desc')
  200. ->first();
  201. }
  202. public static function chargeStats($id, $amount, $uid)
  203. {
  204. if (!Redis::Sismember('qapp:free:virtual:uids' . $id, $uid)) {
  205. return;
  206. }
  207. $now = date('Y-m-d');
  208. $amount = $amount * 100;
  209. Redis::hincrby('qapp:book:free:charge:' . $id, $now, $amount);
  210. #Redis::sadd('qapp:free:charge'.$now,$id);
  211. Redis::sadd('qapp:free:actuality' . $now, $id);
  212. Redis::sadd('qapp:free:charge:uids' . $now . $id, $uid);
  213. }
  214. public static function getBookByField($bids, $field)
  215. {
  216. if (!$bids || !$field) return null;
  217. return BookConfig::join('books', 'books.id', '=', 'book_configs.bid')->
  218. whereIn('bid', $bids)->select($field)->get();
  219. }
  220. /**
  221. * 根据书籍bid去除无用bid
  222. * name: getAvailableBIdsbyBids
  223. * @param $bids
  224. * @param mixed $channel_id
  225. * @param mixed $is_external_shelf
  226. * @return array
  227. * date 2022/09/20 10:39
  228. */
  229. public static function getAvailableBIdsbyBids($bids, $channel_id = 0, $is_external_shelf = true)
  230. {
  231. if (empty($bids)) {
  232. return [];
  233. }
  234. $res = BookConfig::whereIn('bid', $bids);
  235. // if($channel_id == config('qapp_public_package_channel')){
  236. // $res->whereNotIn('cp_source',getHiddenCp());
  237. // }else{
  238. // $res->whereNotIn('cp_source',array_merge(getHiddenCp(),['lianshang']));
  239. // }
  240. $res->whereNotIn('cp_source', getHiddenCp($channel_id, 2));
  241. if (config('app.is_shen_he') == 1){
  242. $res->where('cp_source','ycsd');
  243. }
  244. if ($is_external_shelf) {
  245. $res->where('is_on_shelf', 2);
  246. } else {
  247. $res->whereIn('is_on_shelf', [1, 2]);
  248. }
  249. return $res->pluck("bid")->toArray();
  250. }
  251. /***
  252. * 推荐位书籍检测和补齐
  253. * name: getCheckBooks
  254. * @param $bid_list
  255. * @param $channel
  256. * @param $package
  257. * @param $is_author
  258. * @return mixed
  259. * date 2022/10/26 10:11
  260. */
  261. public static function getCheckBooks($bid_list, $channel, $package, $is_author)
  262. {
  263. $hidden_cp = getHiddenCp($package);
  264. // if(!is_public_package($package)){
  265. // $hidden_cp = array_merge($hidden_cp,['lianshang']);
  266. // }
  267. //获取书本数量
  268. $count = count($bid_list);
  269. if (!$is_author) {
  270. $where = [
  271. ['book_configs.charge_type', '!=', 'BOOK'],
  272. ['book_configs.cp_source', '=', 'ycsd'],
  273. ];
  274. } else {
  275. $where = [
  276. ['book_configs.charge_type', '!=', 'BOOK'],
  277. ];
  278. }
  279. //获取书籍交集bid,过滤掉不符合要求的书
  280. $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  281. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  282. ->whereIn('book_configs.bid', $bid_list)
  283. ->where('book_configs.is_on_shelf', 2)
  284. ->where($where)
  285. ->whereNotIn('book_configs.cp_source', $hidden_cp)
  286. ->where('book_categories.pid', $channel)
  287. ->pluck('book_configs.bid')->all();
  288. $book_count = count($bid_list);
  289. if ($book_count === $count) {
  290. return $bid_list;
  291. }
  292. $supplement_count = $count - $book_count;
  293. //获取随机的有效的书籍bid
  294. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  295. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  296. ->where('book_configs.is_on_shelf', 2)
  297. ->where($where)
  298. ->whereNotIn('book_configs.cp_source', $hidden_cp)
  299. // ->where('book_configs.is_high_quality',1)
  300. ->where('book_categories.pid', $channel)
  301. ->orderBy('book_configs.is_high_quality', 'desc')
  302. // ->inRandomOrder()
  303. ->orderBy('book_configs.recommend_index', 'desc')
  304. ->limit($supplement_count)
  305. ->pluck('book_configs.bid')->all();
  306. return array_filter(array_merge($bid_list, $rand_bid));
  307. }
  308. public static function getRecommendBids($package = '', $channel = 1, $no_in_bid = [], $limit = 36, $is_on_shelf = true)
  309. {
  310. DB::enableQueryLog();
  311. $where = [
  312. ['book_configs.charge_type', '!=', 'BOOK'],
  313. ];
  314. //获取书籍交集bid,过滤掉不符合要求的书
  315. $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  316. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  317. ->distinct("books.name")
  318. ->where($where)
  319. ->whereNotIn('book_configs.cp_source', getHiddenCp($package))
  320. ->where('book_categories.pid', $channel);
  321. if ($is_on_shelf) {
  322. $bid_list->where('book_configs.is_on_shelf', 2);
  323. } else {
  324. $bid_list->whereIn('book_configs.is_on_shelf', [1, 2]);
  325. }
  326. if (config('app.is_shen_he') == 1){
  327. $bid_list->where('book_configs.cp_source','ycsd');
  328. }
  329. if (!empty($no_in_bid)) {
  330. $bid_list->whereNotIn('book_configs.bid', $no_in_bid);
  331. }
  332. return $bid_list->orderBy('recommend_index', 'desc')->orderBy("book_configs.is_high_quality", 'desc')->orderBy('books.id', 'asc')->limit($limit)->pluck('book_configs.bid')->toArray();
  333. }
  334. /***
  335. * 书籍授权情况 2黑名单 1白名单 其他 无特殊添加
  336. * name: bookCopyright
  337. * @param mixed $bid
  338. * @param mixed $distribution_channel_id
  339. * date 2022/11/14 17:16
  340. */
  341. public static function bookCopyright($bid, $distribution_channel_id = 0)
  342. {
  343. if ($distribution_channel_id < 1) {
  344. return 0;
  345. }
  346. return BookCopyright::where('distribution_channel_id', $distribution_channel_id)
  347. ->where('bid', $bid)
  348. ->where('is_enabled', 1)
  349. ->where('platform', "qapp")
  350. ->value('type');
  351. }
  352. public static function getNewRecommendBids($package, $channel, $bids = [], $limit = 12, $is_on_shelf = true)
  353. {
  354. $begin_time = date('Y-m-d', strtotime('-2 month'));
  355. $end_time = date('Y-m-d');
  356. $where = " and book_configs.charge_type = 'CHAPTER' and book_categories.pid= {$channel}";
  357. $hide = getHiddenCp($package);
  358. if ($hide) {
  359. $where .= " and book_configs.cp_source not in ( '" . implode("','", $hide) . "')";
  360. }
  361. if ($is_on_shelf) {
  362. $where .= " and book_configs.is_on_shelf = 2";
  363. } else {
  364. $where .= " and book_configs.is_on_shelf in (1,2)";
  365. }
  366. if (!empty($bids)) {
  367. $where .= " and books.id not in (" . implode(",", $bids) . ")";
  368. }
  369. $sql = "select book_configs.bid from book_promotion_day_stats
  370. INNER JOIN books on books.id=book_promotion_day_stats.bid
  371. INNER JOIN book_configs on book_configs.bid = books.id
  372. INNER JOIN book_categories ON book_categories.id=books.category_id
  373. where date >='{$begin_time}' and date <= '{$end_time}' {$where} group by bid order by sum(rechareg_amount) desc limit {$limit}";
  374. // $bids = DB::select($sql);
  375. // echo "<pre><hr>";
  376. // var_export($bids);
  377. // echo "<hr>";
  378. // var_export($sql);
  379. // die();
  380. }
  381. /***
  382. * 获取排行榜数据
  383. * name: getRankList
  384. * @param $package
  385. * @param mixed $sex 1:男频 2:女频
  386. * @param int $limit 条数默认30
  387. * date 2022/11/15 14:36
  388. */
  389. public static function getRankList($package, $sex = 1, $limit = 30)
  390. {
  391. if ($sex == 1){
  392. return [14928,14500,61020,14927,14016,14021,19409,14423,61596,62297,16451,15103,14022,14793,14351,17652,63721,63417,62781,63482,13300,14297,14072,13928,13879,16438,14312,16712,13929,14408,19993,14299,59580,60623,62666,19675,65187,15978,59784,58687,61944,13840,13286,16236,64284,58969,14811,14006,13254,13275,14000,9744,13266,15251,62875,6644,16434,13291,16431,59687,13994,16256,14272,19958,16435,13271,61564,13878,14302,61945,3483,13281,16453,59723,16443,13992,16455,9753,11408,62461,59389,13260,13257,17954,13280,11457,13284,13282,7680,16448,64225,21910,2939,22376,16442,13287,16440,16427,60827,21700,13361,59688,2079,20416,7956,16545,10418,13930,13867,64325,14300,16445,17242,14308,13288,19449,10537,12393,13250,13888,16454,14926,16484,13278,21139,61941,10417,21757,64239,13265,13272,13253,60175,8461,21376,2941,22056,13269,14806,14097,14303,59388,22148,16447,2053,13258,16433,16452,16476,2936,59677,20027,22157,16566,16441,17689,16251,13285,63989,2928,14925,2942,13931,13264,2945,20236,21999,46203,5618,16842,64340,59385,14295,64346,13884,61684,12480,59728,16798,94,20711,20072,20368,8253,2915,16822,20046,2993,12103,17268,12494,22153,32802,3542,61567,20837,16436,62355,64329,16542];
  393. }else{
  394. return [11316,11672,11679,11680,11681,11999,12331,12462,12464,12468,12949,13421,14220,14349,14620,15221,15537,15720,16301,17055,19382,58195,58593,59004,59579,61012,61425,61699,62175,62538,62865,64729,1389,2061,4938,6043,8141,8295,8476,9893,10682,10721,11063,11632,11648,11653,11658,11660,11661,11662,11928,12224,12281,12283,12470,12471,12689,12701,12739,13023,13038,13081,13088,13095,13113,13125,13154,13155,13157,13169,13173,13175,13176,13177,13183,13189,13190,13192,13193,13201,13207,13397,13439,13520,13593,13608,13853,14083,14318,14417,14684,14697,14709,14744,14892,14924,15255,15281,15368,15431,15779,15997,16005,16035,16112,16392,16395,16731,16951,17379,18817,19217,19249,19411,19430,58223,58236,58281,58344,58469,58481,58483,58731,58763,58776,58790,58869,58946,58950,59075,59214,59242,59296,59378,59512,60135,60610,60902,61079,61554,62202,62878,63333,63884,64027,1,11663,11664,11665,13182,3588,3697,6063,9525,10823,10961,11670,12474,13082,13092,13093,13104,13160,13166,13324,13374,13761,13842,14421,14561,14664,14890,15145,15282,15806,16053,16411,19381,24366,58216,58396,58864,58906,59549,60636,60672,60736,62473,62537,62864,62935,63413,64808,11671,12367,16289,19220,61958,61979,59546,11471];
  395. }
  396. $hide = getHiddenCp($package);
  397. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  398. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  399. ->where('book_configs.is_on_shelf', 2)
  400. ->where('book_configs.charge_type', "CHAPTER")
  401. ->whereNotIn('book_configs.cp_source', $hide)
  402. ->where('book_categories.pid', $sex);
  403. if (config('app.is_shen_he') == 1){
  404. $bids->where('book_configs.cp_source','ycsd');
  405. }
  406. $bids->pluck('book_configs.bid')->all();
  407. if (empty($bids)) {
  408. return [];
  409. }
  410. $begin_time = date('Y-m-d H:i:s', strtotime('-3 month'));
  411. $end_time = date('Y-m-d H:i:s');
  412. return Order::where("from_bid", ">", 0)->whereIn("from_bid", $bids)
  413. ->where('created_at', ">=", $begin_time)->where('created_at', "<=", $end_time)
  414. ->where('status','PAID')
  415. ->groupBy('from_bid')
  416. ->orderByRaw("SUM(price) desc")
  417. ->limit($limit)
  418. ->pluck('from_bid')
  419. ->all();
  420. }
  421. public static function bookReplenish($bidArr, $replenishArr,$count = 6)
  422. {
  423. if(count($bidArr) == $count){
  424. return [$bidArr,$replenishArr];
  425. }
  426. $bidArr = array_merge($bidArr,array_splice($replenishArr,0,$count-count($bidArr)));
  427. return [$bidArr,$replenishArr];
  428. }
  429. public static function HomePageRecomBookCheck($hotBids, $liveBids, $recomBids, $newBids,$package_id, $package,$channel, $is_external_shelf = true){
  430. $bids = BookConfigService::getAvailableBIdsbyBids(array_merge($hotBids, $liveBids, $recomBids, $newBids), $package_id,$is_external_shelf);
  431. $data = BookConfigService::getRecommendBids($package, $channel,$bids, 30);
  432. if (count($bids) < 1) {
  433. $data = array_chunk($data, 6);
  434. $hotBids = array_shift($data);
  435. $liveBids = array_shift($data);
  436. $recomBids = array_shift($data);
  437. $newBids = array_shift($data);
  438. } else {
  439. [$hotBids, $data] = self::bookReplenish(array_intersect($bids, $hotBids), $data);
  440. [$liveBids, $data] = self::bookReplenish(array_intersect($bids, $liveBids), $data);
  441. [$recomBids, $data] = self::bookReplenish(array_intersect($bids, $recomBids), $data);
  442. [$newBids, $data] = self::bookReplenish(array_intersect($bids, $newBids), $data);
  443. }
  444. return [$hotBids,$liveBids,$recomBids,$newBids];
  445. }
  446. }