<?php

namespace App\Console\Commands\Book;

use Illuminate\Console\Command;
use App\Modules\Book\Models\Chapter;
use App\Modules\Book\Models\Book;
use App\Modules\Book\Models\BookConfig;
use App\Modules\Book\Models\BookRechargeExpectedPositions;

class BookRechargePredict extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book_recharge_predict';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '书籍充值预测';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $params = array(
            ['first_recharge_amount'=>50,'first_recharge_bookcoin'=>9000,'secondChargeAmount'=>50,'second_recharge_bookcoin'=>9000],
            ['first_recharge_amount'=>50,'first_recharge_bookcoin'=>9000,'secondChargeAmount'=>30,'second_recharge_bookcoin'=>3000],
            ['first_recharge_amount'=>30,'first_recharge_bookcoin'=>3000,'secondChargeAmount'=>50,'second_recharge_bookcoin'=>9000],
            ['first_recharge_amount'=>30,'first_recharge_bookcoin'=>3000,'secondChargeAmount'=>30,'second_recharge_bookcoin'=>3000],
        );
        $books = BookConfig::whereIn('is_on_shelf',[1,2])->get();
        foreach ($books as $book){
            if($book->charge_type == 'BOOK'){
                continue;
            }
            foreach ($params as $param){
                //dump($book->name);
                $this->simulationDeduction($book,$param);
            }
        }
    }

    public function simulationDeduction($book,$param){
        $price_rate = 0.04;//默认4毛
        echo('千字价位为:'.$price_rate);
        $give_bookcoin = 80;
        $prediction = array();
        $prediction['bid'] = $book->bid;
        $seq=$book->vip_seq;
        $blance = 0;
        $prediction['first_recharge_amount'] = $param['first_recharge_amount'];
        $blance += $param['first_recharge_bookcoin'] + $give_bookcoin;
        $prediction['balance_after_first_recharge'] = $blance;
        //dump('首充金额:'.$prediction['first_recharge_amount'].'首充后余额:'.$blance.'首充章节'.$seq);
        while($blance>0) {
            $chapter = Chapter::getChapterByBidAndSeq($book->bid, $seq);
            if ($chapter) {
                $price = $this->getPrice($chapter,$price_rate);
                $real_fee = $chapter->size*$price_rate;
                if($price>$blance){break;}
                $blance -= $price;
                //dump("第{$seq}章节,共{$chapter->size}字数,应收{$real_fee},实际收费{$price},扣费后余额为{$blance}");
                $seq++;
            } else {
                break;
            }
        }

        if($chapter){
            $prediction['sec_chapter_id'] = $chapter->id;
            $prediction['sec_chapter_seq'] = $chapter->sequence;
            $prediction['sec_chapter_name'] = $chapter->name;
            $prediction['balance_before_sec_recharge'] = $blance;
            $prediction['sec_recharge_amount'] = $param['secondChargeAmount'];
            $blance += $param['second_recharge_bookcoin'] + $give_bookcoin;
            $prediction['balance_after_sec_recharge'] = $blance;
            //dump('二充金额:'.$prediction['sec_recharge_amount'].'二充前余额:'.$prediction['balance_before_sec_recharge'].'二充后余额:'.$blance.'二充章节'.$seq);
            while($blance>0) {
                $chapter = Chapter::getChapterByBidAndSeq($book->bid, $seq);
                if ($chapter) {
                    $price = $this->getPrice($chapter,$price_rate);
                    $real_fee = $chapter->size*$price_rate;
                    if($price>$blance){break;}
                    $blance -= $price;
                    //dump("第{$seq}章节,共{$chapter->size}字数,应收{$real_fee},实际收费{$price},扣费后余额为{$blance}");
                    $seq++;
                } else {
                    break;
                }
            }

            if($chapter){
                $prediction['third_chapter_id'] = $chapter->id;
                $prediction['third_chapter_seq'] = $chapter->sequence;
                $prediction['third_chapter_name'] = $chapter->name;
                $prediction['balance_before_third_recharge'] = $blance;
            }else{
                $prediction['third_chapter_id'] = '';
                $prediction['third_chapter_seq'] = '';
                $prediction['third_chapter_name'] = '';
                $prediction['balance_before_third_recharge'] = '';
            }
            //dump('三充前余额:'.$prediction['balance_before_third_recharge'].'二充章节'.$seq);
        }else{
            $prediction['sec_chapter_id'] = '';
            $prediction['sec_chapter_seq'] = '';
            $prediction['sec_chapter_name'] = '';
            $prediction['balance_after_first_recharge'] = '';
            $prediction['balance_before_sec_recharge'] = '';
            $prediction['sec_recharge_amount'] = '';
            $prediction['third_chapter_id'] = '';
            $prediction['third_chapter_seq'] = '';
            $prediction['third_chapter_name'] = '';
            $prediction['balance_after_sec_recharge'] = '';
            $prediction['balance_before_third_recharge'] = '';
        }

        BookRechargeExpectedPositions::updateOrCreate([
            'bid'=>$prediction['bid'],
            'first_recharge_amount'=>$prediction['first_recharge_amount'],
            'sec_recharge_amount'=>$prediction['sec_recharge_amount'],
            ]
            ,$prediction);

    }

    public static function getPrice($chapter,$price_rate){

        $fee = ceil($chapter->size * $price_rate);
        if($fee >189) $fee = 189;
        if($fee <37) $fee = 37;

        return $fee;
    }
}