BookService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 getBookById($bid)
  62. {
  63. return Book::find($bid);
  64. }
  65. public static function getPrice($book_info,$distribution_channel_id,$size,$account=''){
  66. // 书默认--- 收费类型
  67. $calculate_price_type = $book_info->calculate_price_type;
  68. // 渠道bid--- 收费类型
  69. $channel_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id, $book_info->bid);
  70. if($channel_calculate_price_type){
  71. $calculate_price_type = $channel_calculate_price_type;
  72. }
  73. // 账户bid优先--- 收费类型
  74. $channel_account_calculate_price_type = Redis::hget('channel:charge_type:setting:' . $distribution_channel_id.':'.$account, $book_info->bid);
  75. if($channel_account_calculate_price_type){
  76. $calculate_price_type = $channel_account_calculate_price_type;
  77. }
  78. // 渠道bid价格
  79. $distribution_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id,$book_info->bid);
  80. // 账户bid默认价格
  81. $distribution_account_channel_id_price = Redis::hget('channel:price:setting:'.$distribution_channel_id.':'.$account, $book_info->bid);
  82. //固定价格
  83. if(strtolower($calculate_price_type) == 'const'){
  84. $price = (int)$book_info->unit_price;
  85. if($distribution_channel_id_price){
  86. $price = (int)$distribution_channel_id_price;
  87. }
  88. if($distribution_account_channel_id_price){
  89. $price = (int)$distribution_account_channel_id_price;
  90. }
  91. $price = $price < 30 ? 30 : $price;
  92. //\Log::info('getprice:book:'.$book_info->bid.' calculate_price_type:'.$calculate_price_type.' distribution_channel_id:'.$distribution_channel_id.' account:'.$account.' price:'.$price);
  93. return $price;
  94. }
  95. //千字价格
  96. $channel_fee = self::getChapterPrice($distribution_channel_id);
  97. if ($channel_fee) {
  98. $price_rate = $channel_fee / 100;
  99. } else {
  100. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  101. }
  102. if($book_info->unit_price){
  103. $price_rate = $book_info->unit_price;
  104. }
  105. if($distribution_channel_id_price){
  106. $price_rate = $distribution_channel_id_price;
  107. }
  108. if($distribution_account_channel_id_price){
  109. $price_rate = $distribution_account_channel_id_price;
  110. }
  111. $fee = ceil($size * $price_rate);
  112. if($fee >189) $fee = 189;
  113. if($fee <37) $fee = 37;
  114. //\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);
  115. return $fee;
  116. }
  117. /**
  118. * 切章后,[是否切章,是否改名]
  119. * @param $bid
  120. * @return array
  121. */
  122. public static function splitContent($bid){
  123. $config = DB::table('book_split_operate_record')->where('bid',$bid)->select('chapter_name_type','is_process')->orderBy('id','desc')->first();
  124. if(!$config) return [0,0];
  125. if($config->is_process == 2){
  126. return [1,$config->chapter_name_type];
  127. }
  128. return [0,0];
  129. }
  130. }