BookConfig.php 19 KB

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