BookConfigService.php 18 KB

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