BookConfigService.php 15 KB

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