BookConfigService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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\Book\Models\BookCopyright;
  12. use App\Modules\Product\Services\ProductService;
  13. use App\Modules\Book\Models\Book;
  14. use App\Modules\Book\Models\BookKeyword;
  15. use App\Modules\Book\Models\FreeBook;
  16. use App\Modules\Book\Models\FreeBookConfig;
  17. use App\Modules\Book\Models\QappUserSearchBookLog;
  18. use phpDocumentor\Reflection\Types\This;
  19. use Redis;
  20. use DB;
  21. use Vinkla\Hashids\Facades\Hashids;
  22. class BookConfigService
  23. {
  24. /**
  25. * 根据id获取图书
  26. * @param $bid
  27. * @return mixed
  28. */
  29. public static function getBookById($bid)
  30. {
  31. return BookConfig::getBookById($bid);
  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. public static function getBookLists(array $where, $order = [],$is_external_shelf=true)
  62. {
  63. if (empty($where)) {
  64. return (object)array();
  65. }
  66. if ($order) {
  67. $res = BookConfig::getBookLists($where,$order,$is_external_shelf);
  68. } else{
  69. $res = BookConfig::getBookLists($where,[],$is_external_shelf);
  70. }
  71. return $res;
  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 getBooks(array $where, array $order = [], $page_size = 15)
  82. {
  83. return BookConfig::getBooks($where, $order, $page_size);
  84. }
  85. /**
  86. * 获取阅读完的推荐
  87. * @param $category_id
  88. * @param int $num
  89. * @return mixed
  90. */
  91. public static function getRecommendBooks($bid, $category_id, $num = 4)
  92. {
  93. return BookConfig::getRecommendBooks($bid, $category_id, $num);
  94. }
  95. /**
  96. * 获取阅读完的推荐(快应用)
  97. * @param $category_id
  98. * @param int $num
  99. * @return mixed
  100. */
  101. public static function getQuickAppRecommendBooks($bid, $category_id, $num = 4)
  102. {
  103. return BookConfig::getQuickAppRecommendBooks($bid, $category_id, $num);
  104. }
  105. public static function getSimpleBooksByIds(array $ids)
  106. {
  107. $str = implode(',', $ids);
  108. $field = 'bid,' . $str;
  109. return BookConfig::whereIn('bid', $ids)->select('bid', 'book_name')->orderBy(DB::raw('field(' . $field . ')'))->get();
  110. }
  111. public static function findBookKeywords(bool $is_all = false)
  112. {
  113. $sql = BookKeyword::where('status', 1)->orderBy('sequence');
  114. if ($is_all) {
  115. return $sql->get();
  116. } else {
  117. return $sql->paginate(10);
  118. }
  119. }
  120. public static function saveUserSearchLog(string $words, int $uid)
  121. {
  122. QappUserSearchBookLog::create([
  123. 'uid' => $uid,
  124. 'words' => $words,
  125. ]);
  126. }
  127. /**
  128. * @return FreeBookConfig
  129. */
  130. public static function findFreeBookConfig(int $sex)
  131. {
  132. return FreeBookConfig::where(['sex' => $sex, 'is_enabled' => 1])
  133. ->where('end_time', '>=', now())
  134. ->orderBy('end_time')
  135. ->first();
  136. }
  137. /**
  138. * 查找限免书籍
  139. * @return array
  140. */
  141. public static function findFreeBooks(int $sex)
  142. {
  143. $config = self::findFreeBookConfig($sex);
  144. \Log::info('return_empty_free_books:'.$sex);
  145. \Log::info($config);
  146. if ($config) {
  147. $free_books = FreeBook::where('config_id', $config->id)
  148. ->where('is_enabled', 1)
  149. ->get();
  150. $bids = $free_books->pluck('bid')->all();
  151. $book_configs = BookConfig::whereIn('bid', $bids)
  152. ->where('is_on_shelf', 2)
  153. ->select('bid', 'book_name', 'cover')
  154. ->get();
  155. $books = Book::whereIn('id', $bids)->select('id', 'intro')->get();
  156. \Log::info('return_empty_data:');
  157. \Log::info($books);
  158. $book_list = $book_configs->transform(function ($item) use ($books) {
  159. $book = $books->where('id', $item->bid)->first();
  160. return [
  161. 'book_id' => Hashids::encode($item->bid),
  162. 'cover_url' => $item->cover,
  163. 'book_name' => $item->book_name,
  164. 'intro' => $book->intro,
  165. ];
  166. })->all();
  167. return [
  168. 'title' => $config->name,
  169. 'end_time' => $config->end_time,
  170. 'list' => $book_list,
  171. ];
  172. }
  173. return [];
  174. }
  175. /**
  176. * 判断书籍是否限免
  177. * @return bool
  178. */
  179. public static function judgeBookIsFree(int $bid)
  180. {
  181. $ids = [];
  182. foreach ([1, 2] as $sex) {
  183. $config = self::findFreeBookConfig($sex);
  184. if ($config) {
  185. $ids[] = $config->id;
  186. }
  187. }
  188. return FreeBook::where('bid', $bid)
  189. ->whereIn('config_id', $ids)
  190. ->where('is_enabled', 1)->select('id')->first();
  191. }
  192. public static function getByBidNoFilter($bid){
  193. return FreeBook::join('free_book_config','free_book_config.id','=','free_books.config_id')
  194. ->where('bid',$bid)
  195. ->select('free_books.id','end_time')
  196. ->where('end_time','<',date('Y-m-d H:i:s'))
  197. ->orderBy('free_book_config.end_time','desc')
  198. ->first();
  199. }
  200. public static function chargeStats($id,$amount,$uid){
  201. if(!Redis::Sismember('qapp:free:virtual:uids'.$id,$uid)){
  202. return ;
  203. }
  204. $now = date('Y-m-d');
  205. $amount = $amount*100;
  206. Redis::hincrby('qapp:book:free:charge:'.$id,$now,$amount);
  207. #Redis::sadd('qapp:free:charge'.$now,$id);
  208. Redis::sadd('qapp:free:actuality' . $now, $id);
  209. Redis::sadd('qapp:free:charge:uids'.$now.$id,$uid);
  210. }
  211. public static function getBookByField($bids,$field){
  212. if(!$bids || !$field) return null;
  213. return BookConfig::join('books','books.id','=','book_configs.bid')->
  214. whereIn('bid',$bids)->select($field)->get();
  215. }
  216. /**
  217. * 根据书籍bid去除无用bid
  218. * name: getAvailableBIdsbyBids
  219. * @param $bids
  220. * @param mixed $channel_id
  221. * @param mixed $is_external_shelf
  222. * @return array
  223. * date 2022/09/20 10:39
  224. */
  225. public static function getAvailableBIdsbyBids($bids,$channel_id=0,$is_external_shelf=true)
  226. {
  227. if (empty($bids)){
  228. return [];
  229. }
  230. $res = BookConfig::whereIn('bid',$bids);
  231. // if($channel_id == config('qapp_public_package_channel')){
  232. // $res->whereNotIn('cp_source',getHiddenCp());
  233. // }else{
  234. // $res->whereNotIn('cp_source',array_merge(getHiddenCp(),['lianshang']));
  235. // }
  236. $res->whereNotIn('cp_source',getHiddenCp($channel_id,2));
  237. if ($is_external_shelf){
  238. $res->where('is_on_shelf',2);
  239. }else{
  240. $res->whereIn('is_on_shelf',[1,2]);
  241. }
  242. return $res->pluck("bid")->toArray();
  243. }
  244. /***
  245. * 推荐位书籍检测和补齐
  246. * name: getCheckBooks
  247. * @param $bid_list
  248. * @param $channel
  249. * @param $package
  250. * @param $is_author
  251. * @return mixed
  252. * date 2022/10/26 10:11
  253. */
  254. public static function getCheckBooks($bid_list,$channel,$package,$is_author)
  255. {
  256. $hidden_cp = getHiddenCp($package);
  257. // if(!is_public_package($package)){
  258. // $hidden_cp = array_merge($hidden_cp,['lianshang']);
  259. // }
  260. //获取书本数量
  261. $count = count($bid_list);
  262. if (!$is_author){
  263. $where = [
  264. ['book_configs.charge_type','!=','BOOK'],
  265. ['book_configs.cp_source','=','ycsd'],
  266. ];
  267. }else{
  268. $where = [
  269. ['book_configs.charge_type','!=','BOOK'],
  270. ];
  271. }
  272. //获取书籍交集bid,过滤掉不符合要求的书
  273. $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  274. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  275. ->whereIn('book_configs.bid',$bid_list)
  276. ->where('book_configs.is_on_shelf',2)
  277. ->where($where)
  278. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  279. ->where('book_categories.pid',$channel)
  280. ->pluck('book_configs.bid')->all();
  281. $book_count = count($bid_list);
  282. if ($book_count === $count){
  283. return $bid_list;
  284. }
  285. $supplement_count = $count - $book_count;
  286. //获取随机的有效的书籍bid
  287. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  288. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  289. ->where('book_configs.is_on_shelf',2)
  290. ->where($where)
  291. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  292. // ->where('book_configs.is_high_quality',1)
  293. ->where('book_categories.pid',$channel)
  294. ->orderBy('book_configs.is_high_quality','desc')
  295. ->inRandomOrder()
  296. ->limit($supplement_count)
  297. ->pluck('book_configs.bid')->all();
  298. return array_filter(array_merge($bid_list,$rand_bid));
  299. }
  300. /***
  301. * 书籍授权情况 2黑名单 1白名单 其他 无特殊添加
  302. * name: bookCopyright
  303. * @param mixed $bid
  304. * @param mixed $distribution_channel_id
  305. * date 2022/11/14 17:16
  306. */
  307. public static function bookCopyright($bid, $distribution_channel_id = 0)
  308. {
  309. if ($distribution_channel_id < 1){
  310. return 0;
  311. }
  312. return BookCopyright::where('distribution_channel_id',$distribution_channel_id)
  313. ->where('bid',$bid)
  314. ->where('is_enabled',1)
  315. ->where('platform',"qapp")
  316. ->value('type');
  317. }
  318. }