BookConfigService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 phpDocumentor\Reflection\Types\This;
  18. use Redis;
  19. use DB;
  20. use Vinkla\Hashids\Facades\Hashids;
  21. class BookConfigService
  22. {
  23. /**
  24. * 根据id获取图书
  25. * @param $bid
  26. * @return mixed
  27. */
  28. public static function getBookById($bid)
  29. {
  30. return BookConfig::getBookById($bid);
  31. }
  32. /**
  33. * 获取图书组合信息
  34. * @param $bid
  35. * @return array
  36. */
  37. public static function getCombinationInfo($bid)
  38. {
  39. $info = BookCombinationConfig::where('bid',$bid)->where('is_enable',1)->select('bid_group')->first();
  40. if($info) return explode(',',$info->bid_group);
  41. return [];
  42. }
  43. /**
  44. * 根据bid数组获取多本图书
  45. * @param $where
  46. * @param null $order
  47. * @return mixed
  48. */
  49. public static function getBooksByIds(array $where, $order = [],$is_external_shelf=true)
  50. {
  51. if (empty($where)) {
  52. return (object)array();
  53. }
  54. if ($order)
  55. $res = BookConfig::getBooksByIds($where,$order,$is_external_shelf);
  56. else
  57. $res = BookConfig::getBooksByIds($where,[],$is_external_shelf);
  58. return $res;
  59. }
  60. public static function getBookLists(array $where, $order = [],$is_external_shelf=true)
  61. {
  62. if (empty($where)) {
  63. return (object)array();
  64. }
  65. if ($order) {
  66. $res = BookConfig::getBookLists($where,$order,$is_external_shelf);
  67. } else{
  68. $res = BookConfig::getBookLists($where,[],$is_external_shelf);
  69. }
  70. return $res;
  71. }
  72. /**
  73. *
  74. * 根据条件获取图书
  75. * @param array $where ['key'=>'根据关键词查询','category_id'=>'根据分类id查询','is_on_shelf'=>上下架查询,'channel_name'=>'频道查询(男频女频)']
  76. * @param array $order 排序 默认是bid排序
  77. * @param int $page_size
  78. * @return mixed
  79. */
  80. public static function getBooks(array $where, array $order = [], $page_size = 15)
  81. {
  82. return BookConfig::getBooks($where, $order, $page_size);
  83. }
  84. /**
  85. * 获取阅读完的推荐
  86. * @param $category_id
  87. * @param int $num
  88. * @return mixed
  89. */
  90. public static function getRecommendBooks($bid, $category_id, $num = 4)
  91. {
  92. return BookConfig::getRecommendBooks($bid, $category_id, $num);
  93. }
  94. /**
  95. * 获取阅读完的推荐(快应用)
  96. * @param $category_id
  97. * @param int $num
  98. * @return mixed
  99. */
  100. public static function getQuickAppRecommendBooks($bid, $category_id, $num = 4)
  101. {
  102. return BookConfig::getQuickAppRecommendBooks($bid, $category_id, $num);
  103. }
  104. public static function getSimpleBooksByIds(array $ids)
  105. {
  106. $str = implode(',', $ids);
  107. $field = 'bid,' . $str;
  108. return BookConfig::whereIn('bid', $ids)->select('bid', 'book_name')->orderBy(DB::raw('field(' . $field . ')'))->get();
  109. }
  110. public static function findBookKeywords(bool $is_all = false)
  111. {
  112. $sql = BookKeyword::where('status', 1)->orderBy('sequence');
  113. if ($is_all) {
  114. return $sql->get();
  115. } else {
  116. return $sql->paginate(10);
  117. }
  118. }
  119. public static function saveUserSearchLog(string $words, int $uid)
  120. {
  121. QappUserSearchBookLog::create([
  122. 'uid' => $uid,
  123. 'words' => $words,
  124. ]);
  125. }
  126. /**
  127. * @return FreeBookConfig
  128. */
  129. public static function findFreeBookConfig(int $sex)
  130. {
  131. return FreeBookConfig::where(['sex' => $sex, 'is_enabled' => 1])
  132. ->where('end_time', '>=', now())
  133. ->orderBy('end_time')
  134. ->first();
  135. }
  136. /**
  137. * 查找限免书籍
  138. * @return array
  139. */
  140. public static function findFreeBooks(int $sex)
  141. {
  142. $config = self::findFreeBookConfig($sex);
  143. \Log::info('return_empty_free_books:'.$sex);
  144. \Log::info($config);
  145. if ($config) {
  146. $free_books = FreeBook::where('config_id', $config->id)
  147. ->where('is_enabled', 1)
  148. ->get();
  149. $bids = $free_books->pluck('bid')->all();
  150. $book_configs = BookConfig::whereIn('bid', $bids)
  151. ->where('is_on_shelf', 2)
  152. ->select('bid', 'book_name', 'cover')
  153. ->get();
  154. $books = Book::whereIn('id', $bids)->select('id', 'intro')->get();
  155. \Log::info('return_empty_data:');
  156. \Log::info($books);
  157. $book_list = $book_configs->transform(function ($item) use ($books) {
  158. $book = $books->where('id', $item->bid)->first();
  159. return [
  160. 'book_id' => Hashids::encode($item->bid),
  161. 'cover_url' => $item->cover,
  162. 'book_name' => $item->book_name,
  163. 'intro' => $book->intro,
  164. ];
  165. })->all();
  166. return [
  167. 'title' => $config->name,
  168. 'end_time' => $config->end_time,
  169. 'list' => $book_list,
  170. ];
  171. }
  172. return [];
  173. }
  174. /**
  175. * 判断书籍是否限免
  176. * @return bool
  177. */
  178. public static function judgeBookIsFree(int $bid)
  179. {
  180. $ids = [];
  181. foreach ([1, 2] as $sex) {
  182. $config = self::findFreeBookConfig($sex);
  183. if ($config) {
  184. $ids[] = $config->id;
  185. }
  186. }
  187. return FreeBook::where('bid', $bid)
  188. ->whereIn('config_id', $ids)
  189. ->where('is_enabled', 1)->select('id')->first();
  190. }
  191. public static function getByBidNoFilter($bid){
  192. return FreeBook::join('free_book_config','free_book_config.id','=','free_books.config_id')
  193. ->where('bid',$bid)
  194. ->select('free_books.id','end_time')
  195. ->where('end_time','<',date('Y-m-d H:i:s'))
  196. ->orderBy('free_book_config.end_time','desc')
  197. ->first();
  198. }
  199. public static function chargeStats($id,$amount,$uid){
  200. if(!Redis::Sismember('qapp:free:virtual:uids'.$id,$uid)){
  201. return ;
  202. }
  203. $now = date('Y-m-d');
  204. $amount = $amount*100;
  205. Redis::hincrby('qapp:book:free:charge:'.$id,$now,$amount);
  206. #Redis::sadd('qapp:free:charge'.$now,$id);
  207. Redis::sadd('qapp:free:actuality' . $now, $id);
  208. Redis::sadd('qapp:free:charge:uids'.$now.$id,$uid);
  209. }
  210. public static function getBookByField($bids,$field){
  211. if(!$bids || !$field) return null;
  212. return BookConfig::join('books','books.id','=','book_configs.bid')->
  213. whereIn('bid',$bids)->select($field)->get();
  214. }
  215. /**
  216. * 根据书籍bid去除无用bid
  217. * name: getAvailableBIdsbyBids
  218. * @param $bids
  219. * @param mixed $channel_id
  220. * @param mixed $is_external_shelf
  221. * @return array
  222. * date 2022/09/20 10:39
  223. */
  224. public static function getAvailableBIdsbyBids($bids,$channel_id=0,$is_external_shelf=true)
  225. {
  226. if (empty($bids)){
  227. return [];
  228. }
  229. $res = BookConfig::whereIn('bid',$bids);
  230. if($channel_id == config('qapp_public_package_channel')){
  231. $res->whereNotIn('cp_source',getHiddenCp());
  232. }else{
  233. $res->whereNotIn('cp_source',array_merge(getHiddenCp(),['lianshang']));
  234. }
  235. if ($is_external_shelf){
  236. $res->where('is_on_shelf',2);
  237. }else{
  238. $res->whereIn('is_on_shelf',[1,2]);
  239. }
  240. return $res->pluck("bid")->toArray();
  241. }
  242. /***
  243. * 推荐位书籍检测和补齐
  244. * name: getCheckBooks
  245. * @param $bid_list
  246. * @param $channel
  247. * @param $package
  248. * @param $is_author
  249. * @return mixed
  250. * date 2022/10/26 10:11
  251. */
  252. public static function getCheckBooks($bid_list,$channel,$package,$is_author)
  253. {
  254. $hidden_cp = getHiddenCp();
  255. if(!is_public_package($package)){
  256. $hidden_cp = array_merge($hidden_cp,['lianshang']);
  257. }
  258. //获取书本数量
  259. $count = count($bid_list);
  260. if (!$is_author){
  261. $where = [
  262. ['book_configs.charge_type','!=','BOOK'],
  263. ['book_configs.cp_source','=','ycsd'],
  264. ];
  265. }else{
  266. $where = [
  267. ['book_configs.charge_type','!=','BOOK'],
  268. ];
  269. }
  270. //获取书籍交集bid,过滤掉不符合要求的书
  271. $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  272. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  273. ->whereIn('book_configs.bid',$bid_list)
  274. ->where('book_configs.is_on_shelf',2)
  275. ->where($where)
  276. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  277. ->where('book_categories.pid',$channel)
  278. ->pluck('book_configs.bid')->all();
  279. $book_count = count($bid_list);
  280. if ($book_count === $count){
  281. return $bid_list;
  282. }
  283. $supplement_count = $count - $book_count;
  284. //获取随机的有效的书籍bid
  285. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  286. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  287. ->where('book_configs.is_on_shelf',2)
  288. ->where($where)
  289. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  290. ->where('book_configs.is_high_quality',1)
  291. ->where('book_categories.pid',$channel)
  292. ->inRandomOrder()
  293. ->limit($supplement_count)
  294. ->get()->pluck('bid')->toArray();
  295. return array_filter(array_merge($bid_list,$rand_bid));
  296. }
  297. }