123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace App\Modules\Book\Services;
- use App\Modules\Book\Models\Book;
- use App\Modules\Book\Models\Chapter;
- use Hashids;
- use Redis;
- use App\Modules\Statistic\Services\WapVisitStatService;
- use DB;
- class BookService
- {
- public static function decodeBidStatic(string $bid)
- {
- return Hashids::decode($bid)[0];
- }
- /**
- * 获取book中存在的分类信息id
- * @return array
- */
- public static function getCategoryId()
- {
- return Book::getCategoryId();
- }
- /**
- * 修改图书描述
- * @param $bid
- * @param $intro
- * @return mixed
- *
- */
- public static function updateIntro($bid, $intro)
- {
- return Book::where('id', $bid)->update(['intro' => $intro]);
- }
- /**
- * 设置章节价格
- * @param int $channel_id
- * @param int $price
- */
- public static function setChapterPrice(int $channel_id, float $price): void
- {
- try {
- Redis::hset('book_chapter_price', $channel_id, $price);
- } catch (\Exception $e) { }
- }
- /**
- * 获取渠道设置的价格
- * @param int $channel_id
- * @return int
- */
- public static function getChapterPrice(int $channel_id)
- {
- try {
- $fee = Redis::hget('book_chapter_price', $channel_id);
- if ($fee)
- return (float) $fee;
- else
- return 0;
- } catch (\Exception $e) { }
- return 0;
- }
- public static function getBookStatistics($smart_push_books)
- {
- // 获取书籍统计数据
- if (!empty($smart_push_books)) {
- foreach ($smart_push_books as $key => $smart_push_book) {
- $book_statistics = WapVisitStatService::smartPushTestBookStats($smart_push_book->bid);
- $smart_push_books[$key]->uv = $book_statistics['uv'];
- $smart_push_books[$key]->pv = $book_statistics['pv'];
- $smart_push_books[$key]->charge_amount = $book_statistics['charge_amount'];
- $smart_push_books[$key]->charge_user_num = $book_statistics['charge_user_num'];
- $smart_push_books[$key]->book_amount = $book_statistics['book_amount'];
- $smart_push_books[$key]->book_user_num = $book_statistics['book_user_num'];
- $smart_push_books[$key]->real_push_user_num = $book_statistics['real_push_user_num'];
- $smart_push_books[$key]->second_chapter_uv = $book_statistics['second_chapter_uv'];
- }
- }
- return $smart_push_books;
- }
- public static function getBookById($bid)
- {
- return Book::find($bid);
- }
- public static function newYunQiBook($bid)
- {
- $old = DB::table('book_yunqi')->where('yq_bid', $bid)->where('type', 'NEW_YUNQI')->first();
- if ($old) {
- return -1;
- }
- $new_yunqi_book = DB::connection('new_yunqi')
- ->table('books')
- ->join('book_configs', 'books.id', '=', 'book_configs.bid')
- ->select(
- 'books.id',
- 'books.author',
- 'books.author',
- 'books.intro',
- 'books.category_name',
- 'books.keyword',
- 'books.status',
- 'books.chapter_count',
- 'books.size',
- 'books.last_chapter',
- 'book_configs.book_name as name',
- 'book_configs.cover',
- 'book_configs.force_subscribe_chapter_seq',
- 'book_configs.charge_type',
- 'book_configs.roles'
- )
- ->where('books.id', $bid)
- ->first();
- if (!$new_yunqi_book) {
- return -2;
- }
- $book = Book::create(
- [
- 'ly_bid' => 0, 'name' => $new_yunqi_book->name, 'author' => $new_yunqi_book->author, 'intro' => $new_yunqi_book->intro, 'cover' => $new_yunqi_book->cover,
- 'category_name' => $new_yunqi_book->category_name, 'keyword' => $new_yunqi_book->keyword, 'category_id' => 0, 'status' => $new_yunqi_book->status,
- 'chapter_count' => $new_yunqi_book->chapter_count, 'first_cid' => 0, 'last_cid' => 0, 'size' => $new_yunqi_book->size, 'last_chapter' => $new_yunqi_book->last_chapter,
- 'sequence' => 0, 'yq_bid' => $bid
- ]
- );
- DB::table('book_yunqi')->insert([
- 'bid' => $book->id,
- 'yq_bid' => $bid,
- 'type' => 'NEW_YUNQI',
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- for ($i = 1; $i <= $new_yunqi_book->chapter_count; $i++) {
- $temp = DB::connection('new_yunqi')->table('chapters')->where('bid', $bid)->where('sequence', $i)->select('name', 'content', 'is_vip', 'size')->first();
- if ($temp) {
- Chapter::create([
- 'bid' => $book->id,
- 'name' => $temp->name,
- 'sequence' => $i,
- 'is_vip' => $temp->is_vip,
- 'size' => $temp->size,
- 'prev_cid' => 0,
- 'next_cid' => 0,
- 'recent_update_at' => date('Y-m-d H:i:s'),
- 'content' => $temp->content,
- 'ly_chapter_id' => 0
- ]);
- }
- }
- \Artisan::call('book:afs', ['bid' => [$book->id]]);
- DB::table('book_configs')->where('bid', $book->id)->update([
- 'force_subscribe_chapter_seq' => $new_yunqi_book->force_subscribe_chapter_seq,
- 'roles' => $new_yunqi_book->roles,
- 'charge_type' => $new_yunqi_book->charge_type,
- 'cp_source' => 'new_yunqi'
- ]);
- return 0;
- }
- public static function getPrice($book_info,$distribution_channel_id,$size){
- $calculate_price_type = $book_info->calculate_price_type;
- $channel_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id, $book_info->bid);
- if($channel_calculate_price_type){
- $calculate_price_type = $channel_calculate_price_type;
- }
- //固定价格
- if(strtolower($calculate_price_type) == 'const'){
- $price = (int)$book_info->unit_price;
- return $price;
- }
- //千字价格
- $channel_fee = self::getChapterPrice($distribution_channel_id);
- if ($channel_fee) {
- $price_rate = $channel_fee / 100;
- } else {
- $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
- }
- if($book_info->unit_price){
- $price_rate = $book_info->unit_price;
- }
- $fee = ceil($size * $price_rate);
- if($fee >189) $fee = 189;
- if($fee <37) $fee = 37;
- return $fee;
- }
- }
|