<?php


namespace App\Http\logic\admin;


use App\Http\helper\NumConfig;
use App\Services\Stock;
use Illuminate\Support\Facades\DB;
use App\Http\helper\PHPExcel;
use phpDocumentor\Reflection\DocBlock\Description;

class StockLogic
{
    public function index($date, $start_time, $end_time, $type, $roomID, $excel)
    {

        $title = '群控库存';

        $today = false;

        $where = [];
        empty($start_time) && $start_time = date('Y-m-d 00:00:00');
        !empty($roomID) && $where[] = ['serverID', $roomID];

        switch ($date) {
            case 1:
                $today = true;
                break;
            case 2:
                $start_time = date('Y-m-d 00:00:00', strtotime('-1 day'));
                $end_time = date('Y-m-d 23:59:59', strtotime('-1 day'));
                break;
            case 3:
                $start_time = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
                break;
            case 4:
                $start_time = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00')); //直接以strtotime生成
                break;
        }

        $roomList = [];
        switch ($type) {
            case 1:
                $ServerIDs = [32, 33, 39];   // 过滤试玩场
                $roomList = DB::connection('read')->table('QPPlatformDB.dbo.GameRoomInfo')->whereNotIn('ServerID', $ServerIDs)->select('ServerID', 'ServerName')->get();
        }


        empty($end_time) && $end_time = date('Y-m-d 23:59:59');


        // 判断是不是同一天
        if (!empty($start_time) && !empty($end_time)) {
            $first_time = date('Y-m-d', strtotime($start_time));
            $last_time = date('Y-m-d', strtotime($end_time));
            if ($first_time == $last_time) {
                $today = true;
            }
        }

        $service = new Stock();


        if ($excel) {

            $query = DB::connection('read')->table('agent.dbo.stock_change')
                ->where($where)
                ->where('mydate','>=',$start_time)
                ->where('mydate','<=',$end_time)
                ->selectRaw("substring(CONVERT(varchar(120), mydate, 120),0,14) mydate,sum(stock) stock")
                ->groupBy(DB::raw('substring(CONVERT(varchar(120), mydate, 120),0,14)'))
                ->orderByRaw('substring(CONVERT(varchar(120), mydate, 120),0,14) asc')
                ->pluck('stock','mydate')->toArray();


            $title = [];
            $cellData = [];
            foreach ($query as $key => &$value) {
                $value = number_float($value/ NumConfig::NUM_VALUE);
                $title[] = $key;
                $cellData[] = $value;
            }

            PHPExcel::downExcel('库存变化曲线',$title,$cellData);
            return '';
        }

        if ($today) {
            // 获取当天数据
            $where[] = ['mydate', '>=', $start_time];
            $where[] = ['mydate', '<=', $end_time];
            $result = $service->todayRes($where);

        } else {
            $result = $service->allDay($start_time, $end_time, $where);
        }


        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $res['list'] = \GuzzleHttp\json_encode($result['list']);
        $res['date'] = \GuzzleHttp\json_encode($result['date']);
        $res['title'] = $title;
        $res['start_time'] = date('Y-m-d', $start_time) . 'T' . date('H:i:s', $start_time);
        $res['end_time'] = date('Y-m-d', $end_time) . 'T' . date('H:i:s', $end_time);
        $res['stocks'] = [1 => '群控库存', 2 => '单控池库存', 3 => '新手池库存'];
        $res['dates'] = [1 => '今日', 2 => '昨日', 3 => '本周', 4 => '本月'];
        $res['choice_date'] = $date;
        $res['type'] = $type;
        $res['room'] = $roomID;
        $res['roomList'] = $roomList;


        return $res;
    }


    public function roomList($type)
    {

        $list = [];
        switch ($type) {
            case 1:
                $ServerIDs = [32, 33, 39];   // 过滤试玩场
                $list = DB::connection('read')->table('QPPlatformDB.dbo.GameRoomInfo')->whereNotIn('ServerID', $ServerIDs)->select('ServerID', 'ServerName')->get();
        }
        return $list;
    }
}
