BookConfig.php 51 KB

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