BookConfigService.php 14 KB

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