BookConfig.php 60 KB

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