<?php

namespace App\Http\Response;

class Response
{
    const SUCCESS = 1;
    //体育数据入库操作
    const HANDLE_ERRORS = 10010;
    const ABNORMAL = 10011;
    const UPDATE_ERROR = 10012;
    const INSERT_ERROR = 10013;
    const COUNTRY_ERROR = 10014;
    const AREA_ERROR = 10015;
    const LEAGUE_ERROR = 10016;
    const MATCH_ERROR = 10017;
    const LOCAL_LEAGUE_ERROR = 10018;
    const ADD_ODDS_ERROR = 10019;
    const ADD_ODDS_R_ERROR = 10020;
    const ADD_LG_R_ERROR = 10021;
    const ADD_MATCH_R_ERROR = 10022;
    const ADD_MATCH_R_R_ERROR = 10023;
    const LG_LASTTIME_ERROR = 10024;
    const HALF_MATCH_ERROR = 10025;
    const AUTH_ERROR = 10026;
    const LOCAL_MATCH_ERROR = 10027;
    const BROADCAST_ERROR = 10028;
    //===更新赛事状态===
    const MATCHID_NULL = 10029;
    const UPSTATUS_ERROR = 10030;


    private static $errorMsgs = [
        self::SUCCESS => '成功',
        //===体育数据写入===
        self::HANDLE_ERRORS =>'数据处理失败',
        self::ABNORMAL =>'提交数据异常',
        self::UPDATE_ERROR =>'数据更新失败',
        self::INSERT_ERROR =>'数据插入失败',
        self::COUNTRY_ERROR =>'国家不存在',
        self::AREA_ERROR =>'地区不存在',
        self::LEAGUE_ERROR =>'联赛不存在',
        self::MATCH_ERROR =>'赛事不存在',
        self::LOCAL_LEAGUE_ERROR =>'写入联赛关联记录失败',
        self::ADD_ODDS_ERROR =>'赛事赔率写入失败',
        self::ADD_ODDS_R_ERROR =>'赛事赔率记录写入失败',
        self::ADD_LG_R_ERROR =>'联赛结果写入失败',
        self::ADD_MATCH_R_ERROR =>'赛事结果写入失败',
        self::ADD_MATCH_R_R_ERROR =>'赛事结果记录写入失败',
        self::LG_LASTTIME_ERROR =>'联赛赛季结束时间不存在',
        self::HALF_MATCH_ERROR =>'所属上半场赛事不存在',
        self::AUTH_ERROR => '无权操作',
        self::LOCAL_MATCH_ERROR =>'写入赛事关联记录失败',
        self::BROADCAST_ERROR =>'写入直播数据失败',
        //===更新赛事状态===
        self::MATCHID_NULL =>'未获取到相关赛事',
        self::UPSTATUS_ERROR =>'赛事状态更新失败',
    ];

    public static function getMsg($code)
    {
        return isset(static::$errorMsgs[$code]) ? static::$errorMsgs[$code] : '';
    }

    public static function generate($front = '',$errCode, $data = [], $errMsg = '')
    {
        $rs['status'] = $errCode;
        $rs['msg'] = $errMsg;
        $rs['msg'] OR $rs['msg'] = static::getMsg($errCode) ?: static::$errorMsgs[static::FAILED];
        $rs['msg'] = $front.$rs['msg'];
        $rs['data'] = $data;
//        return response()->json($rs, 200, ['Content-Type' => 'application/json; charset=UTF-8'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        return json_encode($rs,JSON_UNESCAPED_UNICODE);
        exit();
    }

    public static function success($data = [])
    {
        return static::generate('',Response::SUCCESS, $data);
        exit();
    }

    public static function throwError($errCode, $errMsg = '',$data = [])
    {
        $errMsg OR $errMsg = static::getMsg($errCode);
        throw new BusinessException($errMsg, $errCode,$data);
    }

}