BookConfig.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. <?php
  2. namespace App\Modules\Book\Models;
  3. use App\Modules\Book\Services\BookTagsService;
  4. use App\Modules\Book\Services\TestBookService;
  5. use App\Modules\User\Services\ReadRecordService;
  6. use App\Modules\Book\Services\BookConfigService;
  7. use App\Modules\Channel\Services\ChannelService;
  8. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  9. use App\Modules\Promotion\Services\PromotionService;
  10. use App\Modules\User\Services\UserService;
  11. use DB;
  12. use Hashids;
  13. use Illuminate\Database\Eloquent\Model;
  14. use Log;
  15. use Redis;
  16. class BookConfig extends Model
  17. {
  18. protected $connection = 'api_mysql';
  19. protected $table = 'book_configs';
  20. protected $fillable = ['bid', 'force_subscribe_chapter_seq', 'price', 'cover', 'book_name',
  21. 'copyright', 'charge_type', 'hot', 'is_on_shelf', 'source_domain', 'recommend_index', 'roles', 'test_status', 'plan_push_user_num', 'test_update_time',
  22. 'is_show_index_content', 'click_count', 'promotion_domain', 'copyright_limit_data', 'recommend_cid', 'is_high_quality', 'vip_seq', 'editor_recommend', 'is_current_week_promotion'];
  23. /**
  24. * 根据条件获取图书
  25. * @param array $where
  26. * @param array $order
  27. * @param int $page_size
  28. * @return mixed
  29. */
  30. public static function getBooks(array $where = [], array $order = [], $page_size = 15)
  31. {
  32. if (!$order) {
  33. $order = ['recommend_index', 'desc'];
  34. }
  35. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  36. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  37. ->select('book_configs.bid', 'book_configs.roles', 'book_configs.force_subscribe_chapter_seq', 'book_configs.cp_source', 'book_configs.vip_seq', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
  38. 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
  39. 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'books.size', 'last_chapter', 'books.keyword',
  40. 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
  41. 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
  42. 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'is_current_week_promotion'
  43. );
  44. if ($where) {
  45. foreach ($where as $key => $v) {
  46. //关键词查询
  47. if ($key == 'key' && $v) {
  48. $res = $res->where('book_configs.book_name', 'like', '%' . $v . '%');
  49. //->orWhere('books.intro', 'like', '%' . $v . '%')
  50. //->orWhere('books.category_name', 'like', '%' . $v . '%')->orWhere('books.author', 'like', '%' . $v . '%')
  51. //->orWhere('books.keyword', 'like', '%' . $v . '%');
  52. }
  53. //分类id查询
  54. if ($key == 'category_id' && $v) {
  55. $res = $res->where('books.category_id', '=', $v);
  56. }
  57. //上架查询
  58. if ($key == 'is_on_shelf' && $v != '') {
  59. if (is_array($v)) {
  60. $res = $res->whereIn('is_on_shelf', $v);
  61. } else {
  62. $res = $res->where('is_on_shelf', '=', $v);
  63. }
  64. }
  65. //频道查询
  66. if ($key == 'channel_name' && $v) {
  67. $res = $res->where('book_categories.channel_name', '=', $v);
  68. }
  69. if ($key == 'status') {
  70. $res = $res->where('books.status', '=', $v);
  71. }
  72. if ($key == 'author') {
  73. $res = $res->where('books.author', 'like', '%' . $v . '%');
  74. }
  75. if ($key == 'roles') {
  76. $res = $res->where('book_configs.roles', 'like', '%' . $v . '%');
  77. }
  78. //旧书名查询
  79. if ($key == 'old_name') {
  80. $res = $res->where('books.name', 'like', '%' . $v . '%');
  81. }
  82. //版权日期查询
  83. if ($key == 'copy_right_date') {
  84. if (is_array($v)) {
  85. $res = $res->whereBetween('book_configs.copyright_limit_data', $v);
  86. } else {
  87. $res = $res->where('book_configs.copyright_limit_data', '<=', $v);
  88. }
  89. }
  90. if ($key == 'domain') {
  91. $res = $res->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
  92. }
  93. if ($key == 'bid') {
  94. $res = $res->where('book_configs.bid', '=', $v);
  95. }
  96. if ($key == 'is_high_quality') {
  97. $res = $res->where('book_configs.is_high_quality', '=', $v);
  98. }
  99. if ($key == 'charge_type') {
  100. $res = $res->where('book_configs.charge_type', '=', $v);
  101. }
  102. if ($key == 'firstChapterContent') {
  103. $res = $res->join('chapters', function ($query) use ($v) {
  104. $query->on('book_configs.bid', '=', 'chapters.bid')
  105. ->where('chapters.sequence', 1);
  106. })->where('chapters.content', 'like', '%' . $v . '%');
  107. }
  108. if ($key == 'tags') {
  109. $tags_filter = BookTagsService::getSearchBooks($v);
  110. $res->whereIn('book_configs.bid', $tags_filter);
  111. }
  112. if ($key == 'is_current_week_promotion') {
  113. $res->where('book_configs.is_current_week_promotion', $v);
  114. }
  115. }
  116. }
  117. //$res->orderBy('book_configs.updated_at','desc');
  118. return $res->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
  119. }
  120. /**
  121. * 根据条件获取图书
  122. * @param array $where
  123. * @param array $order
  124. * @param int $page_size
  125. * @return mixed
  126. */
  127. public static function getPromotionBooks(array $where = [], array $bids, array $order = [], $page_size = 15)
  128. {
  129. if (!$order) {
  130. $order = ['recommend_index', 'desc'];
  131. }
  132. $res1 = self::join('books', 'book_configs.bid', '=', 'books.id')
  133. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  134. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.cp_source', 'book_configs.vip_seq', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
  135. 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
  136. 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword',
  137. 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
  138. 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
  139. 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  140. );
  141. if ($where) {
  142. foreach ($where as $key => $v) {
  143. //关键词查询
  144. if ($key == 'key' && $v) {
  145. $res1 = $res1->where('book_configs.book_name', 'like', '%' . $v . '%');
  146. }
  147. //分类id查询
  148. if ($key == 'category_id' && $v) {
  149. $res1 = $res1->where('books.category_id', '=', $v);
  150. }
  151. //上架查询
  152. if ($key == 'is_on_shelf' && $v != '') {
  153. if (is_array($v)) {
  154. $res1 = $res1->whereIn('is_on_shelf', $v);
  155. } else {
  156. $res1 = $res1->where('is_on_shelf', '=', $v);
  157. }
  158. }
  159. //频道查询
  160. if ($key == 'channel_name' && $v) {
  161. $res1 = $res1->where('book_categories.channel_name', '=', $v);
  162. }
  163. if ($key == 'status') {
  164. $res1 = $res1->where('books.status', '=', $v);
  165. }
  166. if ($key == 'author') {
  167. $res1 = $res1->where('books.author', 'like', '%' . $v . '%');
  168. }
  169. //旧书名查询
  170. if ($key == 'old_name') {
  171. $res1 = $res1->where('books.name', 'like', '%' . $v . '%');
  172. }
  173. //版权日期查询
  174. if ($key == 'copy_right_date') {
  175. if (is_array($v)) {
  176. $res1 = $res1->whereBetween('book_configs.copyright_limit_data', $v);
  177. } else {
  178. $res1 = $res1->where('book_configs.copyright_limit_data', '<=', $v);
  179. }
  180. }
  181. if ($key == 'domain') {
  182. $res1 = $res1->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
  183. }
  184. if ($key == 'bid') {
  185. $res1 = $res1->where('book_configs.bid', '=', $v);
  186. }
  187. if ($key == 'is_high_quality') {
  188. $res1 = $res1->where('book_configs.is_high_quality', '=', $v);
  189. }
  190. if ($key == 'charge_type') {
  191. $res1 = $res1->where('book_configs.charge_type', '=', $v);
  192. }
  193. if ($key == 'hidden_books') {
  194. $res1 = $res1->whereNotIn('book_configs.bid', $v);
  195. }
  196. }
  197. if ($bids) {
  198. //$res1 = $res1->orwhereIn('book_configs.id', $bids);
  199. if ((isset($where['key']) && !empty($where['key'])) && (isset($where['channel_name']) && !empty($where['channel_name']))) {
  200. $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
  201. $query->where('is_on_shelf', 1)
  202. ->whereIn('book_configs.bid', $bids)
  203. ->where('book_configs.book_name', 'like', '%' . $where['key'] . '%')
  204. ->where('book_categories.channel_name', '=', $where['channel_name']);;
  205. });
  206. } elseif (isset($where['key']) && !empty($where['key'])) {
  207. $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
  208. $query->where('is_on_shelf', 1)
  209. ->whereIn('book_configs.bid', $bids)
  210. ->where('book_configs.book_name', 'like', '%' . $where['key'] . '%');
  211. });
  212. } elseif (isset($where['channel_name']) && !empty($where['channel_name'])) {
  213. $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
  214. $query->where('is_on_shelf', 1)
  215. ->whereIn('book_configs.bid', $bids)
  216. ->where('book_categories.channel_name', '=', $where['channel_name']);
  217. });
  218. } else {
  219. $res1 = $res1->orwhereIn('book_configs.bid', $bids);
  220. }
  221. }
  222. }
  223. return $res1->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
  224. }
  225. /**
  226. * 根据id数组获取图书信息
  227. * @param array $bid_arr
  228. * @param array $order
  229. * @return mixed
  230. */
  231. public static function getBooksByIds(array $bid_arr, array $order = [])
  232. {
  233. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  234. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  235. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  236. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  237. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'book_configs.click_count',
  238. 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content',
  239. 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.updated_at as last_update_time',
  240. 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid'
  241. )
  242. ->whereIn('book_configs.bid', $bid_arr);
  243. if ($order) {
  244. $res->orderBy($order[0], $order[1]);
  245. } else {
  246. $str = implode(',', $bid_arr);
  247. $field = 'bid,' . $str;
  248. $res->orderBy(DB::raw('field(' . $field . ')'));
  249. }
  250. return $res->limit(30)->get();
  251. }
  252. /**
  253. * 根据bid获取图书信息
  254. * @param $bid
  255. * @return mixed
  256. */
  257. public static function getBookById($bid)
  258. {
  259. if (empty($bid)) return null;
  260. return self::join('books', 'book_configs.bid', '=', 'books.id')
  261. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  262. ->select('book_configs.bid', 'book_configs.is_on_shelf', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.cp_source', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
  263. 'book_configs.copyright', 'book_configs.created_at', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
  264. 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index',
  265. 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.updated_at as last_update_time',
  266. 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  267. )->where('book_configs.bid', $bid)->first();
  268. }
  269. /**
  270. * 根据关键词获取图书
  271. * @param $key
  272. * @param int $page_size
  273. * @param null $is_on_shelf
  274. * @return mixed
  275. */
  276. public static function getBooksByKey($key, $page_size = 15, $is_on_shelf = null)
  277. {
  278. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  279. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  280. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  281. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  282. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'book_configs.click_count',
  283. 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content',
  284. 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.updated_at as last_update_time',
  285. 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  286. )
  287. ->where('book_configs.book_name', 'like', '%' . $key . '%');
  288. $res = $res->whereIn('book_configs.is_on_shelf', [2])->orderBy('is_on_shelf', 'desc')->limit(5)->get();
  289. foreach ($res as $v) {
  290. $v->book_url = '/detail?id=' . Hashids::encode($v->bid);
  291. }
  292. return $res;
  293. }
  294. /**
  295. * 根据product_id获取图书
  296. * @param $product_id
  297. * @return mixed
  298. */
  299. public static function getBookByProduct($product_id)
  300. {
  301. return self::join('books', 'book_configs.bid', '=', 'books.id')
  302. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  303. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  304. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  305. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  306. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  307. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  308. )->where('book_configs.product_id', $product_id)->first();
  309. }
  310. /**
  311. * 更新图书
  312. * @param $bid
  313. * @param array $param
  314. * @return bool
  315. */
  316. public static function updateBookInfo($bid, array $param)
  317. {
  318. $book_info = self::getBookById($bid);
  319. if (!$book_info) return false;
  320. $update_data = [];
  321. if (isset($param['force_subscribe_chapter_seq']) && !empty($param['force_subscribe_chapter_seq'])) $update_data['force_subscribe_chapter_seq'] = $param['force_subscribe_chapter_seq'];
  322. if (isset($param['product_id']) && !empty($param['product_id'])) $update_data['product_id'] = $param['product_id'];
  323. if (isset($param['book_name']) && !empty($param['book_name'])) $update_data['book_name'] = $param['book_name'];
  324. if (isset($param['price']) && !empty($param['price'])) $update_data['price'] = $param['price'];
  325. if (isset($param['cover']) && !empty($param['cover'])) $update_data['cover'] = $param['cover'];
  326. if (isset($param['charge_type']) && !empty($param['charge_type'])) {
  327. $update_data['charge_type'] = $param['charge_type'];
  328. if (is_numeric($update_data['charge_type'])) {
  329. $update_data['charge_type'] = 'CHAPTER';
  330. }
  331. }
  332. if (isset($param['hot']) && !empty($param['hot'])) $update_data['hot'] = $param['hot'];
  333. if (isset($param['roles']) && !empty($param['roles'])) $update_data['roles'] = $param['roles'];
  334. if (isset($param['is_on_shelf']) && $param['is_on_shelf'] != '') $update_data['is_on_shelf'] = $param['is_on_shelf'];
  335. if (isset($param['recommend_index']) && !empty($param['recommend_index'])) $update_data['recommend_index'] = $param['recommend_index'];
  336. if (isset($param['is_show_index_content'])) $update_data['is_show_index_content'] = $param['is_show_index_content'];
  337. if (isset($param['click_count']) && $param['click_count'] != '') $update_data['click_count'] = $param['click_count'];
  338. if (isset($param['copyright_limit_data']) && $param['copyright_limit_data'] != '') $update_data['copyright_limit_data'] = $param['copyright_limit_data'];
  339. if (isset($param['promotion_domain']) && $param['promotion_domain'] != '') $update_data['promotion_domain'] = $param['promotion_domain'];
  340. if (isset($param['copyright']) && $param['copyright'] != '') $update_data['copyright'] = $param['copyright'];
  341. $res1 = null;
  342. if (isset($param['status'])) {
  343. $res1 = Book::where('id', $bid)->update(['status' => $param['status']]);
  344. }
  345. if (isset($param['book_category_id'])) {
  346. $catagory = BookCategory::select('category_name')->where('id', $param['book_category_id'])->first();
  347. Book::where('id', $bid)->update(['category_id' => $param['book_category_id'], 'category_name' => $catagory->category_name]);
  348. }
  349. if (empty($update_data) && !$res1) return false;
  350. return self::where('bid', $bid)->update($update_data);
  351. }
  352. /*
  353. * 获取渠道,用户的全部书籍列表
  354. */
  355. public static function getLeftRecommendBook($channel_name, $is_high_quality, $force_update = false): array
  356. {
  357. if ($force_update) {
  358. \Log::info('force_set_full_book_channel_name:' . $channel_name);
  359. Redis::set('full_book_channel_name:' . $channel_name, null);
  360. }
  361. // 存redis里面
  362. $full_book_bids = Redis::get('full_book_channel_name:' . $channel_name);
  363. $full_book_bids = json_decode($full_book_bids);
  364. if (!empty($full_book_bids)) {
  365. \Log::info('direct_get_full_book_bids_from_redis:' . $channel_name);
  366. } else {
  367. // 获取全集
  368. $full_book_bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  369. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  370. ->select('book_configs.bid')
  371. ->where('book_categories.channel_name', $channel_name)
  372. ->where('book_configs.is_high_quality', $is_high_quality)
  373. ->where('book_configs.test_status', 0)// 不含测书
  374. ->whereIn('book_configs.is_on_shelf', [2])
  375. ->orderBy('book_configs.id', 'desc')
  376. ->get()->pluck('bid')->all();
  377. if (!empty($full_book_bids)) {
  378. \Log::info('set_full_book_channel_name:' . $channel_name . ' data:' . json_encode($full_book_bids));
  379. Redis::set('full_book_channel_name:' . $channel_name, json_encode($full_book_bids));
  380. }
  381. }
  382. // \Log::info('$full_book_bids:'.json_encode($full_book_bids));
  383. return $full_book_bids;
  384. }
  385. /*
  386. * 获取渠道,用户的全部需要测试的书籍列表
  387. */
  388. public static function getLeftRecommendTestBook($channel_name)
  389. {
  390. // 获取test全集
  391. $test_bids = self::getLeftRecommendTestBookConfigs($channel_name);
  392. // \Log::info('getLeftRecommendTestBook:'.json_encode($test_bids));
  393. $last_bids = [];
  394. if (!empty($test_bids)) {
  395. foreach ($test_bids as $test_bid) {
  396. $bid = $test_bid->bid;
  397. $redis_bid_push_num = Redis::hget('SmartPushBookUserNum', $bid);
  398. $plan_push_user_num = $test_bid->plan_push_user_num;
  399. \Log::info('left_test_book,bid:' . $bid . ' redis_bid_push_num:' . $redis_bid_push_num . ' plan_push_user_num:' . $plan_push_user_num);
  400. if ($redis_bid_push_num >= $plan_push_user_num) {
  401. \Log::info('full_update_test_book,bid:' . $bid . ' redis_bid_push_num:' . $redis_bid_push_num . ' plan_push_user_num:' . $plan_push_user_num);
  402. self::where('bid', $bid)->update(['test_status' => 2, 'test_update_time' => date('Y-m-d H:i:s')]);
  403. continue;
  404. }
  405. $last_bids[] = $bid;
  406. }
  407. }
  408. return $last_bids;
  409. }
  410. /*
  411. * 获取渠道,用户的全部需要测试的书籍列表
  412. */
  413. public static function getLeftRecommendTestBookConfigs($channel_name)
  414. {
  415. // 获取全集
  416. return self::join('books', 'book_configs.bid', '=', 'books.id')
  417. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  418. ->select('book_configs.bid', 'book_configs.plan_push_user_num')
  419. ->where('book_categories.channel_name', $channel_name)
  420. ->where('book_configs.test_status', 1)// 待测
  421. ->groupBy('book_configs.bid')
  422. ->orderBy('book_configs.test_update_time', 'asc')
  423. ->limit(1)// 每次1本
  424. ->get();
  425. }
  426. /*
  427. * 获取用户的曾经推荐过的书籍列表
  428. */
  429. public static function getUserRecommendRecords($uid)
  430. {
  431. $recommend_bids = Redis::smembers('userRecommendBids:' . $uid);
  432. return $recommend_bids;
  433. }
  434. /*
  435. * 添加用户的曾经推荐过的书籍列表
  436. */
  437. public static function addUserRecommendRecords($uid, $bids)
  438. {
  439. foreach ($bids as $bid) {
  440. Redis::sadd('userRecommendBids:' . $uid, $bid);
  441. }
  442. }
  443. /*
  444. * 清楚用户的曾经推荐过的书籍列表
  445. */
  446. public static function truncateUserRecommendRecords($uid)
  447. {
  448. \Log::info('truncateUserRecommendRecords:' . $uid);
  449. Redis::del('userRecommendBids:' . $uid);
  450. }
  451. /*
  452. * 书籍推送量+1
  453. */
  454. public static function incrBidPushNum($last_bids)
  455. {
  456. if (!empty($last_bids)) {
  457. foreach ($last_bids as $last_bid) {
  458. // \Log::info('incrBidPushNum:test_bid' . $last_bid);
  459. Redis::hincrby('SmartPushBookUserNum', $last_bid, 1);
  460. }
  461. }
  462. }
  463. /*
  464. * 获取相同频道的4本高推荐图书
  465. * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
  466. */
  467. public static function getLeftRecommendBids($uid = '', $channel_name, $num = 4, $loop = 1)
  468. {
  469. // \Log::info('getLeftRecommendBids_loop:'.$loop.' $uid:'.$uid.' $channel_name:'.$channel_name.' $num:'.$num.' $loop:'.$loop);
  470. if ($loop > 3) {
  471. \Log::info('getLeftRecommendBids_return_loop:' . $loop . ' $uid:' . $uid . ' $channel_name:' . $channel_name);
  472. return [];
  473. }
  474. // 测试书籍优先
  475. $test_bids = self::getLeftRecommendTestBook($channel_name);
  476. // 获取全集,不含测试书籍
  477. $full_bid_no_tests = self::getLeftRecommendBook($channel_name, 1, false);
  478. $full_bids = array_merge($test_bids, $full_bid_no_tests);
  479. // 获取用户推荐过的详情
  480. $recommend_bids = self::getUserRecommendRecords($uid);
  481. // 获取用户阅读记录详情
  482. $read_bids = ReadRecordService::getSimpleReadRecord($uid);
  483. // 得到差集
  484. $left_bids = array_diff($full_bids, $recommend_bids, $read_bids);
  485. // \Log::info('full_bids:'.json_encode($full_bids).' full_bid_no_tests:'.json_encode($full_bid_no_tests).' test_bids:'.json_encode($test_bids).' recommend_bids:'.json_encode($recommend_bids).' read_bids:'.json_encode($read_bids).' left_bids:'.json_encode($left_bids));
  486. $left_bid_num = count($left_bids);
  487. // 如果不够则清空推荐记录,重新追加剩下的
  488. $last_bids = $need_bids = array();
  489. if ($left_bid_num < $num) {
  490. self::truncateUserRecommendRecords($uid);
  491. $need_bids = self::getLeftRecommendBids($uid, $channel_name, $num - $left_bid_num, $loop + 1);
  492. // \Log::info('full_bids2:'.json_encode($full_bids));
  493. // \Log::info('merge_left_bids:'.json_encode($left_bids).' need_bids:'.json_encode($need_bids));
  494. $last_bids = array_merge($left_bids, $need_bids);
  495. } else {
  496. $last_bids = array_slice($left_bids, 0, $num, false);
  497. }
  498. // \Log::info('uid:'.$uid.' last_bids:'.json_encode($last_bids));
  499. // 加入已经推荐
  500. self::addUserRecommendRecords($uid, $last_bids);
  501. // 书籍推送量+1
  502. if ($loop == 1) {
  503. self::incrBidPushNum($last_bids);
  504. }
  505. return $last_bids;
  506. }
  507. /*
  508. * 获取相同频道的4本高推荐图书(循环获取)
  509. * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
  510. */
  511. public static function getSimpleChannelBookLoop($bid, $num = 4, $uid = '')
  512. {
  513. // \Log::info('getSimpleChannelBookLoop:bid:'.$bid.' num:'.$num.' uid:'.$uid);
  514. if (empty($uid)) {
  515. return self::getSimpleChannelBook($bid, $num);
  516. }
  517. $book_info = self::getBookById($bid);
  518. $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '女频';
  519. // 获取全集,减去阅读记录和推荐过的书籍id
  520. $bids = self::getLeftRecommendBids($uid, $channel_name, $num, 1);
  521. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  522. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  523. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  524. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  525. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  526. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  527. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  528. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  529. )
  530. ->whereIn('book_configs.bid', $bids)
  531. ->orderBy('book_configs.id', 'desc')
  532. ->get();
  533. foreach ($res as $v) {
  534. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  535. }
  536. return $res;
  537. }
  538. public static function getHotRandomRecommendBooks($uid, $num = 2)
  539. {
  540. $sex = ForceSubscribeService::getSimpleSexByUid($uid);
  541. $channel_name = $sex == 1 ? '男频' : '女频';
  542. \Log::info('getHotRandomRecommendBooks,uid:' . $uid . ' num:' . $num . ' channel_name:' . $channel_name);
  543. $bids = [];
  544. $bids = self::getRandomRecommendBooks($channel_name, $num);
  545. return $bids;
  546. }
  547. public static function getSpecialHotRandomRecommendBooks($num = 2, $channel_name = '女频')
  548. {
  549. $bids = self::getRandomRecommendBooks($channel_name, $num);
  550. return self::getBidRecommendBooks($bids);
  551. }
  552. public static function getHotRandomRecommendBookText($distribution_channel_id, $uid, $num)
  553. {
  554. $bids = self::getHotRandomRecommendBooks($uid, $num);
  555. $recomm_books = BookConfigService::getBooksByIds($bids, ['bid', 'asc']);
  556. $content = '';
  557. if ($recomm_books) {
  558. // 判断是否不展示书名
  559. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  560. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  561. $recommend_titles = [];
  562. if ($hide_type == 'hide_book_name') {
  563. \Log::info('hide_book_name:' . $distribution_channel_id . ' uid:' . $uid);
  564. $sex = ForceSubscribeService::getSimpleSexByUid($uid);
  565. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  566. }
  567. $content .= "\n\n" . '热门书籍推荐';
  568. foreach ($recomm_books as $key => $book) {
  569. $book_name = $book->book_name;
  570. if ($hide_type == 'hide_book_name') {
  571. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book->book_name;
  572. }
  573. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  574. $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  575. }
  576. }
  577. return $content;
  578. }
  579. /**
  580. * 获取h5推荐的图书
  581. */
  582. public static function getH5RecommendBooks($uid, $pos, $num = 4)
  583. {
  584. $sex = ForceSubscribeService::getSimpleSexByUid($uid);
  585. $channel_name = $sex == 1 ? '男频' : '女频';
  586. \Log::info('getH5RecommendBooks:pos:' . $pos . ' uid:' . $uid . ' num:' . $num . ' channel_name:' . $channel_name);
  587. $bids = [];
  588. // 先从缓存取,1天有效期
  589. $h5_book_cache = Redis::get('userH5RecommendBids:' . $uid . ':' . $pos);
  590. if (!empty($h5_book_cache)) {
  591. \Log::info('h5_book_cache_exist:' . $uid);
  592. return json_decode($h5_book_cache);
  593. }
  594. $bids = [];
  595. $random_recommend = true;
  596. if ($random_recommend) {
  597. $bids = self::getRandomRecommendBooks($channel_name, $num);
  598. }
  599. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  600. $distribution_channel_id = isset($forceSubscribeUser->distribution_channel_id) ? $forceSubscribeUser->distribution_channel_id : '';
  601. \Log::info('getH5RecommendBooks:uid:' . $uid . ' distribution_channel_id:' . $distribution_channel_id . ' bids:' . json_encode($bids));
  602. if (!empty($bids)) {
  603. $books = BookConfigService::getBooksByIds($bids, ['bid', 'asc']);
  604. $data = $books->toArray();
  605. // 有效期24小时
  606. $redis_key = 'userH5RecommendBids:' . $uid . ':' . $pos;
  607. Redis::set($redis_key, json_encode(object_to_array($data)));
  608. Redis::expire($redis_key, 3600 * 24);
  609. }
  610. return $books;
  611. }
  612. /**
  613. * 获取随机的推荐书籍bid
  614. */
  615. public static function getRandomRecommendBooks($channel_name, $num)
  616. {
  617. $bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  618. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  619. ->select('book_configs.bid')
  620. ->where('book_categories.channel_name', $channel_name)
  621. ->where('book_configs.is_high_quality', 1)
  622. ->where('book_configs.test_status', 0)// 不含测书
  623. ->whereIn('book_configs.is_on_shelf', [2])
  624. ->inRandomOrder()
  625. ->limit($num)
  626. ->get()->pluck('bid')->all();
  627. return $bids;
  628. }
  629. /*
  630. * 获取相同频道的4本高推荐图书
  631. */
  632. public static function getSimpleChannelBook($bid, $num = 4)
  633. {
  634. $book_info = self::getBookById($bid);
  635. if (!$book_info) return false;
  636. $channel_name = $book_info->channel_name;
  637. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  638. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  639. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  640. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  641. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  642. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  643. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  644. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  645. )
  646. ->where('book_categories.channel_name', $channel_name)
  647. ->where('book_configs.is_high_quality', 1)
  648. ->whereIn('book_configs.is_on_shelf', [1, 2])
  649. ->orderBy('recommend_index', 'desc')
  650. ->get()
  651. ->random($num);
  652. foreach ($res as $v) {
  653. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  654. }
  655. return $res;
  656. }
  657. /*
  658. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  659. * 新版1条
  660. */
  661. public static function getTrusteeShipChannelBook($distribution_channel_id, $channel_name, $num = 4)
  662. {
  663. // 找头条
  664. $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  665. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  666. ->select('book_configs.bid', 'book_configs.recommend_index')
  667. ->where('book_categories.channel_name', $channel_name)
  668. ->where('book_configs.is_high_quality', 1)
  669. // ->where('book_configs.recommend_index', '>=',95)
  670. ->whereIn('book_configs.is_on_shelf', [2])
  671. ->orderBy('recommend_index', 'desc')
  672. ->limit(50)
  673. ->get()
  674. ->random(1);
  675. \Log::info('$first_res');
  676. \Log::info($first_res);
  677. $bids = [];
  678. $bids[] = $first_res[0]->bid;
  679. \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
  680. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  681. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  682. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  683. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  684. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  685. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  686. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  687. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  688. )
  689. ->where('book_categories.channel_name', $channel_name)
  690. ->where('book_configs.is_high_quality', 1)
  691. ->whereIn('book_configs.bid', $bids)
  692. ->orderBy('recommend_index', 'desc')
  693. ->get();
  694. foreach ($res as $v) {
  695. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  696. }
  697. \Log::info('last_res');
  698. \Log::info($res);
  699. return $res;
  700. }
  701. /*
  702. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  703. * 老版多条
  704. */
  705. public static function getTrusteeShipChannelBookMulty($distribution_channel_id, $channel_name, $num = 4)
  706. {
  707. // TODO 内部渠道的书=内部+外部,外部渠道的书=外部
  708. // 内部上架判断有点复杂,先统一外部上架
  709. // 找头条
  710. $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  711. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  712. ->select('book_configs.bid', 'book_configs.recommend_index')
  713. ->where('book_categories.channel_name', $channel_name)
  714. ->where('book_configs.is_high_quality', 1)
  715. // ->where('book_configs.recommend_index', '>=',95)
  716. ->whereIn('book_configs.is_on_shelf', [2])
  717. ->orderBy('recommend_index', 'desc')
  718. ->limit(10)
  719. ->get()
  720. ->random(1);
  721. \Log::info('$first_res');
  722. \Log::info($first_res);
  723. $bids = [];
  724. $bids[] = $first_res[0]->bid;
  725. // 找其余3条
  726. $left_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  727. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  728. ->select('book_configs.bid')
  729. ->where('book_categories.channel_name', $channel_name)
  730. ->where('book_configs.is_high_quality', 1)
  731. ->whereNotIn('book_configs.bid', $bids)
  732. ->whereIn('book_configs.is_on_shelf', [2])
  733. ->orderBy('recommend_index', 'desc')
  734. ->get()
  735. ->random(3);
  736. \Log::info('left_res');
  737. \Log::info($left_res);
  738. foreach ($left_res as $left_r) {
  739. $bids[] = $left_r->bid;
  740. }
  741. \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
  742. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  743. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  744. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  745. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  746. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  747. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  748. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  749. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  750. )
  751. ->where('book_categories.channel_name', $channel_name)
  752. ->where('book_configs.is_high_quality', 1)
  753. ->whereIn('book_configs.bid', $bids)
  754. ->orderBy('recommend_index', 'desc')
  755. ->get();
  756. foreach ($res as $v) {
  757. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  758. }
  759. \Log::info('last_res');
  760. \Log::info($res);
  761. return $res;
  762. }
  763. /*
  764. * H5专用,用户阅读完推荐
  765. * 获取相同推荐
  766. */
  767. public static function getRecommendBooks($bid, $channel_name, $num = 4)
  768. {
  769. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  770. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  771. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  772. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  773. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  774. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  775. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  776. , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
  777. )
  778. ->where('book_categories.channel_name', $channel_name)
  779. ->where('book_configs.bid', '!=', $bid)
  780. ->where('book_configs.is_high_quality', 1)
  781. ->orderBy('recommend_index', 'desc')->get();
  782. $count = $res->count() >= $num ? $num : $res->count();
  783. return $res->random($count);
  784. }
  785. /*
  786. * 签到专用
  787. * 获取相同推荐
  788. */
  789. public static function getSignRecommendBooks(array $bid, $channel_name, $num = 2)
  790. {
  791. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  792. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  793. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  794. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  795. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  796. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  797. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  798. , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
  799. )
  800. ->where('book_categories.channel_name', $channel_name)
  801. ->whereIn('book_configs.is_on_shelf', [2])
  802. ->whereNotIn('book_configs.bid', $bid)
  803. ->where('book_configs.is_high_quality', 1)
  804. ->get();
  805. $count = $res->count() >= $num ? $num : $res->count();
  806. return $res->random($count);
  807. }
  808. /*
  809. * 签到专用缓存版本
  810. * 获取相同推荐
  811. */
  812. public static function getSignRecommendBooksFromCache(array $bid, $channel_name, $num = 2)
  813. {
  814. $key = '';
  815. if ($channel_name == '女频') $key = 'female_high_reco_books';
  816. if ($channel_name == '男频') $key = 'male_high_reco_books';
  817. $reco_books = Redis::hgetall($key);
  818. //去除已经阅读过图书
  819. foreach ($bid as $_bid) {
  820. unset($reco_books[$_bid]);
  821. }
  822. $return_bids = array_rand($reco_books, $num);
  823. $return_books = [];
  824. foreach ($return_bids as $item) {
  825. $return_books[] = json_decode($reco_books[$item]);//object
  826. }
  827. return $return_books;
  828. }
  829. //获取男、女频优质书库图书
  830. public static function getChannelHighRecommendBooks($channel_name)
  831. {
  832. return self::join('books', 'book_configs.bid', '=', 'books.id')
  833. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  834. ->select('book_configs.bid', 'book_configs.book_name', 'first_cid')
  835. ->where('book_categories.channel_name', $channel_name)
  836. ->whereIn('book_configs.is_on_shelf', [2])
  837. ->where('book_configs.is_high_quality', 1)
  838. ->get();
  839. }
  840. /*
  841. * 获取指定bid的书籍推荐
  842. */
  843. public static function getBidRecommendBooks(array $bids)
  844. {
  845. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  846. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  847. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  848. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  849. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  850. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  851. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  852. , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
  853. )
  854. ->whereIn('book_configs.bid', $bids)
  855. ->get();
  856. if (!empty($res)) {
  857. foreach ($res as $v) {
  858. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  859. }
  860. }
  861. return $res;
  862. }
  863. /*
  864. * 获取指定bid的阅读链接
  865. */
  866. public static function getBidReadUrl($bid, $chapter_num)
  867. {
  868. $offset_chapter_num = $chapter_num - 2;
  869. $res = DB::select('select chapters.bid,chapters.sequence,chapters.next_cid
  870. from chapters where bid=' . $bid . '
  871. order by sequence asc
  872. limit 1
  873. offset ' . $offset_chapter_num);
  874. $read_url = '';
  875. if (!empty($res)) {
  876. foreach ($res as $v) {
  877. $read_url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->next_cid;
  878. break;
  879. }
  880. }
  881. if (empty($read_url)) $read_url = '?';
  882. \Log::Info('getBidReadUrl,$bid:' . $bid . ' $chapter_num:' . $chapter_num . ' $read_url:' . $read_url);
  883. return $read_url;
  884. }
  885. /**
  886. * 修改vip章节
  887. */
  888. public static function updateVipSeq($bid, $seq)
  889. {
  890. return self::where('bid', $bid)->update(['vip_seq' => $seq]);
  891. }
  892. public static function getAllBooks($on_shelf, $order)
  893. {
  894. if (!$order) {
  895. $order = ['id', 'asc'];
  896. }
  897. return self::whereIn('is_on_shelf', $on_shelf)->select('bid', 'book_name')->orderBy($order[0], $order[1])->get();
  898. }
  899. /**
  900. * 根据条件获取书籍,没有分页
  901. */
  902. public static function getBooksNoPage(array $where = [], array $order = [], array $on_shelf, $limit = 20)
  903. {
  904. if (!$order) {
  905. $order = [['recommend_index', 'desc']];
  906. }
  907. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  908. ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
  909. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.cp_source', 'book_configs.vip_seq', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
  910. 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
  911. 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword',
  912. 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
  913. 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
  914. 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  915. );
  916. if ($where) {
  917. foreach ($where as $v) {
  918. $res->where($v[0], $v[1], $v[2]);
  919. }
  920. }
  921. $res->whereIn('is_on_shelf', $on_shelf);
  922. foreach ($order as $v) {
  923. $res->orderBy($v[0], $v[1]);
  924. }
  925. return $res->limit($limit)->get();
  926. }
  927. public static function getAllCps()
  928. {
  929. $cps = self::select('cp_source')->groupBy('cp_source')->get();
  930. $result = array();
  931. foreach ($cps as $cp) {
  932. if (!empty($cp['cp_source'])) {
  933. $result[] = $cp['cp_source'];
  934. }
  935. }
  936. return $result;
  937. }
  938. public static function getAllCpBooks()
  939. {
  940. $result = array();
  941. $cp_books = self::select('cp_source', 'bid')->groupBy('cp_source')->groupBy('bid')->get();
  942. foreach ($cp_books as $cp_book) {
  943. $result[$cp_book['cp_source']][] = $cp_book['bid'];
  944. }
  945. return $result;
  946. }
  947. /*
  948. * 得到cp某段时间某本书的消费
  949. */
  950. public static function getAllCpBookConsume($start_date, $end_date, $cps, $yestoday)
  951. {
  952. $result = self::leftjoin('book_order_statistical', 'book_order_statistical.bid', '=', 'book_configs.bid')
  953. //
  954. ->leftjoin('cp_book_time_configs', function ($join) {
  955. $join->on('cp_book_time_configs.bid', '=', 'book_configs.bid')
  956. ->on('cp_book_time_configs.cp_source', '=', 'book_configs.cp_source');
  957. })
  958. ->select('book_order_statistical.bid',
  959. DB::raw('sum(book_order_statistical.charge_balance) as charge_balance'),
  960. DB::raw('(select book_order_statistical.charge_balance from book_order_statistical where book_order_statistical.bid=book_configs.bid and day="' . $yestoday . '" limit 1) as yestoday_charge_balance'),
  961. 'book_configs.book_name', 'book_configs.is_on_shelf', 'book_configs.cp_source'
  962. )
  963. ->whereIn('book_configs.cp_source', $cps)
  964. // 2个起始值取最大值
  965. ->where('book_order_statistical.day', '>=', DB::raw("ifnull(cp_book_time_configs.start_date,'')"))
  966. ->where('book_order_statistical.day', '>=', $start_date)
  967. ->where('book_order_statistical.day', '<=', $end_date)
  968. ->groupBy('book_configs.cp_source')
  969. ->groupBy('book_order_statistical.bid')
  970. ->get();
  971. return $result;
  972. }
  973. public static function getBookByIdAndStatus($bid, $status)
  974. {
  975. return self::where('bid', $bid)->whereIn('is_on_shelf', $status)->first();
  976. }
  977. public static function get_all_test_books($is_all = false)
  978. {
  979. if ($is_all) {
  980. return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->get();
  981. } else {
  982. return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->paginate();
  983. }
  984. }
  985. public static function get_test_books($test_status)
  986. {
  987. return self::where('test_status', $test_status)->get();
  988. }
  989. public static function updateTestBook($bid, $test_status, $plan_push_user_num = 10000)
  990. {
  991. return self::where('bid', $bid)->update(['test_status' => $test_status, 'plan_push_user_num' => $plan_push_user_num, 'test_update_time' => date('Y-m-d H:i:s')]);
  992. }
  993. public static function get_all_smart_push_books($is_all = false)
  994. {
  995. if ($is_all) {
  996. return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->get();
  997. } else {
  998. return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->paginate();
  999. }
  1000. }
  1001. /**
  1002. * 获取书本的id,名称,作者,封面 并带分页
  1003. * @param bool $isAll 是否查询所有
  1004. * @param int $pageSize 每页的条数
  1005. * @param int $pageCount 页数
  1006. * @return mixed
  1007. */
  1008. static function getBookCoverInfos($isAll = false, $pageSize = 200, $pageCount = 0)
  1009. {
  1010. $obj = self::join('books', 'book_configs.bid', '=', 'books.id')->select('book_configs.bid', 'book_configs.book_name', 'books.author', 'book_configs.cover');
  1011. if ($isAll) {
  1012. return $obj->get();
  1013. } else {
  1014. return $obj->limit($pageSize)->offset($pageCount * $pageSize)->get();
  1015. }
  1016. }
  1017. /**
  1018. * 获取书本的总数
  1019. * @return mixed
  1020. */
  1021. static function getBooksCount()
  1022. {
  1023. return self::count();
  1024. }
  1025. /**
  1026. * 通过书名模糊搜索bid
  1027. * @param $book_name
  1028. * @return mixed
  1029. */
  1030. static function getIdByName($book_name)
  1031. {
  1032. return self::select('bid')->where('book_name', 'like', '%' . $book_name . '%')->get();
  1033. }
  1034. //关键词搜(书名、主角)
  1035. static function getFromRole($key)
  1036. {
  1037. return self::leftjoin('book_roles', 'book_configs.bid', '=', 'book_roles.bid')
  1038. ->where('book_roles.role', $key)
  1039. ->whereIn('is_on_shelf', [1])
  1040. ->orderBy('is_on_shelf', 'desc')
  1041. ->first();
  1042. }
  1043. /*
  1044. * 获取相同频道的4本高推荐图书
  1045. * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
  1046. */
  1047. public static function getLeftRecomBids($uid, $channel_name, $num = 4, $loop = 1)
  1048. {
  1049. // 获取用户阅读记录详情
  1050. $read_bids = ReadRecordService::getSimpleReadRecord($uid);
  1051. // 获取用户推荐过的详情
  1052. $recommend_bids = self::getUserRecommendRecords($uid);
  1053. // 获取全集,不含测试书籍
  1054. $full_bids = $full_bid_no_tests = self::getLeftRecommendBook($channel_name, 1, false);
  1055. \Log::info('uid:'.$uid.' read_bids:' . json_encode($read_bids) . ' recommend_bids:' . json_encode($recommend_bids). ' full_bids:'.json_encode($full_bids));
  1056. //判断如果注册时间小于10天,不推测试书籍
  1057. $test_book = null;
  1058. $test_bid = 0;
  1059. $last_bids = [];
  1060. try {
  1061. if ($uid) {
  1062. $user = UserService::getById($uid);
  1063. if ($user && (time() - strtotime($user->created_at) > 10 * 86400)) {
  1064. $channel_sex = $channel_name === '男频' ? 1 : 2;
  1065. $temp = TestBookService::getTestBook($channel_sex);
  1066. if ($temp && !in_array($temp->bid, $read_bids)) {
  1067. $test_book = $temp;
  1068. $test_bid = $temp->bid;
  1069. }
  1070. }
  1071. }
  1072. \Log::info('test_book:' . json_encode($test_book));
  1073. $test_book && $full_bids = array_merge([$test_book->bid], $full_bid_no_tests);
  1074. // 得到差集
  1075. $left_bids = array_diff($full_bids, $recommend_bids, $read_bids);
  1076. $left_bid_num = count($left_bids);
  1077. if ($left_bid_num) {
  1078. $last_bids = array_slice($left_bids, 0, $num, false);
  1079. } else {
  1080. //判断推送的书都被阅读情况下 随机从优质书中取
  1081. if (!$not_read_bids = array_diff($full_bids, $read_bids)) {
  1082. $last_bids[] = $full_bid_no_tests[array_rand($full_bid_no_tests)];
  1083. } else {
  1084. //推送过但未阅读情况下,清空继续推
  1085. self::truncateUserRecommendRecords($uid);
  1086. $last_bids = array_slice($not_read_bids, 0, $num, false);
  1087. }
  1088. }
  1089. // 加入已经推荐
  1090. self::addUserRecommendRecords($uid, $last_bids);
  1091. // 书籍推送量+1
  1092. self::incrBidPushNum($last_bids);
  1093. foreach ($last_bids as $_bid) {
  1094. if ($test_book && $test_book->bid == $_bid) {
  1095. $redis_bid_push_num = Redis::hget('SmartPushBookUserNum', $_bid);
  1096. $plan_push_user_num = $test_book->plan_push_user_num;
  1097. $uid && TestBookService::record($test_book->id, $uid);
  1098. if ($redis_bid_push_num >= $plan_push_user_num) {
  1099. TestBookService::finishTest($_bid);
  1100. }
  1101. }
  1102. }
  1103. } catch (Exception $e) {
  1104. \Illuminate\Support\Facades\Log::error('get test book error:' . $e->getMessage() . $e->getLine());
  1105. $last_bids = [$full_bid_no_tests[array_rand($full_bid_no_tests)]];
  1106. }
  1107. \Log::info('last_bids:' . json_encode($last_bids));
  1108. return [$last_bids, $test_bid];
  1109. }
  1110. //获取图书
  1111. public static function getSimpleBooksByIds($bids)
  1112. {
  1113. $fields = ['book_name','bid','cover'];
  1114. return self::select($fields)->whereIn('id',$bids)->where('is_on_shelf',2)->order('id','desc')->get();
  1115. }
  1116. }