BookConfig.php 60 KB

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