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

namespace App\Console\Commands;

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) {
            $userRegisterNum = UserService::getChannelDayTotal($channel['id'], $yesterStartDay);
            $sendOrderDataNum = SendOrderService::getDuraingPromotionCountByChannelId($channel['id'], $yesterStartDay, $yesterEndDay);
            $data = WapVisitStatService::getReaderUvAndPv($channel, date('Y-m-d',strtotime('-1 day')));
            $whole_pv=$whole_uv=0;
            if ($data) {
                $whole_uv = $data['uv'];
                $whole_pv = $data['pv'];
            }
            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),
                    'whole_site_uv'=>$whole_uv,
                    'whole_site_pv'=>$whole_pv,
                    'auto_force_sub_num'=>BusinessChannelDayStatService::getAutoForceSubUserNum($channel['id'],$yesterStartDay,$yesterEndDay),
                    'self_force_sub_num'=>BusinessChannelDayStatService::getSelfForceSubUserNum($channel['id'],$yesterStartDay,$yesterEndDay),
                    'subscribe_user_num'=>BusinessChannelDayStatService::getChannelSubscribeNum($channel['id'],$yesterStartDay,$yesterEndDay),
                    'subscribe_amount'  =>BusinessChannelDayStatService::getChannelSubscribeRecharge($channel['id'],$yesterStartDay,$yesterEndDay)
                ];
                BusinessChannelDayStatService::createBusinessChannelDayStat($paramsA);
            }catch (\Exception $exception){
                \Log::error($exception->getMessage());
            }

            $yesterday_register_user_num = $userRegisterNum;
            $current_month_register_user_num = UserService::getChannelDayToDayTotal([$channel['id']], $current_monthStartDay, $yesterEndDay);
            $last_month_register_user_num = UserService::getChannelDayToDayTotal([$channel['id']], $last_monthStartDay, $current_monthStartDay);
            $total_register_user_num = UserService::getChannelDayToDayTotal([$channel['id']], '', $yesterEndDay);
            $total_send_order_num = SendOrderService::getPromotionCountByChannelId($channel['id']);
            $current_month_new_channel_recharge = ChannelOrdersService::getCurrentMonthNewChannelsOrdersSum($channel['id']);
            $current_month_channel_recharge = ChannelOrdersService::getChannelsOrdersSum($channel['id'],$current_monthStartDay);

            $paramsB = [
                'yesterday_register_user_num' => $yesterday_register_user_num,
                'current_month_register_user_num' => $current_month_register_user_num,
                'last_month_register_user_num' => $last_month_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' =>BusinessChannelStatService::getLoginDays($channel['id'],$beginLastweek,$endLastweek),
                'current_week_login_days'=>BusinessChannelStatService::getLoginDays($channel['id'],$current_week_start,$current_week_end),
                'last_week_actual_send_orders'=>SendOrderService::getPeriodActualSendOrdersNum($channel['id'], $beginLastweek,$endLastweek),
                'current_week_actual_send_orders'=>SendOrderService::getPeriodActualSendOrdersNum($channel['id'], $current_week_start,$current_week_end),
                'yesterday_create_orders'=>SendOrderService::getDuraingPromotionCountByChannelId($channel['id'],$yesterStartDay,$yesterEndDay),
                'is_login_yesterday'=>$paramsA['is_login_yesterday'],
                'last_month_channels_recharge'=>BusinessChannelStatService::getLastMonthChannelRecharge($channel['id']),
                'last_month_new_channel_recharge'=>BusinessChannelStatService::getLastMonthNewChannelRecharge($channel['id']),

            ];
            //\Log::info('paramB'.json_encode($paramsB));
            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"));
    }
}