<?php

namespace App\Jobs;

use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\Statistic\Models\SendOrderBreadevenStat;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendStatisticsList extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    private $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $data['send_time']=date("Y-m-d H:i:s");
        $this->data = $data;
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if($this->data['type'] == 'order_info'){
            $this->orderInfo($this->data['data']);
        }else{
            $this->subscribe_info($this->data['data']);
        }

    }


    private function orderInfo($order_info){

        $uid = $order_info['uid'];
        \Log::info('orderInfo_start:'.$uid);
        $sub_info = ForceSubscribeUsers::where('uid',$uid)->select('official_account_id','appid','created_at','distribution_channel_id')->first();
        if(is_null($sub_info)){
        	\Log::info('sub_info_is_null:'.$uid);
        	return ;
        }
        $official = OfficialAccount::where('appid',$sub_info->appid)->select('id','nickname')->first();

        $date = date('Y-m-d',strtotime($sub_info->created_at));
        $old = $this->getDateRes($date,$sub_info->distribution_channel_id,$official->id);
        $pay_end = $order_info['pay_end_at'];
        $sub_timestamp = strtotime($sub_info->created_at);
        if($pay_end){
            $pay_end_timestamp = strtotime($pay_end);
        }else{
            $pay_end_timestamp = time();
        }
        $del = $pay_end_timestamp-$sub_timestamp;
        if($old){

            if($del <= 86400){
                $old->recharge_amount_in_one_day = $old->recharge_amount_in_one_day+$order_info['price'];
                $old->recharge_amount_in_three_days = $old->recharge_amount_in_three_days+$order_info['price'];
                $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
                $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
                $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
            }
            if($del > 86400 && $del<= 86400*3){
                $old->recharge_amount_in_three_days = $old->recharge_amount_in_three_days+$order_info['price'];
                $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
                $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
                $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
            }

            if($del > 86400*3 && $del <= 30*86400){
                $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
                $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
                $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
            }

            if($del > 30*86400 && $del <= 60*86400){
                $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
                $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
            }

            if($del> 60*86400 && $del <= 90*86400){
                $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
            }
            $old->save();
        }else{

            $nickname = '未知';
            $official_account_id = 0;
            if($official && isset($official->nickname)){
                $nickname = $official->nickname;
            }
            if($official && isset($official->id)){
                $official_account_id = $official->id;
            }
            $data = [];
            $data['recharge_amount_in_one_day'] = 0;
            $data['recharge_amount_in_three_days'] = 0;
            $data['recharge_amount_in_one_month'] = 0;
            $data['recharge_amount_in_two_months'] = 0;
            $data['recharge_amount_in_three_months'] = 0;
            $data['distribution_channel_id'] = $sub_info->distribution_channel_id;
            $data['official_account_id'] = $official_account_id;
            $data['official_account_name'] = $nickname;
            $data['date'] = $date;
            $data['force_user_num'] = 0;
            $data['total_force_user_num']  = 0;
            $data['unsubscribe_in_one_day_user_num'] =0;
            if($del <= 86400){
                $data['recharge_amount_in_one_day'] = $order_info['price'];
                $data['recharge_amount_in_three_days'] = $order_info['price'];
                $data['recharge_amount_in_one_month'] = $order_info['price'];
                $data['recharge_amount_in_two_months'] = $order_info['price'];
                $data['recharge_amount_in_three_months'] = $order_info['price'];
            }
            if($del > 86400 && $del<= 86400*3){
                $data['recharge_amount_in_three_days'] = $order_info['price'];
                $data['recharge_amount_in_one_month'] = $order_info['price'];
                $data['recharge_amount_in_two_months'] = $order_info['price'];
                $data['recharge_amount_in_three_months'] = $order_info['price'];
            }

            if($del > 86400*3 && $del <= 30*86400){
                $data['recharge_amount_in_one_month'] = $order_info['price'];
                $data['recharge_amount_in_two_months'] = $order_info['price'];
                $data['recharge_amount_in_three_months'] = $order_info['price'];
            }

            if($del > 30*86400 && $del <= 60*86400){
                $data['recharge_amount_in_two_months'] = $order_info['price'];
                $data['recharge_amount_in_three_months'] = $order_info['price'];
            }

            if($del> 60*86400 && $del <= 90*86400){
                $data['recharge_amount_in_three_months'] = $order_info['price'];
            }

            SendOrderBreadevenStat::create($data);
        }
    }

    private function subscribe_info($subscribe_info){
        $subscribe_info = (array) $subscribe_info;
        $date = date('Y-m-d',strtotime($subscribe_info['created_at']));
        $official = OfficialAccount::where('appid',$subscribe_info['appid'])->select('id','nickname')->first();
        $old = $this->getDateRes($date,$subscribe_info['distribution_channel_id'],$official->id);

        if($old){
            if( (strtotime($subscribe_info['unsubscribe_time']) -  strtotime($subscribe_info['created_at']) ) < 86400){
                $old->unsubscribe_in_one_day_user_num +=1;
                $old->save();
            }
        }else{
            $data = [];


            $nickname = '未知';
            $official_account_id = 0;
            if($official && isset($official->nickname)){
                $nickname = $official->nickname;
            }
            if($official && isset($official->id)){
                $official_account_id = $official->id;
            }
            $data['recharge_amount_in_one_day'] = 0;
            $data['recharge_amount_in_three_days'] = 0;
            $data['recharge_amount_in_one_month'] = 0;
            $data['recharge_amount_in_two_months'] = 0;
            $data['recharge_amount_in_three_months'] = 0;
            $data['distribution_channel_id'] = $subscribe_info['distribution_channel_id'];
            $data['official_account_id'] = $official_account_id;
            $data['official_account_name'] = $nickname;
            $data['date'] = $date;
            $data['force_user_num'] = 0;
            $data['total_force_user_num']  = 0;

            if( (strtotime($subscribe_info['unsubscribe_time']) -  strtotime($subscribe_info['created_at']) ) < 86400){
                $data['unsubscribe_in_one_day_user_num'] =1;
            }

            SendOrderBreadevenStat::create($data);
        }
    }


    private function getDateRes($date,$distribution_channel_id,$official_account_id){
        return SendOrderBreadevenStat::where('date',$date)
            ->where('distribution_channel_id',$distribution_channel_id)
            ->where('official_account_id',$official_account_id)
            ->first();
    }


}
