BookRechargePredict.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Console\Commands\Book;
  3. use Illuminate\Console\Command;
  4. use App\Modules\Book\Models\Chapter;
  5. use App\Modules\Book\Models\Book;
  6. use App\Modules\Book\Models\BookConfig;
  7. use App\Modules\Book\Models\BookRechargeExpectedPositions;
  8. class BookRechargePredict extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'book_recharge_predict';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '书籍充值预测';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $params = array(
  39. ['first_recharge_amount'=>50,'first_recharge_bookcoin'=>9000,'secondChargeAmount'=>50,'second_recharge_bookcoin'=>9000],
  40. ['first_recharge_amount'=>50,'first_recharge_bookcoin'=>9000,'secondChargeAmount'=>30,'second_recharge_bookcoin'=>3000],
  41. ['first_recharge_amount'=>30,'first_recharge_bookcoin'=>3000,'secondChargeAmount'=>50,'second_recharge_bookcoin'=>9000],
  42. ['first_recharge_amount'=>30,'first_recharge_bookcoin'=>3000,'secondChargeAmount'=>30,'second_recharge_bookcoin'=>3000],
  43. );
  44. $books = BookConfig::whereIn('is_on_shelf',[1,2])->get();
  45. foreach ($books as $book){
  46. if($book->charge_type == 'BOOK'){
  47. continue;
  48. }
  49. foreach ($params as $param){
  50. //dump($book->name);
  51. $this->simulationDeduction($book,$param);
  52. }
  53. }
  54. }
  55. public function simulationDeduction($book,$param){
  56. $price_rate = 0.04;//默认4毛
  57. echo('千字价位为:'.$price_rate);
  58. $give_bookcoin = 80;
  59. $prediction = array();
  60. $prediction['bid'] = $book->bid;
  61. $seq=$book->vip_seq;
  62. $blance = 0;
  63. $prediction['first_recharge_amount'] = $param['first_recharge_amount'];
  64. $blance += $param['first_recharge_bookcoin'] + $give_bookcoin;
  65. $prediction['balance_after_first_recharge'] = $blance;
  66. //dump('首充金额:'.$prediction['first_recharge_amount'].'首充后余额:'.$blance.'首充章节'.$seq);
  67. while($blance>0) {
  68. $chapter = Chapter::getChapterByBidAndSeq($book->bid, $seq);
  69. if ($chapter) {
  70. $price = $this->getPrice($chapter,$price_rate);
  71. $real_fee = $chapter->size*$price_rate;
  72. if($price>$blance){break;}
  73. $blance -= $price;
  74. //dump("第{$seq}章节,共{$chapter->size}字数,应收{$real_fee},实际收费{$price},扣费后余额为{$blance}");
  75. $seq++;
  76. } else {
  77. break;
  78. }
  79. }
  80. if($chapter){
  81. $prediction['sec_chapter_id'] = $chapter->id;
  82. $prediction['sec_chapter_seq'] = $chapter->sequence;
  83. $prediction['sec_chapter_name'] = $chapter->name;
  84. $prediction['balance_before_sec_recharge'] = $blance;
  85. $prediction['sec_recharge_amount'] = $param['secondChargeAmount'];
  86. $blance += $param['second_recharge_bookcoin'] + $give_bookcoin;
  87. $prediction['balance_after_sec_recharge'] = $blance;
  88. //dump('二充金额:'.$prediction['sec_recharge_amount'].'二充前余额:'.$prediction['balance_before_sec_recharge'].'二充后余额:'.$blance.'二充章节'.$seq);
  89. while($blance>0) {
  90. $chapter = Chapter::getChapterByBidAndSeq($book->bid, $seq);
  91. if ($chapter) {
  92. $price = $this->getPrice($chapter,$price_rate);
  93. $real_fee = $chapter->size*$price_rate;
  94. if($price>$blance){break;}
  95. $blance -= $price;
  96. //dump("第{$seq}章节,共{$chapter->size}字数,应收{$real_fee},实际收费{$price},扣费后余额为{$blance}");
  97. $seq++;
  98. } else {
  99. break;
  100. }
  101. }
  102. if($chapter){
  103. $prediction['third_chapter_id'] = $chapter->id;
  104. $prediction['third_chapter_seq'] = $chapter->sequence;
  105. $prediction['third_chapter_name'] = $chapter->name;
  106. $prediction['balance_before_third_recharge'] = $blance;
  107. }else{
  108. $prediction['third_chapter_id'] = '';
  109. $prediction['third_chapter_seq'] = '';
  110. $prediction['third_chapter_name'] = '';
  111. $prediction['balance_before_third_recharge'] = '';
  112. }
  113. //dump('三充前余额:'.$prediction['balance_before_third_recharge'].'二充章节'.$seq);
  114. }else{
  115. $prediction['sec_chapter_id'] = '';
  116. $prediction['sec_chapter_seq'] = '';
  117. $prediction['sec_chapter_name'] = '';
  118. $prediction['balance_after_first_recharge'] = '';
  119. $prediction['balance_before_sec_recharge'] = '';
  120. $prediction['sec_recharge_amount'] = '';
  121. $prediction['third_chapter_id'] = '';
  122. $prediction['third_chapter_seq'] = '';
  123. $prediction['third_chapter_name'] = '';
  124. $prediction['balance_after_sec_recharge'] = '';
  125. $prediction['balance_before_third_recharge'] = '';
  126. }
  127. BookRechargeExpectedPositions::updateOrCreate([
  128. 'bid'=>$prediction['bid'],
  129. 'first_recharge_amount'=>$prediction['first_recharge_amount'],
  130. 'sec_recharge_amount'=>$prediction['sec_recharge_amount'],
  131. ]
  132. ,$prediction);
  133. }
  134. public static function getPrice($chapter,$price_rate){
  135. $fee = ceil($chapter->size * $price_rate);
  136. if($fee >189) $fee = 189;
  137. if($fee <37) $fee = 37;
  138. return $fee;
  139. }
  140. }