<?php


namespace App\Http\logic\admin;


use App\Http\helper\NumConfig;
use App\Models\ControlRecord;
use Illuminate\Support\Facades\DB;

class ControlRecordLogic
{

    protected $TPKindIDS = [1005];
    protected $RMKindIDS = [2030, 2050];

    public function monitor($start_time, $end_time, $date, $excel)
    {


        switch ($date) {
            case 1:
                $start_time = date('Y-m-d');
                $end_time = date('Y-m-d');
                break;
            case 2:
                $start_time = date('Y-m-d', strtotime('-1 day'));
                $end_time = date('Y-m-d');
                break;
            case 3:
                $start_time = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
                $end_time = date('Y-m-d');
                break;
            case 4:
                $start_time = date('Y-m-01');
                $end_time = date('Y-m-d');
                break;
        }

        $db = DB::connection('read')->table('QPTreasureDB.dbo.GameControlPool');
        if (empty($start_time) && empty($end_time)) {

            // 总库存
            $total = DB::connection('read')->table('QPTreasureDB.dbo.GameControlPool')
                ->selectRaw('IsNull(sum(TotalPool),0) TotalPool,IsNull(sum(TotalIncome),0) TotalIncome,IsNull(sum(TotalExpend),0) TotalExpend')
                ->first();

            // TP单控池总库存
            $tp = $db->whereIn('KindID', $this->TPKindIDS)
                ->selectRaw('IsNull(sum(TotalPool),0) TotalPool,IsNull(sum(TotalIncome),0) TotalIncome,IsNull(sum(TotalExpend),0) TotalExpend')
                ->first();
            // RM单控池总库存
            $rm = $db->whereIn('KindID', $this->RMKindIDS)
                ->selectRaw('IsNull(sum(TotalPool),0) TotalPool,IsNull(sum(TotalIncome),0) TotalIncome,IsNull(sum(TotalExpend),0) TotalExpend')
                ->first();

            foreach ($tp as &$val) {
                $val = number_float($val / 100);
            }

            foreach ($rm as &$val) {
                $val = number_float($val / 100);
            }

        } else {
            $where = [];
            !empty($start_time) && $where[] = ['ControlDate', '>=', $start_time . ' 00:00:00'];
            !empty($end_time) && $where[] = ['ControlDate', '<=', $end_time . ' 23:59:59'];

            $db = DB::connection('read')->table('QPRecordDB.dbo.RecordUserScoreControl');


            // TP 单控池库存
            $tp = $db->whereIn('KindID', $this->TPKindIDS)
                ->where($where)
                ->selectRaw('IsNull(sum(EffectiveScore),0) TotalPool')
                ->first();
            $tp_totalIncome = $db->whereIn('KindID', $this->TPKindIDS)
                    ->where($where)
                    ->where('EffectiveScore', '<', 0)
                    ->selectRaw('IsNull(sum(EffectiveScore),0) TotalIncome')
                    ->first()->TotalIncome ?? 0;
            $tp_totalExpend = $db->whereIn('KindID', $this->TPKindIDS)
                    ->where($where)
                    ->where('EffectiveScore', '>', 0)
                    ->selectRaw('IsNull(sum(EffectiveScore),0) TotalIncome')
                    ->first()->TotalIncome ?? 0;

            // RM单控池库存
            $rm = $db->whereIn('KindID', $this->RMKindIDS)
                ->where($where)
                ->selectRaw('IsNull(sum(EffectiveScore),0) TotalPool')
                ->first();
            $rm_totalIncome = $db->whereIn('KindID', $this->RMKindIDS)
                    ->where($where)
                    ->where('EffectiveScore', '<', 0)
                    ->selectRaw('IsNull(sum(EffectiveScore),0) TotalIncome')
                    ->first()->TotalIncome ?? 0;
            $rm_totalExpend = $db->whereIn('KindID', $this->RMKindIDS)
                    ->where($where)
                    ->where('EffectiveScore', '>', 0)
                    ->selectRaw('IsNull(sum(EffectiveScore),0) TotalExpend')
                    ->first()->TotalExpend ?? 0;
            $rm->TotalPool = $this->return_val($rm->TotalPool);
            $rm->TotalIncome = $this->return_val($rm_totalIncome);
            $rm->TotalExpend = $this->return_val($rm_totalExpend);
            $tp->TotalPool = $this->return_val($tp->TotalPool);
            $tp->TotalIncome = $this->return_val($tp_totalIncome);
            $tp->TotalExpend = $this->return_val($tp_totalExpend);

            // 总库存
            $total = new \stdClass();
            $total->TotalPool = $rm->TotalPool + $tp->TotalPool;
            $total->TotalIncome = $rm->TotalIncome + $tp->TotalIncome;
            $total->TotalExpend = $rm->TotalExpend + $tp->TotalExpend;
        }

        if ($excel) {
            $list = [];
            $list[] = [
                'TotalPool' => $total->TotalPool,
                'TotalIncome' => $total->TotalIncome,
                'TotalExpend' => $total->TotalExpend,
                'tp_TotalPool' => $tp->TotalPool,
                'tp_TotalIncome' => $tp->TotalIncome,
                'tp_TotalExpend' => $tp->TotalExpend,
                'rm_TotalPool' => $rm->TotalPool,
                'rm_TotalIncome' => $rm->TotalIncome,
                'rm_TotalExpend' => $rm->TotalExpend,
            ];
            $title = ['单控池总库存', '单控池累计成功回收', '单控池累计成功吐分', 'TP单控池总库存', 'TP单控池累计成功回收', 'TP单控池累计成功吐分', 'RM单控池总库存', 'RM单控池累计成功回收', 'RM单控池累计成功吐分'];
            downloadExcel($list, $title, '单控池监控' . date('YmdHis'));
        }


        return compact('total', 'tp', 'rm', 'start_time', 'end_time');
    }

    public function query_list($user_id, $time)
    {
        $where = [];

        list($start_time, $end_time) = [$time['start_time'], $time['end_time']];

        !empty($start_time) && $where[] = ['cr.created_at', '>=', $start_time];
        !empty($end_time) && $where[] = ['cr.created_at', '<=', $end_time];

        !empty($user_id) && $where[] = ['GameID', '=', $user_id];

        $field = ['cr.created_at', 'contents', 'GameID', 'ai.NickName', 'account', 'before_config', 'score'];
        $list = DB::connection('read')->table('agent.dbo.control_record as cr')
            ->join('QPAccountsDB.dbo.AccountsInfo as ai', 'cr.user_id', '=', 'ai.UserID')
            ->leftJoin('agent.dbo.admin_users as au', 'cr.admin_id', '=', 'au.id')
            ->where($where)
            ->select($field)
            ->orderByDesc('cr.created_at')
            ->paginate(10);

        foreach ($list as &$value) {
            $value->score = $value->score > 0 ? number_float($value->score / NumConfig::NUM_VALUE) : 0;
        }unset($value);

        return compact('list', 'start_time', 'end_time', 'user_id');
    }

    protected function return_val($val)
    {
        return $val < 0 ? number_float(-$val / 100) : number_float($val / 100);
    }
}
