<?php

namespace App\Http\Controllers\Admin;

use App\Facades\CommonService;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Jobs\AddErrorLog;
use App\Models\ChefUser;
use App\Models\Coach;
use App\Models\CustomerServiceUser;
use App\Models\MainCoach;
use App\Models\Order;
use App\Models\Quota;
use App\Models\QuotaLog;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class DataStatController extends Controller
{
    use ResponseJson;

    /**
     * 获取数据统计
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function getDataStat(){
        try {
            //获取订单统计
            $order_data = Order::select(
                DB::raw('count(if(pay_status = "PAID",1,null)) as total_order'),
                DB::raw('count(if(pay_status = "PAID" and status = "STARTING",1,null)) as starting_order'),
                DB::raw('count(if(pay_status = "PAID" and status = "FINISHED",1,null)) as finished_order'),
            )->first();
            $user_data = User::select(DB::raw('count(*) as total_user'))->first();
            $service_coach_data = Coach::select(DB::raw('count(if(status = 0,1,null)) as apply_num'),DB::raw('count(if(status = 1,1,null)) as pass_num'))->first();
            $service_main_coach_data = MainCoach::select(DB::raw('count(if(status = 0,1,null)) as apply_num'),DB::raw('count(if(status = 1,1,null)) as pass_num'))->first();
            $service_chef_data = ChefUser::select(DB::raw('count(if(status = 0,1,null)) as apply_num'),DB::raw('count(if(status = 1,1,null)) as pass_num'))->first();
            $service_customer_data = CustomerServiceUser::select(DB::raw('count(if(status = 0,1,null)) as apply_num'),DB::raw('count(if(status = 1,1,null)) as pass_num'))->first();

            $service_apply_data = [];
            $service_apply_data['coach_num'] = $service_coach_data['apply_num'];
            $service_apply_data['main_coach_num'] = $service_main_coach_data['apply_num'];
            $service_apply_data['chef_num'] = $service_chef_data['apply_num'];
            $service_apply_data['customer_num'] = $service_customer_data['apply_num'];

            $service_pass_data = [];
            $service_pass_data['coach_num'] = $service_coach_data['pass_num'];
            $service_pass_data['main_coach_num'] = $service_main_coach_data['pass_num'];
            $service_pass_data['chef_num'] = $service_chef_data['pass_num'];
            $service_pass_data['customer_num'] = $service_customer_data['pass_num'];

            //查询名额
            $quota = Quota::select(DB::raw('sum(num) as total_quota'),DB::raw('sum(if(agency_id is null,num,0)) as personal_quota'),DB::raw('sum(if(agency_id is not null,num,0)) as agency_quota'))->first();
            $quota_log = QuotaLog::select(DB::raw('sum(num) as total_use_quota'))->first();
            $quota_data = [];
            $quota_data['un_use_total_quota'] = $quota['total_quota'];
            $quota_data['personal_quota'] = $quota['personal_quota'];
            $quota_data['agency_quota'] = $quota['agency_quota'];
            $quota_data['total_use_quota'] = $quota_log['total_use_quota'];
            $quota_data['total_quota'] = (string)($quota['total_quota']+$quota_log['total_use_quota']);
            return $this->success('ok',compact('order_data','user_data','service_apply_data','service_pass_data','quota_data'));
        }catch (\Exception $e) {
            Log::error('getDataStat:' . $e->getMessage());
            return $this->failure('获取失败');
        }
    }

    public function getOrderStat(){
        try {
            $order_data = Order::select(
                DB::raw('count(*) as total_order'),
                DB::raw('count(if(pay_status = "PAID",1,null)) as total_pay_order'),
                DB::raw('count(if(pay_status = "PAID" and status = "STARTING",1,null)) as starting_order'),
                DB::raw('count(if(pay_status = "PAID" and status = "FINISHED",1,null)) as finished_order'),
                DB::raw('count(if(pay_status = "UNPAID",1,null)) as un_pay_order'),
            )->first();
            //获取是否绑定角色订单
            $bind_order = Order::join('service_role_orders as s','s.order_id','=','orders.id')
                ->select(
                    DB::raw('count(if(role_id = 1,1,null)) as main_coach_orders'),
                    DB::raw('count(if(role_id = 2,1,null)) as coach_orders'),
                    DB::raw('count(if(role_id = 3,1,null)) as customer_orders'))
                ->first();

            $un_complete_info = Order::leftJoin('users as u','u.id','=','orders.user_id')
                ->select(DB::raw('count(if(ufutx_u.name is null or ufutx_u.birthday is null or ufutx_u.stature is null,1,null)) as un_complete_orders'))
                ->first();

            $list = [];
            $list['total_order'] = $order_data->total_order??0;
            $list['total_pay_order'] = $order_data->total_pay_order??0;
            $list['starting_order'] = $order_data->starting_order??0;
            $list['finished_order'] = $order_data->finished_order??0;
            $list['un_pay_order'] = $order_data->un_pay_order??0;
            $list['un_bind_main_coach_orders'] = $order_data->total_pay_order-$bind_order->main_coach_orders??0;
            $list['un_bind_coach_orders'] = $order_data->total_pay_order-$bind_order->coach_orders??0;
            $list['un_bind_customer_orders'] = $order_data->total_pay_order-$bind_order->customer_orders??0;
            $list['un_complete_orders'] = $un_complete_info->un_complete_orders??0;
            return $this->success('ok',$list);
        }catch (\Exception $e){
            AddErrorLog::dispatch('getOrderStat file:'.$e->getFile().' line:'.$e->getLine().' message:'.$e->getMessage())->onQueue('health');
            return $this->failure('获取失败');
        }
    }
}