<?php

namespace App\Console\Commands;

use App\Modules\Activity\Models\ActivityStatistic;
use App\Modules\Channel\Models\Channel;
use App\Modules\Finance\Models\CommissionRate;
use App\Modules\Trade\Services\OrderService;
use DB;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '活动数据生成';


    public function handle()
    {
        $activities = \DB::table('activity')
            ->whereIn('distribution_channel_id',[0])
            ->where('end_time','<=',date('Y-m-d H:i:s'))
            ->where('stats_created',0)
            ->get();
        foreach ($activities as $key=>$activity) {
            $start_time = $activity->start_time;
            $end_time = $activity->end_time;
            $channels = Channel::getAllChannels();
            foreach ($channels as $channel){
                $activity_stats = \DB::table('activity_statistic')
                        ->where('activity_id',$activity->id)
                        ->where('distribution_channel_id',$channel->id)
                        ->where('daily_stats_created',1)
                        ->first();
                if(!$activity_stats) {
                    $stats = \DB::table('orders')
                        ->where('activity_id',$activity->id)
                        ->where('distribution_channel_id',$channel->id)
                        ->whereBetween('created_at',[$start_time,$end_time])
                        ->where('status','PAID')
                        ->select(\DB::raw("sum(price) as recharge_amount,count(*) as success_order_num"))
                        ->first();

                    $orders_count = \DB::table('orders')
                        ->where('distribution_channel_id',$channel->id)
                        ->where('activity_id',$activity->id)
                        ->whereBetween('created_at',[$start_time,$end_time])
                        //->where('status','PAID')
                        ->count('id');

                    $insert = ['activity_id'=>$activity->id,'distribution_channel_id'=>$channel->id];
                    $update = [
                        'recharge_amount'=>$stats->recharge_amount,
                        'success_order_num'=>$stats->success_order_num,
                        'order_num'=>$orders_count,
                        'promotion_stats_created'=>1,
                        'daily_stats_created'=>1
                    ];
                    if($stats->recharge_amount>0){
                        ActivityStatistic::updateOrCreate($insert,$update);
                    }
                }

            }
            \DB::table('activity')->where('id',$activity->id)->update(['stats_created'=>1]);
        }
    }

    static public function getUserClient()
    {
        return new Client(['base_uri' => env('AUTH_API_BASE_URI')]);
    }

}
