BookConfigService.php 14 KB

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