BookConfig.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <?php
  2. namespace App\Modules\Book\Models;
  3. use App\Modules\Book\Services\BookRoleService;
  4. use App\Modules\Book\Services\BookTagsService;
  5. use App\Modules\User\Services\ReadRecordService;
  6. use App\Modules\Book\Services\BookConfigService;
  7. use App\Modules\Channel\Services\ChannelService;
  8. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  9. use DB;
  10. use Hashids;
  11. use Illuminate\Database\Eloquent\Model;
  12. use App\Modules\User\Models\User;
  13. use Log;
  14. use Redis;
  15. class BookConfig extends Model
  16. {
  17. protected $table = 'book_configs';
  18. protected $fillable = [
  19. 'bid', 'force_subscribe_chapter_seq', 'price', 'cover', 'book_name',
  20. 'copyright', 'charge_type', 'hot', 'is_on_shelf', 'source_domain', 'recommend_index', 'roles', 'test_status', 'plan_push_user_num', 'test_update_time',
  21. 'is_show_index_content', 'click_count', 'promotion_domain', 'copyright_limit_data', 'recommend_cid', 'is_high_quality', 'vip_seq', 'editor_recommend', 'is_current_week_promotion'
  22. ];
  23. /**
  24. * 根据条件获取图书
  25. * @param array $where
  26. * @param array $order
  27. * @param int $page_size
  28. * @return mixed
  29. */
  30. public static function getBooks(array $where = [], array $order = [], $page_size = 15)
  31. {
  32. if (!$order) {
  33. $order = ['recommend_index', 'desc'];
  34. }
  35. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  36. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  37. ->select(
  38. 'book_configs.bid',
  39. 'book_configs.roles',
  40. 'book_configs.force_subscribe_chapter_seq',
  41. 'book_configs.cp_source',
  42. 'book_configs.vip_seq',
  43. 'book_configs.price',
  44. 'book_configs.cover',
  45. 'book_configs.book_name',
  46. 'book_configs.copyright',
  47. 'book_configs.charge_type',
  48. 'book_configs.is_on_shelf',
  49. 'books.author',
  50. 'books.intro',
  51. 'book_categories.category_name',
  52. 'category_id',
  53. 'status',
  54. 'chapter_count',
  55. 'book_configs.click_count',
  56. 'first_cid',
  57. 'last_cid',
  58. 'books.size',
  59. 'last_chapter',
  60. 'books.keyword',
  61. 'book_configs.recommend_index',
  62. 'book_configs.is_show_index_content',
  63. 'book_configs.product_id',
  64. 'book_categories.channel_name',
  65. 'books.last_cid',
  66. 'books.last_chapter',
  67. 'book_configs.product_id',
  68. 'book_configs.copyright_limit_data',
  69. 'books.updated_at as last_update_time',
  70. 'book_configs.promotion_domain',
  71. 'books.name as old_name',
  72. 'book_configs.recommend_cid',
  73. 'book_configs.is_high_quality',
  74. 'is_current_week_promotion'
  75. );
  76. if ($where) {
  77. foreach ($where as $key => $v) {
  78. //关键词查询
  79. if ($key == 'key' && $v) {
  80. $res = $res->where(function ($query) use ($v) {
  81. $query->where('book_configs.book_name', 'like', '%' . $v . '%');
  82. if (mb_strlen($v) <= 5) {
  83. $roles_bids = BookRoleService::getBidsByRole($v);
  84. if (count($roles_bids) > 0) {
  85. $query->orWhereIn('book_configs.bid', $roles_bids);
  86. }
  87. }
  88. });
  89. }
  90. //分类id查询
  91. if ($key == 'category_id' && $v) {
  92. $res = $res->where('books.category_id', '=', $v);
  93. }
  94. //上架查询
  95. if ($key == 'is_on_shelf' && $v != '') {
  96. if (is_array($v)) {
  97. $res = $res->whereIn('is_on_shelf', $v);
  98. } else {
  99. $res = $res->where('is_on_shelf', '=', $v);
  100. }
  101. }
  102. //频道查询
  103. if ($key == 'channel_name' && $v) {
  104. $res = $res->where('book_categories.channel_name', '=', $v);
  105. }
  106. if ($key == 'status') {
  107. $res = $res->where('books.status', '=', $v);
  108. }
  109. if ($key == 'author') {
  110. $res = $res->where('books.author', 'like', '%' . $v . '%');
  111. }
  112. if ($key == 'roles') {
  113. $res = $res->where('book_configs.roles', 'like', '%' . $v . '%');
  114. }
  115. //旧书名查询
  116. if ($key == 'old_name') {
  117. $res = $res->where('books.name', 'like', '%' . $v . '%');
  118. }
  119. //版权日期查询
  120. if ($key == 'copy_right_date') {
  121. if (is_array($v)) {
  122. $res = $res->whereBetween('book_configs.copyright_limit_data', $v);
  123. } else {
  124. $res = $res->where('book_configs.copyright_limit_data', '<=', $v);
  125. }
  126. }
  127. if ($key == 'domain') {
  128. $res = $res->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
  129. }
  130. if ($key == 'bid') {
  131. $res = $res->where('book_configs.bid', '=', $v);
  132. }
  133. if ($key == 'is_high_quality') {
  134. $res = $res->where('book_configs.is_high_quality', '=', $v);
  135. }
  136. if ($key == 'charge_type') {
  137. $res = $res->where('book_configs.charge_type', '=', $v);
  138. }
  139. if ($key == 'firstChapterContent') {
  140. $res = $res->join('chapters', function ($query) use ($v) {
  141. $query->on('book_configs.bid', '=', 'chapters.bid')
  142. ->where('chapters.sequence', 1);
  143. })->where('chapters.content', 'like', '%' . $v . '%');
  144. }
  145. if ($key == 'tags') {
  146. $tags_filter = BookTagsService::getSearchBooks($v);
  147. $res->whereIn('book_configs.bid', $tags_filter);
  148. }
  149. if ($key == 'is_current_week_promotion') {
  150. $res->where('book_configs.is_current_week_promotion', $v);
  151. }
  152. if($key == "cp_source"){
  153. $res = $res->where('book_configs.cp_source', '=', $v);
  154. }
  155. }
  156. }
  157. // if(isset($where['channel_id']) && is_public_package_channel_id($where['channel_id'])){
  158. // $res->whereNotIn('book_configs.cp_source',getHiddenCp());
  159. // }else{
  160. // $res->whereNotIn('book_configs.cp_source',array_merge(getHiddenCp(),['lianshang']));
  161. // }
  162. $res->whereNotIn('book_configs.cp_source',getHiddenCp($where['channel_id']??0,2));
  163. return $res->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
  164. }
  165. /**
  166. * 根据id数组获取图书信息
  167. * @param array $bid_arr
  168. * @param array $order
  169. * @return mixed
  170. */
  171. public static function getBooksByIds(array $bid_arr, array $order = [], $is_external_shelf = true)
  172. {
  173. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  174. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  175. ->select(
  176. 'book_configs.bid',
  177. 'book_configs.force_subscribe_chapter_seq',
  178. 'book_configs.vip_seq',
  179. 'book_configs.price',
  180. 'book_configs.is_on_shelf',
  181. 'book_configs.cover',
  182. 'book_configs.book_name',
  183. 'book_configs.copyright',
  184. 'book_configs.charge_type',
  185. 'book_configs.is_on_shelf',
  186. 'book_configs.cp_source',
  187. 'books.author',
  188. 'books.intro',
  189. 'book_categories.category_name',
  190. 'category_id',
  191. 'status',
  192. 'chapter_count',
  193. 'book_configs.click_count',
  194. 'first_cid',
  195. 'last_cid',
  196. 'size',
  197. 'last_chapter',
  198. 'books.keyword',
  199. 'book_configs.recommend_index',
  200. 'book_configs.is_show_index_content',
  201. 'book_configs.product_id',
  202. 'book_categories.channel_name',
  203. 'books.last_cid',
  204. 'books.last_chapter',
  205. 'book_configs.product_id',
  206. 'books.updated_at as last_update_time',
  207. 'book_configs.copyright_limit_data',
  208. 'book_configs.promotion_domain',
  209. 'books.name as old_name',
  210. 'book_configs.recommend_cid'
  211. )
  212. ->whereIn('book_configs.bid', $bid_arr);
  213. if($is_external_shelf) $res->where('is_on_shelf',2);// 默认外部上架
  214. if ($order) {
  215. $res->orderBy($order[0], $order[1]);
  216. } else {
  217. $str = implode(',', $bid_arr);
  218. $field = 'bid,' . $str;
  219. $res->orderBy(DB::raw('field(' . $field . ')'));
  220. }
  221. return $res->limit(30)->get();
  222. }
  223. public static function getBookLists(array $where, array $order = [], $is_external_shelf = true)
  224. {
  225. if (empty($where)) {
  226. return [];
  227. }
  228. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  229. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  230. ->select(
  231. 'book_configs.bid',
  232. 'book_configs.force_subscribe_chapter_seq',
  233. 'book_configs.vip_seq',
  234. 'book_configs.price',
  235. 'book_configs.is_on_shelf',
  236. 'book_configs.cover',
  237. 'book_configs.book_name',
  238. 'book_configs.copyright',
  239. 'book_configs.charge_type',
  240. 'book_configs.is_on_shelf',
  241. 'books.author',
  242. 'books.intro',
  243. 'book_categories.category_name',
  244. 'category_id',
  245. 'status',
  246. 'chapter_count',
  247. 'book_configs.click_count',
  248. 'first_cid',
  249. 'last_cid',
  250. 'size',
  251. 'last_chapter',
  252. 'books.keyword',
  253. 'book_configs.recommend_index',
  254. 'book_configs.is_show_index_content',
  255. 'book_configs.product_id',
  256. 'book_categories.channel_name',
  257. 'books.last_cid',
  258. 'books.last_chapter',
  259. 'book_configs.product_id',
  260. 'books.updated_at as last_update_time',
  261. 'book_configs.copyright_limit_data',
  262. 'book_configs.promotion_domain',
  263. 'books.name as old_name',
  264. 'book_configs.recommend_cid'
  265. );
  266. if(isset($where['bids']) && !empty($where['bids'])){
  267. $res->whereIn('book_configs.bid', $where['bids']);
  268. }
  269. if($is_external_shelf) $res->where('is_on_shelf',2);// 默认外部上架
  270. if ($order) {
  271. $res->orderBy($order[0], $order[1]);
  272. } else {
  273. $str = implode(',', $where['bids']);
  274. $field = 'bid,' . $str;
  275. $res->orderBy(DB::raw('field(' . $field . ')'));
  276. }
  277. // if(isset($where['channel_id']) && is_public_package_channel_id($where['channel_id'])){
  278. // $res->whereNotIn('book_configs.cp_source',getHiddenCp());
  279. // }else{
  280. // $res->whereNotIn('book_configs.cp_source',array_merge(getHiddenCp(),['lianshang']));
  281. // }
  282. $res->whereNotIn('book_configs.cp_source',getHiddenCp($where['channel_id']??0,2));
  283. return $res->limit(30)->get();
  284. }
  285. /**
  286. * 根据bid获取图书信息
  287. * @param $bid
  288. * @return mixed
  289. */
  290. public static function getBookById($bid)
  291. {
  292. if (empty($bid)) return null;
  293. return self::join('books', 'book_configs.bid', '=', 'books.id')
  294. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  295. ->select(
  296. 'book_configs.bid',
  297. 'book_configs.is_on_shelf',
  298. 'book_configs.force_subscribe_chapter_seq',
  299. 'book_configs.vip_seq',
  300. 'book_configs.cp_source',
  301. 'book_configs.price',
  302. 'book_configs.cover',
  303. 'book_configs.book_name',
  304. 'book_configs.copyright',
  305. 'book_configs.created_at',
  306. 'book_configs.charge_type',
  307. 'book_configs.is_on_shelf',
  308. 'books.author',
  309. 'books.intro',
  310. 'book_categories.category_name',
  311. 'category_id',
  312. 'status',
  313. 'chapter_count',
  314. 'first_cid',
  315. 'last_cid',
  316. 'size',
  317. 'last_chapter',
  318. 'books.keyword',
  319. 'book_configs.recommend_index',
  320. 'book_configs.test_status',
  321. 'book_configs.is_show_index_content',
  322. 'book_configs.click_count',
  323. 'book_configs.product_id',
  324. 'book_categories.channel_name',
  325. 'books.last_cid',
  326. 'books.updated_at as last_update_time',
  327. 'books.last_chapter',
  328. 'book_configs.product_id',
  329. 'book_configs.copyright_limit_data',
  330. 'book_configs.promotion_domain',
  331. 'books.name as old_name',
  332. 'book_configs.recommend_cid',
  333. 'book_configs.is_high_quality',
  334. 'book_configs.unit_price',
  335. 'book_configs.calculate_price_type'
  336. )->where('book_configs.bid', $bid)->first();
  337. }
  338. /*
  339. * 获取渠道,用户的全部书籍列表
  340. */
  341. public static function getLeftRecommendBook($channel_name, $is_high_quality, $force_update = false): array
  342. {
  343. if ($force_update) {
  344. \Log::info('force_set_full_book_channel_name:' . $channel_name);
  345. Redis::set('full_book_channel_name:' . $channel_name, null);
  346. }
  347. // 存redis里面
  348. $full_book_bids = Redis::get('full_book_channel_name:' . $channel_name);
  349. $full_book_bids = json_decode($full_book_bids);
  350. if (!empty($full_book_bids)) {
  351. \Log::info('direct_get_full_book_bids_from_redis:' . $channel_name);
  352. } else {
  353. // 获取全集
  354. $full_book_bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  355. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  356. ->select('book_configs.bid')
  357. ->where('book_categories.channel_name', $channel_name)
  358. ->where('book_configs.is_high_quality', $is_high_quality)
  359. ->where('book_configs.test_status', 0) // 不含测书
  360. ->whereIn('book_configs.is_on_shelf', [2])
  361. ->orderBy('book_configs.id', 'desc')
  362. ->get()->pluck('bid')->all();
  363. if (!empty($full_book_bids)) {
  364. \Log::info('set_full_book_channel_name:' . $channel_name . ' data:' . json_encode($full_book_bids));
  365. Redis::set('full_book_channel_name:' . $channel_name, json_encode($full_book_bids));
  366. }
  367. }
  368. // \Log::info('$full_book_bids:'.json_encode($full_book_bids));
  369. return $full_book_bids;
  370. }
  371. /*
  372. * 获取用户的曾经推荐过的书籍列表
  373. */
  374. public static function getUserRecommendRecords($uid)
  375. {
  376. $recommend_bids = Redis::smembers('userRecommendBids:' . $uid);
  377. return $recommend_bids;
  378. }
  379. /*
  380. * 添加用户的曾经推荐过的书籍列表
  381. */
  382. public static function addUserRecommendRecords($uid, $bids)
  383. {
  384. foreach ($bids as $bid) {
  385. Redis::sadd('userRecommendBids:' . $uid, $bid);
  386. }
  387. }
  388. /*
  389. * 清楚用户的曾经推荐过的书籍列表
  390. */
  391. public static function truncateUserRecommendRecords($uid)
  392. {
  393. \Log::info('truncateUserRecommendRecords:' . $uid);
  394. Redis::del('userRecommendBids:' . $uid);
  395. }
  396. /*
  397. *快应用专用,用户阅读完推荐
  398. * 获取相同推荐
  399. */
  400. public static function getQuickAppRecommendBooks($bid, $categories_id, $num = 4)
  401. {
  402. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  403. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  404. ->select(
  405. 'book_configs.bid',
  406. 'book_configs.force_subscribe_chapter_seq',
  407. 'book_configs.vip_seq',
  408. 'book_configs.price',
  409. 'book_configs.cover',
  410. 'book_configs.book_name',
  411. 'book_configs.copyright',
  412. 'book_configs.charge_type',
  413. 'book_configs.is_on_shelf',
  414. 'books.author',
  415. 'books.intro',
  416. 'book_categories.category_name',
  417. 'category_id',
  418. 'status',
  419. 'chapter_count',
  420. 'first_cid',
  421. 'last_cid',
  422. 'size',
  423. 'last_chapter',
  424. 'books.keyword',
  425. 'book_configs.recommend_index',
  426. 'book_configs.is_show_index_content',
  427. 'book_configs.click_count',
  428. 'book_configs.product_id',
  429. 'book_categories.channel_name',
  430. 'books.last_cid',
  431. 'books.last_chapter',
  432. 'book_configs.product_id',
  433. 'books.name as old_name',
  434. 'book_configs.recommend_cid',
  435. 'book_configs.is_high_quality',
  436. 'books.updated_at as last_update_time'
  437. )
  438. ->where('book_categories.id', $categories_id)
  439. ->where('book_configs.bid', '!=', $bid)
  440. ->where('book_configs.is_high_quality', 1)
  441. ->where('book_configs.is_on_shelf', 2)
  442. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  443. ->orderBy('recommend_index', 'desc')->get();
  444. $count = $res->count() >= $num ? $num : $res->count();
  445. return $res->random($count);
  446. }
  447. /*
  448. * H5专用,用户阅读完推荐
  449. * 获取相同推荐
  450. */
  451. public static function getRecommendBooks($bid, $channel_name, $num = 4)
  452. {
  453. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  454. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  455. ->select(
  456. 'book_configs.bid',
  457. 'book_configs.force_subscribe_chapter_seq',
  458. 'book_configs.vip_seq',
  459. 'book_configs.price',
  460. 'book_configs.cover',
  461. 'book_configs.book_name',
  462. 'book_configs.copyright',
  463. 'book_configs.charge_type',
  464. 'book_configs.is_on_shelf',
  465. 'books.author',
  466. 'books.intro',
  467. 'book_categories.category_name',
  468. 'category_id',
  469. 'status',
  470. 'chapter_count',
  471. 'first_cid',
  472. 'last_cid',
  473. 'size',
  474. 'last_chapter',
  475. 'books.keyword',
  476. 'book_configs.recommend_index',
  477. 'book_configs.is_show_index_content',
  478. 'book_configs.click_count',
  479. 'book_configs.product_id',
  480. 'book_categories.channel_name',
  481. 'books.last_cid',
  482. 'books.last_chapter',
  483. 'book_configs.product_id',
  484. 'books.name as old_name',
  485. 'book_configs.recommend_cid',
  486. 'book_configs.is_high_quality',
  487. 'books.updated_at as last_update_time'
  488. )
  489. ->where('book_categories.channel_name', $channel_name)
  490. ->where('book_configs.bid', '!=', $bid)
  491. ->where('book_configs.is_high_quality', 1)
  492. ->where('book_configs.is_on_shelf', 2)
  493. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  494. ->orderBy('recommend_index', 'desc')->get();
  495. $count = $res->count() >= $num ? $num : $res->count();
  496. return $res->random($count);
  497. }
  498. public static function getAllCps()
  499. {
  500. $cps = self::select('cp_source')->groupBy('cp_source')->get();
  501. $result = array();
  502. foreach ($cps as $cp) {
  503. if (!empty($cp['cp_source'])) {
  504. $result[] = $cp['cp_source'];
  505. }
  506. }
  507. return $result;
  508. }
  509. public static function getAllCpBooks()
  510. {
  511. $result = array();
  512. $cp_books = self::select('cp_source', 'bid')->groupBy('cp_source')->groupBy('bid')->get();
  513. foreach ($cp_books as $cp_book) {
  514. $result[$cp_book['cp_source']][] = $cp_book['bid'];
  515. }
  516. return $result;
  517. }
  518. /*
  519. * 得到cp某段时间某本书的消费
  520. */
  521. public static function getAllCpBookConsume($start_date, $end_date, $cps)
  522. {
  523. $result = self::leftjoin('book_order_statistical', 'book_order_statistical.bid', '=', 'book_configs.bid')
  524. ->select(
  525. 'book_order_statistical.bid',
  526. DB::raw('sum(book_order_statistical.charge_balance) as charge_balance'),
  527. 'book_configs.book_name',
  528. 'book_configs.is_on_shelf',
  529. 'book_configs.cp_source'
  530. )
  531. ->whereIn('book_configs.cp_source', $cps)
  532. ->where('book_order_statistical.day', '>=', $start_date)
  533. ->where('book_order_statistical.day', '<=', $end_date)
  534. ->groupBy('book_configs.cp_source')
  535. ->groupBy('book_order_statistical.bid')
  536. ->get();
  537. return $result;
  538. }
  539. }