123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254 |
- <?php
- namespace App\Modules\Book\Models;
- use App\Modules\Book\Services\BookTagsService;
- use App\Modules\Book\Services\TestBookService;
- use App\Modules\User\Services\ReadRecordService;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Channel\Services\ChannelService;
- use App\Modules\OfficialAccount\Services\ForceSubscribeService;
- use App\Modules\Promotion\Services\PromotionService;
- use App\Modules\User\Services\UserService;
- use DB;
- use Hashids;
- use Illuminate\Database\Eloquent\Model;
- use Log;
- use Redis;
- class BookConfig extends Model
- {
- protected $connection = 'api_mysql';
- protected $table = 'book_configs';
- protected $fillable = ['bid', 'force_subscribe_chapter_seq', 'price', 'cover', 'book_name',
- 'copyright', 'charge_type', 'hot', 'is_on_shelf', 'source_domain', 'recommend_index', 'roles', 'test_status', 'plan_push_user_num', 'test_update_time',
- 'is_show_index_content', 'click_count', 'promotion_domain', 'copyright_limit_data', 'recommend_cid', 'is_high_quality', 'vip_seq', 'editor_recommend', 'is_current_week_promotion'];
-
- /**
- * 根据条件获取图书
- * @param array $where
- * @param array $order
- * @param int $page_size
- * @return mixed
- */
- public static function getBooks(array $where = [], array $order = [], $page_size = 15)
- {
- if (!$order) {
- $order = ['recommend_index', 'desc'];
- }
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.roles', 'book_configs.force_subscribe_chapter_seq', 'book_configs.cp_source', 'book_configs.vip_seq', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
- 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
- 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'books.size', 'last_chapter', 'books.keyword',
- 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
- 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
- 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'is_current_week_promotion'
- );
- if ($where) {
- foreach ($where as $key => $v) {
- //关键词查询
- if ($key == 'key' && $v) {
- $res = $res->where('book_configs.book_name', 'like', '%' . $v . '%');
- //->orWhere('books.intro', 'like', '%' . $v . '%')
- //->orWhere('books.category_name', 'like', '%' . $v . '%')->orWhere('books.author', 'like', '%' . $v . '%')
- //->orWhere('books.keyword', 'like', '%' . $v . '%');
- }
- //分类id查询
- if ($key == 'category_id' && $v) {
- $res = $res->where('books.category_id', '=', $v);
- }
- //上架查询
- if ($key == 'is_on_shelf' && $v != '') {
- if (is_array($v)) {
- $res = $res->whereIn('is_on_shelf', $v);
- } else {
- $res = $res->where('is_on_shelf', '=', $v);
- }
- }
- //频道查询
- if ($key == 'channel_name' && $v) {
- $res = $res->where('book_categories.channel_name', '=', $v);
- }
- if ($key == 'status') {
- $res = $res->where('books.status', '=', $v);
- }
- if ($key == 'author') {
- $res = $res->where('books.author', 'like', '%' . $v . '%');
- }
- if ($key == 'roles') {
- $res = $res->where('book_configs.roles', 'like', '%' . $v . '%');
- }
- //旧书名查询
- if ($key == 'old_name') {
- $res = $res->where('books.name', 'like', '%' . $v . '%');
- }
- //版权日期查询
- if ($key == 'copy_right_date') {
- if (is_array($v)) {
- $res = $res->whereBetween('book_configs.copyright_limit_data', $v);
- } else {
- $res = $res->where('book_configs.copyright_limit_data', '<=', $v);
- }
- }
- if ($key == 'domain') {
- $res = $res->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
- }
- if ($key == 'bid') {
- $res = $res->where('book_configs.bid', '=', $v);
- }
- if ($key == 'is_high_quality') {
- $res = $res->where('book_configs.is_high_quality', '=', $v);
- }
- if ($key == 'charge_type') {
- $res = $res->where('book_configs.charge_type', '=', $v);
- }
- if ($key == 'firstChapterContent') {
- $res = $res->join('chapters', function ($query) use ($v) {
- $query->on('book_configs.bid', '=', 'chapters.bid')
- ->where('chapters.sequence', 1);
- })->where('chapters.content', 'like', '%' . $v . '%');
- }
- if ($key == 'tags') {
- $tags_filter = BookTagsService::getSearchBooks($v);
- $res->whereIn('book_configs.bid', $tags_filter);
- }
- if ($key == 'is_current_week_promotion') {
- $res->where('book_configs.is_current_week_promotion', $v);
- }
- }
- }
- //$res->orderBy('book_configs.updated_at','desc');
- return $res->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
- }
- /**
- * 根据条件获取图书
- * @param array $where
- * @param array $order
- * @param int $page_size
- * @return mixed
- */
- public static function getPromotionBooks(array $where = [], array $bids, array $order = [], $page_size = 15)
- {
- if (!$order) {
- $order = ['recommend_index', 'desc'];
- }
- $res1 = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.cp_source', 'book_configs.vip_seq', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
- 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
- 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword',
- 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
- 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
- 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- );
- if ($where) {
- foreach ($where as $key => $v) {
- //关键词查询
- if ($key == 'key' && $v) {
- $res1 = $res1->where('book_configs.book_name', 'like', '%' . $v . '%');
- }
- //分类id查询
- if ($key == 'category_id' && $v) {
- $res1 = $res1->where('books.category_id', '=', $v);
- }
- //上架查询
- if ($key == 'is_on_shelf' && $v != '') {
- if (is_array($v)) {
- $res1 = $res1->whereIn('is_on_shelf', $v);
- } else {
- $res1 = $res1->where('is_on_shelf', '=', $v);
- }
- }
- //频道查询
- if ($key == 'channel_name' && $v) {
- $res1 = $res1->where('book_categories.channel_name', '=', $v);
- }
- if ($key == 'status') {
- $res1 = $res1->where('books.status', '=', $v);
- }
- if ($key == 'author') {
- $res1 = $res1->where('books.author', 'like', '%' . $v . '%');
- }
- //旧书名查询
- if ($key == 'old_name') {
- $res1 = $res1->where('books.name', 'like', '%' . $v . '%');
- }
- //版权日期查询
- if ($key == 'copy_right_date') {
- if (is_array($v)) {
- $res1 = $res1->whereBetween('book_configs.copyright_limit_data', $v);
- } else {
- $res1 = $res1->where('book_configs.copyright_limit_data', '<=', $v);
- }
- }
- if ($key == 'domain') {
- $res1 = $res1->where('book_configs.promotion_domain', 'like', '%' . $v . '%');
- }
- if ($key == 'bid') {
- $res1 = $res1->where('book_configs.bid', '=', $v);
- }
- if ($key == 'is_high_quality') {
- $res1 = $res1->where('book_configs.is_high_quality', '=', $v);
- }
- if ($key == 'charge_type') {
- $res1 = $res1->where('book_configs.charge_type', '=', $v);
- }
- if ($key == 'hidden_books') {
- $res1 = $res1->whereNotIn('book_configs.bid', $v);
- }
- }
- if ($bids) {
- //$res1 = $res1->orwhereIn('book_configs.id', $bids);
- if ((isset($where['key']) && !empty($where['key'])) && (isset($where['channel_name']) && !empty($where['channel_name']))) {
- $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
- $query->where('is_on_shelf', 1)
- ->whereIn('book_configs.bid', $bids)
- ->where('book_configs.book_name', 'like', '%' . $where['key'] . '%')
- ->where('book_categories.channel_name', '=', $where['channel_name']);;
- });
- } elseif (isset($where['key']) && !empty($where['key'])) {
- $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
- $query->where('is_on_shelf', 1)
- ->whereIn('book_configs.bid', $bids)
- ->where('book_configs.book_name', 'like', '%' . $where['key'] . '%');
- });
- } elseif (isset($where['channel_name']) && !empty($where['channel_name'])) {
- $res1 = $res1->orWhere(function ($query) use ($bids, $where) {
- $query->where('is_on_shelf', 1)
- ->whereIn('book_configs.bid', $bids)
- ->where('book_categories.channel_name', '=', $where['channel_name']);
- });
- } else {
- $res1 = $res1->orwhereIn('book_configs.bid', $bids);
- }
- }
- }
- return $res1->orderBy($order[0], $order[1])->orderBy('book_configs.updated_at', 'desc')->paginate($page_size);
- }
- /**
- * 根据id数组获取图书信息
- * @param array $bid_arr
- * @param array $order
- * @return mixed
- */
- public static function getBooksByIds(array $bid_arr, array $order = [])
- {
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'book_configs.click_count',
- 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content',
- 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.updated_at as last_update_time',
- 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid'
- )
- ->whereIn('book_configs.bid', $bid_arr);
- if ($order) {
- $res->orderBy($order[0], $order[1]);
- } else {
- $str = implode(',', $bid_arr);
- $field = 'bid,' . $str;
- $res->orderBy(DB::raw('field(' . $field . ')'));
- }
- return $res->limit(30)->get();
- }
- /**
- * 根据bid获取图书信息
- * @param $bid
- * @return mixed
- */
- public static function getBookById($bid)
- {
- if (empty($bid)) return null;
- return self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.is_on_shelf', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.cp_source', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
- 'book_configs.copyright', 'book_configs.created_at', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
- 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index',
- 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.updated_at as last_update_time',
- 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )->where('book_configs.bid', $bid)->first();
- }
- /**
- * 根据关键词获取图书
- * @param $key
- * @param int $page_size
- * @param null $is_on_shelf
- * @return mixed
- */
- public static function getBooksByKey($key, $page_size = 15, $is_on_shelf = null)
- {
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'book_configs.click_count',
- 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content',
- 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.updated_at as last_update_time',
- 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )
- ->where('book_configs.book_name', 'like', '%' . $key . '%');
- $res = $res->whereIn('book_configs.is_on_shelf', [2])->orderBy('is_on_shelf', 'desc')->limit(5)->get();
- foreach ($res as $v) {
- $v->book_url = '/detail?id=' . Hashids::encode($v->bid);
- }
- return $res;
- }
- /**
- * 根据product_id获取图书
- * @param $product_id
- * @return mixed
- */
- public static function getBookByProduct($product_id)
- {
- return self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )->where('book_configs.product_id', $product_id)->first();
- }
- /**
- * 更新图书
- * @param $bid
- * @param array $param
- * @return bool
- */
- public static function updateBookInfo($bid, array $param)
- {
- $book_info = self::getBookById($bid);
- if (!$book_info) return false;
- $update_data = [];
- if (isset($param['force_subscribe_chapter_seq']) && !empty($param['force_subscribe_chapter_seq'])) $update_data['force_subscribe_chapter_seq'] = $param['force_subscribe_chapter_seq'];
- if (isset($param['product_id']) && !empty($param['product_id'])) $update_data['product_id'] = $param['product_id'];
- if (isset($param['book_name']) && !empty($param['book_name'])) $update_data['book_name'] = $param['book_name'];
- if (isset($param['price']) && !empty($param['price'])) $update_data['price'] = $param['price'];
- if (isset($param['cover']) && !empty($param['cover'])) $update_data['cover'] = $param['cover'];
- if (isset($param['charge_type']) && !empty($param['charge_type'])) {
- $update_data['charge_type'] = $param['charge_type'];
- if (is_numeric($update_data['charge_type'])) {
- $update_data['charge_type'] = 'CHAPTER';
- }
- }
- if (isset($param['hot']) && !empty($param['hot'])) $update_data['hot'] = $param['hot'];
- if (isset($param['roles']) && !empty($param['roles'])) $update_data['roles'] = $param['roles'];
- if (isset($param['is_on_shelf']) && $param['is_on_shelf'] != '') $update_data['is_on_shelf'] = $param['is_on_shelf'];
- if (isset($param['recommend_index']) && !empty($param['recommend_index'])) $update_data['recommend_index'] = $param['recommend_index'];
- if (isset($param['is_show_index_content'])) $update_data['is_show_index_content'] = $param['is_show_index_content'];
- if (isset($param['click_count']) && $param['click_count'] != '') $update_data['click_count'] = $param['click_count'];
- if (isset($param['copyright_limit_data']) && $param['copyright_limit_data'] != '') $update_data['copyright_limit_data'] = $param['copyright_limit_data'];
- if (isset($param['promotion_domain']) && $param['promotion_domain'] != '') $update_data['promotion_domain'] = $param['promotion_domain'];
- if (isset($param['copyright']) && $param['copyright'] != '') $update_data['copyright'] = $param['copyright'];
- $res1 = null;
- if (isset($param['status'])) {
- $res1 = Book::where('id', $bid)->update(['status' => $param['status']]);
- }
- if (isset($param['book_category_id'])) {
- $catagory = BookCategory::select('category_name')->where('id', $param['book_category_id'])->first();
- Book::where('id', $bid)->update(['category_id' => $param['book_category_id'], 'category_name' => $catagory->category_name]);
- }
- if (empty($update_data) && !$res1) return false;
- return self::where('bid', $bid)->update($update_data);
- }
- /*
- * 获取渠道,用户的全部书籍列表
- */
- public static function getLeftRecommendBook($channel_name, $is_high_quality, $force_update = false): array
- {
- if ($force_update) {
- \Log::info('force_set_full_book_channel_name:' . $channel_name);
- Redis::set('full_book_channel_name:' . $channel_name, null);
- }
- // 存redis里面
- $full_book_bids = Redis::get('full_book_channel_name:' . $channel_name);
- $full_book_bids = json_decode($full_book_bids);
- if (!empty($full_book_bids)) {
- \Log::info('direct_get_full_book_bids_from_redis:' . $channel_name);
- } else {
- // 获取全集
- $full_book_bids = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid')
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', $is_high_quality)
- ->where('book_configs.test_status', 0)// 不含测书
- ->whereIn('book_configs.is_on_shelf', [2])
- ->orderBy('book_configs.id', 'desc')
- ->get()->pluck('bid')->all();
- if (!empty($full_book_bids)) {
- \Log::info('set_full_book_channel_name:' . $channel_name . ' data:' . json_encode($full_book_bids));
- Redis::set('full_book_channel_name:' . $channel_name, json_encode($full_book_bids));
- }
- }
- // \Log::info('$full_book_bids:'.json_encode($full_book_bids));
- return $full_book_bids;
- }
- /*
- * 获取渠道,用户的全部需要测试的书籍列表
- */
- public static function getLeftRecommendTestBook($channel_name)
- {
- // 获取test全集
- $test_bids = self::getLeftRecommendTestBookConfigs($channel_name);
- // \Log::info('getLeftRecommendTestBook:'.json_encode($test_bids));
- $last_bids = [];
- if (!empty($test_bids)) {
- foreach ($test_bids as $test_bid) {
- $bid = $test_bid->bid;
- $redis_bid_push_num = Redis::hget('SmartPushBookUserNum', $bid);
- $plan_push_user_num = $test_bid->plan_push_user_num;
- \Log::info('left_test_book,bid:' . $bid . ' redis_bid_push_num:' . $redis_bid_push_num . ' plan_push_user_num:' . $plan_push_user_num);
- if ($redis_bid_push_num >= $plan_push_user_num) {
- \Log::info('full_update_test_book,bid:' . $bid . ' redis_bid_push_num:' . $redis_bid_push_num . ' plan_push_user_num:' . $plan_push_user_num);
- self::where('bid', $bid)->update(['test_status' => 2, 'test_update_time' => date('Y-m-d H:i:s')]);
- continue;
- }
- $last_bids[] = $bid;
- }
- }
- return $last_bids;
- }
- /*
- * 获取渠道,用户的全部需要测试的书籍列表
- */
- public static function getLeftRecommendTestBookConfigs($channel_name)
- {
- // 获取全集
- return self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.plan_push_user_num')
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.test_status', 1)// 待测
- ->groupBy('book_configs.bid')
- ->orderBy('book_configs.test_update_time', 'asc')
- ->limit(1)// 每次1本
- ->get();
- }
- /*
- * 获取用户的曾经推荐过的书籍列表
- */
- public static function getUserRecommendRecords($uid)
- {
- $recommend_bids = Redis::smembers('userRecommendBids:' . $uid);
- return $recommend_bids;
- }
- /*
- * 添加用户的曾经推荐过的书籍列表
- */
- public static function addUserRecommendRecords($uid, $bids)
- {
- foreach ($bids as $bid) {
- Redis::sadd('userRecommendBids:' . $uid, $bid);
- }
- }
- /*
- * 清楚用户的曾经推荐过的书籍列表
- */
- public static function truncateUserRecommendRecords($uid)
- {
- \Log::info('truncateUserRecommendRecords:' . $uid);
- Redis::del('userRecommendBids:' . $uid);
- }
- /*
- * 书籍推送量+1
- */
- public static function incrBidPushNum($last_bids)
- {
- if (!empty($last_bids)) {
- foreach ($last_bids as $last_bid) {
- // \Log::info('incrBidPushNum:test_bid' . $last_bid);
- Redis::hincrby('SmartPushBookUserNum', $last_bid, 1);
- }
- }
- }
- /*
- * 获取相同频道的4本高推荐图书
- * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
- */
- public static function getLeftRecommendBids($uid = '', $channel_name, $num = 4, $loop = 1)
- {
- // \Log::info('getLeftRecommendBids_loop:'.$loop.' $uid:'.$uid.' $channel_name:'.$channel_name.' $num:'.$num.' $loop:'.$loop);
- if ($loop > 3) {
- \Log::info('getLeftRecommendBids_return_loop:' . $loop . ' $uid:' . $uid . ' $channel_name:' . $channel_name);
- return [];
- }
- // 测试书籍优先
- $test_bids = self::getLeftRecommendTestBook($channel_name);
- // 获取全集,不含测试书籍
- $full_bid_no_tests = self::getLeftRecommendBook($channel_name, 1, false);
- $full_bids = array_merge($test_bids, $full_bid_no_tests);
- // 获取用户推荐过的详情
- $recommend_bids = self::getUserRecommendRecords($uid);
- // 获取用户阅读记录详情
- $read_bids = ReadRecordService::getSimpleReadRecord($uid);
- // 得到差集
- $left_bids = array_diff($full_bids, $recommend_bids, $read_bids);
- // \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));
- $left_bid_num = count($left_bids);
- // 如果不够则清空推荐记录,重新追加剩下的
- $last_bids = $need_bids = array();
- if ($left_bid_num < $num) {
- self::truncateUserRecommendRecords($uid);
- $need_bids = self::getLeftRecommendBids($uid, $channel_name, $num - $left_bid_num, $loop + 1);
- // \Log::info('full_bids2:'.json_encode($full_bids));
- // \Log::info('merge_left_bids:'.json_encode($left_bids).' need_bids:'.json_encode($need_bids));
- $last_bids = array_merge($left_bids, $need_bids);
- } else {
- $last_bids = array_slice($left_bids, 0, $num, false);
- }
- // \Log::info('uid:'.$uid.' last_bids:'.json_encode($last_bids));
- // 加入已经推荐
- self::addUserRecommendRecords($uid, $last_bids);
- // 书籍推送量+1
- if ($loop == 1) {
- self::incrBidPushNum($last_bids);
- }
- return $last_bids;
- }
- /*
- * 获取相同频道的4本高推荐图书(循环获取)
- * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
- */
- public static function getSimpleChannelBookLoop($bid, $num = 4, $uid = '')
- {
- // \Log::info('getSimpleChannelBookLoop:bid:'.$bid.' num:'.$num.' uid:'.$uid);
- if (empty($uid)) {
- return self::getSimpleChannelBook($bid, $num);
- }
- $book_info = self::getBookById($bid);
- $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '女频';
- // 获取全集,减去阅读记录和推荐过的书籍id
- $bids = self::getLeftRecommendBids($uid, $channel_name, $num, 1);
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )
- ->whereIn('book_configs.bid', $bids)
- ->orderBy('book_configs.id', 'desc')
- ->get();
- foreach ($res as $v) {
- $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
- }
- return $res;
- }
- public static function getHotRandomRecommendBooks($uid, $num = 2)
- {
- $sex = ForceSubscribeService::getSimpleSexByUid($uid);
- $channel_name = $sex == 1 ? '男频' : '女频';
- \Log::info('getHotRandomRecommendBooks,uid:' . $uid . ' num:' . $num . ' channel_name:' . $channel_name);
- $bids = [];
- $bids = self::getRandomRecommendBooks($channel_name, $num);
- return $bids;
- }
- public static function getSpecialHotRandomRecommendBooks($num = 2, $channel_name = '女频')
- {
- $bids = self::getRandomRecommendBooks($channel_name, $num);
- return self::getBidRecommendBooks($bids);
- }
- public static function getHotRandomRecommendBookText($distribution_channel_id, $uid, $num)
- {
- $bids = self::getHotRandomRecommendBooks($uid, $num);
- $recomm_books = BookConfigService::getBooksByIds($bids, ['bid', 'asc']);
- $content = '';
- if ($recomm_books) {
- // 判断是否不展示书名
- $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
- $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
- $recommend_titles = [];
- if ($hide_type == 'hide_book_name') {
- \Log::info('hide_book_name:' . $distribution_channel_id . ' uid:' . $uid);
- $sex = ForceSubscribeService::getSimpleSexByUid($uid);
- $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
- }
- $content .= "\n\n" . '热门书籍推荐';
- foreach ($recomm_books as $key => $book) {
- $book_name = $book->book_name;
- if ($hide_type == 'hide_book_name') {
- $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book->book_name;
- }
- $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
- $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
- }
- }
- return $content;
- }
- /**
- * 获取h5推荐的图书
- */
- public static function getH5RecommendBooks($uid, $pos, $num = 4)
- {
- $sex = ForceSubscribeService::getSimpleSexByUid($uid);
- $channel_name = $sex == 1 ? '男频' : '女频';
- \Log::info('getH5RecommendBooks:pos:' . $pos . ' uid:' . $uid . ' num:' . $num . ' channel_name:' . $channel_name);
- $bids = [];
- // 先从缓存取,1天有效期
- $h5_book_cache = Redis::get('userH5RecommendBids:' . $uid . ':' . $pos);
- if (!empty($h5_book_cache)) {
- \Log::info('h5_book_cache_exist:' . $uid);
- return json_decode($h5_book_cache);
- }
- $bids = [];
- $random_recommend = true;
- if ($random_recommend) {
- $bids = self::getRandomRecommendBooks($channel_name, $num);
- }
- $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
- $distribution_channel_id = isset($forceSubscribeUser->distribution_channel_id) ? $forceSubscribeUser->distribution_channel_id : '';
- \Log::info('getH5RecommendBooks:uid:' . $uid . ' distribution_channel_id:' . $distribution_channel_id . ' bids:' . json_encode($bids));
- if (!empty($bids)) {
- $books = BookConfigService::getBooksByIds($bids, ['bid', 'asc']);
- $data = $books->toArray();
- // 有效期24小时
- $redis_key = 'userH5RecommendBids:' . $uid . ':' . $pos;
- Redis::set($redis_key, json_encode(object_to_array($data)));
- Redis::expire($redis_key, 3600 * 24);
- }
- return $books;
- }
- /**
- * 获取随机的推荐书籍bid
- */
- public static function getRandomRecommendBooks($channel_name, $num)
- {
- $bids = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid')
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- ->where('book_configs.test_status', 0)// 不含测书
- ->whereIn('book_configs.is_on_shelf', [2])
- ->inRandomOrder()
- ->limit($num)
- ->get()->pluck('bid')->all();
- return $bids;
- }
- /*
- * 获取相同频道的4本高推荐图书
- */
- public static function getSimpleChannelBook($bid, $num = 4)
- {
- $book_info = self::getBookById($bid);
- if (!$book_info) return false;
- $channel_name = $book_info->channel_name;
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- ->whereIn('book_configs.is_on_shelf', [1, 2])
- ->orderBy('recommend_index', 'desc')
- ->get()
- ->random($num);
- foreach ($res as $v) {
- $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
- }
- return $res;
- }
- /*
- * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
- * 新版1条
- */
- public static function getTrusteeShipChannelBook($distribution_channel_id, $channel_name, $num = 4)
- {
- // 找头条
- $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.recommend_index')
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- // ->where('book_configs.recommend_index', '>=',95)
- ->whereIn('book_configs.is_on_shelf', [2])
- ->orderBy('recommend_index', 'desc')
- ->limit(50)
- ->get()
- ->random(1);
- \Log::info('$first_res');
- \Log::info($first_res);
- $bids = [];
- $bids[] = $first_res[0]->bid;
- \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- ->whereIn('book_configs.bid', $bids)
- ->orderBy('recommend_index', 'desc')
- ->get();
- foreach ($res as $v) {
- $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
- }
- \Log::info('last_res');
- \Log::info($res);
- return $res;
- }
- /*
- * 获取托管智能推送的书籍,头条要95分以上,其余4条优质书库随机,按分数倒叙排列
- * 老版多条
- */
- public static function getTrusteeShipChannelBookMulty($distribution_channel_id, $channel_name, $num = 4)
- {
- // TODO 内部渠道的书=内部+外部,外部渠道的书=外部
- // 内部上架判断有点复杂,先统一外部上架
- // 找头条
- $first_res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.recommend_index')
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- // ->where('book_configs.recommend_index', '>=',95)
- ->whereIn('book_configs.is_on_shelf', [2])
- ->orderBy('recommend_index', 'desc')
- ->limit(10)
- ->get()
- ->random(1);
- \Log::info('$first_res');
- \Log::info($first_res);
- $bids = [];
- $bids[] = $first_res[0]->bid;
- // 找其余3条
- $left_res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid')
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- ->whereNotIn('book_configs.bid', $bids)
- ->whereIn('book_configs.is_on_shelf', [2])
- ->orderBy('recommend_index', 'desc')
- ->get()
- ->random(3);
- \Log::info('left_res');
- \Log::info($left_res);
- foreach ($left_res as $left_r) {
- $bids[] = $left_r->bid;
- }
- \Log::info('getTrusteeShipChannelBook_bids:' . json_encode($bids));
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price', 'books.updated_at as last_update_time',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- )
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.is_high_quality', 1)
- ->whereIn('book_configs.bid', $bids)
- ->orderBy('recommend_index', 'desc')
- ->get();
- foreach ($res as $v) {
- $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
- }
- \Log::info('last_res');
- \Log::info($res);
- return $res;
- }
- /*
- * H5专用,用户阅读完推荐
- * 获取相同推荐
- */
- public static function getRecommendBooks($bid, $channel_name, $num = 4)
- {
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
- )
- ->where('book_categories.channel_name', $channel_name)
- ->where('book_configs.bid', '!=', $bid)
- ->where('book_configs.is_high_quality', 1)
- ->orderBy('recommend_index', 'desc')->get();
- $count = $res->count() >= $num ? $num : $res->count();
- return $res->random($count);
- }
- /*
- * 签到专用
- * 获取相同推荐
- */
- public static function getSignRecommendBooks(array $bid, $channel_name, $num = 2)
- {
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
- )
- ->where('book_categories.channel_name', $channel_name)
- ->whereIn('book_configs.is_on_shelf', [2])
- ->whereNotIn('book_configs.bid', $bid)
- ->where('book_configs.is_high_quality', 1)
- ->get();
- $count = $res->count() >= $num ? $num : $res->count();
- return $res->random($count);
- }
- /*
- * 签到专用缓存版本
- * 获取相同推荐
- */
- public static function getSignRecommendBooksFromCache(array $bid, $channel_name, $num = 2)
- {
- $key = '';
- if ($channel_name == '女频') $key = 'female_high_reco_books';
- if ($channel_name == '男频') $key = 'male_high_reco_books';
- $reco_books = Redis::hgetall($key);
- //去除已经阅读过图书
- foreach ($bid as $_bid) {
- unset($reco_books[$_bid]);
- }
- $return_bids = array_rand($reco_books, $num);
- $return_books = [];
- foreach ($return_bids as $item) {
- $return_books[] = json_decode($reco_books[$item]);//object
- }
- return $return_books;
- }
- //获取男、女频优质书库图书
- public static function getChannelHighRecommendBooks($channel_name)
- {
- return self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.book_name', 'first_cid')
- ->where('book_categories.channel_name', $channel_name)
- ->whereIn('book_configs.is_on_shelf', [2])
- ->where('book_configs.is_high_quality', 1)
- ->get();
- }
- /*
- * 获取指定bid的书籍推荐
- */
- public static function getBidRecommendBooks(array $bids)
- {
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
- 'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
- 'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter',
- 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id'
- , 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.name as old_name'
- , 'book_configs.recommend_cid', 'book_configs.is_high_quality', 'books.updated_at as last_update_time'
- )
- ->whereIn('book_configs.bid', $bids)
- ->get();
- if (!empty($res)) {
- foreach ($res as $v) {
- $v->url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->first_cid;
- }
- }
- return $res;
- }
- /*
- * 获取指定bid的阅读链接
- */
- public static function getBidReadUrl($bid, $chapter_num)
- {
- $offset_chapter_num = $chapter_num - 2;
- $res = DB::select('select chapters.bid,chapters.sequence,chapters.next_cid
- from chapters where bid=' . $bid . '
- order by sequence asc
- limit 1
- offset ' . $offset_chapter_num);
- $read_url = '';
- if (!empty($res)) {
- foreach ($res as $v) {
- $read_url = '/reader?bid=' . Hashids::encode($v->bid) . '&cid=' . $v->next_cid;
- break;
- }
- }
- if (empty($read_url)) $read_url = '?';
- \Log::Info('getBidReadUrl,$bid:' . $bid . ' $chapter_num:' . $chapter_num . ' $read_url:' . $read_url);
- return $read_url;
- }
- /**
- * 修改vip章节
- */
- public static function updateVipSeq($bid, $seq)
- {
- return self::where('bid', $bid)->update(['vip_seq' => $seq]);
- }
- public static function getAllBooks($on_shelf, $order)
- {
- if (!$order) {
- $order = ['id', 'asc'];
- }
- return self::whereIn('is_on_shelf', $on_shelf)->select('bid', 'book_name')->orderBy($order[0], $order[1])->get();
- }
- /**
- * 根据条件获取书籍,没有分页
- */
- public static function getBooksNoPage(array $where = [], array $order = [], array $on_shelf, $limit = 20)
- {
- if (!$order) {
- $order = [['recommend_index', 'desc']];
- }
- $res = self::join('books', 'book_configs.bid', '=', 'books.id')
- ->leftjoin('book_categories', 'books.category_id', '=', 'book_categories.id')
- ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.cp_source', 'book_configs.vip_seq', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
- 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
- 'category_id', 'status', 'chapter_count', 'book_configs.click_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword',
- 'book_configs.recommend_index', 'book_configs.is_show_index_content', 'book_configs.product_id', 'book_categories.channel_name',
- 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'books.updated_at as last_update_time',
- 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
- );
- if ($where) {
- foreach ($where as $v) {
- $res->where($v[0], $v[1], $v[2]);
- }
- }
- $res->whereIn('is_on_shelf', $on_shelf);
- foreach ($order as $v) {
- $res->orderBy($v[0], $v[1]);
- }
- return $res->limit($limit)->get();
- }
- public static function getAllCps()
- {
- $cps = self::select('cp_source')->groupBy('cp_source')->get();
- $result = array();
- foreach ($cps as $cp) {
- if (!empty($cp['cp_source'])) {
- $result[] = $cp['cp_source'];
- }
- }
- return $result;
- }
- public static function getAllCpBooks()
- {
- $result = array();
- $cp_books = self::select('cp_source', 'bid')->groupBy('cp_source')->groupBy('bid')->get();
- foreach ($cp_books as $cp_book) {
- $result[$cp_book['cp_source']][] = $cp_book['bid'];
- }
- return $result;
- }
- /*
- * 得到cp某段时间某本书的消费
- */
- public static function getAllCpBookConsume($start_date, $end_date, $cps, $yestoday)
- {
- $result = self::leftjoin('book_order_statistical', 'book_order_statistical.bid', '=', 'book_configs.bid')
- //
- ->leftjoin('cp_book_time_configs', function ($join) {
- $join->on('cp_book_time_configs.bid', '=', 'book_configs.bid')
- ->on('cp_book_time_configs.cp_source', '=', 'book_configs.cp_source');
- })
- ->select('book_order_statistical.bid',
- DB::raw('sum(book_order_statistical.charge_balance) as charge_balance'),
- DB::raw('(select book_order_statistical.charge_balance from book_order_statistical where book_order_statistical.bid=book_configs.bid and day="' . $yestoday . '" limit 1) as yestoday_charge_balance'),
- 'book_configs.book_name', 'book_configs.is_on_shelf', 'book_configs.cp_source'
- )
- ->whereIn('book_configs.cp_source', $cps)
- // 2个起始值取最大值
- ->where('book_order_statistical.day', '>=', DB::raw("ifnull(cp_book_time_configs.start_date,'')"))
- ->where('book_order_statistical.day', '>=', $start_date)
- ->where('book_order_statistical.day', '<=', $end_date)
- ->groupBy('book_configs.cp_source')
- ->groupBy('book_order_statistical.bid')
- ->get();
- return $result;
- }
- public static function getBookByIdAndStatus($bid, $status)
- {
- return self::where('bid', $bid)->whereIn('is_on_shelf', $status)->first();
- }
- public static function get_all_test_books($is_all = false)
- {
- if ($is_all) {
- return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->get();
- } else {
- return self::where('test_status', '<>', 0)->orderBy('test_status', 'asc')->orderBy('test_update_time', 'desc')->paginate();
- }
- }
- public static function get_test_books($test_status)
- {
- return self::where('test_status', $test_status)->get();
- }
- public static function updateTestBook($bid, $test_status, $plan_push_user_num = 10000)
- {
- 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')]);
- }
- public static function get_all_smart_push_books($is_all = false)
- {
- if ($is_all) {
- return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->get();
- } else {
- return self::where('is_on_shelf', 2)->where('is_high_quality', 1)->orderBy('id', 'desc')->paginate();
- }
- }
- /**
- * 获取书本的id,名称,作者,封面 并带分页
- * @param bool $isAll 是否查询所有
- * @param int $pageSize 每页的条数
- * @param int $pageCount 页数
- * @return mixed
- */
- static function getBookCoverInfos($isAll = false, $pageSize = 200, $pageCount = 0)
- {
- $obj = self::join('books', 'book_configs.bid', '=', 'books.id')->select('book_configs.bid', 'book_configs.book_name', 'books.author', 'book_configs.cover');
- if ($isAll) {
- return $obj->get();
- } else {
- return $obj->limit($pageSize)->offset($pageCount * $pageSize)->get();
- }
- }
- /**
- * 获取书本的总数
- * @return mixed
- */
- static function getBooksCount()
- {
- return self::count();
- }
- /**
- * 通过书名模糊搜索bid
- * @param $book_name
- * @return mixed
- */
- static function getIdByName($book_name)
- {
- return self::select('bid')->where('book_name', 'like', '%' . $book_name . '%')->get();
- }
- //关键词搜(书名、主角)
- static function getFromRole($key)
- {
- return self::leftjoin('book_roles', 'book_configs.bid', '=', 'book_roles.bid')
- ->where('book_roles.role', $key)
- ->whereIn('is_on_shelf', [1])
- ->orderBy('is_on_shelf', 'desc')
- ->first();
- }
- /*
- * 获取相同频道的4本高推荐图书
- * 1、新书倒序 2、用户推荐过的,有阅读记录的不再推荐
- */
- public static function getLeftRecomBids($uid, $channel_name, $num = 4, $loop = 1)
- {
- // 获取用户阅读记录详情
- $read_bids = ReadRecordService::getSimpleReadRecord($uid);
- // 获取用户推荐过的详情
- $recommend_bids = self::getUserRecommendRecords($uid);
- // 获取全集,不含测试书籍
- $full_bids = $full_bid_no_tests = self::getLeftRecommendBook($channel_name, 1, false);
- \Log::info('uid:'.$uid.' read_bids:' . json_encode($read_bids) . ' recommend_bids:' . json_encode($recommend_bids). ' full_bids:'.json_encode($full_bids));
- //判断如果注册时间小于10天,不推测试书籍
- $test_book = null;
- $test_bid = 0;
- $last_bids = [];
- try {
- if ($uid) {
- $user = UserService::getById($uid);
- if ($user && (time() - strtotime($user->created_at) > 10 * 86400)) {
- $channel_sex = $channel_name === '男频' ? 1 : 2;
- $temp = TestBookService::getTestBook($channel_sex);
- if ($temp && !in_array($temp->bid, $read_bids)) {
- $test_book = $temp;
- $test_bid = $temp->bid;
- }
- }
- }
- \Log::info('test_book:' . json_encode($test_book));
- $test_book && $full_bids = array_merge([$test_book->bid], $full_bid_no_tests);
- // 得到差集
- $left_bids = array_diff($full_bids, $recommend_bids, $read_bids);
- $left_bid_num = count($left_bids);
- if ($left_bid_num) {
- $last_bids = array_slice($left_bids, 0, $num, false);
- } else {
- //判断推送的书都被阅读情况下 随机从优质书中取
- if (!$not_read_bids = array_diff($full_bids, $read_bids)) {
- $last_bids[] = $full_bid_no_tests[array_rand($full_bid_no_tests)];
- } else {
- //推送过但未阅读情况下,清空继续推
- self::truncateUserRecommendRecords($uid);
- $last_bids = array_slice($not_read_bids, 0, $num, false);
- }
- }
- // 加入已经推荐
- self::addUserRecommendRecords($uid, $last_bids);
- // 书籍推送量+1
- self::incrBidPushNum($last_bids);
- foreach ($last_bids as $_bid) {
- if ($test_book && $test_book->bid == $_bid) {
- $redis_bid_push_num = Redis::hget('SmartPushBookUserNum', $_bid);
- $plan_push_user_num = $test_book->plan_push_user_num;
- $uid && TestBookService::record($test_book->id, $uid);
- if ($redis_bid_push_num >= $plan_push_user_num) {
- TestBookService::finishTest($_bid);
- }
- }
- }
- } catch (Exception $e) {
- \Illuminate\Support\Facades\Log::error('get test book error:' . $e->getMessage() . $e->getLine());
- $last_bids = [$full_bid_no_tests[array_rand($full_bid_no_tests)]];
- }
- \Log::info('last_bids:' . json_encode($last_bids));
- return [$last_bids, $test_bid];
- }
- //获取图书
- public static function getSimpleBooksByIds($bids)
- {
- $fields = ['book_name','bid','cover'];
- return self::select($fields)->whereIn('id',$bids)->where('is_on_shelf',2)->order('id','desc')->get();
- }
- }
|