<?php
/**
 * Created by PhpStorm.
 * User: songdb
 * Date: 2017/12/26
 * Time: 下午5:26
 */

namespace App\Console\Commands\Trade;

use App\Modules\Trade\Models\OrderStat;
use App\Modules\Trade\Services\OrderStatService;
use App\Modules\Channel\Services\ChannelService;
use Log;
use DB;
use Illuminate\Console\Command;

class GenerateOrderStat extends Command
{
    /**
     * 执行命令   php artisan generate_order_stat
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generate_order_stat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '渠道订单统计数据生成';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        print_r("======渠道订单统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        Log::info("======渠道订单统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        $channels = ChannelService::getAllChannels();

        $date = date('Y-m-d',strtotime('-1 day'));
        if(count($channels))
        {
            $channels->each(function($channel) use($date){
                OrderStatService::add($channel->id, $date);
                try{
                    $this->GenerateSeasonOrderStat($channel->id, $date);
                }catch (\Exception $e){
                }
            });
        }

        Log::info("======渠道订单统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r("======渠道订单统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
    }

    public function GenerateSeasonOrderStat($distribution_channel_id,$date){
        $result = OrderStat::where('distribution_channel_id',$distribution_channel_id)->first();
        if(!$result) return ;
        //Or
        $sql_format = "SELECT `status`,count(*) as num,sum(price) as amount FROM orders WHERE distribution_channel_id = %s and  created_at >= '%s'  and  created_at <='%s' and order_type = 'QUARTER' GROUP BY `status`";
        $sql = sprintf($sql_format,$distribution_channel_id,$date,$date.' 23:59:59');
        $season_result = DB::select($sql);
        $amount = 0;
        $paid_num = 0;
        $unpaid_num = 0;
        if($season_result){
            foreach ($season_result as $item){
                if($item->status == 'PAID'){
                    $paid_num = $item->num;
                    $amount = $item->amount;
                }
                if($item->status == 'UNPAID'){
                    $unpaid_num = $item->num;
                }
            }
        }
        $result->yesterday_season_recharge_amount = $amount;
        $result->yesterday_season_recharge_paid_num = $paid_num;
        $result->yesterday_season_recharge_unpaid_num = $unpaid_num;

        if(date('d',strtotime($date)) == '01'){
            $result->last_month_season_recharge_amount = $result->current_month_season_recharge_amount;
            $result->last_month_season_recharge_paid_num = $result->current_month_season_recharge_paid_num;
            $result->last_month_season_recharge_unpaid_num = $result->current_month_season_recharge_unpaid_mum;

            $result->current_month_season_recharge_amount = $amount;
            $result->current_month_season_recharge_paid_num = $paid_num;
            $result->current_month_season_recharge_unpaid_num = $unpaid_num;
        }else{
            $result->current_month_season_recharge_amount += $amount;
            $result->current_month_season_recharge_paid_num += $paid_num;
            $result->current_month_season_recharge_unpaid_num += $unpaid_num;
        }
        $result->save();
    }
}