<?php


namespace App\Console\Commands;


use App\Facade\TableName;
use App\Http\helper\NumConfig;
use App\Models\AccountsInfo;
use App\Models\RecordScoreInfo;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * 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()
    {
        $date = Carbon::yesterday();
//        $date = Carbon::parse('20220525');
//        $RecordPlatformDataModel = new \App\Models\RecordPlatformData();

        $dateID = $date->format('Ymd');

        $service = new \App\Services\RecordPlatformData();

        $dayBefore = Carbon::now()->subDays(30)->format('Ymd');

        $list = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
            ->where('DateID', '>=', $dayBefore)
            ->get();
        $now = Carbon::now()->format('Ymd');

        foreach ($list as $value) {
            $dateDay = $this->dateDiff($value->DateID, $now);
            switch ($dateDay) {
                case 2:
                    $service->Retain($value->DateID, Carbon::parse($value->DateID)->addDay(1), 'RetainOne');
                    $service->activeRetain($value->DateID, Carbon::parse($value->DateID)->addDay(1), 'ActiveRetainOne');
                    $service->payRetain($value->DateID, Carbon::parse($value->DateID)->addDay(1), 'PayRetainOne');
                    break;
                case 3:
                    // 注册三日留存
                    $service->Retain($value->DateID, Carbon::parse($value->DateID)->addDay(2), 'RetainThree');
                    // 活跃三日留存
                    $service->activeRetain($value->DateID, Carbon::parse($value->DateID)->addDay(2), 'ActiveRetainThree');
                    // 付费三日留存
                    $service->payRetain($value->DateID, Carbon::parse($value->DateID)->addDay(2), 'PayRetainThree');
                    break;
                case 7:
                    // 注册七日留存
                    $service->Retain($value->DateID, Carbon::parse($value->DateID)->addDay(6), 'RetainSeven');
                    // 活跃七日留存
                    $service->activeRetain($value->DateID, Carbon::parse($value->DateID)->addDay(6), 'ActiveRetainSeven');
                    // 付费七日留存
                    $service->payRetain($value->DateID, Carbon::parse($value->DateID)->addDay(6), 'PayRetainSeven');
                    break;
                case 30:
                    // 注册三十日留存
                    $service->Retain($value->DateID, Carbon::parse($value->DateID)->addDay(29), 'RetainThirty');
                    // 活跃三十日留存
                    $service->activeRetain($value->DateID, Carbon::parse($value->DateID)->addDay(29), 'ActiveRetainThirty');
                    // 付费三十日留存
                    $service->payRetain($value->DateID, Carbon::parse($value->DateID)->addDay(29), 'PayRetainThirty');
                    break;
            }
        }

        // 分渠道修改
        $this->affixation($date, $dateID);
    }


    public function affixation($date, $dateID)
    {
        $RecordPlatformDataModel = new \App\Models\RecordPlatformData();
        $dao = new \App\dao\RecordPlatformData\RecordPlatformData();

        // 申请提现金额,申请人数，申请笔数
        $OrderWithDraw = $dao->ApplyWithDraw($date);
        // 总申请提现金额
        $TotalOrderWithDraw = $dao->TotalOrderWithDraw($date);
        // 提现成功的手续费
        $ServiceFee = $dao->WithDrawOkFree($dateID);
        // 游戏人数
        $GameUserCounts = $dao->GameUserCount($dateID);
        // 新增参游人数
        $NewGameUserCounts = $dao->NewGameUserCount($date);
        // 赠送金币 =====  增加赠送金币
        //$GiveGolds = RecordScoreInfo::todayLottery($date->format('Y-m-d'));
        // 充值彩金
//        $RechargeWinnings = $dao->RechargeWinnings($dateID);

        // 分享彩金
        //$ShareWinnings = $dao->ShareWinnings($dateID);
        // 低保
        //$Dibao = $dao->dibao($dateID);
        // 付费低保
        //$payDibao = $dao->payDibao($dateID);
        // 签到人数、金额
        //$signIn = $dao->signIn($dateID);


        $list = $RecordPlatformDataModel->where('DateID', $dateID)->where('Channel', '>=', 0)->get();

        foreach ($list as $value) {


            if ($OrderWithDraw->isNotEmpty()) {
                foreach ($OrderWithDraw as $item) {
                    $data = ['DateID' => $dateID, 'ApplyWithdraw' => $item->WithDraw, 'ApplyWithdrawCount' => $item->UserCount, 'ApplyWithdrawBi' => $item->CountBi];
                    // 申请提现渠道修改
                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
                }
            }


            if ($ServiceFee->isNotEmpty()) {
                // 提现完成手续费渠道修改
                foreach ($ServiceFee as $item) {
                    $data = ['DateID' => $dateID, 'WithdrawFree' => $item->ServiceFee];

                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);

                }
            }

            // 游戏人数
            if ($GameUserCounts->isNotEmpty()) {

                foreach ($GameUserCounts as $item) {
                    $data = ['DateID' => $dateID, 'GameUserCount' => $item->UserCount];

                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
                }
            }


//            if ($GiveGolds->isNotEmpty()) {
//                // 赠送金币
//                foreach ($GiveGolds as $item) {
//                    $data = ['DateID' => $dateID, 'GiveGold' => $item->ChangeScore];
//
//                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
//
//                }
//            }

            if ($NewGameUserCounts->isNotEmpty()) {
                // 新增游戏人数
                foreach ($NewGameUserCounts as $item) {

                    $data = ['DateID' => $dateID, 'NewGameUserCount' => $item->UserCount];

                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
                }
            }

//            if ($RechargeWinnings->isNotEmpty()) {
//                // 充值彩金
//                foreach ($RechargeWinnings as $item) {
//
//                    $data = ['DateID' => $dateID, 'RechargeWinnings' => $item->Score];
//
//                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
//                }
//            }

//            if ($ShareWinnings->isNotEmpty()) {
//                // 分享彩金
//                foreach ($ShareWinnings as $item) {
//
//                    $data = ['DateID' => $dateID, 'ShareWinnings' => $item->Score];
//
//                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
//                }
//            }

//            if ($Dibao->isNotEmpty()) {
//                // 低保彩金
//                foreach ($Dibao as $item) {
//
//                    $data = ['DateID' => $dateID, 'Dibao' => $item->Score,'TotalDibaoCount'=>$item->TotalDibaoCount];
//
//                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
//                }
//            }

//            if ($payDibao->isNotEmpty()) {
//                // 付费低保人数
//                foreach ($payDibao as $item) {
//
//                    $data = ['DateID' => $dateID, 'PayDibao' => $item->PayDibao];
//
//                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
//                }
//            }

//            if ($signIn->isNotEmpty()) {
//                // 付费低保人数
//                foreach ($signIn as $item) {
//
//                    $data = ['DateID' => $dateID, 'SignInSum' => $item->SignInSum,'SignInCount'=>$item->SignInCount];
//
//                    $dao->Update($data, $value->Channel, $item->Channel, $dateID);
//                }
//            }


            $ARPU = $value->ActivePeple > 0 ? number_format(($value->PayTotal / $value->ActivePeple) / NumConfig::NUM_VALUE,2,'.','') : 0;
            $ARPPU = $value->PayPeple > 0 ? number_format(($value->PayTotal / $value->PayPeple) / NumConfig::NUM_VALUE,2,'.','') : 0;
            // 申请提现金额 、 人数 、 笔数
            $ApplyWithdraw = $TotalOrderWithDraw->WithDraw ?? 0;
            $ApplyWithdrawCount = $TotalOrderWithDraw->UserCount ?? 0;
            $ApplyWithdrawBi = $TotalOrderWithDraw->CountBi ?? 0;
            // 提现成功手续费
            $WithdrawFree = $ServiceFee->sum('ServiceFee');
            $GameUserCount = $GameUserCounts->sum('UserCount');
            $NewGameUserCount = $NewGameUserCounts->sum('UserCount');


//            $GiveGold = $GiveGolds->sum('ChangeScore');
//            $RechargeWinningsScore = $RechargeWinnings->sum('Score');
//
//
//            $ShareWinningsScore = $ShareWinnings->sum('Score');
//            $DibaoScore = $Dibao->sum('Score');
//            $TotalDibaoCount = $Dibao->sum('TotalDibaoCount');
//            $PayDibaoCont = $payDibao->sum('PayDibao');
//            $SignInSum = $signIn->sum('SignInSum');
//            $SignInCount = $signIn->sum('SignInCount');

            $GiveGold = 0;
            $RechargeWinningsScore = 0;
            $ShareWinningsScore = 0;
            $DibaoScore = 0;
            $TotalDibaoCount = 0;
            $PayDibaoCont = 0;
            $SignInSum = 0;
            $SignInCount = 0;

            $data = compact('ARPPU', 'ARPU', 'ApplyWithdraw', 'WithdrawFree', 'GameUserCount', 'NewGameUserCount', 'GiveGold', 'ApplyWithdrawBi',
                'ApplyWithdrawCount','TotalDibaoCount','SignInSum','SignInCount');
            $data['RechargeWinnings'] = $RechargeWinningsScore;
            $data['ShareWinnings'] = $ShareWinningsScore;
            $data['Dibao'] = $DibaoScore;
            $data['PayDibao'] = $PayDibaoCont;
            $RecordPlatformDataModel->where('DateID', $dateID)->where('Channel', -1)->update($data);

        }
    }

    // 日期差
    public function dateDiff($time1, $time2)
    {

        $datetime_start = date_create(date('Y-m-d', strtotime($time1, 1)));

        $datetime_end = date_create(date('Y-m-d', strtotime($time2)));

        $days = date_diff($datetime_start, $datetime_end)->days;
        return $days;
    }
}
