BookService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Modules\Book\Services;
  3. use App\Modules\BaseService;
  4. use App\Modules\Book\Models\Book;
  5. use App\Modules\Book\Models\Chapter;
  6. use Hashids;
  7. use Redis;
  8. use App\Modules\Statistic\Services\WapVisitStatService;
  9. use DB;
  10. /**
  11. *
  12. * @method static int decodeBidStatic(string $bid) 解析bid
  13. */
  14. class BookService
  15. {
  16. use BaseService;
  17. public function decodeBid(string $bid)
  18. {
  19. return Hashids::decode($bid)[0];
  20. }
  21. /**
  22. * 获取book中存在的分类信息id
  23. * @return array
  24. */
  25. public static function getCategoryId()
  26. {
  27. return Book::getCategoryId();
  28. }
  29. /**
  30. * 修改图书描述
  31. * @param $bid
  32. * @param $intro
  33. * @return mixed
  34. *
  35. */
  36. public static function updateIntro($bid, $intro)
  37. {
  38. return Book::where('id', $bid)->update(['intro' => $intro]);
  39. }
  40. /**
  41. * 设置章节价格
  42. * @param int $channel_id
  43. * @param int $price
  44. */
  45. public static function setChapterPrice(int $channel_id, float $price): void
  46. {
  47. try {
  48. Redis::hset('book_chapter_price', $channel_id, $price);
  49. } catch (\Exception $e) { }
  50. }
  51. /**
  52. * 获取渠道设置的价格
  53. * @param int $channel_id
  54. * @return int
  55. */
  56. public static function getChapterPrice(int $channel_id)
  57. {
  58. try {
  59. $fee = Redis::hget('book_chapter_price', $channel_id);
  60. if ($fee)
  61. return (float) $fee;
  62. else
  63. return 0;
  64. } catch (\Exception $e) { }
  65. return 0;
  66. }
  67. public static function getBookStatistics($smart_push_books)
  68. {
  69. // 获取书籍统计数据
  70. if (!empty($smart_push_books)) {
  71. foreach ($smart_push_books as $key => $smart_push_book) {
  72. $book_statistics = WapVisitStatService::smartPushTestBookStats($smart_push_book->bid);
  73. $smart_push_books[$key]->uv = $book_statistics['uv'];
  74. $smart_push_books[$key]->pv = $book_statistics['pv'];
  75. $smart_push_books[$key]->charge_amount = $book_statistics['charge_amount'];
  76. $smart_push_books[$key]->charge_user_num = $book_statistics['charge_user_num'];
  77. $smart_push_books[$key]->book_amount = $book_statistics['book_amount'];
  78. $smart_push_books[$key]->book_user_num = $book_statistics['book_user_num'];
  79. $smart_push_books[$key]->real_push_user_num = $book_statistics['real_push_user_num'];
  80. $smart_push_books[$key]->second_chapter_uv = $book_statistics['second_chapter_uv'];
  81. }
  82. }
  83. return $smart_push_books;
  84. }
  85. public static function getBookById($bid)
  86. {
  87. return Book::find($bid);
  88. }
  89. public static function newYunQiBook($bid)
  90. {
  91. $old = DB::table('book_yunqi')->where('yq_bid', $bid)->where('type', 'NEW_YUNQI')->first();
  92. if ($old) {
  93. return -1;
  94. }
  95. $new_yunqi_book = DB::connection('new_yunqi')
  96. ->table('books')
  97. ->join('book_configs', 'books.id', '=', 'book_configs.bid')
  98. ->select(
  99. 'books.id',
  100. 'books.author',
  101. 'books.author',
  102. 'books.intro',
  103. 'books.category_name',
  104. 'books.keyword',
  105. 'books.status',
  106. 'books.chapter_count',
  107. 'books.size',
  108. 'books.last_chapter',
  109. 'book_configs.book_name as name',
  110. 'book_configs.cover',
  111. 'book_configs.force_subscribe_chapter_seq',
  112. 'book_configs.charge_type',
  113. 'book_configs.roles'
  114. )
  115. ->where('books.id', $bid)
  116. ->first();
  117. if (!$new_yunqi_book) {
  118. return -2;
  119. }
  120. $book = Book::create(
  121. [
  122. 'ly_bid' => 0, 'name' => $new_yunqi_book->name, 'author' => $new_yunqi_book->author, 'intro' => $new_yunqi_book->intro, 'cover' => $new_yunqi_book->cover,
  123. 'category_name' => $new_yunqi_book->category_name, 'keyword' => $new_yunqi_book->keyword, 'category_id' => 0, 'status' => $new_yunqi_book->status,
  124. '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,
  125. 'sequence' => 0, 'yq_bid' => $bid
  126. ]
  127. );
  128. DB::table('book_yunqi')->insert([
  129. 'bid' => $book->id,
  130. 'yq_bid' => $bid,
  131. 'type' => 'NEW_YUNQI',
  132. 'created_at' => date('Y-m-d H:i:s'),
  133. 'updated_at' => date('Y-m-d H:i:s')
  134. ]);
  135. for ($i = 1; $i <= $new_yunqi_book->chapter_count; $i++) {
  136. $temp = DB::connection('new_yunqi')->table('chapters')->where('bid', $bid)->where('sequence', $i)->select('name', 'content', 'is_vip', 'size')->first();
  137. if ($temp) {
  138. Chapter::create([
  139. 'bid' => $book->id,
  140. 'name' => $temp->name,
  141. 'sequence' => $i,
  142. 'is_vip' => $temp->is_vip,
  143. 'size' => $temp->size,
  144. 'prev_cid' => 0,
  145. 'next_cid' => 0,
  146. 'recent_update_at' => date('Y-m-d H:i:s'),
  147. 'content' => $temp->content,
  148. 'ly_chapter_id' => 0
  149. ]);
  150. }
  151. }
  152. \Artisan::call('book:afs', ['bid' => [$book->id]]);
  153. DB::table('book_configs')->where('bid', $book->id)->update([
  154. 'force_subscribe_chapter_seq' => $new_yunqi_book->force_subscribe_chapter_seq,
  155. 'roles' => $new_yunqi_book->roles,
  156. 'charge_type' => $new_yunqi_book->charge_type,
  157. 'cp_source' => 'new_yunqi'
  158. ]);
  159. return 0;
  160. }
  161. public static function getPrice($book_info,$distribution_channel_id,$size){
  162. $calculate_price_type = $book_info->calculate_price_type;
  163. $channel_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id, $book_info->bid);
  164. if($channel_calculate_price_type){
  165. $calculate_price_type = $channel_calculate_price_type;
  166. }
  167. //固定价格
  168. if(strtolower($calculate_price_type) == 'const'){
  169. $price = (int)$book_info->unit_price;
  170. return $price;
  171. }
  172. //千字价格
  173. $channel_fee = self::getChapterPrice($distribution_channel_id);
  174. if ($channel_fee) {
  175. $price_rate = $channel_fee / 100;
  176. } else {
  177. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  178. }
  179. if($book_info->unit_price){
  180. $price_rate = $book_info->unit_price;
  181. }
  182. $fee = ceil($size * $price_rate);
  183. if($fee >189) $fee = 189;
  184. if($fee <37) $fee = 37;
  185. return $fee;
  186. }
  187. }