BookConfig.php 61 KB

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