<?php


namespace App\Http\logic\admin;


use App\Http\helper\NumConfig;
use App\Http\logic\api\BaseApiLogic;
use Illuminate\Support\Facades\DB;

class RoomServiceLogic extends BaseApiLogic
{
    public function service_data($start_time, $end_time, $excel)
    {

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

        if ($excel) {

            $field = ['ServerName', 'LastWriteDate', 'CntrlSysWinCnt', 'CntrlSysWinFailCnt', 'CntrlSysLostCnt', 'CntrlSysLostFailCnt', 'CntrlSysLostPlyrWrngCnt', 'RsltSysWinCnt', 'RsltSysWinAverageScore', 'RsltSysLostCnt', 'RsltSysLostAverageScore'];
            $list = DB::connection('read')->table('QPPlatformDB.dbo.GameRoomInfo as gi')
                ->leftJoin('QPRecordDB.dbo.RecordStatistics as rs', 'gi.ServerID', 'rs.ServerID')
                ->select($field)
                ->where($where)
                ->orderByDesc('LastWriteDate')
                ->orderByDesc('ServerName')
                ->limit(1000)
                ->get();
            foreach ($list as $key=>&$val) {

                $val->RsltSysLostAverageScore = number_float($val->RsltSysLostAverageScore / NumConfig::NUM_VALUE);
                $val->RsltSysWinAverageScore = number_float($val->RsltSysWinAverageScore / NumConfig::NUM_VALUE);
                $val->winsuccess = $val->CntrlSysWinCnt - $val->CntrlSysWinFailCnt;
                $val->failsuccess = $val->CntrlSysLostCnt - $val->CntrlSysLostFailCnt - $val->CntrlSysLostPlyrWrngCnt;
                $val->winResult = $val->CntrlSysWinCnt.'/'.$val->winsuccess .'/'. $val->CntrlSysWinFailCnt;
                $val->failResult = $val->CntrlSysLostCnt .'/'. $val->failsuccess .'/'. $val->CntrlSysLostFailCnt. '/'. $val->CntrlSysLostPlyrWrngCnt;
                unset($list[$key]->CntrlSysWinCnt);
                unset($list[$key]->CntrlSysWinFailCnt);
                unset($list[$key]->CntrlSysLostCnt);
                unset($list[$key]->CntrlSysLostFailCnt);
                unset($list[$key]->CntrlSysLostPlyrWrngCnt);
                unset($list[$key]->winsuccess);
                unset($list[$key]->failsuccess);
            }


            $title = ['游戏房间', '游戏时间', '系统赢玩家的总局数', '系统赢玩家平均值', '系统输玩家总局数', '系统输玩家平均值', '群控赢触发次数/实际成功局数/失效局数', '群控输触发次数/实际成功局数/失效局数/对象失效局数'];
            $list = json_decode(json_encode($list), true);
            downloadExcel($list, $title, '房间服务端数据统计' . date('YmdHis'));

        } else {
            $list = DB::connection('read')->table('QPPlatformDB.dbo.GameRoomInfo as gi')
                ->leftJoin('QPRecordDB.dbo.RecordStatistics as rs', 'gi.ServerID', 'rs.ServerID')
                ->select('rs.*', 'ServerName')
                ->where($where)
                ->orderByDesc('LastWriteDate')
                ->orderByDesc('ServerName')
                ->paginate();
            foreach ($list as &$val) {
                $val->RsltSysLostAverageScore = number_float($val->RsltSysLostAverageScore / NumConfig::NUM_VALUE);
                $val->RsltSysWinAverageScore = number_float($val->RsltSysWinAverageScore / NumConfig::NUM_VALUE);
                $val->winsuccess = $val->CntrlSysWinCnt - $val->CntrlSysWinFailCnt;
                $val->failsuccess = $val->CntrlSysLostCnt - $val->CntrlSysLostFailCnt - $val->CntrlSysLostPlyrWrngCnt;
            }
            return compact('list', 'start_time', 'end_time');
        }



    }

    public function show($StatisticsID)
    {
        $RsltSysAverageRound = DB::table('QPRecordDB.dbo.RecordStatistics')->where('StatisticsID', $StatisticsID)->value('RsltSysAverageRound');
        $RsltSysAverageRound = number_float($RsltSysAverageRound);

        $list = DB::connection('read')->table('QPRecordDB.dbo.RecordGameRound')
            ->where('StatisticsID', $StatisticsID)
            ->paginate(10);

        return compact('RsltSysAverageRound', 'list');

    }
}
