<?php

namespace App\Console\Commands\SmartPush;

use App\Modules\MediaPush\Models\MediaPushDayStats;
use App\Modules\MediaPush\Models\OrderParam;
use App\Modules\PersonalOp\Models\MediaPushDetailRecord;
use DB;
use Illuminate\Console\Command;

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

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


    public function updateOneDayRechargeInfo()
    {
        $start_time = $date = date('Y-m-d H:i:s', strtotime('-1 day'));
        $params = ['is_one_day' => 1, 'start_time' => $start_time];
        $info = MediaPushDetailRecord::getUnUpdatedInfos($params);

        $officialChargeAmountArray = [];
        foreach ($info as $item) {
            $push_time = $item->push_time;
            $promotion_id = $item->promotion_id;
            $end_time = date('Y-m-d H:i:s', strtotime($item->push_time) + 86400);
            $params = ['start_time' => $push_time, 'end_time' => $end_time, 'promotion_id' => $promotion_id];;
            $recharge_in_one_day = OrderParam::getChargeAmount($params);
            $item->recharge_in_one_day = $recharge_in_one_day;
            $item->save();

            //更新dayStats表
            $appid = $item->appid;
            $date = date('Y-m-d', strtotime($item->push_time));
            if (isset($officialChargeAmountArray[$date][$appid])) {
                $officialChargeAmountArray[$date][$appid] = $officialChargeAmountArray[$date][$appid] + $recharge_in_one_day;
            } else {
                $officialChargeAmountArray[$date][$appid] = $recharge_in_one_day;
            }
        }

        foreach ($officialChargeAmountArray as $k => $v) {
            foreach ($v as $index => $value) {
                $date = $k;
                $appid = $index;
                $recharge_in_one_day = $value;

                $mediaPushDayInfo = MediaPushDayStats::getByAppidAndDate($appid, $date);
                if ($mediaPushDayInfo) {
                    if (empty($mediaPushDayInfo->recharge_in_one_day)) {
                        $mediaPushDayInfo->recharge_in_one_day = $recharge_in_one_day;
                    } else {
                        $mediaPushDayInfo->recharge_in_one_day += $recharge_in_one_day;
                    }
                    $mediaPushDayInfo->save();
                }
            }
        }
    }

    public function updateThreeDayRechargeInfo()
    {
        $start_time = $date = date('Y-m-d H:i:s', strtotime('-3 day'));
        $params = ['is_three_day' => 1, 'start_time' => $start_time];
        $info = MediaPushDetailRecord::getUnUpdatedInfos($params);
        $officialChargeAmountArray = [];
        foreach ($info as $item) {
            $push_time = $item->push_time;
            $promotion_id = $item->promotion_id;
            $end_time = date('Y-m-d H:i:s', strtotime($item->push_time) + 3 * 86400);
            $params = ['start_time' => $push_time, 'end_time' => $end_time, 'promotion_id' => $promotion_id];;
            $recharge_in_three_days = OrderParam::getChargeAmount($params);
            $item->recharge_in_three_days = $recharge_in_three_days;
            $item->save();

            //更新dayStats表
            $appid = $item->appid;
            $date = date('Y-m-d', strtotime($item->push_time));

            if (isset($officialChargeAmountArray[$date][$appid])) {
                $officialChargeAmountArray[$date][$appid] = $officialChargeAmountArray[$date][$appid] + $recharge_in_three_days;
            } else {
                $officialChargeAmountArray[$date][$appid] = $recharge_in_three_days;
            }
        }

        foreach ($officialChargeAmountArray as $k => $v) {
            foreach ($v as $index => $value) {
                $date = $k;
                $appid = $index;
                $recharge_in_three_days = $value;

                $mediaPushDayInfo = MediaPushDayStats::getByAppidAndDate($appid, $date);
                if ($mediaPushDayInfo) {
                    if (empty($mediaPushDayInfo->recharge_in_three_days)) {
                        $mediaPushDayInfo->recharge_in_three_days = $recharge_in_three_days;
                    } else {
                        $mediaPushDayInfo->recharge_in_three_days = $mediaPushDayInfo->recharge_in_three_days + $recharge_in_three_days;
                    }
                    $mediaPushDayInfo->save();
                }
            }
        }
    }
}