BookConfig.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. }
  153. }
  154. if(isset($where['channel_id']) && $where['channel_id'] == 7477){
  155. $res->whereNotIn('book_configs.cp_source',getHiddenCp());
  156. }else{
  157. $res->whereNotIn('book_configs.cp_source',array_merge(getHiddenCp(),['lianshang']));
  158. }
  159. return $res->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
  160. }
  161. /**
  162. * 根据id数组获取图书信息
  163. * @param array $bid_arr
  164. * @param array $order
  165. * @return mixed
  166. */
  167. public static function getBooksByIds(array $bid_arr, array $order = [], $is_external_shelf = true)
  168. {
  169. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  170. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  171. ->select(
  172. 'book_configs.bid',
  173. 'book_configs.force_subscribe_chapter_seq',
  174. 'book_configs.vip_seq',
  175. 'book_configs.price',
  176. 'book_configs.is_on_shelf',
  177. 'book_configs.cover',
  178. 'book_configs.book_name',
  179. 'book_configs.copyright',
  180. 'book_configs.charge_type',
  181. 'book_configs.is_on_shelf',
  182. 'books.author',
  183. 'books.intro',
  184. 'book_categories.category_name',
  185. 'category_id',
  186. 'status',
  187. 'chapter_count',
  188. 'book_configs.click_count',
  189. 'first_cid',
  190. 'last_cid',
  191. 'size',
  192. 'last_chapter',
  193. 'books.keyword',
  194. 'book_configs.recommend_index',
  195. 'book_configs.is_show_index_content',
  196. 'book_configs.product_id',
  197. 'book_categories.channel_name',
  198. 'books.last_cid',
  199. 'books.last_chapter',
  200. 'book_configs.product_id',
  201. 'books.updated_at as last_update_time',
  202. 'book_configs.copyright_limit_data',
  203. 'book_configs.promotion_domain',
  204. 'books.name as old_name',
  205. 'book_configs.recommend_cid'
  206. )
  207. ->whereIn('book_configs.bid', $bid_arr);
  208. if($is_external_shelf) $res->where('is_on_shelf',2);// 默认外部上架
  209. if ($order) {
  210. $res->orderBy($order[0], $order[1]);
  211. } else {
  212. $str = implode(',', $bid_arr);
  213. $field = 'bid,' . $str;
  214. $res->orderBy(DB::raw('field(' . $field . ')'));
  215. }
  216. $res->whereNotIn('book_configs.cp_source',getHiddenCp());
  217. return $res->limit(30)->get();
  218. }
  219. public static function getBookLists(array $where, array $order = [], $is_external_shelf = true)
  220. {
  221. if (empty($where)) {
  222. return [];
  223. }
  224. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  225. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  226. ->select(
  227. 'book_configs.bid',
  228. 'book_configs.force_subscribe_chapter_seq',
  229. 'book_configs.vip_seq',
  230. 'book_configs.price',
  231. 'book_configs.is_on_shelf',
  232. 'book_configs.cover',
  233. 'book_configs.book_name',
  234. 'book_configs.copyright',
  235. 'book_configs.charge_type',
  236. 'book_configs.is_on_shelf',
  237. 'books.author',
  238. 'books.intro',
  239. 'book_categories.category_name',
  240. 'category_id',
  241. 'status',
  242. 'chapter_count',
  243. 'book_configs.click_count',
  244. 'first_cid',
  245. 'last_cid',
  246. 'size',
  247. 'last_chapter',
  248. 'books.keyword',
  249. 'book_configs.recommend_index',
  250. 'book_configs.is_show_index_content',
  251. 'book_configs.product_id',
  252. 'book_categories.channel_name',
  253. 'books.last_cid',
  254. 'books.last_chapter',
  255. 'book_configs.product_id',
  256. 'books.updated_at as last_update_time',
  257. 'book_configs.copyright_limit_data',
  258. 'book_configs.promotion_domain',
  259. 'books.name as old_name',
  260. 'book_configs.recommend_cid'
  261. );
  262. if(isset($where['bids']) && !empty($where['bids'])){
  263. $res->whereIn('book_configs.bid', $where['bids']);
  264. }
  265. if($is_external_shelf) $res->where('is_on_shelf',2);// 默认外部上架
  266. if ($order) {
  267. $res->orderBy($order[0], $order[1]);
  268. } else {
  269. $str = implode(',', $where['bids']);
  270. $field = 'bid,' . $str;
  271. $res->orderBy(DB::raw('field(' . $field . ')'));
  272. }
  273. if(isset($where['channel_id']) && $where['channel_id'] == 7477){
  274. $res->whereNotIn('book_configs.cp_source',getHiddenCp());
  275. }else{
  276. $res->whereNotIn('book_configs.cp_source',array_merge(getHiddenCp(),['lianshang']));
  277. }
  278. return $res->limit(30)->get();
  279. }
  280. /**
  281. * 根据bid获取图书信息
  282. * @param $bid
  283. * @return mixed
  284. */
  285. public static function getBookById($bid)
  286. {
  287. if (empty($bid)) return null;
  288. return self::join('books', 'book_configs.bid', '=', 'books.id')
  289. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  290. ->select(
  291. 'book_configs.bid',
  292. 'book_configs.is_on_shelf',
  293. 'book_configs.force_subscribe_chapter_seq',
  294. 'book_configs.vip_seq',
  295. 'book_configs.cp_source',
  296. 'book_configs.price',
  297. 'book_configs.cover',
  298. 'book_configs.book_name',
  299. 'book_configs.copyright',
  300. 'book_configs.created_at',
  301. 'book_configs.charge_type',
  302. 'book_configs.is_on_shelf',
  303. 'books.author',
  304. 'books.intro',
  305. 'book_categories.category_name',
  306. 'category_id',
  307. 'status',
  308. 'chapter_count',
  309. 'first_cid',
  310. 'last_cid',
  311. 'size',
  312. 'last_chapter',
  313. 'books.keyword',
  314. 'book_configs.recommend_index',
  315. 'book_configs.test_status',
  316. 'book_configs.is_show_index_content',
  317. 'book_configs.click_count',
  318. 'book_configs.product_id',
  319. 'book_categories.channel_name',
  320. 'books.last_cid',
  321. 'books.updated_at as last_update_time',
  322. 'books.last_chapter',
  323. 'book_configs.product_id',
  324. 'book_configs.copyright_limit_data',
  325. 'book_configs.promotion_domain',
  326. 'books.name as old_name',
  327. 'book_configs.recommend_cid',
  328. 'book_configs.is_high_quality',
  329. 'book_configs.unit_price',
  330. 'book_configs.calculate_price_type'
  331. )->where('book_configs.bid', $bid)->first();
  332. }
  333. /*
  334. * 获取渠道,用户的全部书籍列表
  335. */
  336. public static function getLeftRecommendBook($channel_name, $is_high_quality, $force_update = false): array
  337. {
  338. if ($force_update) {
  339. \Log::info('force_set_full_book_channel_name:' . $channel_name);
  340. Redis::set('full_book_channel_name:' . $channel_name, null);
  341. }
  342. // 存redis里面
  343. $full_book_bids = Redis::get('full_book_channel_name:' . $channel_name);
  344. $full_book_bids = json_decode($full_book_bids);
  345. if (!empty($full_book_bids)) {
  346. \Log::info('direct_get_full_book_bids_from_redis:' . $channel_name);
  347. } else {
  348. // 获取全集
  349. $full_book_bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  350. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  351. ->select('book_configs.bid')
  352. ->where('book_categories.channel_name', $channel_name)
  353. ->where('book_configs.is_high_quality', $is_high_quality)
  354. ->where('book_configs.test_status', 0) // 不含测书
  355. ->whereIn('book_configs.is_on_shelf', [2])
  356. ->orderBy('book_configs.id', 'desc')
  357. ->get()->pluck('bid')->all();
  358. if (!empty($full_book_bids)) {
  359. \Log::info('set_full_book_channel_name:' . $channel_name . ' data:' . json_encode($full_book_bids));
  360. Redis::set('full_book_channel_name:' . $channel_name, json_encode($full_book_bids));
  361. }
  362. }
  363. // \Log::info('$full_book_bids:'.json_encode($full_book_bids));
  364. return $full_book_bids;
  365. }
  366. /*
  367. * 获取用户的曾经推荐过的书籍列表
  368. */
  369. public static function getUserRecommendRecords($uid)
  370. {
  371. $recommend_bids = Redis::smembers('userRecommendBids:' . $uid);
  372. return $recommend_bids;
  373. }
  374. /*
  375. * 添加用户的曾经推荐过的书籍列表
  376. */
  377. public static function addUserRecommendRecords($uid, $bids)
  378. {
  379. foreach ($bids as $bid) {
  380. Redis::sadd('userRecommendBids:' . $uid, $bid);
  381. }
  382. }
  383. /*
  384. * 清楚用户的曾经推荐过的书籍列表
  385. */
  386. public static function truncateUserRecommendRecords($uid)
  387. {
  388. \Log::info('truncateUserRecommendRecords:' . $uid);
  389. Redis::del('userRecommendBids:' . $uid);
  390. }
  391. /*
  392. *快应用专用,用户阅读完推荐
  393. * 获取相同推荐
  394. */
  395. public static function getQuickAppRecommendBooks($bid, $categories_id, $num = 4)
  396. {
  397. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  398. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  399. ->select(
  400. 'book_configs.bid',
  401. 'book_configs.force_subscribe_chapter_seq',
  402. 'book_configs.vip_seq',
  403. 'book_configs.price',
  404. 'book_configs.cover',
  405. 'book_configs.book_name',
  406. 'book_configs.copyright',
  407. 'book_configs.charge_type',
  408. 'book_configs.is_on_shelf',
  409. 'books.author',
  410. 'books.intro',
  411. 'book_categories.category_name',
  412. 'category_id',
  413. 'status',
  414. 'chapter_count',
  415. 'first_cid',
  416. 'last_cid',
  417. 'size',
  418. 'last_chapter',
  419. 'books.keyword',
  420. 'book_configs.recommend_index',
  421. 'book_configs.is_show_index_content',
  422. 'book_configs.click_count',
  423. 'book_configs.product_id',
  424. 'book_categories.channel_name',
  425. 'books.last_cid',
  426. 'books.last_chapter',
  427. 'book_configs.product_id',
  428. 'books.name as old_name',
  429. 'book_configs.recommend_cid',
  430. 'book_configs.is_high_quality',
  431. 'books.updated_at as last_update_time'
  432. )
  433. ->where('book_categories.id', $categories_id)
  434. ->where('book_configs.bid', '!=', $bid)
  435. ->where('book_configs.is_high_quality', 1)
  436. ->where('book_configs.is_on_shelf', 2)
  437. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  438. ->orderBy('recommend_index', 'desc')->get();
  439. $count = $res->count() >= $num ? $num : $res->count();
  440. return $res->random($count);
  441. }
  442. /*
  443. * H5专用,用户阅读完推荐
  444. * 获取相同推荐
  445. */
  446. public static function getRecommendBooks($bid, $channel_name, $num = 4)
  447. {
  448. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  449. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  450. ->select(
  451. 'book_configs.bid',
  452. 'book_configs.force_subscribe_chapter_seq',
  453. 'book_configs.vip_seq',
  454. 'book_configs.price',
  455. 'book_configs.cover',
  456. 'book_configs.book_name',
  457. 'book_configs.copyright',
  458. 'book_configs.charge_type',
  459. 'book_configs.is_on_shelf',
  460. 'books.author',
  461. 'books.intro',
  462. 'book_categories.category_name',
  463. 'category_id',
  464. 'status',
  465. 'chapter_count',
  466. 'first_cid',
  467. 'last_cid',
  468. 'size',
  469. 'last_chapter',
  470. 'books.keyword',
  471. 'book_configs.recommend_index',
  472. 'book_configs.is_show_index_content',
  473. 'book_configs.click_count',
  474. 'book_configs.product_id',
  475. 'book_categories.channel_name',
  476. 'books.last_cid',
  477. 'books.last_chapter',
  478. 'book_configs.product_id',
  479. 'books.name as old_name',
  480. 'book_configs.recommend_cid',
  481. 'book_configs.is_high_quality',
  482. 'books.updated_at as last_update_time'
  483. )
  484. ->where('book_categories.channel_name', $channel_name)
  485. ->where('book_configs.bid', '!=', $bid)
  486. ->where('book_configs.is_high_quality', 1)
  487. ->where('book_configs.is_on_shelf', 2)
  488. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  489. ->orderBy('recommend_index', 'desc')->get();
  490. $count = $res->count() >= $num ? $num : $res->count();
  491. return $res->random($count);
  492. }
  493. public static function getAllCps()
  494. {
  495. $cps = self::select('cp_source')->groupBy('cp_source')->get();
  496. $result = array();
  497. foreach ($cps as $cp) {
  498. if (!empty($cp['cp_source'])) {
  499. $result[] = $cp['cp_source'];
  500. }
  501. }
  502. return $result;
  503. }
  504. public static function getAllCpBooks()
  505. {
  506. $result = array();
  507. $cp_books = self::select('cp_source', 'bid')->groupBy('cp_source')->groupBy('bid')->get();
  508. foreach ($cp_books as $cp_book) {
  509. $result[$cp_book['cp_source']][] = $cp_book['bid'];
  510. }
  511. return $result;
  512. }
  513. /*
  514. * 得到cp某段时间某本书的消费
  515. */
  516. public static function getAllCpBookConsume($start_date, $end_date, $cps)
  517. {
  518. $result = self::leftjoin('book_order_statistical', 'book_order_statistical.bid', '=', 'book_configs.bid')
  519. ->select(
  520. 'book_order_statistical.bid',
  521. DB::raw('sum(book_order_statistical.charge_balance) as charge_balance'),
  522. 'book_configs.book_name',
  523. 'book_configs.is_on_shelf',
  524. 'book_configs.cp_source'
  525. )
  526. ->whereIn('book_configs.cp_source', $cps)
  527. ->where('book_order_statistical.day', '>=', $start_date)
  528. ->where('book_order_statistical.day', '<=', $end_date)
  529. ->groupBy('book_configs.cp_source')
  530. ->groupBy('book_order_statistical.bid')
  531. ->get();
  532. return $result;
  533. }
  534. }