<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/26
 * Time: 下午3:29
 */

namespace App\Console\Commands;
use App\Modules\Trade\Models\OrderDayStat;
use App\Modules\Trade\Models\OrderStat;
use App\Modules\Channel\Services\BusinessChannelDayStatService;
use App\Modules\Channel\Services\BusinessChannelStatService;
use App\Modules\Channel\Services\ChannelOrdersService;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Statistic\Services\WapVisitStatService;
use App\Modules\User\Services\UserService;
use Log;
use Illuminate\Console\Command;

class BusinessChannelStatTask extends Command
{

    /**
     * 执行命令   php artisan BusinessChannelStat_task
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'BusinessChannelStat_task';

    /**
     * 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"));


        $dateNow = date("Y-m-d");
//        $dateNow = "2018-01-30";

        $yesterStartDay = date('Y-m-d', strtotime($dateNow." -1 day"))." 00:00:00";
        $yesterEndDay = date('Y-m-d', strtotime($dateNow))." 00:00:00";

        $last_monthStartDay = date('Y-m', strtotime($dateNow." -1 month"))."-01 00:00:00";
        $current_monthStartDay = date('Y-m', strtotime($dateNow))."-01  00:00:00";

        $beginLastweek=date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
        $endLastweek=date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));

        $current_week_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
        $current_week_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));

        $channels = ChannelService::getAllChannels();
        foreach ($channels as $channel) {
            $channel_data = OrderStat::getByChannelId($channel['id']);
            $channel_day_data = OrderDayStat::getYesterdaySumByChannelId($channel['id']);
            $business_stat = BusinessChannelStatService::getByChannelID($channel->id);

            if(!$channel_data || !$channel_day_data){
                Log::info($channel['id']."日数据未生成".date("y-m-d H:i:s"."\n"));
                continue;
            }
            $userRegisterNum = $channel_day_data->register_user_num;//昨日注册
            $sendOrderDataNum = $channel_day_data->send_order_num;//昨日派单创建数

            try{
                $paramsA = [
                    'distribution_channel_id' => $channel['id'],
                    'date' => $yesterStartDay,
                    'register_user_num' => $userRegisterNum,
                    'send_order_num' => $sendOrderDataNum,
                    'is_login_yesterday'=>UserService::judgeUserYesterdayLogin($channel['id'],$yesterStartDay,$yesterEndDay)
                ];
                BusinessChannelDayStatService::createBusinessChannelDayStat($paramsA);
            }catch (\Exception $exception){
                \Log::error($exception->getMessage());
            }

            $yesterday_register_user_num = $userRegisterNum;
            $total_register_user_num = $channel_data->register_user_num;
            $total_send_order_num = $channel_data->send_order_num;
            //当月新增站点充值
            $current_month_new_channel_recharge = strtotime($channel->created_at) >= strtotime(date('Y-m-01')) ? $channel_data->current_month_recharge_amount : 0;
            $current_month_channel_recharge = $channel_data->current_month_recharge_amount;//当月总充值
            $channel_total_recharge=$channel_data->total_recharge_amount;//历史总充值

            $paramsB = [
                'yesterday_register_user_num' => $yesterday_register_user_num,
                'total_register_user_num' => $total_register_user_num,
                'total_send_order_num' => $total_send_order_num,
                'current_month_new_channels_recharge'=>$current_month_new_channel_recharge,
                'current_month_channels_recharge'=>$current_month_channel_recharge,
                'service_account_num' =>BusinessChannelStatService::getServiceAccountNum($channel['id']),
                'last_week_login_days' =>0,//BusinessChannelStatService::getLoginDays($channel['id'],$beginLastweek,$endLastweek),
                'current_week_login_days'=>0,//BusinessChannelStatService::getLoginDays($channel['id'],$current_week_start,$current_week_end),
                'yesterday_create_orders'=>$sendOrderDataNum,
                'is_login_yesterday'=>$paramsA['is_login_yesterday'],
                'channel_total_recharge'=>$channel_total_recharge,

            ];
            if(date('d')=='01'){//月初第一天
                $paramsB['last_month_channels_recharge'] = $channel_data->last_month_recharge_amount;
                $paramsB['last_month_new_channel_recharge'] =$business_stat ? $business_stat->current_month_new_channels_recharge : 0;
                $paramsB['last_month_register_user_num'] = $business_stat ? $business_stat->current_month_register_user_num : 0;
                $paramsB['current_month_register_user_num'] = $userRegisterNum;
            }else{
                $paramsB['current_month_register_user_num'] = $business_stat ? $business_stat->current_month_register_user_num + $userRegisterNum : $userRegisterNum;
            }

            if(date('w')=='1'){//周一
                $paramsB['last_week_actual_send_orders'] = $business_stat ? $business_stat->current_week_actual_send_orders : 0;
            }else{
                $paramsB['current_week_actual_send_orders'] = SendOrderService::getPeriodActualSendOrdersNum($channel['id'], $current_week_start,$current_week_end);
            }
            BusinessChannelStatService::crateUpdate($channel['id'], $paramsB);
        }

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