<?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;

    //===更新赔率 状态===
    const ODDS_SOLE_ERR = 10031;

    //===获取 用户token ==
    const TOKEN_ERR = 10032;

    //联赛uuid 为空
    const LG__UUID_NULL = 10033;
    //更新赛事tag
    const UPMATCHTAG_ERROR = 10034;
    //未获取到数据源lg_id
    const S_LG_ID_NULL = 10035;
    //未获取到本地lg_id
    const L_LG_ID_NULL = 10036;
    //请求数据写日志记录失败
    const SPORTS_RECORD_ERR = 10037;
    //未获取到本地赛事id
    const L_MATCH_ID_ERR = 10038;
    //删除历史直播数据
    const DEL_PAST_BROADCAST_ERR = 10039;

    //球队数据处理失败
    const SET_TEAM_ERR = 10040;

    //===删除无效赔率失败===
    const DEL_ODDS_ERR = 10041;

    //===写预植赛果失败===
    const SET_MATCH_ERR = 10042;

    //===写赛果失败===
    const SET_MATCH_R_ERR = 10043;

    //===写入危险球失败===
    const SET_WARN_ERR = 10044;

    //===滚球投注自动审核不通过处理失败===
    const HANDLE_ORDER_N_ERR = 10045;

     //===滚球投注自动审核不通过处理失败===
     const HANDLE_ORDER_Y_ERR = 10046;
     //===用户信息更新失败===
     const UP_USER_ERR =10047;

     //===用户资金更新失败===
     const UP_MONEY_ERR = 10048;
     //===订单数据更新失败===
     const UP_ORDER_ERR = 10049;
     //===赛事结束写入结算通知表失败===
     const ADD_NOTICE_ERR = 10050;
     //===管理员账号或密码错误==
     const USER_ERR = 10051;



    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 =>'赛事状态更新失败',
        self::UPMATCHTAG_ERROR =>'赛事tag更新失败',
        //===更新赔率状态===
        self::ODDS_SOLE_ERR =>'更新赔率状态失败',
        //===获取用户 token===
        self::TOKEN_ERR => '用户token异常，请重新获取',
        //===联赛uuid 为空====
        self::LG__UUID_NULL=> '联赛uuid为空',
        //===未获取到数据源lg_id===
        self::S_LG_ID_NULL =>'未获取到数据源lg_id',
        //===未获取到本地lg_id===
        self::L_LG_ID_NULL => '未获取到本地lg_id',
        //===写请求数据日志记录失败===
        self::SPORTS_RECORD_ERR=>'写请求数据日志记录失败',
        //===未获取到本地赛事id===
        self::L_MATCH_ID_ERR=>'未获取到赔率所属本地赛事id',
        //===删除历史直播数据失败===
        self::DEL_PAST_BROADCAST_ERR=>'删除历史直播数据失败',
        //===球队数据处理失败===
        self::SET_TEAM_ERR =>'球队数据处理失败',
        //===删除无效赔率失败===
        self::DEL_ODDS_ERR =>'删除无效赔率失败',

        //===写预植赛果失败===
        self::SET_MATCH_ERR =>'写预置赛果失败',

        //===写赛果失败===
        self::SET_MATCH_R_ERR =>'写赛果失败',

        //===写危险球失败===
        self::SET_WARN_ERR =>'写危险球失败',

        //===滚球投注自动审核不通过处理失败===
        self::HANDLE_ORDER_N_ERR =>'滚球投注自动审核不通过处理失败',

         //===滚球投注自动审核通过处理失败===
         self::HANDLE_ORDER_Y_ERR =>'滚球投注自动审核通过处理失败',
         //===用户信息更新失败===
         self::UP_USER_ERR =>'用户信息更新失败',

         //===用户资金更新失败===
         self::UP_MONEY_ERR =>'用户资金更新失败',

         //===订单数据更新失败===
         self::UP_ORDER_ERR =>'订单数据更新失败',

         //===赛事结束写入结算通知表失败===
         self::ADD_NOTICE_ERR=>'赛事结束写入结算通知表失败',

         //===管理员账号或密码错误===
         self::USER_ERR =>'管理员账号或密码错误',

    ];

    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);
    }

}