BookConfigService.php 17 KB

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