BookService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $decodeBid = Hashids::decode($bid);
  15. $decodeBid = is_array($decodeBid) ? array_shift($decodeBid) : $decodeBid;
  16. if(empty($decodeBid)){
  17. myLog("QappBookBidDecodeError")->info("bid : {$bid}, decodeBid : ".var_export($decodeBid,true));
  18. }
  19. return intval($decodeBid);
  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 getBookById($bid)
  68. {
  69. return Book::find($bid);
  70. }
  71. public static function getVipSequence($bid,$distribution_channel_id,$send_order_id){
  72. if(!$send_order_id) return 0;
  73. $account_send_order = QappSendOrder::getSendOrderById($send_order_id);
  74. $key = 'channel:chapterfee:setting:' . $distribution_channel_id;
  75. if($account_send_order){
  76. $account = isset($account_send_order['account'])?$account_send_order['account']:'';
  77. if($account){
  78. $key = 'channel:chapterfee:setting:' . $distribution_channel_id.':'.$account;
  79. }
  80. }
  81. return Redis::hget($key, $bid);
  82. }
  83. public static function getPrice($book_info,$distribution_channel_id,$size,$account=''){
  84. // 书默认--- 收费类型
  85. $calculate_price_type = $book_info->calculate_price_type;
  86. // 渠道bid--- 收费类型
  87. $channel_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id, $book_info->bid);
  88. if($channel_calculate_price_type){
  89. $calculate_price_type = $channel_calculate_price_type;
  90. }
  91. // 账户bid优先--- 收费类型
  92. $channel_account_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id.':'.$account, $book_info->bid);
  93. if($channel_account_calculate_price_type){
  94. $calculate_price_type = $channel_account_calculate_price_type;
  95. }
  96. // 渠道bid价格
  97. $distribution_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id,$book_info->bid);
  98. // 账户bid默认价格
  99. $distribution_account_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id.':'.$account, $book_info->bid);
  100. //固定价格
  101. if(strtolower($calculate_price_type) == 'const'){
  102. $price = (int)$book_info->unit_price;
  103. if($distribution_channel_id_price){
  104. $price = (int)$distribution_channel_id_price;
  105. }
  106. if($distribution_account_channel_id_price){
  107. $price = (int)$distribution_account_channel_id_price;
  108. }
  109. $price = $price < 30 ? 30 : $price;
  110. //\Log::info('getprice:book:'.$book_info->bid.' calculate_price_type:'.$calculate_price_type.' distribution_channel_id:'.$distribution_channel_id.' account:'.$account.' price:'.$price);
  111. return $price;
  112. }
  113. //千字价格
  114. $channel_fee = self::getChapterPrice($distribution_channel_id);
  115. if ($channel_fee) {
  116. $price_rate = $channel_fee / 100;
  117. } else {
  118. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  119. }
  120. if($book_info->unit_price){
  121. $price_rate = $book_info->unit_price;
  122. }
  123. if($distribution_channel_id_price){
  124. $price_rate = $distribution_channel_id_price;
  125. }
  126. if($distribution_account_channel_id_price){
  127. $price_rate = $distribution_account_channel_id_price;
  128. }
  129. $fee = ceil($size * $price_rate);
  130. if($fee >189) $fee = 189;
  131. if($fee <37) $fee = 37;
  132. //\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);
  133. return $fee;
  134. }
  135. /**
  136. * 切章后,[是否切章,是否改名]
  137. * @param $bid
  138. * @return array
  139. */
  140. public static function splitContent($bid){
  141. $config = DB::table('book_split_operate_record')->where('bid',$bid)->select('chapter_name_type','is_process')->orderBy('id','desc')->first();
  142. if(!$config) return [0,0];
  143. if($config->is_process == 2){
  144. return [1,$config->chapter_name_type];
  145. }
  146. return [0,0];
  147. }
  148. }