BookConfig.php 52 KB

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