BookConfigService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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\BookConfig;
  10. use App\Modules\Product\Services\ProductService;
  11. use App\Modules\Book\Models\Book;
  12. use App\Modules\Book\Models\BookKeyword;
  13. use App\Modules\Book\Models\FreeBook;
  14. use App\Modules\Book\Models\QappUserSearchBookLog;
  15. use Redis;
  16. use DB;
  17. use Vinkla\Hashids\Facades\Hashids;
  18. class BookConfigService
  19. {
  20. /**
  21. * 根据id获取图书
  22. * @param $bid
  23. * @return mixed
  24. */
  25. public static function getBookById($bid)
  26. {
  27. $res = BookConfig::getBookById($bid);
  28. //$res->tags = BookTagsService::getBookTags($bid);
  29. return $res;
  30. }
  31. /**
  32. * 根据bid数组获取多本图书
  33. * @param $where
  34. * @param null $order
  35. * @return mixed
  36. */
  37. public static function getBooksByIds(array $where, $order = [])
  38. {
  39. if (empty($where)) {
  40. return (object)array();
  41. }
  42. if ($order)
  43. $res = BookConfig::getBooksByIds($where, $order);
  44. else
  45. $res = BookConfig::getBooksByIds($where);
  46. return $res;
  47. }
  48. /**
  49. *
  50. * 根据条件获取图书
  51. * @param array $where ['key'=>'根据关键词查询','category_id'=>'根据分类id查询','is_on_shelf'=>上下架查询,'channel_name'=>'频道查询(男频女频)']
  52. * @param array $order 排序 默认是bid排序
  53. * @param int $page_size
  54. * @return mixed
  55. */
  56. public static function getBooks(array $where, array $order = [], $page_size = 15)
  57. {
  58. return BookConfig::getBooks($where, $order, $page_size);
  59. }
  60. /**
  61. *
  62. * 根据条件获取图书
  63. * @param array $where ['key'=>'根据关键词查询','category_id'=>'根据分类id查询','is_on_shelf'=>上下架查询,'channel_name'=>'频道查询(男频女频)']
  64. * @param array $order 排序 默认是bid排序
  65. * @param int $page_size
  66. * @return mixed
  67. */
  68. public static function getPromotionBooks(array $where, array $bids, array $order = [], $page_size = 15)
  69. {
  70. return BookConfig::getPromotionBooks($where, $bids, $order, $page_size);
  71. }
  72. /**
  73. * @param array $where
  74. * @param array $data
  75. */
  76. public static function getPromotionBooksV2(array $where, array $whereIn, array $data, array $orwhereIn, $whereDeedIn = '', $superior_lib = '', $keywords = '')
  77. {
  78. //全站派单
  79. $total_send_order_sum = '(select sum(num) as total_send_order_sum from book_send_order_stats where book_send_order_stats.bid=book_configs.bid) as total_send_order_sum';
  80. //七天派单数
  81. $week_send_orders = sprintf(
  82. "(select sum(num) as week_send_order_sum from book_send_order_stats as a where a.bid=book_configs.bid and a.day>='%s') as week_send_order_sum",
  83. date('Y-m-d', strtotime('-7 day'))
  84. );
  85. //我的派单
  86. $channel_send_orders = sprintf(
  87. "(select sum(num) as channel_send_order_sum from book_send_order_stats as a where a.bid=book_configs.bid and a.distribution_channel_id=%s) as channel_send_order_sum",
  88. $data['distribution_channel_id']
  89. );
  90. //我的派单数
  91. $res = BookConfig::join('books', 'book_configs.bid', '=', 'Books.id')
  92. ->leftjoin('chapters', 'books.first_cid', '=', 'chapters.id')
  93. ->leftjoin('book_categories', 'book_categories.id', 'books.category_id')
  94. ->leftjoin('book_channel_scores', function ($join) use ($data) {
  95. $join->where('book_channel_scores.distribution_channel_id', '=', $data['distribution_channel_id'])
  96. ->on('book_channel_scores.bid', '=', 'book_configs.bid');
  97. });
  98. if ($superior_lib) {
  99. $res->leftjoin($superior_lib, $superior_lib . '.bid', '=', 'book_configs.bid');
  100. }
  101. $res->select(
  102. 'book_configs.book_name',
  103. 'book_configs.bid',
  104. 'book_categories.category_name',
  105. 'book_categories.channel_name',
  106. 'books.chapter_count',
  107. 'books.size',
  108. 'book_configs.cover',
  109. 'book_configs.charge_type',
  110. 'books.status',
  111. 'book_channel_scores.score as own_score',
  112. 'book_configs.recommend_index',
  113. 'book_configs.editor_recommend',
  114. 'book_configs.created_at',
  115. 'book_configs.editor_recommend',
  116. DB::raw($total_send_order_sum),
  117. DB::raw($week_send_orders),
  118. DB::raw($channel_send_orders),
  119. 'books.first_cid',
  120. 'chapters.name as first_chapter_name',
  121. 'books.chapter_count'
  122. )->where($where);
  123. if ($whereIn) {
  124. $res->whereNotIn('book_configs.bid', $whereIn);
  125. }
  126. if ($whereDeedIn) {
  127. $res->whereIn('book_configs.bid', $whereDeedIn);
  128. }
  129. $res->where(function ($query) use ($orwhereIn) {
  130. $query->where('book_configs.is_on_shelf', 2);
  131. if ($orwhereIn) {
  132. $query->orWhere('book_configs.is_on_shelf', 1)->whereIn('book_configs.bid', $orwhereIn);
  133. }
  134. });
  135. if ($keywords) {
  136. $res->where(function ($query) use ($keywords) {
  137. $query->where('book_configs.book_name', 'like', '%' . $keywords . '%')
  138. ->orWhere([
  139. ['book_configs.roles', 'like', '%' . $keywords . '%'],
  140. ['book_configs.is_on_shelf', '=', 2]
  141. ]);
  142. //->orWhere('book_configs.roles', 'like', '%' . $keywords . '%');
  143. });
  144. }
  145. /*if ($orwhere) {
  146. $res->orWhere(function ($query) use ($orwhere, $orwhereIn,$whereDeedIn) {
  147. if($whereDeedIn){
  148. $orwhereIn = $whereDeedIn;
  149. }
  150. $query->where($orwhere)->whereIn('book_configs.bid', $orwhereIn);
  151. });
  152. }*/
  153. //\Log::info('books_list:books_data:'.json_encode($data));
  154. if (in_array($data['order_field'], ['total_send_order_sum', 'week_send_order_sum', 'order_index'])) {
  155. //\Log::info('books_list:order_filed:'.$data['order_field']);
  156. return $res->orderBy($data['order_field'], $data['order_type'])
  157. ->orderBy('recommend_index', 'desc')
  158. ->orderBy('books.size', 'desc')
  159. ->paginate();
  160. } else {
  161. $res->orderBy($data['order_field'], $data['order_type']);
  162. if ($data['order_field'] == 'recommend_index') {
  163. $res->orderBy('books.size', 'desc');
  164. }
  165. return $res->paginate();
  166. }
  167. }
  168. /**
  169. * 根据关键词查询
  170. * @param $key
  171. * @param int $page_size
  172. * @param int|Array $is_on_shelf 上架信息
  173. * @return mixed
  174. */
  175. public static function getBooksByKey($key, $page_size = 15, $is_on_shelf = null)
  176. {
  177. if (!$is_on_shelf) {
  178. $is_on_shelf = [1, 2];
  179. }
  180. $res = BookConfig::getBooksByKey($key, $page_size, $is_on_shelf);
  181. return $res;
  182. }
  183. /**
  184. * 更新图书
  185. * 可以修改的字段
  186. * ['force_subscribe_chapter_seq'=>'强关章节','price'=>价格,cover=>封面,book_name,charge_type,hot,
  187. * is_on_shelf,recommend_index,is_show_index_content,click_count,copyright_limit_data]
  188. * @param $bid
  189. * @param array $data
  190. * @return bool
  191. */
  192. public static function updateBookConfig($bid, array $data)
  193. {
  194. if (empty($data)) return false;
  195. $book_info = BookConfig::getBookById($bid);
  196. if (!$book_info) return false;
  197. if (isset($data['price']) && $data['price'] != '') {
  198. if ($data['price'] != $book_info->price) {
  199. $product = ProductService::addProduct(['price' => $data['price'], 'type' => 'BOOK_ORDER', 'given' => 0]);
  200. $data['product_id'] = $product->id;
  201. }
  202. }
  203. return BookConfig::updateBookInfo($bid, $data);
  204. }
  205. /**
  206. * @param $protuct_id
  207. * @return mixed
  208. */
  209. public static function getBookByProduct($protuct_id)
  210. {
  211. return BookConfig::getBookByProduct($protuct_id);
  212. }
  213. /**
  214. * 获取相同频道的高推荐书籍 循环获取未读的
  215. * @param $bid
  216. * @param int $num
  217. * @return bool
  218. */
  219. public static function getSimpleChannelBookLoop($bid, $num, $uid)
  220. {
  221. return BookConfig::getSimpleChannelBookLoop($bid, $num, $uid);
  222. }
  223. /**
  224. * 获取相同频道的高推荐书籍 超哥客服消息专用
  225. * @param $bid
  226. * @param int $num
  227. * @return bool
  228. */
  229. public static function getSimpleChannelBook($bid, $num = 4)
  230. {
  231. return BookConfig::getSimpleChannelBook($bid, $num);
  232. }
  233. /**
  234. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  235. * @param $bid
  236. * @param int $num
  237. * @return bool
  238. */
  239. public static function getTrusteeShipChannelBook($distribution_channel_id, $channel_name, $num = 4)
  240. {
  241. return BookConfig::getTrusteeShipChannelBook($distribution_channel_id, $channel_name, $num);
  242. }
  243. /**
  244. * 获取阅读完的推荐
  245. * @param $category_id
  246. * @param int $num
  247. * @return mixed
  248. */
  249. public static function getRecommendBooks($bid, $category_id, $num = 4)
  250. {
  251. return BookConfig::getRecommendBooks($bid, $category_id, $num);
  252. }
  253. /**
  254. * 获取阅读完的推荐(快应用)
  255. * @param $category_id
  256. * @param int $num
  257. * @return mixed
  258. */
  259. public static function getQuickAppRecommendBooks($bid, $category_id, $num = 4)
  260. {
  261. return BookConfig::getQuickAppRecommendBooks($bid, $category_id, $num);
  262. }
  263. /**
  264. * 修改推荐位
  265. * @param int $bid
  266. * @param int $cid
  267. * @return mixed
  268. */
  269. public static function editRecommendCid($bid, $cid)
  270. {
  271. return BookConfig::where('bid', $bid)->update(['recommend_cid' => $cid]);
  272. }
  273. /**
  274. * 是否优质书籍
  275. * @param int $bid
  276. * @param int $high
  277. * @return mixed
  278. */
  279. public static function editIsHighQuality($bid, $high)
  280. {
  281. return BookConfig::where('bid', $bid)->update(['is_high_quality' => $high]);
  282. }
  283. /**
  284. * 签到推荐
  285. * @param array $bid
  286. * @param $channel_name
  287. * @param int $num
  288. * @return mixed
  289. */
  290. public static function getSignRecommendBooks(array $bid, $channel_name, $num = 2)
  291. {
  292. return BookConfig::getSignRecommendBooks($bid, $channel_name, $num);
  293. }
  294. /**
  295. * 获取指定bid的书籍
  296. */
  297. public static function getBidRecommendBooks(array $bids)
  298. {
  299. return BookConfig::getBidRecommendBooks($bids);
  300. }
  301. public static function getH5RecommendBooks($uid, $pos, $num)
  302. {
  303. return BookConfig::getH5RecommendBooks($uid, $pos, $num);
  304. }
  305. /**
  306. * 修改vip卡点
  307. */
  308. public static function editVipSeq($bid, $seq)
  309. {
  310. return BookConfig::updateVipSeq($bid, $seq);
  311. }
  312. public static function getAllBooks($on_shelf, $order = [])
  313. {
  314. return BookConfig::getAllBooks($on_shelf, $order);
  315. }
  316. /**
  317. * 根据条件获取 不分页
  318. */
  319. public static function getBooksNoPage(array $where = [], array $order = [], array $on_shelf, $limit = 20)
  320. {
  321. return BookConfig::getBooksNoPage($where, $order, $on_shelf, $limit);
  322. }
  323. /**
  324. * @param string $name
  325. */
  326. public static function getBooksByName(string $name)
  327. {
  328. return BookConfig::where('book_name', 'like', '%' . $name . '%')->whereIn('is_on_shelf', [1, 2])->select('bid', 'book_name')->limit(10)->get();
  329. }
  330. public static function getSimpleBooksByIds(array $ids)
  331. {
  332. $str = implode(',', $ids);
  333. $field = 'bid,' . $str;
  334. return BookConfig::whereIn('bid', $ids)->select('bid', 'book_name')->orderBy(DB::raw('field(' . $field . ')'))->get();
  335. }
  336. /**
  337. * 获取图书简介
  338. * @param int $bid
  339. * @return mixed
  340. */
  341. public static function getBookIntroByBid(int $bid, string $book_name)
  342. {
  343. $where = [];
  344. if ($bid) {
  345. $where[] = ['book_configs.bid', $bid];
  346. }
  347. if ($book_name) {
  348. $where[] = ['book_configs.book_name', 'like', '%' . $book_name . '%'];
  349. }
  350. if (empty($where)) {
  351. return false;
  352. }
  353. return BookConfig::where($where)
  354. ->join('books', 'book_configs.bid', '=', 'books.id')
  355. ->select(
  356. 'books.intro',
  357. DB::raw('concat(book_configs.book_name,"(",book_configs.bid,")") as book_name')
  358. )
  359. ->get();
  360. }
  361. public static function getBookByIdAndStatus($bid, $status)
  362. {
  363. return BookConfig::getBookByIdAndStatus($bid, $status);
  364. }
  365. public static function get_all_test_books($is_all)
  366. {
  367. return BookConfig::get_all_test_books($is_all);
  368. }
  369. public static function get_test_books($status)
  370. {
  371. return BookConfig::get_test_books($status);
  372. }
  373. public static function updateTestBook($bid, $status, $plan_push_user_num)
  374. {
  375. return BookConfig::updateTestBook($bid, $status, $plan_push_user_num);
  376. }
  377. public static function get_all_smart_push_books($is_all)
  378. {
  379. return BookConfig::get_all_smart_push_books($is_all);
  380. }
  381. public static function getHotRandomRecommendBookText($distribution_channel_id, $uid, $num)
  382. {
  383. return BookConfig::getHotRandomRecommendBookText($distribution_channel_id, $uid, $num);
  384. }
  385. public static function resetBookLibRedis($category_type)
  386. {
  387. $force_update = true;
  388. $is_high_quality = 1;
  389. $boy = '男频';
  390. $girl = '女频';
  391. \Log::info('resetBookLibRedis,category_type:' . $category_type);
  392. try {
  393. // 更新全库
  394. BookConfig::getLeftRecommendBook($boy, $is_high_quality, $force_update);
  395. BookConfig::getLeftRecommendBook($girl, $is_high_quality, $force_update);
  396. } catch (Exception $e) {
  397. \Log::info('resetBookLibRedis_ept:' . $e->getMessage());
  398. }
  399. }
  400. public static function getRandomOneHighQualityBook($sex)
  401. {
  402. return BookConfig::join('books', 'books.id', '=', 'book_configs.bid')
  403. ->join('book_categories', 'books.category_id', '=', 'book_categories.id')
  404. ->select('books.intro', 'books.first_cid', 'book_configs.cover', 'book_configs.book_name', 'book_configs.bid')
  405. ->where('book_configs.is_high_quality', 1)
  406. ->where('book_categories.pid', $sex)
  407. ->orderBy('book_configs.bid')
  408. ->get()
  409. ->random(1)->first();
  410. }
  411. public static function findBookKeywords(bool $is_all = false)
  412. {
  413. $sql = BookKeyword::where('status', 1)->orderBy('sequence');
  414. if ($is_all) {
  415. return $sql->get();
  416. } else {
  417. return $sql->paginate(10);
  418. }
  419. }
  420. public static function saveUserSearchLog(string $words, int $uid)
  421. {
  422. QappUserSearchBookLog::create([
  423. 'uid' => $uid,
  424. 'words' => $words,
  425. ]);
  426. }
  427. /**
  428. * 查找限免书籍
  429. */
  430. public static function findFreeBooks(int $sex)
  431. {
  432. $free_books = FreeBook::where('start_time', '<=', now())->where('end_time', '>=', now())
  433. ->where('sex', $sex)
  434. ->where('is_enabled', 1)
  435. ->get();
  436. $bids = $free_books->pluck('bid')->all();
  437. $book_configs = BookConfig::whereIn('bid', $bids)
  438. ->where('is_on_shelf', 2)
  439. ->select('bid', 'book_name', 'cover')
  440. ->get();
  441. $books = Book::whereIn('id', $bids)->select('id', 'intro')->get();
  442. return $book_configs->transform(function ($item) use ($books, $free_books) {
  443. $free_book = $free_books->where('bid', $item->bid)->first();
  444. $book = $books->where('id', $item->bid)->first();
  445. return [
  446. 'bid' => Hashids::encode($item->bid),
  447. 'cover' => $item->cover,
  448. 'book_name' => $item->book_name,
  449. 'intro' => $book->intro,
  450. 'start_time' => $free_book->start_time,
  451. 'end_time' => $free_book->end_time,
  452. ];
  453. })->all();
  454. }
  455. /**
  456. * 判断书籍是否限免
  457. */
  458. public static function judgeBookIsFree(int $bid)
  459. {
  460. return FreeBook::where('start_time', '<=', now())->where('end_time', '>=', now())
  461. ->where('bid', $bid)
  462. ->where('is_enabled', 1)
  463. ->exists();
  464. }
  465. }