BookService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Modules\Book\Services;
  3. use App\Modules\Book\Models\Book;
  4. use App\Modules\Book\Models\Chapter;
  5. use App\Modules\SendOrder\Models\QappSendOrder;
  6. use Hashids;
  7. use Redis;
  8. use App\Modules\Statistic\Services\WapVisitStatService;
  9. use DB;
  10. class BookService
  11. {
  12. public static function decodeBidStatic(string $bid)
  13. {
  14. return Hashids::decode($bid)[0];
  15. }
  16. /**
  17. * 获取book中存在的分类信息id
  18. * @return array
  19. */
  20. public static function getCategoryId()
  21. {
  22. return Book::getCategoryId();
  23. }
  24. /**
  25. * 修改图书描述
  26. * @param $bid
  27. * @param $intro
  28. * @return mixed
  29. *
  30. */
  31. public static function updateIntro($bid, $intro)
  32. {
  33. return Book::where('id', $bid)->update(['intro' => $intro]);
  34. }
  35. /**
  36. * 设置章节价格
  37. * @param int $channel_id
  38. * @param int $price
  39. */
  40. public static function setChapterPrice(int $channel_id, float $price): void
  41. {
  42. try {
  43. Redis::hset('book_chapter_price', $channel_id, $price);
  44. } catch (\Exception $e) { }
  45. }
  46. /**
  47. * 获取渠道设置的价格
  48. * @param int $channel_id
  49. * @return int
  50. */
  51. public static function getChapterPrice(int $channel_id)
  52. {
  53. try {
  54. $fee = Redis::hget('book_chapter_price', $channel_id);
  55. if ($fee)
  56. return (float) $fee;
  57. else
  58. return 0;
  59. } catch (\Exception $e) { }
  60. return 0;
  61. }
  62. public static function getBookById($bid)
  63. {
  64. return Book::find($bid);
  65. }
  66. public static function getVipSequence($bid,$distribution_channel_id,$send_order_id){
  67. if(!$send_order_id) return 0;
  68. $account_send_order = QappSendOrder::getSendOrderById($send_order_id);
  69. $key = 'channel:chapterfee:setting:' . $distribution_channel_id;
  70. if($account_send_order){
  71. $account = isset($account_send_order['account'])?$account_send_order['account']:'';
  72. if($account){
  73. $key = 'channel:chapterfee:setting:' . $distribution_channel_id.':'.$account;
  74. }
  75. }
  76. return Redis::hget($key, $bid);
  77. }
  78. public static function getPrice($book_info,$distribution_channel_id,$size,$account=''){
  79. // 书默认--- 收费类型
  80. $calculate_price_type = $book_info->calculate_price_type;
  81. // 渠道bid--- 收费类型
  82. $channel_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id, $book_info->bid);
  83. if($channel_calculate_price_type){
  84. $calculate_price_type = $channel_calculate_price_type;
  85. }
  86. // 账户bid优先--- 收费类型
  87. $channel_account_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id.':'.$account, $book_info->bid);
  88. if($channel_account_calculate_price_type){
  89. $calculate_price_type = $channel_account_calculate_price_type;
  90. }
  91. // 渠道bid价格
  92. $distribution_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id,$book_info->bid);
  93. // 账户bid默认价格
  94. $distribution_account_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id.':'.$account, $book_info->bid);
  95. //固定价格
  96. if(strtolower($calculate_price_type) == 'const'){
  97. $price = (int)$book_info->unit_price;
  98. if($distribution_channel_id_price){
  99. $price = (int)$distribution_channel_id_price;
  100. }
  101. if($distribution_account_channel_id_price){
  102. $price = (int)$distribution_account_channel_id_price;
  103. }
  104. $price = $price < 30 ? 30 : $price;
  105. //\Log::info('getprice:book:'.$book_info->bid.' calculate_price_type:'.$calculate_price_type.' distribution_channel_id:'.$distribution_channel_id.' account:'.$account.' price:'.$price);
  106. return $price;
  107. }
  108. //千字价格
  109. $channel_fee = self::getChapterPrice($distribution_channel_id);
  110. if ($channel_fee) {
  111. $price_rate = $channel_fee / 100;
  112. } else {
  113. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  114. }
  115. if($book_info->unit_price){
  116. $price_rate = $book_info->unit_price;
  117. }
  118. if($distribution_channel_id_price){
  119. $price_rate = $distribution_channel_id_price;
  120. }
  121. if($distribution_account_channel_id_price){
  122. $price_rate = $distribution_account_channel_id_price;
  123. }
  124. $fee = ceil($size * $price_rate);
  125. if($fee >189) $fee = 189;
  126. if($fee <37) $fee = 37;
  127. //\Log::info('getprice:book:'.$book_info->bid.' calculate_price_type:'.$calculate_price_type.' distribution_channel_id:'.$distribution_channel_id.' account:'.$account.' price:'.$fee.' size:'.$size.' $price_rate:'.$price_rate);
  128. return $fee;
  129. }
  130. /**
  131. * 切章后,[是否切章,是否改名]
  132. * @param $bid
  133. * @return array
  134. */
  135. public static function splitContent($bid){
  136. $config = DB::table('book_split_operate_record')->where('bid',$bid)->select('chapter_name_type','is_process')->orderBy('id','desc')->first();
  137. if(!$config) return [0,0];
  138. if($config->is_process == 2){
  139. return [1,$config->chapter_name_type];
  140. }
  141. return [0,0];
  142. }
  143. }