BookService.php 6.7 KB

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