BookConfigService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. {
  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. {
  202. if (!Redis::Sismember('qapp:free:virtual:uids' . $id, $uid)) {
  203. return;
  204. }
  205. $now = date('Y-m-d');
  206. $amount = $amount * 100;
  207. Redis::hincrby('qapp:book:free:charge:' . $id, $now, $amount);
  208. #Redis::sadd('qapp:free:charge'.$now,$id);
  209. Redis::sadd('qapp:free:actuality' . $now, $id);
  210. Redis::sadd('qapp:free:charge:uids' . $now . $id, $uid);
  211. }
  212. public static function getBookByField($bids, $field)
  213. {
  214. if (!$bids || !$field) return null;
  215. return BookConfig::join('books', 'books.id', '=', 'book_configs.bid')->
  216. whereIn('bid', $bids)->select($field)->get();
  217. }
  218. /**
  219. * 根据书籍bid去除无用bid
  220. * name: getAvailableBIdsbyBids
  221. * @param $bids
  222. * @param mixed $channel_id
  223. * @param mixed $is_external_shelf
  224. * @return array
  225. * date 2022/09/20 10:39
  226. */
  227. public static function getAvailableBIdsbyBids($bids, $channel_id = 0, $is_external_shelf = true)
  228. {
  229. if (empty($bids)) {
  230. return [];
  231. }
  232. $res = BookConfig::whereIn('bid', $bids);
  233. // if($channel_id == config('qapp_public_package_channel')){
  234. // $res->whereNotIn('cp_source',getHiddenCp());
  235. // }else{
  236. // $res->whereNotIn('cp_source',array_merge(getHiddenCp(),['lianshang']));
  237. // }
  238. $res->whereNotIn('cp_source', getHiddenCp());
  239. if ($is_external_shelf) {
  240. $res->where('is_on_shelf', 2);
  241. } else {
  242. $res->whereIn('is_on_shelf', [1, 2]);
  243. }
  244. return $res->pluck("bid")->toArray();
  245. }
  246. /***
  247. * 推荐位书籍检测和补齐
  248. * name: getCheckBooks
  249. * @param $bid_list
  250. * @param $channel
  251. * @param $package
  252. * @param $is_author
  253. * @return mixed
  254. * date 2022/10/26 10:11
  255. */
  256. public static function getCheckBooks($bid_list, $channel, $package, $is_author)
  257. {
  258. $hidden_cp = getHiddenCp();
  259. // if(!is_public_package($package)){
  260. // $hidden_cp = array_merge($hidden_cp,['lianshang']);
  261. // }
  262. //获取书本数量
  263. $count = count($bid_list);
  264. if (!$is_author) {
  265. $where = [
  266. ['book_configs.charge_type', '!=', 'BOOK'],
  267. ['book_configs.cp_source', '=', 'ycsd'],
  268. ];
  269. } else {
  270. $where = [
  271. ['book_configs.charge_type', '!=', 'BOOK'],
  272. ];
  273. }
  274. //获取书籍交集bid,过滤掉不符合要求的书
  275. $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  276. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  277. ->whereIn('book_configs.bid', $bid_list)
  278. ->where('book_configs.is_on_shelf', 2)
  279. ->where($where)
  280. ->whereNotIn('book_configs.cp_source', $hidden_cp)
  281. ->where('book_categories.pid', $channel)
  282. ->pluck('book_configs.bid')->all();
  283. $book_count = count($bid_list);
  284. if ($book_count === $count) {
  285. return $bid_list;
  286. }
  287. $supplement_count = $count - $book_count;
  288. //获取随机的有效的书籍bid
  289. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  290. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  291. ->where('book_configs.is_on_shelf', 2)
  292. ->where($where)
  293. ->whereNotIn('book_configs.cp_source', $hidden_cp)
  294. // ->where('book_configs.is_high_quality',1)
  295. ->where('book_categories.pid', $channel)
  296. ->orderBy('book_configs.is_high_quality', 'desc')
  297. ->inRandomOrder()
  298. ->limit($supplement_count)
  299. ->pluck('book_configs.bid')->all();
  300. return array_filter(array_merge($bid_list, $rand_bid));
  301. }
  302. public static function getRecommendBids($package = '', $channel = 1, $is_auth = true, $no_in_bid = [], $limit = 64)
  303. {
  304. $where = [
  305. ['book_configs.charge_type', '!=', 'BOOK'],
  306. ];
  307. if (!$is_auth) {
  308. $where[] = ['book_configs.cp_source', '=', 'ycsd'];
  309. }
  310. //获取书籍交集bid,过滤掉不符合要求的书
  311. $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  312. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  313. ->where('book_configs.is_on_shelf', 2)
  314. ->where($where)
  315. ->whereNotIn('book_configs.cp_source', getHiddenCp())
  316. ->where('book_categories.pid', $channel);
  317. if (!empty($no_in_bid)) {
  318. $bid_list->whereNotIn('book_configs.bid', $no_in_bid);
  319. }
  320. $bid_list = $bid_list->orderBy('recommend_index' , 'desc')->limit($limit)->pluck('book_configs.bid');
  321. echo "<pre><hr>";
  322. var_export($bid_list);
  323. echo "<hr>";
  324. die();
  325. }
  326. }