BookConfig.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. <?php
  2. namespace App\Modules\Book\Models;
  3. use App\Modules\Book\Services\BookRoleService;
  4. use App\Modules\Book\Services\BookTagsService;
  5. use App\Modules\User\Services\ReadRecordService;
  6. use App\Modules\Book\Services\BookConfigService;
  7. use App\Modules\Channel\Services\ChannelService;
  8. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  9. use DB;
  10. use Hashids;
  11. use Illuminate\Database\Eloquent\Model;
  12. use App\Modules\User\Models\User;
  13. use Log;
  14. use Redis;
  15. class BookConfig extends Model
  16. {
  17. protected $table = 'book_configs';
  18. protected $fillable = [
  19. 'bid', 'force_subscribe_chapter_seq', 'price', 'cover', 'book_name',
  20. 'copyright', 'charge_type', 'hot', 'is_on_shelf', 'source_domain', 'recommend_index', 'roles', 'test_status', 'plan_push_user_num', 'test_update_time',
  21. 'is_show_index_content', 'click_count', 'promotion_domain', 'copyright_limit_data', 'recommend_cid', 'is_high_quality', 'vip_seq', 'editor_recommend', 'is_current_week_promotion'
  22. ];
  23. /*
  24. public static function getBooks($where, $order, $page_size = 15, $category = [], $key = null)
  25. {
  26. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  27. ->join('book_categories', 'books.category_id', 'book_categories.id')
  28. ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.price',
  29. 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
  30. 'books.author', 'books.intro', 'books.category_name', 'books.category_id', 'status', 'chapter_count', 'book_configs.click_count',
  31. 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id'
  32. , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter'
  33. )
  34. ->where($where)
  35. ->orderBy($order[0], $order[1]);
  36. if ($category) {
  37. $res = $res->whereIn('books.category_id', $category);
  38. }
  39. if ($key) {
  40. $res = $res->orwhere('book_configs.name', 'like', $key['name'])->orwhere('books.author', 'like', $key['author']);
  41. }
  42. return $res->paginate($page_size);
  43. }
  44. */
  45. /**
  46. * 根据条件获取图书
  47. * @param array $where
  48. * @param array $order
  49. * @param int $page_size
  50. * @return mixed
  51. */
  52. public static function getBooks(array $where = [], array $order = [], $page_size = 15)
  53. {
  54. if (!$order) {
  55. $order = ['recommend_index', 'desc'];
  56. }
  57. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  58. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  59. ->select(
  60. 'book_configs.bid',
  61. 'book_configs.roles',
  62. 'book_configs.force_subscribe_chapter_seq',
  63. 'book_configs.cp_source',
  64. 'book_configs.vip_seq',
  65. 'book_configs.price',
  66. 'book_configs.cover',
  67. 'book_configs.book_name',
  68. 'book_configs.copyright',
  69. 'book_configs.charge_type',
  70. 'book_configs.is_on_shelf',
  71. 'books.author',
  72. 'books.intro',
  73. 'book_categories.category_name',
  74. 'category_id',
  75. 'status',
  76. 'chapter_count',
  77. 'book_configs.click_count',
  78. 'first_cid',
  79. 'last_cid',
  80. 'books.size',
  81. 'last_chapter',
  82. 'books.keyword',
  83. 'book_configs.recommend_index',
  84. 'book_configs.is_show_index_content',
  85. 'book_configs.product_id',
  86. 'book_categories.channel_name',
  87. 'books.last_cid',
  88. 'books.last_chapter',
  89. 'book_configs.product_id',
  90. 'book_configs.copyright_limit_data',
  91. 'books.updated_at as last_update_time',
  92. 'book_configs.promotion_domain',
  93. 'books.name as old_name',
  94. 'book_configs.recommend_cid',
  95. 'book_configs.is_high_quality',
  96. 'is_current_week_promotion'
  97. );
  98. if ($where) {
  99. foreach ($where as $key => $v) {
  100. //关键词查询
  101. if ($key == 'key' && $v) {
  102. //$res = $res->where('book_configs.book_name', 'like', '%' . $v . '%');
  103. $res = $res->where(function ($query) use ($v) {
  104. $query->where('book_configs.book_name', 'like', '%' . $v . '%');
  105. if (mb_strlen($v) <= 5) {
  106. $roles_bids = BookRoleService::getBidsByRole($v);
  107. if (count($roles_bids) > 0) {
  108. $query->orWhereIn('book_configs.bid', $roles_bids);
  109. }
  110. }
  111. });
  112. //->orWhere('books.intro', 'like', '%' . $v . '%')
  113. //->orWhere('books.category_name', 'like', '%' . $v . '%')->orWhere('books.author', 'like', '%' . $v . '%')
  114. //->orWhere('books.keyword', 'like', '%' . $v . '%');
  115. }
  116. //分类id查询
  117. if ($key == 'category_id' && $v) {
  118. $res = $res->where('books.category_id', '=', $v);
  119. }
  120. //上架查询
  121. if ($key == 'is_on_shelf' && $v != '') {
  122. if (is_array($v)) {
  123. $res = $res->whereIn('is_on_shelf', $v);
  124. } else {
  125. $res = $res->where('is_on_shelf', '=', $v);
  126. }
  127. }
  128. //频道查询
  129. if ($key == 'channel_name' && $v) {
  130. $res = $res->where('book_categories.channel_name', '=', $v);
  131. }
  132. if ($key == 'status') {
  133. $res = $res->where('books.status', '=', $v);
  134. }
  135. if ($key == 'author') {
  136. $res = $res->where('books.author', 'like', '%' . $v . '%');
  137. }
  138. if ($key == 'roles') {
  139. $res = $res->where('book_configs.roles', 'like', '%' . $v . '%');
  140. }
  141. //旧书名查询
  142. if ($key == 'old_name') {
  143. $res = $res->where('books.name', 'like', '%' . $v . '%');
  144. }
  145. //版权日期查询
  146. if ($key == 'copy_right_date') {
  147. if (is_array($v)) {
  148. $res = $res->whereBetween('book_configs.copyright_limit_data', $v);
  149. } else {
  150. $res = $res->where('book_configs.copyright_limit_data', '<=', $v);
  151. }
  152. }
  153. if ($key == 'domain') {
  154. $res = $res->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
  155. }
  156. if ($key == 'bid') {
  157. $res = $res->where('book_configs.bid', '=', $v);
  158. }
  159. if ($key == 'is_high_quality') {
  160. $res = $res->where('book_configs.is_high_quality', '=', $v);
  161. }
  162. if ($key == 'charge_type') {
  163. $res = $res->where('book_configs.charge_type', '=', $v);
  164. }
  165. if ($key == 'firstChapterContent') {
  166. $res = $res->join('chapters', function ($query) use ($v) {
  167. $query->on('book_configs.bid', '=', 'chapters.bid')
  168. ->where('chapters.sequence', 1);
  169. })->where('chapters.content', 'like', '%' . $v . '%');
  170. }
  171. if ($key == 'tags') {
  172. $tags_filter = BookTagsService::getSearchBooks($v);
  173. $res->whereIn('book_configs.bid', $tags_filter);
  174. }
  175. if ($key == 'is_current_week_promotion') {
  176. $res->where('book_configs.is_current_week_promotion', $v);
  177. }
  178. }
  179. }
  180. //$res->orderBy('book_configs.updated_at','desc');
  181. return $res->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
  182. }
  183. /**
  184. * 根据条件获取图书
  185. * @param array $where
  186. * @param array $order
  187. * @param int $page_size
  188. * @return mixed
  189. */
  190. public static function getPromotionBooks(array $where = [], array $bids, array $order = [], $page_size = 15)
  191. {
  192. if (!$order) {
  193. $order = ['recommend_index', 'desc'];
  194. }
  195. $res1 = self::join('books', 'book_configs.bid', '=', 'books.id')
  196. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  197. ->select(
  198. 'book_configs.bid',
  199. 'book_configs.force_subscribe_chapter_seq',
  200. 'book_configs.cp_source',
  201. 'book_configs.vip_seq',
  202. 'book_configs.price',
  203. 'book_configs.cover',
  204. 'book_configs.book_name',
  205. 'book_configs.copyright',
  206. 'book_configs.charge_type',
  207. 'book_configs.is_on_shelf',
  208. 'books.author',
  209. 'books.intro',
  210. 'book_categories.category_name',
  211. 'category_id',
  212. 'status',
  213. 'chapter_count',
  214. 'book_configs.click_count',
  215. 'first_cid',
  216. 'last_cid',
  217. 'size',
  218. 'last_chapter',
  219. 'books.keyword',
  220. 'book_configs.recommend_index',
  221. 'book_configs.is_show_index_content',
  222. 'book_configs.product_id',
  223. 'book_categories.channel_name',
  224. 'books.last_cid',
  225. 'books.last_chapter',
  226. 'book_configs.product_id',
  227. 'book_configs.copyright_limit_data',
  228. 'books.updated_at as last_update_time',
  229. 'book_configs.promotion_domain',
  230. 'books.name as old_name',
  231. 'book_configs.recommend_cid',
  232. 'book_configs.is_high_quality'
  233. );
  234. if ($where) {
  235. foreach ($where as $key => $v) {
  236. //关键词查询
  237. if ($key == 'key' && $v) {
  238. $res1 = $res1->where('book_configs.book_name', 'like', '%' . $v . '%');
  239. }
  240. //分类id查询
  241. if ($key == 'category_id' && $v) {
  242. $res1 = $res1->where('books.category_id', '=', $v);
  243. }
  244. //上架查询
  245. if ($key == 'is_on_shelf' && $v != '') {
  246. if (is_array($v)) {
  247. $res1 = $res1->whereIn('is_on_shelf', $v);
  248. } else {
  249. $res1 = $res1->where('is_on_shelf', '=', $v);
  250. }
  251. }
  252. //频道查询
  253. if ($key == 'channel_name' && $v) {
  254. $res1 = $res1->where('book_categories.channel_name', '=', $v);
  255. }
  256. if ($key == 'status') {
  257. $res1 = $res1->where('books.status', '=', $v);
  258. }
  259. if ($key == 'author') {
  260. $res1 = $res1->where('books.author', 'like', '%' . $v . '%');
  261. }
  262. //旧书名查询
  263. if ($key == 'old_name') {
  264. $res1 = $res1->where('books.name', 'like', '%' . $v . '%');
  265. }
  266. //版权日期查询
  267. if ($key == 'copy_right_date') {
  268. if (is_array($v)) {
  269. $res1 = $res1->whereBetween('book_configs.copyright_limit_data', $v);
  270. } else {
  271. $res1 = $res1->where('book_configs.copyright_limit_data', '<=', $v);
  272. }
  273. }
  274. if ($key == 'domain') {
  275. $res1 = $res1->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
  276. }
  277. if ($key == 'bid') {
  278. $res1 = $res1->where('book_configs.bid', '=', $v);
  279. }
  280. if ($key == 'is_high_quality') {
  281. $res1 = $res1->where('book_configs.is_high_quality', '=', $v);
  282. }
  283. if ($key == 'charge_type') {
  284. $res1 = $res1->where('book_configs.charge_type', '=', $v);
  285. }
  286. if ($key == 'hidden_books') {
  287. $res1 = $res1->whereNotIn('book_configs.bid', $v);
  288. }
  289. }
  290. if ($bids) {
  291. //$res1 = $res1->orwhereIn('book_configs.id', $bids);
  292. if ((isset($where['key']) && !empty($where['key'])) && (isset($where['channel_name']) && !empty($where['channel_name']))) {
  293. $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
  294. $query->where('is_on_shelf', 1)
  295. ->whereIn('book_configs.bid', $bids)
  296. ->where('book_configs.book_name', 'like', '%' . $where['key'] . '%')
  297. ->where('book_categories.channel_name', '=', $where['channel_name']);;
  298. });
  299. } elseif (isset($where['key']) && !empty($where['key'])) {
  300. $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
  301. $query->where('is_on_shelf', 1)
  302. ->whereIn('book_configs.bid', $bids)
  303. ->where('book_configs.book_name', 'like', '%' . $where['key'] . '%');
  304. });
  305. } elseif (isset($where['channel_name']) && !empty($where['channel_name'])) {
  306. $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
  307. $query->where('is_on_shelf', 1)
  308. ->whereIn('book_configs.bid', $bids)
  309. ->where('book_categories.channel_name', '=', $where['channel_name']);
  310. });
  311. } else {
  312. $res1 = $res1->orwhereIn('book_configs.bid', $bids);
  313. }
  314. }
  315. }
  316. return $res1->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
  317. }
  318. /**
  319. * 根据id数组获取图书信息
  320. * @param array $bid_arr
  321. * @param array $order
  322. * @return mixed
  323. */
  324. public static function getBooksByIds(array $bid_arr, array $order = [])
  325. {
  326. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  327. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  328. ->select(
  329. 'book_configs.bid',
  330. 'book_configs.force_subscribe_chapter_seq',
  331. 'book_configs.vip_seq',
  332. 'book_configs.price',
  333. 'book_configs.is_on_shelf',
  334. 'book_configs.cover',
  335. 'book_configs.book_name',
  336. 'book_configs.copyright',
  337. 'book_configs.charge_type',
  338. 'book_configs.is_on_shelf',
  339. 'books.author',
  340. 'books.intro',
  341. 'book_categories.category_name',
  342. 'category_id',
  343. 'status',
  344. 'chapter_count',
  345. 'book_configs.click_count',
  346. 'first_cid',
  347. 'last_cid',
  348. 'size',
  349. 'last_chapter',
  350. 'books.keyword',
  351. 'book_configs.recommend_index',
  352. 'book_configs.is_show_index_content',
  353. 'book_configs.product_id',
  354. 'book_categories.channel_name',
  355. 'books.last_cid',
  356. 'books.last_chapter',
  357. 'book_configs.product_id',
  358. 'books.updated_at as last_update_time',
  359. 'book_configs.copyright_limit_data',
  360. 'book_configs.promotion_domain',
  361. 'books.name as old_name',
  362. 'book_configs.recommend_cid'
  363. )
  364. ->whereIn('book_configs.bid', $bid_arr);
  365. if ($order) {
  366. $res->orderBy($order[0], $order[1]);
  367. } else {
  368. $str = implode(',', $bid_arr);
  369. $field = 'bid,' . $str;
  370. $res->orderBy(DB::raw('field(' . $field . ')'));
  371. }
  372. return $res->limit(30)->get();
  373. }
  374. /**
  375. * 根据bid获取图书信息
  376. * @param $bid
  377. * @return mixed
  378. */
  379. public static function getBookById($bid)
  380. {
  381. if (empty($bid)) return null;
  382. return self::join('books', 'book_configs.bid', '=', 'books.id')
  383. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  384. ->select(
  385. 'book_configs.bid',
  386. 'book_configs.is_on_shelf',
  387. 'book_configs.force_subscribe_chapter_seq',
  388. 'book_configs.vip_seq',
  389. 'book_configs.cp_source',
  390. 'book_configs.price',
  391. 'book_configs.cover',
  392. 'book_configs.book_name',
  393. 'book_configs.copyright',
  394. 'book_configs.created_at',
  395. 'book_configs.charge_type',
  396. 'book_configs.is_on_shelf',
  397. 'books.author',
  398. 'books.intro',
  399. 'book_categories.category_name',
  400. 'category_id',
  401. 'status',
  402. 'chapter_count',
  403. 'first_cid',
  404. 'last_cid',
  405. 'size',
  406. 'last_chapter',
  407. 'books.keyword',
  408. 'book_configs.recommend_index',
  409. 'book_configs.test_status',
  410. 'book_configs.is_show_index_content',
  411. 'book_configs.click_count',
  412. 'book_configs.product_id',
  413. 'book_categories.channel_name',
  414. 'books.last_cid',
  415. 'books.updated_at as last_update_time',
  416. 'books.last_chapter',
  417. 'book_configs.product_id',
  418. 'book_configs.copyright_limit_data',
  419. 'book_configs.promotion_domain',
  420. 'books.name as old_name',
  421. 'book_configs.recommend_cid',
  422. 'book_configs.is_high_quality'
  423. )->where('book_configs.bid', $bid)->first();
  424. }
  425. /**
  426. * 根据关键词获取图书
  427. * @param $key
  428. * @param int $page_size
  429. * @param null $is_on_shelf
  430. * @return mixed
  431. */
  432. public static function getBooksByKey($key, $page_size = 15, $is_on_shelf = null)
  433. {
  434. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  435. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  436. ->select(
  437. 'book_configs.bid',
  438. 'book_configs.force_subscribe_chapter_seq',
  439. 'book_configs.vip_seq',
  440. 'book_configs.price',
  441. 'book_configs.cover',
  442. 'book_configs.book_name',
  443. 'book_configs.copyright',
  444. 'book_configs.charge_type',
  445. 'book_configs.is_on_shelf',
  446. 'books.author',
  447. 'books.intro',
  448. 'book_categories.category_name',
  449. 'category_id',
  450. 'status',
  451. 'chapter_count',
  452. 'book_configs.click_count',
  453. 'first_cid',
  454. 'last_cid',
  455. 'size',
  456. 'last_chapter',
  457. 'books.keyword',
  458. 'book_configs.recommend_index',
  459. 'book_configs.is_show_index_content',
  460. 'book_configs.product_id',
  461. 'book_categories.channel_name',
  462. 'books.last_cid',
  463. 'books.last_chapter',
  464. 'book_configs.product_id',
  465. 'books.updated_at as last_update_time',
  466. 'book_configs.copyright_limit_data',
  467. 'book_configs.promotion_domain',
  468. 'books.name as old_name',
  469. 'book_configs.recommend_cid',
  470. 'book_configs.is_high_quality'
  471. )
  472. ->where('book_configs.book_name', 'like', '%' . $key . '%');
  473. //->orWhere('books.intro', 'like', '%' . $key . '%')
  474. //->orWhere('books.keyword', 'like', '%' . $key . '%')
  475. //->orWhere('books.category_name', 'like', '%' . $key . '%')
  476. //->orWhere('books.author', 'like', '%' . $key . '%');
  477. /*
  478. if ($is_on_shelf) {
  479. if (is_array($is_on_shelf)) {
  480. $res->whereIn('book_configs.is_on_shelf', $is_on_shelf);
  481. } else {
  482. $res->where('book_configs.is_on_shelf', '=', $is_on_shelf);
  483. }
  484. }*/
  485. $res->whereIn('book_configs.is_on_shelf', [1, 2]);
  486. $res = $res->paginate($page_size);
  487. foreach ($res as $v) {
  488. $v->book_url = '/detail?id=' . Hashids::encode($v->bid);
  489. }
  490. return $res;
  491. }
  492. /**
  493. * 根据product_id获取图书
  494. * @param $product_id
  495. * @return mixed
  496. */
  497. public static function getBookByProduct($product_id)
  498. {
  499. return self::join('books', 'book_configs.bid', '=', 'books.id')
  500. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  501. ->select(
  502. 'book_configs.bid',
  503. 'book_configs.force_subscribe_chapter_seq',
  504. 'book_configs.vip_seq',
  505. 'book_configs.price',
  506. 'book_configs.cover',
  507. 'book_configs.book_name',
  508. 'book_configs.copyright',
  509. 'book_configs.charge_type',
  510. 'book_configs.is_on_shelf',
  511. 'books.author',
  512. 'books.intro',
  513. 'book_categories.category_name',
  514. 'category_id',
  515. 'status',
  516. 'chapter_count',
  517. 'first_cid',
  518. 'last_cid',
  519. 'size',
  520. 'last_chapter',
  521. 'books.keyword',
  522. 'book_configs.recommend_index',
  523. 'book_configs.is_show_index_content',
  524. 'book_configs.click_count',
  525. 'book_configs.product_id',
  526. 'book_categories.channel_name',
  527. 'books.last_cid',
  528. 'books.last_chapter',
  529. 'book_configs.product_id',
  530. 'book_configs.recommend_cid',
  531. 'book_configs.is_high_quality'
  532. )->where('book_configs.product_id', $product_id)->first();
  533. }
  534. /**
  535. * 更新图书
  536. * @param $bid
  537. * @param array $param
  538. * @return bool
  539. */
  540. public static function updateBookInfo($bid, array $param)
  541. {
  542. $book_info = self::getBookById($bid);
  543. if (!$book_info) return false;
  544. $update_data = [];
  545. if (isset($param['force_subscribe_chapter_seq']) && !empty($param['force_subscribe_chapter_seq'])) $update_data['force_subscribe_chapter_seq'] = $param['force_subscribe_chapter_seq'];
  546. if (isset($param['product_id']) && !empty($param['product_id'])) $update_data['product_id'] = $param['product_id'];
  547. if (isset($param['book_name']) && !empty($param['book_name'])) $update_data['book_name'] = $param['book_name'];
  548. if (isset($param['price']) && !empty($param['price'])) $update_data['price'] = $param['price'];
  549. if (isset($param['cover']) && !empty($param['cover'])) $update_data['cover'] = $param['cover'];
  550. if (isset($param['charge_type']) && !empty($param['charge_type'])) {
  551. $update_data['charge_type'] = $param['charge_type'];
  552. if (is_numeric($update_data['charge_type'])) {
  553. $update_data['charge_type'] = 'CHAPTER';
  554. }
  555. }
  556. if (isset($param['hot']) && !empty($param['hot'])) $update_data['hot'] = $param['hot'];
  557. if (isset($param['roles']) && !empty($param['roles'])) $update_data['roles'] = $param['roles'];
  558. if (isset($param['is_on_shelf']) && $param['is_on_shelf'] != '') $update_data['is_on_shelf'] = $param['is_on_shelf'];
  559. if (isset($param['recommend_index']) && !empty($param['recommend_index'])) $update_data['recommend_index'] = $param['recommend_index'];
  560. if (isset($param['is_show_index_content'])) $update_data['is_show_index_content'] = $param['is_show_index_content'];
  561. if (isset($param['click_count']) && $param['click_count'] != '') $update_data['click_count'] = $param['click_count'];
  562. if (isset($param['copyright_limit_data']) && $param['copyright_limit_data'] != '') $update_data['copyright_limit_data'] = $param['copyright_limit_data'];
  563. if (isset($param['promotion_domain']) && $param['promotion_domain'] != '') $update_data['promotion_domain'] = $param['promotion_domain'];
  564. if (isset($param['copyright']) && $param['copyright'] != '') $update_data['copyright'] = $param['copyright'];
  565. $res1 = null;
  566. if (isset($param['status'])) {
  567. $res1 = Book::where('id', $bid)->update(['status' => $param['status']]);
  568. }
  569. if (isset($param['book_category_id'])) {
  570. $catagory = BookCategory::select('category_name')->where('id', $param['book_category_id'])->first();
  571. Book::where('id', $bid)->update(['category_id' => $param['book_category_id'], 'category_name' => $catagory->category_name]);
  572. }
  573. if (empty($update_data) && !$res1) return false;
  574. return self::where('bid', $bid)->update($update_data);
  575. }
  576. /*
  577. * 获取渠道,用户的全部书籍列表
  578. */
  579. public static function getLeftRecommendBook($channel_name, $is_high_quality, $force_update = false): array
  580. {
  581. if ($force_update) {
  582. \Log::info('force_set_full_book_channel_name:' . $channel_name);
  583. Redis::set('full_book_channel_name:' . $channel_name, null);
  584. }
  585. // 存redis里面
  586. $full_book_bids = Redis::get('full_book_channel_name:' . $channel_name);
  587. $full_book_bids = json_decode($full_book_bids);
  588. if (!empty($full_book_bids)) {
  589. \Log::info('direct_get_full_book_bids_from_redis:' . $channel_name);
  590. } else {
  591. // 获取全集
  592. $full_book_bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  593. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  594. ->select('book_configs.bid')
  595. ->where('book_categories.channel_name', $channel_name)
  596. ->where('book_configs.is_high_quality', $is_high_quality)
  597. ->where('book_configs.test_status', 0) // 不含测书
  598. ->whereIn('book_configs.is_on_shelf', [2])
  599. ->orderBy('book_configs.id', 'desc')
  600. ->get()->pluck('bid')->all();
  601. if (!empty($full_book_bids)) {
  602. \Log::info('set_full_book_channel_name:' . $channel_name . ' data:' . json_encode($full_book_bids));
  603. Redis::set('full_book_channel_name:' . $channel_name, json_encode($full_book_bids));
  604. }
  605. }
  606. // \Log::info('$full_book_bids:'.json_encode($full_book_bids));
  607. return $full_book_bids;
  608. }
  609. /*
  610. * 获取渠道,用户的全部需要测试的书籍列表
  611. */
  612. public static function getLeftRecommendTestBook($channel_name)
  613. {
  614. // 获取test全集
  615. $test_bids = self::getLeftRecommendTestBookConfigs($channel_name);
  616. // \Log::info('getLeftRecommendTestBook:'.json_encode($test_bids));
  617. $last_bids = [];
  618. if (!empty($test_bids)) {
  619. foreach ($test_bids as $test_bid) {
  620. $bid = $test_bid->bid;
  621. $redis_bid_push_num = Redis::hget('SmartPushBookUserNum', $bid);
  622. $plan_push_user_num = $test_bid->plan_push_user_num;
  623. \Log::info('left_test_book,bid:' . $bid . ' redis_bid_push_num:' . $redis_bid_push_num . ' plan_push_user_num:' . $plan_push_user_num);
  624. if ($redis_bid_push_num >= $plan_push_user_num) {
  625. \Log::info('full_update_test_book,bid:' . $bid . ' redis_bid_push_num:' . $redis_bid_push_num . ' plan_push_user_num:' . $plan_push_user_num);
  626. self::where('bid', $bid)->update(['test_status' => 2, 'test_update_time' => date('Y-m-d H:i:s')]);
  627. continue;
  628. }
  629. $last_bids[] = $bid;
  630. }
  631. }
  632. return $last_bids;
  633. }
  634. /*
  635. * 获取渠道,用户的全部需要测试的书籍列表
  636. */
  637. public static function getLeftRecommendTestBookConfigs($channel_name)
  638. {
  639. // 获取全集
  640. return self::join('books', 'book_configs.bid', '=', 'books.id')
  641. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  642. ->select('book_configs.bid', 'book_configs.plan_push_user_num')
  643. ->where('book_categories.channel_name', $channel_name)
  644. ->where('book_configs.test_status', 1) // 待测
  645. ->groupBy('book_configs.bid')
  646. ->orderBy('book_configs.test_update_time', 'asc')
  647. ->limit(1) // 每次1本
  648. ->get();
  649. }
  650. /*
  651. * 获取用户的曾经推荐过的书籍列表
  652. */
  653. public static function getUserRecommendRecords($uid)
  654. {
  655. $recommend_bids = Redis::smembers('userRecommendBids:' . $uid);
  656. return $recommend_bids;
  657. }
  658. /*
  659. * 添加用户的曾经推荐过的书籍列表
  660. */
  661. public static function addUserRecommendRecords($uid, $bids)
  662. {
  663. foreach ($bids as $bid) {
  664. Redis::sadd('userRecommendBids:' . $uid, $bid);
  665. }
  666. }
  667. /*
  668. * 清楚用户的曾经推荐过的书籍列表
  669. */
  670. public static function truncateUserRecommendRecords($uid)
  671. {
  672. \Log::info('truncateUserRecommendRecords:' . $uid);
  673. Redis::del('userRecommendBids:' . $uid);
  674. }
  675. /*
  676. * 书籍推送量+1
  677. */
  678. public static function incrBidPushNum($last_bids)
  679. {
  680. if (!empty($last_bids)) {
  681. foreach ($last_bids as $last_bid) {
  682. // \Log::info('incrBidPushNum:test_bid' . $last_bid);
  683. Redis::hincrby('SmartPushBookUserNum', $last_bid, 1);
  684. }
  685. }
  686. }
  687. /*
  688. * 获取相同频道的4本高推荐图书
  689. * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
  690. */
  691. public static function getLeftRecommendBids($uid = '', $channel_name, $num = 4, $loop = 1)
  692. {
  693. // \Log::info('getLeftRecommendBids_loop:'.$loop.' $uid:'.$uid.' $channel_name:'.$channel_name.' $num:'.$num.' $loop:'.$loop);
  694. if ($loop > 3) {
  695. \Log::info('getLeftRecommendBids_return_loop:' . $loop . ' $uid:' . $uid . ' $channel_name:' . $channel_name);
  696. return [];
  697. }
  698. // 测试书籍优先
  699. $test_bids = self::getLeftRecommendTestBook($channel_name);
  700. // 获取全集,不含测试书籍
  701. $full_bid_no_tests = self::getLeftRecommendBook($channel_name, 1, false);
  702. $full_bids = array_merge($test_bids, $full_bid_no_tests);
  703. // 获取用户推荐过的详情
  704. $recommend_bids = self::getUserRecommendRecords($uid);
  705. // 获取用户阅读记录详情
  706. $read_bids = ReadRecordService::getSimpleReadRecord($uid);
  707. // 得到差集
  708. $left_bids = array_diff($full_bids, $recommend_bids, $read_bids);
  709. // \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));
  710. $left_bid_num = count($left_bids);
  711. // 如果不够则清空推荐记录,重新追加剩下的
  712. $last_bids = $need_bids = array();
  713. if ($left_bid_num < $num) {
  714. self::truncateUserRecommendRecords($uid);
  715. $need_bids = self::getLeftRecommendBids($uid, $channel_name, $num - $left_bid_num, $loop + 1);
  716. // \Log::info('full_bids2:'.json_encode($full_bids));
  717. // \Log::info('merge_left_bids:'.json_encode($left_bids).' need_bids:'.json_encode($need_bids));
  718. $last_bids = array_merge($left_bids, $need_bids);
  719. } else {
  720. $last_bids = array_slice($left_bids, 0, $num, false);
  721. }
  722. // \Log::info('uid:'.$uid.' last_bids:'.json_encode($last_bids));
  723. // 加入已经推荐
  724. self::addUserRecommendRecords($uid, $last_bids);
  725. // 书籍推送量+1
  726. if ($loop == 1) {
  727. self::incrBidPushNum($last_bids);
  728. }
  729. return $last_bids;
  730. }
  731. /*
  732. * 获取相同频道的4本高推荐图书(循环获取)
  733. * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
  734. */
  735. public static function getSimpleChannelBookLoop($bid, $num = 4, $uid = '')
  736. {
  737. // \Log::info('getSimpleChannelBookLoop:bid:'.$bid.' num:'.$num.' uid:'.$uid);
  738. if (empty($uid)) {
  739. return self::getSimpleChannelBook($bid, $num);
  740. }
  741. $book_info = self::getBookById($bid);
  742. $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '女频';
  743. // 获取全集,减去阅读记录和推荐过的书籍id
  744. $bids = self::getLeftRecommendBids($uid, $channel_name, $num, 1);
  745. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  746. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  747. ->select(
  748. 'book_configs.bid',
  749. 'book_configs.force_subscribe_chapter_seq',
  750. 'book_configs.vip_seq',
  751. 'book_configs.price',
  752. 'books.updated_at as last_update_time',
  753. 'book_configs.cover',
  754. 'book_configs.book_name',
  755. 'book_configs.copyright',
  756. 'book_configs.charge_type',
  757. 'book_configs.is_on_shelf',
  758. 'books.author',
  759. 'books.intro',
  760. 'book_categories.category_name',
  761. 'category_id',
  762. 'status',
  763. 'chapter_count',
  764. 'first_cid',
  765. 'last_cid',
  766. 'size',
  767. 'last_chapter',
  768. 'books.keyword',
  769. 'book_configs.recommend_index',
  770. 'book_configs.is_show_index_content',
  771. 'book_configs.click_count',
  772. 'book_configs.product_id',
  773. 'book_categories.channel_name',
  774. 'books.last_cid',
  775. 'books.last_chapter',
  776. 'book_configs.product_id',
  777. 'books.name as old_name',
  778. 'book_configs.recommend_cid',
  779. 'book_configs.is_high_quality'
  780. )
  781. ->whereIn('book_configs.bid', $bids)
  782. ->orderBy('book_configs.id', 'desc')
  783. ->get();
  784. foreach ($res as $v) {
  785. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  786. }
  787. return $res;
  788. }
  789. public static function getHotRandomRecommendBooks($uid, $num = 2)
  790. {
  791. $sex = ForceSubscribeService::getSimpleSexByUid($uid);
  792. $channel_name = $sex == 1 ? '男频' : '女频';
  793. \Log::info('getHotRandomRecommendBooks,uid:' . $uid . ' num:' . $num . ' channel_name:' . $channel_name);
  794. $bids = [];
  795. $bids = self::getRandomRecommendBooks($channel_name, $num);
  796. return $bids;
  797. }
  798. public static function getHotRandomRecommendBookText($distribution_channel_id, $uid, $num)
  799. {
  800. $bids = self::getHotRandomRecommendBooks($uid, $num);
  801. $recomm_books = BookConfigService::getBooksByIds($bids, ['bid', 'asc']);
  802. $content = '';
  803. if ($recomm_books) {
  804. $content .= "\n\n" . '热门书籍推荐';
  805. foreach ($recomm_books as $book) {
  806. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  807. $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book->book_name . '》</a>';
  808. }
  809. }
  810. return $content;
  811. }
  812. /**
  813. * 获取h5推荐的图书
  814. */
  815. public static function getH5RecommendBooks($uid, $pos, $num = 4)
  816. {
  817. // 先从缓存取,1天有效期
  818. $h5_book_cache = Redis::get('userH5RecommendBids:' . $uid . ':' . $pos);
  819. if (!empty($h5_book_cache)) {
  820. \Log::info('h5_book_cache_exist:' . $uid);
  821. return json_decode($h5_book_cache);
  822. }
  823. // 判断渠道男女频
  824. $user = User::getById($uid);
  825. $distribution_channel_id = isset($user->distribution_channel_id) ? $user->distribution_channel_id : '';
  826. // 男频强制推广男频书
  827. $channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
  828. $channel_name = '';
  829. if ($channel_sex == 1) {
  830. $channel_name = '男频';
  831. } else {
  832. $sex = ForceSubscribeService::getSimpleSexByUid($uid);
  833. $channel_name = $sex == 1 ? '男频' : '女频';
  834. }
  835. \Log::info('getH5RecommendBooks:pos:' . $pos . ' uid:' . $uid . ' num:' . $num . ' channel_name:' . $channel_name . ' channel_sex:' . $channel_sex . ' distribution_channel_id:' . $distribution_channel_id);
  836. $bids = [];
  837. $random_recommend = true;
  838. if ($random_recommend) {
  839. $bids = self::getRandomRecommendBooks($channel_name, $num);
  840. }
  841. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  842. $distribution_channel_id = isset($forceSubscribeUser->distribution_channel_id) ? $forceSubscribeUser->distribution_channel_id : '';
  843. if (in_array($distribution_channel_id, [4042, 4043, 4044]) && $sex == 2) {
  844. $bids = [2323, 1347, 2168, 1550, 1295, 1574];
  845. }
  846. \Log::info('getH5RecommendBooks:uid:' . $uid . ' distribution_channel_id:' . $distribution_channel_id . ' bids:' . json_encode($bids));
  847. if (!empty($bids)) {
  848. $books = BookConfigService::getBooksByIds($bids, ['bid', 'asc']);
  849. $data = $books->toArray();
  850. // 有效期24小时
  851. $redis_key = 'userH5RecommendBids:' . $uid . ':' . $pos;
  852. Redis::set($redis_key, json_encode(object_to_array($data)));
  853. Redis::expire($redis_key, 3600 * 24);
  854. }
  855. return $books;
  856. }
  857. /**
  858. * 获取随机的推荐书籍bid
  859. */
  860. public static function getRandomRecommendBooks($channel_name, $num)
  861. {
  862. if ($channel_name == '男频') {
  863. $channel_name_replace = 'male';
  864. } else {
  865. $channel_name_replace = 'female';
  866. }
  867. $redis_key = sprintf('channel_name:%s:num:%s', $channel_name_replace, $num);
  868. $cache = Redis::get($redis_key);
  869. if ($cache) return explode(',', $cache);
  870. $bids = self::join('books', 'book_configs.bid', '=', 'books.id')
  871. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  872. ->select('book_configs.bid')
  873. ->where('book_categories.channel_name', $channel_name)
  874. ->where('book_configs.is_high_quality', 1)
  875. ->where('book_configs.test_status', 0) // 不含测书
  876. ->whereIn('book_configs.is_on_shelf', [2])
  877. ->inRandomOrder()
  878. ->limit($num)
  879. ->get()->pluck('bid')->all();
  880. if ($bids) {
  881. Redis::setex($redis_key, 7200, implode(',', $bids));
  882. }
  883. return $bids;
  884. }
  885. /*
  886. * 获取相同频道的4本高推荐图书
  887. */
  888. public static function getSimpleChannelBook($bid, $num = 4)
  889. {
  890. $book_info = self::getBookById($bid);
  891. if (!$book_info) return false;
  892. $channel_name = $book_info->channel_name;
  893. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  894. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  895. ->select(
  896. 'book_configs.bid',
  897. 'book_configs.force_subscribe_chapter_seq',
  898. 'book_configs.vip_seq',
  899. 'book_configs.price',
  900. 'books.updated_at as last_update_time',
  901. 'book_configs.cover',
  902. 'book_configs.book_name',
  903. 'book_configs.copyright',
  904. 'book_configs.charge_type',
  905. 'book_configs.is_on_shelf',
  906. 'books.author',
  907. 'books.intro',
  908. 'book_categories.category_name',
  909. 'category_id',
  910. 'status',
  911. 'chapter_count',
  912. 'first_cid',
  913. 'last_cid',
  914. 'size',
  915. 'last_chapter',
  916. 'books.keyword',
  917. 'book_configs.recommend_index',
  918. 'book_configs.is_show_index_content',
  919. 'book_configs.click_count',
  920. 'book_configs.product_id',
  921. 'book_categories.channel_name',
  922. 'books.last_cid',
  923. 'books.last_chapter',
  924. 'book_configs.product_id',
  925. 'books.name as old_name',
  926. 'book_configs.recommend_cid',
  927. 'book_configs.is_high_quality'
  928. )
  929. ->where('book_categories.channel_name', $channel_name)
  930. ->where('book_configs.is_high_quality', 1)
  931. ->whereIn('book_configs.is_on_shelf', [1, 2])
  932. ->orderBy('recommend_index', 'desc')
  933. ->get()
  934. ->random($num);
  935. foreach ($res as $v) {
  936. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  937. }
  938. return $res;
  939. }
  940. /*
  941. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  942. * 新版1条
  943. */
  944. public static function getTrusteeShipChannelBook($distribution_channel_id, $channel_name, $num = 4)
  945. {
  946. // 找头条
  947. $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  948. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  949. ->select('book_configs.bid', 'book_configs.recommend_index')
  950. ->where('book_categories.channel_name', $channel_name)
  951. ->where('book_configs.is_high_quality', 1)
  952. // ->where('book_configs.recommend_index', '>=',95)
  953. ->whereIn('book_configs.is_on_shelf', [2])
  954. ->orderBy('recommend_index', 'desc')
  955. ->limit(50)
  956. ->get()
  957. ->random(1);
  958. \Log::info('$first_res');
  959. \Log::info($first_res);
  960. $bids = [];
  961. $bids[] = $first_res[0]->bid;
  962. \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
  963. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  964. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  965. ->select(
  966. 'book_configs.bid',
  967. 'book_configs.force_subscribe_chapter_seq',
  968. 'book_configs.vip_seq',
  969. 'book_configs.price',
  970. 'books.updated_at as last_update_time',
  971. 'book_configs.cover',
  972. 'book_configs.book_name',
  973. 'book_configs.copyright',
  974. 'book_configs.charge_type',
  975. 'book_configs.is_on_shelf',
  976. 'books.author',
  977. 'books.intro',
  978. 'book_categories.category_name',
  979. 'category_id',
  980. 'status',
  981. 'chapter_count',
  982. 'first_cid',
  983. 'last_cid',
  984. 'size',
  985. 'last_chapter',
  986. 'books.keyword',
  987. 'book_configs.recommend_index',
  988. 'book_configs.is_show_index_content',
  989. 'book_configs.click_count',
  990. 'book_configs.product_id',
  991. 'book_categories.channel_name',
  992. 'books.last_cid',
  993. 'books.last_chapter',
  994. 'book_configs.product_id',
  995. 'books.name as old_name',
  996. 'book_configs.recommend_cid',
  997. 'book_configs.is_high_quality'
  998. )
  999. ->where('book_categories.channel_name', $channel_name)
  1000. ->where('book_configs.is_high_quality', 1)
  1001. ->whereIn('book_configs.bid', $bids)
  1002. ->orderBy('recommend_index', 'desc')
  1003. ->get();
  1004. foreach ($res as $v) {
  1005. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  1006. }
  1007. \Log::info('last_res');
  1008. \Log::info($res);
  1009. return $res;
  1010. }
  1011. /*
  1012. * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
  1013. * 老版多条
  1014. */
  1015. public static function getTrusteeShipChannelBookMulty($distribution_channel_id, $channel_name, $num = 4)
  1016. {
  1017. // TODO 内部渠道的书=内部+外部,外部渠道的书=外部
  1018. // 内部上架判断有点复杂,先统一外部上架
  1019. // 找头条
  1020. $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1021. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1022. ->select('book_configs.bid', 'book_configs.recommend_index')
  1023. ->where('book_categories.channel_name', $channel_name)
  1024. ->where('book_configs.is_high_quality', 1)
  1025. // ->where('book_configs.recommend_index', '>=',95)
  1026. ->whereIn('book_configs.is_on_shelf', [2])
  1027. ->orderBy('recommend_index', 'desc')
  1028. ->limit(10)
  1029. ->get()
  1030. ->random(1);
  1031. \Log::info('$first_res');
  1032. \Log::info($first_res);
  1033. $bids = [];
  1034. $bids[] = $first_res[0]->bid;
  1035. // 找其余3条
  1036. $left_res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1037. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1038. ->select('book_configs.bid')
  1039. ->where('book_categories.channel_name', $channel_name)
  1040. ->where('book_configs.is_high_quality', 1)
  1041. ->whereNotIn('book_configs.bid', $bids)
  1042. ->whereIn('book_configs.is_on_shelf', [2])
  1043. ->orderBy('recommend_index', 'desc')
  1044. ->get()
  1045. ->random(3);
  1046. \Log::info('left_res');
  1047. \Log::info($left_res);
  1048. foreach ($left_res as $left_r) {
  1049. $bids[] = $left_r->bid;
  1050. }
  1051. \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
  1052. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1053. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1054. ->select(
  1055. 'book_configs.bid',
  1056. 'book_configs.force_subscribe_chapter_seq',
  1057. 'book_configs.vip_seq',
  1058. 'book_configs.price',
  1059. 'books.updated_at as last_update_time',
  1060. 'book_configs.cover',
  1061. 'book_configs.book_name',
  1062. 'book_configs.copyright',
  1063. 'book_configs.charge_type',
  1064. 'book_configs.is_on_shelf',
  1065. 'books.author',
  1066. 'books.intro',
  1067. 'book_categories.category_name',
  1068. 'category_id',
  1069. 'status',
  1070. 'chapter_count',
  1071. 'first_cid',
  1072. 'last_cid',
  1073. 'size',
  1074. 'last_chapter',
  1075. 'books.keyword',
  1076. 'book_configs.recommend_index',
  1077. 'book_configs.is_show_index_content',
  1078. 'book_configs.click_count',
  1079. 'book_configs.product_id',
  1080. 'book_categories.channel_name',
  1081. 'books.last_cid',
  1082. 'books.last_chapter',
  1083. 'book_configs.product_id',
  1084. 'books.name as old_name',
  1085. 'book_configs.recommend_cid',
  1086. 'book_configs.is_high_quality'
  1087. )
  1088. ->where('book_categories.channel_name', $channel_name)
  1089. ->where('book_configs.is_high_quality', 1)
  1090. ->whereIn('book_configs.bid', $bids)
  1091. ->orderBy('recommend_index', 'desc')
  1092. ->get();
  1093. foreach ($res as $v) {
  1094. $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
  1095. }
  1096. \Log::info('last_res');
  1097. \Log::info($res);
  1098. return $res;
  1099. }
  1100. /*
  1101. *快应用专用,用户阅读完推荐
  1102. * 获取相同推荐
  1103. */
  1104. public static function getQuickAppRecommendBooks($bid, $categories_id, $num = 4)
  1105. {
  1106. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1107. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1108. ->select(
  1109. 'book_configs.bid',
  1110. 'book_configs.force_subscribe_chapter_seq',
  1111. 'book_configs.vip_seq',
  1112. 'book_configs.price',
  1113. 'book_configs.cover',
  1114. 'book_configs.book_name',
  1115. 'book_configs.copyright',
  1116. 'book_configs.charge_type',
  1117. 'book_configs.is_on_shelf',
  1118. 'books.author',
  1119. 'books.intro',
  1120. 'book_categories.category_name',
  1121. 'category_id',
  1122. 'status',
  1123. 'chapter_count',
  1124. 'first_cid',
  1125. 'last_cid',
  1126. 'size',
  1127. 'last_chapter',
  1128. 'books.keyword',
  1129. 'book_configs.recommend_index',
  1130. 'book_configs.is_show_index_content',
  1131. 'book_configs.click_count',
  1132. 'book_configs.product_id',
  1133. 'book_categories.channel_name',
  1134. 'books.last_cid',
  1135. 'books.last_chapter',
  1136. 'book_configs.product_id',
  1137. 'books.name as old_name',
  1138. 'book_configs.recommend_cid',
  1139. 'book_configs.is_high_quality',
  1140. 'books.updated_at as last_update_time'
  1141. )
  1142. ->where('book_categories.id', $categories_id)
  1143. ->where('book_configs.bid', '!=', $bid)
  1144. ->where('book_configs.is_high_quality', 1)
  1145. ->orderBy('recommend_index', 'desc')->get();
  1146. $count = $res->count() >= $num ? $num : $res->count();
  1147. return $res->random($count);
  1148. }
  1149. /*
  1150. * H5专用,用户阅读完推荐
  1151. * 获取相同推荐
  1152. */
  1153. public static function getRecommendBooks($bid, $channel_name, $num = 4)
  1154. {
  1155. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1156. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1157. ->select(
  1158. 'book_configs.bid',
  1159. 'book_configs.force_subscribe_chapter_seq',
  1160. 'book_configs.vip_seq',
  1161. 'book_configs.price',
  1162. 'book_configs.cover',
  1163. 'book_configs.book_name',
  1164. 'book_configs.copyright',
  1165. 'book_configs.charge_type',
  1166. 'book_configs.is_on_shelf',
  1167. 'books.author',
  1168. 'books.intro',
  1169. 'book_categories.category_name',
  1170. 'category_id',
  1171. 'status',
  1172. 'chapter_count',
  1173. 'first_cid',
  1174. 'last_cid',
  1175. 'size',
  1176. 'last_chapter',
  1177. 'books.keyword',
  1178. 'book_configs.recommend_index',
  1179. 'book_configs.is_show_index_content',
  1180. 'book_configs.click_count',
  1181. 'book_configs.product_id',
  1182. 'book_categories.channel_name',
  1183. 'books.last_cid',
  1184. 'books.last_chapter',
  1185. 'book_configs.product_id',
  1186. 'books.name as old_name',
  1187. 'book_configs.recommend_cid',
  1188. 'book_configs.is_high_quality',
  1189. 'books.updated_at as last_update_time'
  1190. )
  1191. ->where('book_categories.channel_name', $channel_name)
  1192. ->where('book_configs.bid', '!=', $bid)
  1193. ->where('book_configs.is_high_quality', 1)
  1194. ->orderBy('recommend_index', 'desc')->get();
  1195. $count = $res->count() >= $num ? $num : $res->count();
  1196. return $res->random($count);
  1197. }
  1198. /*
  1199. * 签到专用
  1200. * 获取相同推荐
  1201. */
  1202. public static function getSignRecommendBooks(array $bid, $channel_name, $num = 2)
  1203. {
  1204. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1205. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1206. ->select(
  1207. 'book_configs.bid',
  1208. 'book_configs.force_subscribe_chapter_seq',
  1209. 'book_configs.vip_seq',
  1210. 'book_configs.price',
  1211. 'book_configs.cover',
  1212. 'book_configs.book_name',
  1213. 'book_configs.copyright',
  1214. 'book_configs.charge_type',
  1215. 'book_configs.is_on_shelf',
  1216. 'books.author',
  1217. 'books.intro',
  1218. 'book_categories.category_name',
  1219. 'category_id',
  1220. 'status',
  1221. 'chapter_count',
  1222. 'first_cid',
  1223. 'last_cid',
  1224. 'size',
  1225. 'last_chapter',
  1226. 'books.keyword',
  1227. 'book_configs.recommend_index',
  1228. 'book_configs.is_show_index_content',
  1229. 'book_configs.click_count',
  1230. 'book_configs.product_id',
  1231. 'book_categories.channel_name',
  1232. 'books.last_cid',
  1233. 'books.last_chapter',
  1234. 'book_configs.product_id',
  1235. 'books.name as old_name',
  1236. 'book_configs.recommend_cid',
  1237. 'book_configs.is_high_quality',
  1238. 'books.updated_at as last_update_time'
  1239. )
  1240. ->where('book_categories.channel_name', $channel_name)
  1241. ->where('book_configs.is_on_shelf', 2)
  1242. ->whereNotIn('book_configs.bid', $bid)
  1243. ->where('book_configs.is_high_quality', 1)
  1244. ->get();
  1245. $count = $res->count() >= $num ? $num : $res->count();
  1246. return $res->random($count);
  1247. }
  1248. /*
  1249. * 获取指定bid的书籍推荐
  1250. */
  1251. public static function getBidRecommendBooks(array $bids)
  1252. {
  1253. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1254. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1255. ->select(
  1256. 'book_configs.bid',
  1257. 'book_configs.force_subscribe_chapter_seq',
  1258. 'book_configs.vip_seq',
  1259. 'book_configs.price',
  1260. 'book_configs.cover',
  1261. 'book_configs.book_name',
  1262. 'book_configs.copyright',
  1263. 'book_configs.charge_type',
  1264. 'book_configs.is_on_shelf',
  1265. 'books.author',
  1266. 'books.intro',
  1267. 'book_categories.category_name',
  1268. 'category_id',
  1269. 'status',
  1270. 'chapter_count',
  1271. 'first_cid',
  1272. 'last_cid',
  1273. 'size',
  1274. 'last_chapter',
  1275. 'books.keyword',
  1276. 'book_configs.recommend_index',
  1277. 'book_configs.is_show_index_content',
  1278. 'book_configs.click_count',
  1279. 'book_configs.product_id',
  1280. 'book_categories.channel_name',
  1281. 'books.last_cid',
  1282. 'books.last_chapter',
  1283. 'book_configs.product_id',
  1284. 'books.name as old_name',
  1285. 'book_configs.recommend_cid',
  1286. 'book_configs.is_high_quality',
  1287. 'books.updated_at as last_update_time'
  1288. )
  1289. ->whereIn('book_configs.bid', $bids)
  1290. ->get();
  1291. return $res;
  1292. }
  1293. /**
  1294. * 修改vip章节
  1295. */
  1296. public static function updateVipSeq($bid, $seq)
  1297. {
  1298. return self::where('bid', $bid)->update(['vip_seq' => $seq]);
  1299. }
  1300. public static function getAllBooks($on_shelf, $order)
  1301. {
  1302. if (!$order) {
  1303. $order = ['id', 'asc'];
  1304. }
  1305. return self::whereIn('is_on_shelf', $on_shelf)->select('bid', 'book_name')->orderBy($order[0], $order[1])->get();
  1306. }
  1307. /**
  1308. * 根据条件获取书籍,没有分页
  1309. */
  1310. public static function getBooksNoPage(array $where = [], array $order = [], array $on_shelf, $limit = 20)
  1311. {
  1312. if (!$order) {
  1313. $order = [['recommend_index', 'desc']];
  1314. }
  1315. $res = self::join('books', 'book_configs.bid', '=', 'books.id')
  1316. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  1317. ->select(
  1318. 'book_configs.bid',
  1319. 'book_configs.force_subscribe_chapter_seq',
  1320. 'book_configs.cp_source',
  1321. 'book_configs.vip_seq',
  1322. 'book_configs.price',
  1323. 'book_configs.cover',
  1324. 'book_configs.book_name',
  1325. 'book_configs.copyright',
  1326. 'book_configs.charge_type',
  1327. 'book_configs.is_on_shelf',
  1328. 'books.author',
  1329. 'books.intro',
  1330. 'book_categories.category_name',
  1331. 'category_id',
  1332. 'status',
  1333. 'chapter_count',
  1334. 'book_configs.click_count',
  1335. 'first_cid',
  1336. 'last_cid',
  1337. 'size',
  1338. 'last_chapter',
  1339. 'books.keyword',
  1340. 'book_configs.recommend_index',
  1341. 'book_configs.is_show_index_content',
  1342. 'book_configs.product_id',
  1343. 'book_categories.channel_name',
  1344. 'books.last_cid',
  1345. 'books.last_chapter',
  1346. 'book_configs.product_id',
  1347. 'book_configs.copyright_limit_data',
  1348. 'books.updated_at as last_update_time',
  1349. 'book_configs.promotion_domain',
  1350. 'books.name as old_name',
  1351. 'book_configs.recommend_cid',
  1352. 'book_configs.is_high_quality'
  1353. );
  1354. if ($where) {
  1355. foreach ($where as $v) {
  1356. $res->where($v[0], $v[1], $v[2]);
  1357. }
  1358. }
  1359. $res->whereIn('is_on_shelf', $on_shelf);
  1360. foreach ($order as $v) {
  1361. $res->orderBy($v[0], $v[1]);
  1362. }
  1363. return $res->limit($limit)->get();
  1364. }
  1365. public static function getAllCps()
  1366. {
  1367. $cps = self::select('cp_source')->groupBy('cp_source')->get();
  1368. $result = array();
  1369. foreach ($cps as $cp) {
  1370. if (!empty($cp['cp_source'])) {
  1371. $result[] = $cp['cp_source'];
  1372. }
  1373. }
  1374. return $result;
  1375. }
  1376. public static function getAllCpBooks()
  1377. {
  1378. $result = array();
  1379. $cp_books = self::select('cp_source', 'bid')->groupBy('cp_source')->groupBy('bid')->get();
  1380. foreach ($cp_books as $cp_book) {
  1381. $result[$cp_book['cp_source']][] = $cp_book['bid'];
  1382. }
  1383. return $result;
  1384. }
  1385. /*
  1386. * 得到cp某段时间某本书的消费
  1387. */
  1388. public static function getAllCpBookConsume($start_date, $end_date, $cps)
  1389. {
  1390. $result = self::leftjoin('book_order_statistical', 'book_order_statistical.bid', '=', 'book_configs.bid')
  1391. ->select(
  1392. 'book_order_statistical.bid',
  1393. DB::raw('sum(book_order_statistical.charge_balance) as charge_balance'),
  1394. 'book_configs.book_name',
  1395. 'book_configs.is_on_shelf',
  1396. 'book_configs.cp_source'
  1397. )
  1398. ->whereIn('book_configs.cp_source', $cps)
  1399. ->where('book_order_statistical.day', '>=', $start_date)
  1400. ->where('book_order_statistical.day', '<=', $end_date)
  1401. ->groupBy('book_configs.cp_source')
  1402. ->groupBy('book_order_statistical.bid')
  1403. ->get();
  1404. return $result;
  1405. }
  1406. public static function getBookByIdAndStatus($bid, $status)
  1407. {
  1408. return self::where('bid', $bid)->whereIn('is_on_shelf', $status)->first();
  1409. }
  1410. public static function get_all_test_books($is_all = false)
  1411. {
  1412. if ($is_all) {
  1413. return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->get();
  1414. } else {
  1415. return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->paginate();
  1416. }
  1417. }
  1418. public static function get_test_books($test_status)
  1419. {
  1420. return self::where('test_status', $test_status)->get();
  1421. }
  1422. public static function updateTestBook($bid, $test_status, $plan_push_user_num = 10000)
  1423. {
  1424. 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')]);
  1425. }
  1426. public static function get_all_smart_push_books($is_all = false)
  1427. {
  1428. if ($is_all) {
  1429. return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->get();
  1430. } else {
  1431. return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->paginate();
  1432. }
  1433. }
  1434. /**
  1435. * 获取书本的id,名称,作者,封面 并带分页
  1436. * @param bool $isAll 是否查询所有
  1437. * @param int $pageSize 每页的条数
  1438. * @param int $pageCount 页数
  1439. * @return mixed
  1440. */
  1441. static function getBookCoverInfos($isAll = false, $pageSize = 200, $pageCount = 0)
  1442. {
  1443. $obj = self::join('books', 'book_configs.bid', '=', 'books.id')->select('book_configs.bid', 'book_configs.book_name', 'books.author', 'book_configs.cover');
  1444. if ($isAll) {
  1445. return $obj->get();
  1446. } else {
  1447. return $obj->limit($pageSize)->offset($pageCount * $pageSize)->get();
  1448. }
  1449. }
  1450. /**
  1451. * 获取书本的总数
  1452. * @return mixed
  1453. */
  1454. static function getBooksCount()
  1455. {
  1456. return self::count();
  1457. }
  1458. /**
  1459. * 通过书名模糊搜索bid
  1460. * @param $book_name
  1461. * @return mixed
  1462. */
  1463. static function getIdByName($book_name)
  1464. {
  1465. return self::select('bid')->where('book_name', 'like', '%' . $book_name . '%')->get();
  1466. }
  1467. }