<?php
/**
 * Created by PhpStorm.
 * User: Hardy
 * Date: 2020/7/31
 * Time: 21:37
 */


namespace App\Console\Commands\SendOrder;


use App\Modules\Order\Services\OrderService;
use App\Modules\SendOrder\Models\SendOrderByRegist;
use App\Modules\SendOrder\Services\SendOrderByRegistService;
use Illuminate\Console\Command;

class GenerateUserRegistStat extends Command
{
    /**
     * The name and signature of the console command.
     * php artisan GenerateUserRegistStat
     * @var string
     */
    protected $signature = 'GenerateUserRegistStat {--begin_date=} {--end_date=}';

    /**
     * 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()
    {
        $this->start();
    }


    public function start()
    {
        $begin_date = $this->option('begin_date');
        $end_date   = $this->option('end_date');

        if(!isset($begin_date))
        {
            $begin_date=date('Y-m-d', strtotime('-1 day',strtotime(now())));
            $end_date =date('Y-m-d', strtotime(now()));
        }


        myLog('SendOrderByRegist')->notice('【' . $begin_date . '->' . $end_date . '】-------  开始');

        $endTime = $end_date;

        $runDay = $begin_date;

        while (strtotime($runDay) < strtotime($endTime)) {
            var_dump($runDay);
            $this->processOneDayNew($runDay);
            $runDay = date('Y-m-d', strtotime('+1 day', strtotime($runDay)));
        }
        myLog('SendOrderByRegist')->notice('【' . $begin_date . '->' . $end_date . '】-------  结束');
    }

    public function processOneDayNew($day)
    {
        //清理当天的派单按日注册
        myLog('SendOrderByRegist')->notice($day . '-------  开始:');
        $startTime = date('Y-m-d', strtotime($day));
        $endTime   = date('Y-m-d', strtotime('+1 day', strtotime($startTime)));

        //查询派单
        $orderService = new SendOrderByRegistService();

        $sendOrderIDs = $orderService->getSendOrderIdFromOrigin($startTime, $endTime);

        //dd(collect($sendOrderIDs)->implode('send_order_id', ', '));

        $bitch =500;
        $partIds = [];
        for ($i = 1; $i <= count($sendOrderIDs); $i++) {
            $partIds[] = $sendOrderIDs[$i - 1]->send_order_id;

            if ($i > 1 && $i % $bitch == 0) {
                $orderService->deleteRecs($partIds);
                $partIds = [];
            }
        }
        if(count($partIds)>0)
        {
            $orderService->deleteRecs($partIds);
        }
        myLog('SendOrderByRegist')->notice($day . '-------  清理历史记录完成');


        myLog('SendOrderByRegist')->notice('有效派单数'.count($sendOrderIDs));
        foreach ($sendOrderIDs as $item) {
            myLog('SendOrderByRegist')->notice('当前派单ID'.$item->send_order_id);
            self:: processSendOrderById($item->send_order_id, $endTime);
        }
        myLog('SendOrderByRegist')->notice($day . '-------  结束');

    }


    /**
     * @param int $send_order_id
     * @param string $endTime 不包含
     */
    public function processSendOrderById($send_order_id, $endTime)
    {
        $modelList['send_order_id'] = $send_order_id;
        //查询首充数及首充金额
        $orderService                  = new OrderService();
        $find                          = $orderService->getSendOrderFirstPayCountAndPriceByID($send_order_id, $endTime);
        $modelList['first_pay_count']  = empty($find) ? 0 : $find['first'];
        $modelList['first_pay_amount'] = empty($find) ? 0 : $find['price'];

        //付费人数,付费次数和充值总额
        $find                             = $orderService->getSendOrderSuccessPayCountByID($send_order_id, $endTime);
        $modelList['success_user_count']  = empty($find) ? 0 : $find['u_num'];
        $modelList['success_order_count'] = empty($find) ? 0 : $find['num'];
        $modelList['total_amount']        = empty($find) ? 0 : $find['price'];

        //24小时充值充值金额
        $hour                 = 24;
        $find                 = $orderService->getSendOrderPayPriceByIdAndHour($send_order_id, $endTime, $hour);
        $modelList['hour_24'] = empty($find) ? 0 : $find['price'];

        //24小时首充用户数
        $find                             = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 1);
        $modelList['hour_24_first_count'] = empty($find) ? 0 : $find['num'];
        //24小时非首充用户数
        $find                                 = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 0);
        $modelList['hour_24_not_first_count'] = empty($find) ? 0 : $find['num'];


        //3天充值充值金额
        $hour               = 24 * 3;
        $find               = $orderService->getSendOrderPayPriceByIdAndHour($send_order_id, $endTime, $hour);
        $modelList['day_3'] = empty($find) ? 0 : $find['price'];

        //3天首充用户数
        $find                           = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 1);
        $modelList['day_3_first_count'] = empty($find) ? 0 : $find['num'];
        //3天非首充用户数
        $find                               = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 0);
        $modelList['day_3_not_first_count'] = empty($find) ? 0 : $find['num'];

        //7天充值充值金额
        $hour               = 24 * 7;
        $find               = $orderService->getSendOrderPayPriceByIdAndHour($send_order_id, $endTime, $hour);
        $modelList['day_7'] = empty($find) ? 0 : $find['price'];

        //7天首充用户数
        $find                           = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 1);
        $modelList['day_7_first_count'] = empty($find) ? 0 : $find['num'];
        //7天非首充用户数
        $find                               = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 0);
        $modelList['day_7_not_first_count'] = empty($find) ? 0 : $find['num'];

        myLog('SendOrderByRegist')->notice('【' . $send_order_id . '】-------  开始保存数据');

        $helper = new SendOrderByRegistService();
        $find   = $helper->search(['send_order_id' => $send_order_id], true)->first();

        if (!empty($find)) {
            $find->first_pay_count         += $modelList['first_pay_count'];
            $find->first_pay_amount        += $modelList['first_pay_amount'];
            $find->success_user_count      += $modelList['success_user_count'];
            $find->success_order_count     += $modelList['success_order_count'];
            $find->total_amount            += $modelList['total_amount'];
            $find->hour_24                 += $modelList['hour_24'];
            $find->hour_24_first_count     += $modelList['hour_24_first_count'];
            $find->hour_24_not_first_count += $modelList['hour_24_not_first_count'];
            $find->day_3                   += $modelList['day_3'];
            $find->day_3_first_count       += $modelList['day_3_first_count'];
            $find->day_3_not_first_count   += $modelList['day_3_not_first_count'];
            $find->day_7                   += $modelList['day_7'];
            $find->day_7_first_count       += $modelList['day_7_first_count'];
            $find->day_7_not_first_count   += $modelList['day_7_not_first_count'];
            $find->save();
        } else {
            SendOrderByRegist::insert($modelList);
        }

        myLog('SendOrderByRegist')->notice('【' . $send_order_id . '】-------  保存数据完成');
    }
}