BookConfig.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. $bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  640. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  641. ->select('book_configs.bid')
  642. ->where('book_categories.channel_name', $channel_name)
  643. ->where('book_configs.is_high_quality', 1)
  644. ->where('book_configs.test_status', 0)// 不含测书
  645. ->whereIn('book_configs.is_on_shelf', [2])
  646. ->inRandomOrder()
  647. ->limit($num)
  648. ->get()->pluck('bid')->all();
  649. return $bids;
  650. }
  651. /*
  652. * 获取相同频道的4本高推荐图书
  653. */
  654. public static function getSimpleChannelBook($bid, $num = 4)
  655. {
  656. $book_info = self::getBookById($bid);
  657. if (!$book_info) return false;
  658. $channel_name = $book_info->channel_name;
  659. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  660. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  661. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  662. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  663. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  664. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  665. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  666. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  667. )
  668. ->where('book_categories.channel_name', $channel_name)
  669. ->where('book_configs.is_high_quality', 1)
  670. ->whereIn('book_configs.is_on_shelf', [1, 2])
  671. ->orderBy('recommend_index', 'desc')
  672. ->get()
  673. ->random($num);
  674. foreach ($res as $v) {
  675. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  676. }
  677. return $res;
  678. }
  679. /*
  680. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  681. * 新版1条
  682. */
  683. public static function getTrusteeShipChannelBook($distribution_channel_id, $channel_name, $num = 4)
  684. {
  685. // 找头条
  686. $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  687. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  688. ->select('book_configs.bid', 'book_configs.recommend_index')
  689. ->where('book_categories.channel_name', $channel_name)
  690. ->where('book_configs.is_high_quality', 1)
  691. // ->where('book_configs.recommend_index', '>=',95)
  692. ->whereIn('book_configs.is_on_shelf', [2])
  693. ->orderBy('recommend_index', 'desc')
  694. ->limit(50)
  695. ->get()
  696. ->random(1);
  697. \Log::info('$first_res');
  698. \Log::info($first_res);
  699. $bids = [];
  700. $bids[] = $first_res[0]->bid;
  701. \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
  702. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  703. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  704. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  705. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  706. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  707. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  708. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  709. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  710. )
  711. ->where('book_categories.channel_name', $channel_name)
  712. ->where('book_configs.is_high_quality', 1)
  713. ->whereIn('book_configs.bid', $bids)
  714. ->orderBy('recommend_index', 'desc')
  715. ->get();
  716. foreach ($res as $v) {
  717. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  718. }
  719. \Log::info('last_res');
  720. \Log::info($res);
  721. return $res;
  722. }
  723. /*
  724. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  725. * 老版多条
  726. */
  727. public static function getTrusteeShipChannelBookMulty($distribution_channel_id, $channel_name, $num = 4)
  728. {
  729. // TODO 内部渠道的书=内部+外部,外部渠道的书=外部
  730. // 内部上架判断有点复杂,先统一外部上架
  731. // 找头条
  732. $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  733. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  734. ->select('book_configs.bid', 'book_configs.recommend_index')
  735. ->where('book_categories.channel_name', $channel_name)
  736. ->where('book_configs.is_high_quality', 1)
  737. // ->where('book_configs.recommend_index', '>=',95)
  738. ->whereIn('book_configs.is_on_shelf', [2])
  739. ->orderBy('recommend_index', 'desc')
  740. ->limit(10)
  741. ->get()
  742. ->random(1);
  743. \Log::info('$first_res');
  744. \Log::info($first_res);
  745. $bids = [];
  746. $bids[] = $first_res[0]->bid;
  747. // 找其余3条
  748. $left_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  749. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  750. ->select('book_configs.bid')
  751. ->where('book_categories.channel_name', $channel_name)
  752. ->where('book_configs.is_high_quality', 1)
  753. ->whereNotIn('book_configs.bid', $bids)
  754. ->whereIn('book_configs.is_on_shelf', [2])
  755. ->orderBy('recommend_index', 'desc')
  756. ->get()
  757. ->random(3);
  758. \Log::info('left_res');
  759. \Log::info($left_res);
  760. foreach ($left_res as $left_r) {
  761. $bids[] = $left_r->bid;
  762. }
  763. \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
  764. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  765. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  766. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
  767. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  768. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  769. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  770. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  771. , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  772. )
  773. ->where('book_categories.channel_name', $channel_name)
  774. ->where('book_configs.is_high_quality', 1)
  775. ->whereIn('book_configs.bid', $bids)
  776. ->orderBy('recommend_index', 'desc')
  777. ->get();
  778. foreach ($res as $v) {
  779. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  780. }
  781. \Log::info('last_res');
  782. \Log::info($res);
  783. return $res;
  784. }
  785. /*
  786. * H5专用,用户阅读完推荐
  787. * 获取相同推荐
  788. */
  789. public static function getRecommendBooks($bid, $channel_name, $num = 4)
  790. {
  791. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  792. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  793. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  794. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  795. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  796. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  797. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  798. , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
  799. )
  800. ->where('book_categories.channel_name', $channel_name)
  801. ->where('book_configs.bid', '!=', $bid)
  802. ->where('book_configs.is_high_quality', 1)
  803. ->orderBy('recommend_index', 'desc')->get();
  804. $count = $res->count() >= $num ? $num : $res->count();
  805. return $res->random($count);
  806. }
  807. /*
  808. * 签到专用
  809. * 获取相同推荐
  810. */
  811. public static function getSignRecommendBooks(array $bid, $channel_name, $num = 2)
  812. {
  813. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  814. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  815. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  816. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  817. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  818. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  819. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  820. , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
  821. )
  822. ->where('book_categories.channel_name', $channel_name)
  823. ->whereIn('book_configs.is_on_shelf', [1,2])
  824. ->whereNotIn('book_configs.bid', $bid)
  825. ->where('book_configs.is_high_quality', 1)
  826. ->get();
  827. $count = $res->count() >= $num ? $num : $res->count();
  828. return $res->random($count);
  829. }
  830. /*
  831. * 获取指定bid的书籍推荐
  832. */
  833. public static function getBidRecommendBooks(array $bids)
  834. {
  835. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  836. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  837. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
  838. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  839. 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
  840. 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
  841. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
  842. , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
  843. )
  844. ->whereIn('book_configs.bid', $bids)
  845. ->get();
  846. return $res;
  847. }
  848. /**
  849. * 修改vip章节
  850. */
  851. public static function updateVipSeq($bid, $seq)
  852. {
  853. return self::where('bid', $bid)->update(['vip_seq' => $seq]);
  854. }
  855. public static function getAllBooks($on_shelf, $order)
  856. {
  857. if (!$order) {
  858. $order = ['id', 'asc'];
  859. }
  860. return self::whereIn('is_on_shelf', $on_shelf)->select('bid', 'book_name')->orderBy($order[0], $order[1])->get();
  861. }
  862. /**
  863. * 根据条件获取书籍,没有分页
  864. */
  865. public static function getBooksNoPage(array $where = [], array $order = [], array $on_shelf, $limit = 20)
  866. {
  867. if (!$order) {
  868. $order = [['recommend_index', 'desc']];
  869. }
  870. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  871. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  872. ->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',
  873. 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
  874. 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword',
  875. 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
  876. 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
  877. 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
  878. );
  879. if ($where) {
  880. foreach ($where as $v) {
  881. $res->where($v[0], $v[1], $v[2]);
  882. }
  883. }
  884. $res->whereIn('is_on_shelf', $on_shelf);
  885. foreach ($order as $v) {
  886. $res->orderBy($v[0], $v[1]);
  887. }
  888. return $res->limit($limit)->get();
  889. }
  890. public static function getAllCps()
  891. {
  892. $cps = self::select('cp_source')->groupBy('cp_source')->get();
  893. $result = array();
  894. foreach ($cps as $cp) {
  895. if (!empty($cp['cp_source'])) {
  896. $result[] = $cp['cp_source'];
  897. }
  898. }
  899. return $result;
  900. }
  901. public static function getAllCpBooks()
  902. {
  903. $result = array();
  904. $cp_books = self::select('cp_source', 'bid')->groupBy('cp_source')->groupBy('bid')->get();
  905. foreach ($cp_books as $cp_book) {
  906. $result[$cp_book['cp_source']][] = $cp_book['bid'];
  907. }
  908. return $result;
  909. }
  910. /*
  911. * 得到cp某段时间某本书的消费
  912. */
  913. public static function getAllCpBookConsume($start_date, $end_date, $cps)
  914. {
  915. $result = self::leftjoin('book_order_statistical', 'book_order_statistical.bid', '=', 'book_configs.bid')
  916. ->select('book_order_statistical.bid',
  917. DB::raw('sum(book_order_statistical.charge_balance) as charge_balance'),
  918. 'book_configs.book_name', 'book_configs.is_on_shelf', 'book_configs.cp_source'
  919. )
  920. ->whereIn('book_configs.cp_source', $cps)
  921. ->where('book_order_statistical.day', '>=', $start_date)
  922. ->where('book_order_statistical.day', '<=', $end_date)
  923. ->groupBy('book_configs.cp_source')
  924. ->groupBy('book_order_statistical.bid')
  925. ->get();
  926. return $result;
  927. }
  928. public static function getBookByIdAndStatus($bid, $status)
  929. {
  930. return self::where('bid', $bid)->whereIn('is_on_shelf', $status)->first();
  931. }
  932. public static function get_all_test_books($is_all=false)
  933. {
  934. if($is_all){
  935. return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->get();
  936. }else{
  937. return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->paginate();
  938. }
  939. }
  940. public static function get_test_books($test_status)
  941. {
  942. return self::where('test_status', $test_status)->get();
  943. }
  944. public static function updateTestBook($bid, $test_status, $plan_push_user_num = 10000)
  945. {
  946. 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')]);
  947. }
  948. public static function get_all_smart_push_books($is_all = false)
  949. {
  950. if($is_all){
  951. return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->get();
  952. }else{
  953. return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->paginate();
  954. }
  955. }
  956. /**
  957. * 获取书本的id,名称,作者,封面 并带分页
  958. * @param bool $isAll 是否查询所有
  959. * @param int $pageSize 每页的条数
  960. * @param int $pageCount 页数
  961. * @return mixed
  962. */
  963. static function getBookCoverInfos($isAll = false, $pageSize = 200, $pageCount = 0)
  964. {
  965. $obj = self::join('books', 'book_configs.bid', '=', 'books.id')->select('book_configs.bid', 'book_configs.book_name', 'books.author', 'book_configs.cover');
  966. if ($isAll) {
  967. return $obj->get();
  968. } else {
  969. return $obj->limit($pageSize)->offset($pageCount * $pageSize)->get();
  970. }
  971. }
  972. /**
  973. * 获取书本的总数
  974. * @return mixed
  975. */
  976. static function getBooksCount()
  977. {
  978. return self::count();
  979. }
  980. /**
  981. * 通过书名模糊搜索bid
  982. * @param $book_name
  983. * @return mixed
  984. */
  985. static function getIdByName($book_name)
  986. {
  987. return self::select('bid')->where('book_name', 'like', '%' . $book_name . '%')->get();
  988. }
  989. }