package com.jiayue.insu.inclientqn.service.client;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jiayue.insu.inclientqn.constant.CommonStant;
import com.jiayue.insu.inclientqn.constant.TemplateStant;
import com.jiayue.insu.inclientqn.entity.*;
import com.jiayue.insu.inclientqn.inenum.QNHLEnum;
import com.jiayue.insu.inclientqn.inenum.StatusEnum;
import com.jiayue.insu.inclientqn.model.RequestVo;
import com.jiayue.insu.inclientqn.model.ResponseVo;
import com.jiayue.insu.inclientqn.permisson.com.ComPermisson;
import com.jiayue.insu.inclientqn.service.*;
import com.jiayue.insu.inclientqn.util.RequestUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 回传数据请求
 * @author: yuanhao
 * @createDate: 2022/6/17
 * @version: 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
@RefreshScope
public class RequestDataService {

    private final BackDataGenService backDataGenService;
    private final BackDataGenBodyService backDataGenBodyService;
    private final StationService stationService;
    private final ComPermisson comPermisson;
    private final RecordService recordService;
    private final BackStatDataService backStatDataService;
    private final BackForeUsForecastDataService backForeUsForecastDataService;
    private final BackForeForecastDataService backForeForecastDataService;

    /**
     * 回传统计数据信息
     *
     * @param signTime
     */
    public void requestBackStat(long signTime) {
        // 获取全部场站
        List<Station> stationList = stationService.findAll();
        Record record;
        LocalDateTime localDateTime = LocalDateTimeUtil.of(signTime);
        // 如果场站大于2个，则为多场站情况
        if (stationList != null && stationList.size() > 1) {
            for (Station station : stationList) {
                record = recordService.findTimeAndTypeAndContentAndStateAndStationCode(localDateTime, CommonStant.RECORD_TYPE_BACK_DATA, QNHLEnum.DATA_STAT_ALL.getCode(), StatusEnum.SUCCESS.getSign(), station.getStationCode());
                if (record != null) {
                    return;
                }
                this.pullBackStatData(station, localDateTime, signTime);
            }
            // 如果只有一个场站
        } else if (stationList != null && stationList.size() == 1) {
            record = recordService.findTimeAndTypeAndContentAndState(localDateTime, CommonStant.RECORD_TYPE_BACK_DATA, QNHLEnum.DATA_STAT_ALL.getCode(), StatusEnum.SUCCESS.getSign());
            if (record != null) {
                return;
            }
            this.pullBackStatData(stationList.get(0), localDateTime, signTime);
        }
    }

//    /**
//     *  回传理论可用功率数据
//     * @param signTime
//     */
//    public void requestLlKyBackStat(long signTime) {
//        // 获取全部场站
//        List<Station> stationList = stationService.findAll();
//        Record record;
//        LocalDateTime localDateTime = LocalDateTimeUtil.of(signTime);
//        // 如果场站大于2个，则为多场站情况
//        if (stationList != null && stationList.size() > 1) {
//            for (Station station : stationList) {
//                record = recordService.findTimeAndTypeAndContentAndStateAndStationCode(localDateTime, CommonStant.RECORD_TYPE_BACK_LLkY_DATA, QNHLEnum.DATA_STAT_KYLL.getCode(), StatusEnum.SUCCESS.getSign(), station.getStationCode());
//                if (record != null) {
//                    return;
//                }
//                this.pullLlKyBackStatData(station, localDateTime, signTime);
//            }
//            // 如果只有一个场站
//        } else if (stationList != null && stationList.size() == 1) {
//            record = recordService.findTimeAndTypeAndContentAndState(localDateTime, CommonStant.RECORD_TYPE_BACK_LLkY_DATA, QNHLEnum.DATA_STAT_KYLL.getCode(), StatusEnum.SUCCESS.getSign());
//            if (record != null) {
//                return;
//            }
//            this.pullLlKyBackStatData(stationList.get(0), localDateTime, signTime);
//        }
//    }

    /**
     * 回传统计数据
     *
     * @param station
     * @param localDateTime
     * @param signTime
     */
    public void pullBackStatData(Station station, LocalDateTime localDateTime, long signTime) {

        Record record = new Record();
        record.setType(CommonStant.RECORD_TYPE_BACK_DATA);
        record.setTime(localDateTime);
        record.setCreateTime(LocalDateTime.now());
        record.setContent(QNHLEnum.DATA_STAT_ALL.getCode());
        record.setStationCode(station.getStationCode());


        StringBuilder stringBuilder = new StringBuilder();
        // 根据时间获取统计数据
        List<BackStatData> backStatDatas = backStatDataService.findBySignTime(localDateTime);
        // 如果统计数据不为空
        if (CollectionUtil.isNotEmpty(backStatDatas)) {
            // 拼接表头
            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, station.getSignCode(), DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            stringBuilder.append(header);
            // 根据统计数据拼接实际功率
            stringBuilder.append(backDataGenBodyService.genStateRealPowerData(backStatDatas));
            // 根据统计数据拼接实际气象
            stringBuilder.append(backDataGenBodyService.genStateRealWeatherData(station.getType(), backStatDatas));
            // 根据统计数据拼接理论可用
            stringBuilder.append(backDataGenBodyService.genStateTheoryPowerData(backStatDatas));
            // 根据统计数据拼接两则准确率
            stringBuilder.append(backDataGenBodyService.genStateAccuracyRate(backStatDatas));
            // 根据统计数据拼接限电
            stringBuilder.append(backDataGenBodyService.genStateLimitPowerFlag(backStatDatas));
            // 根据统计数据拼接短期上报
            stringBuilder.append(backDataGenBodyService.genStateForecastPowerReport(backStatDatas));
            // 根据统计数据拼接超短期上报
            stringBuilder.append(backDataGenBodyService.genStateUltraShortTermForecastReport(backStatDatas));
            // 组装请求参数
            RequestVo requestVo = RequestVo.UPLOAD_STAT(station.getSignCode(), stringBuilder.toString(), signTime);
            //校验请求token是否有效
            if (comPermisson.isToken(station)) {
                // 将请求参数转为jsonString类型
                JSONObject json = JSONUtil.parseObj(requestVo, true);
                String body = json.toString();
                // 发送请求
                String response = RequestUtils.post(station.getBackurl(), body, station.getComKey());
                // 返回结果校验成功
                if (RequestUtils.checkResponse(response, record)) {
                    ResponseVo responseVo = JSONUtil.toBean(response, ResponseVo.class);
                    String code = responseVo.getRetCode();
                    // 如果请求成功
                    if (code.equals(QNHLEnum.REQUEST_SUCCESS.getCode())) {
                        // 设置日志状态
                        record.setState(StatusEnum.SUCCESS.getSign());
                        record.setStateContent(responseVo.getRetMsg());
                        // 获取统计数据
                        BackStatData backStatData = backStatDatas.get(0);
                        // 设置统计时间
                        backStatData.setBackTime(LocalDateTime.now());
                        backStatData.setStationCode(station.getStationCode());
                        try {
                            // 保存或更新统计数据
                            backStatDataService.saveOrUpdate(backStatData);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        // 请求失败
                    } else {
                        // 设置日志状态
                        record.setState(code);
                        record.setStateContent(responseVo.getRetMsg());
                    }
                }
                // token无效
            } else {
                // 设置日志状态
                record.setState(QNHLEnum.PERMISSON_TOKEN_INVALID.getCode());
                record.setStateContent(QNHLEnum.PERMISSON_TOKEN_INVALID.getMsg());
            }
            // 如果统计数据为空
        } else {
            // 设置日志状态
            record.setState(StatusEnum.DATA_DEFICIENCY.getCode());
            record.setStateContent(StatusEnum.DATA_DEFICIENCY.getMsg());
        }
        // 保存日志信息
        recordService.save(record);

    }

    /**
     *  回传理论可用功率数据
     * @param station
     * @param localDateTime
     * @param signTime
     */
    public void pullLlKyBackStatData(Station station, LocalDateTime localDateTime, long signTime) {

        Record record = new Record();
        record.setType(CommonStant.RECORD_TYPE_BACK_LLkY_DATA);
        record.setTime(localDateTime);
        record.setCreateTime(LocalDateTime.now());
        record.setContent(QNHLEnum.DATA_STAT_KYLL.getCode());
        record.setStationCode(station.getStationCode());


        StringBuilder stringBuilder = new StringBuilder();
        // 根据时间获取统计数据
        List<BackStatData> backStatDatas = backStatDataService.findBySignTime(localDateTime);
        // 如果统计数据不为空
        if (CollectionUtil.isNotEmpty(backStatDatas)) {
            // 拼接表头
            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, station.getSignCode(), DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            stringBuilder.append(header);
            // 根据统计数据拼接理论可用功率
            stringBuilder.append(backDataGenBodyService.genStateTheoryPowerData(backStatDatas));
            // 组装请求参数
            RequestVo requestVo = RequestVo.UPLOAD_STAT(station.getSignCode(), stringBuilder.toString(), signTime);
            //校验请求token是否有效
            if (comPermisson.isToken(station)) {
                // 将请求参数转为jsonString类型
                JSONObject json = JSONUtil.parseObj(requestVo, true);
                String body = json.toString();
                // 发送请求
                String response = RequestUtils.post(station.getBackurl(), body, station.getComKey());
                // 返回结果校验成功
                if (RequestUtils.checkResponse(response, record)) {
                    ResponseVo responseVo = JSONUtil.toBean(response, ResponseVo.class);
                    String code = responseVo.getRetCode();
                    // 如果请求成功
                    if (code.equals(QNHLEnum.REQUEST_SUCCESS.getCode())) {
                        // 设置日志状态
                        record.setState(StatusEnum.SUCCESS.getSign());
                        record.setStateContent(responseVo.getRetMsg());
                        // 获取统计数据
                        BackStatData backStatData = backStatDatas.get(0);
                        // 设置统计时间
                        backStatData.setBackTime(LocalDateTime.now());
                        backStatData.setStationCode(station.getStationCode());
                        try {
                            // 保存或更新统计数据
                            backStatDataService.saveOrUpdate(backStatData);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        // 请求失败
                    } else {
                        // 设置日志状态
                        record.setState(code);
                        record.setStateContent(responseVo.getRetMsg());
                    }
                }
                // token无效
            } else {
                // 设置日志状态
                record.setState(QNHLEnum.PERMISSON_TOKEN_INVALID.getCode());
                record.setStateContent(QNHLEnum.PERMISSON_TOKEN_INVALID.getMsg());
            }
            // 如果统计数据为空
        } else {
            // 设置日志状态
            record.setState(StatusEnum.DATA_DEFICIENCY.getCode());
            record.setStateContent(StatusEnum.DATA_DEFICIENCY.getMsg());
        }
        // 保存日志信息
        recordService.save(record);

    }


    /**
     * 回传短期数据
     *
     * @param signTime
     */
    public void requestBackFore(long signTime) {
        List<Station> stationList = stationService.findAll();

        Record record;
        // 是否为ab机
        boolean abFlag = false;
        // 如果场站大于2个，则为多场站情况
        if (stationList != null && stationList.size() > 1) {
            abFlag = true;
            // 循环所有场站
            for (Station station : stationList) {
                // 查询该场站是否有回传短期记录，如果有则直接返回
                record = recordService.findTimeAndTypeAndContentAndStateAndStationCode(LocalDateTimeUtil.of(signTime), CommonStant.RECORD_TYPE_BACK_DATA, QNHLEnum.DATA_FORE_ALL.getCode(), StatusEnum.SUCCESS.getSign(), station.getStationCode());
                if (record != null) {
                    break;
                }
                analysisAndSave(signTime, station, abFlag);
            }
        } else if (stationList != null && stationList.size() == 1) {
            record = recordService.findTimeAndTypeAndContentAndState(LocalDateTimeUtil.of(signTime), CommonStant.RECORD_TYPE_BACK_DATA, QNHLEnum.DATA_FORE_ALL.getCode(), StatusEnum.SUCCESS.getSign());
            if (record != null) {
                return;
            }
            analysisAndSave(signTime, stationList.get(0), abFlag);
        }
    }

    /**
     * 解析并保存
     *
     * @param signTime
     * @param station
     * @param abFlag
     */
    public void analysisAndSave(long signTime, Station station, boolean abFlag) {
        // 获取场站信息
        String stationCode = station.getStationCode();
        // 获取当天凌晨时间
        LocalDateTime localDateTime = LocalDateTimeUtil.of(signTime);
        LocalDateTime beginDay = LocalDateTimeUtil.beginOfDay(localDateTime);
        // 初始化记录
        Record record = new Record(CommonStant.RECORD_TYPE_BACK_DATA, QNHLEnum.DATA_FORE_ALL.getCode(), localDateTime, LocalDateTime.now(), null, null, stationCode);
        // 需要传输的字符串
        StringBuilder stringBuilder = new StringBuilder();
        // 格式化表头
        String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, station.getSignCode(), DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
        stringBuilder.append(header);
        // 根据时间获取超短期数据
        List<BackForeUsForecastData> uList;
        // 根据时间获取短期数据
        List<BackForeForecastData> list;

        // 如果是场站分AB机的情况
        if (abFlag) {
            uList = backForeUsForecastDataService.findBySignTimeAndStationCode(localDateTime, stationCode);
            list = backForeForecastDataService.findBySignTimeAndStationCode(beginDay, stationCode);
        } else {
            uList = backForeUsForecastDataService.findBySignTime(localDateTime);
            list = backForeForecastDataService.findBySignTime(beginDay);
        }

        // 状态内容
        String stateContent = "";
        // 如果超短期数据不为空
        if (CollectionUtil.isNotEmpty(uList)) {
            // 拼接超短期数据
            stringBuilder.append(backDataGenBodyService.genForeUltraShortTermForecast(station.getType(), uList));
            // 设置回传时间
            uList.forEach(u -> u.setBackTime(LocalDateTime.now()));
            stateContent += "cdq,";
        }

        // 如果短期数据不为空
        if (CollectionUtil.isNotEmpty(list)) {
            //当数据有回传记录则不再回传
            List<BackForeForecastData> fList = list.stream().filter(b -> b.getBackTime() == null).collect(Collectors.toList());
            // 如果有回传时间不为空的数据，拼接短期数据
            if (CollectionUtil.isNotEmpty(fList)) {
                stringBuilder.append(backDataGenBodyService.genForeForecast(list));
                list.forEach(u -> u.setBackTime(LocalDateTime.now()));
                stateContent += "dq,";
            }
        }

        // 组装请求vo
        RequestVo requestVo = RequestVo.UPLOAD_FORE(station.getSignCode(), stringBuilder.toString(), signTime);
        //校验请求数据是否为null、token是否有效
        if ((CollectionUtil.isNotEmpty(uList) || CollectionUtil.isNotEmpty(list)) && comPermisson.isToken(station)) {
            // 将请求数据转为jsonString
            JSONObject json = JSONUtil.parseObj(requestVo, true);
            String body = json.toString();
            // 发送请求
            String response = RequestUtils.post(station.getBackurl(), body, station.getComKey());
            // 校验请求返回结果
            if (RequestUtils.checkResponse(response, record)) {
                // 将请求结果字符转转为bean
                ResponseVo responseVo = JSONUtil.toBean(response, ResponseVo.class);
                String code = responseVo.getRetCode();
                // 如果请求成功
                if (code.equals(QNHLEnum.REQUEST_SUCCESS.getCode())) {
                    // 设置日志请求状态
                    record.setState(StatusEnum.SUCCESS.getSign());
                    record.setStateContent(stateContent);
                    try {
                        // 更新短期超短期数据
                        backForeUsForecastDataService.saveOrUpdateBatch(uList);
                        backForeForecastDataService.saveOrUpdateBatch(list);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    // 如果请求失败
                } else {
                    // 设置日志请求状态
                    record.setState(code);
                    record.setStateContent(responseVo.getRetMsg());
                }
            }
            //校验请求数据是为null或token无效
        } else {
            // 设置日志请求状态
            record.setState(StatusEnum.DATA_DEFICIENCY.getSign());
            record.setStateContent(StatusEnum.DATA_DEFICIENCY.getMsg());
        }
        // 保存日志记录
        recordService.save(record);

    }

    /**
     * 回传统计数据
     *
     * @param qnhlEnum 类型
     * @param signTime 标记时间
     */
    public void requestBackStatData(QNHLEnum qnhlEnum, long signTime) {

        LocalDateTime localDateTime = LocalDateTimeUtil.of(signTime);

        Record record = new Record();
        record.setType(CommonStant.RECORD_TYPE_BACK_DATA);
        record.setTime(localDateTime);
        record.setCreateTime(LocalDateTime.now());
        record.setContent(qnhlEnum.getCode());

        Station station = stationService.findThis();

        List<BackStatData> backStatDatas = backStatDataService.findBySignTime(localDateTime);

        RequestVo requestVo = null;
        try {

            switch (qnhlEnum.getCode()) {
                case "RealPower":
                    requestVo = backDataGenService.genStateRealPowerData(station.getSignCode(), signTime, backStatDatas);
                    break;
                case "RealWeather":
                    requestVo = backDataGenService.genStateRealWeatherData(station.getSignCode(), station.getType(), signTime, backStatDatas);
                    break;
                case "ForecastPowerReport":
                    requestVo = backDataGenService.genStateForecastPowerReport(station.getSignCode(), signTime, backStatDatas);
                    break;
                case "UltraShortTermForecastReport":
                    requestVo = backDataGenService.genStateUltraShortTermForecastReport(station.getSignCode(), signTime, backStatDatas);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
            record.setState(StatusEnum.BACK_DATA_GEN_FAIL.getSign());
            record.setStateContent(StatusEnum.BACK_DATA_GEN_FAIL.getMsg());
        }
        //校验请求数据是否为null  和  token是否有效
        if (requestVo != null && comPermisson.isToken(station)) {
            JSONObject json = JSONUtil.parseObj(requestVo, true);
            String body = json.toString();
            System.out.println("----------------------------------------------------------------------");
            System.out.println(body);
            System.out.println("----------------------------------------------------------------------");
            String response = RequestUtils.post(station.getBackurl(), body, station.getComKey());

            if (RequestUtils.checkResponse(response, record)) {

                ResponseVo responseVo = JSONUtil.toBean(response, ResponseVo.class);
                String code = responseVo.getRetCode();

                if (code.equals(QNHLEnum.REQUEST_SUCCESS.getCode())) {

                    record.setState(StatusEnum.SUCCESS.getSign());
                    record.setStateContent(responseVo.getRetMsg());

                    BackStatData backStatData = backStatDatas.get(0);

                    switch (qnhlEnum.getCode()) {

                        case "RealPower":
                            backStatData.setBackTimeRealPower(LocalDateTime.now());
                            break;
                        case "RealWeather":
                            backStatData.setBackTimeRealWeather(LocalDateTime.now());
                            break;
                        case "ForecastPowerReport":
                            backStatData.setBackTimeForecastReport(LocalDateTime.now());
                            break;
                        case "UltraShortTermForecastReport":
                            backStatData.setBackTimeUsForecastReport(LocalDateTime.now());
                            break;
                    }

                    try {
                        backStatDataService.saveOrUpdate(backStatData);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    record.setState(code);
                    record.setStateContent(responseVo.getRetMsg());
                }
            }

        } else {
            record.setState(StatusEnum.BACK_DATA_GEN_FAIL.getSign());
            record.setStateContent("token失效或请求体为null");
        }

        recordService.save(record);
    }

    /**
     * 回传超短期数据
     *
     * @param qnhlEnum 类型
     * @param signTime 标记时间
     */
    public void requestBackForeUsData(QNHLEnum qnhlEnum, long signTime) {

        LocalDateTime localDateTime = LocalDateTimeUtil.of(signTime);

        Record record = new Record();
        record.setType(CommonStant.RECORD_TYPE_BACK_DATA);
        record.setTime(localDateTime);
        record.setCreateTime(LocalDateTime.now());
        record.setContent(qnhlEnum.getCode());

        Station station = stationService.findThis();

        List<BackForeUsForecastData> list = backForeUsForecastDataService.findBySignTime(localDateTime);

        RequestVo requestVo = null;
        try {
            requestVo = backDataGenService.genForeUltraShortTermForecast(station.getSignCode(), station.getType(), signTime, list);

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (requestVo != null && comPermisson.isToken(station)) {
            JSONObject json = JSONUtil.parseObj(requestVo, true);
            String body = json.toString();
            System.out.println("----------------------------------------------------------------------");
            System.out.println(body);
            System.out.println("----------------------------------------------------------------------");
            String response = RequestUtils.post(station.getBackurl(), body, station.getComKey());

            if (RequestUtils.checkResponse(response, record)) {
                ResponseVo responseVo = JSONUtil.toBean(response, ResponseVo.class);
                String code = responseVo.getRetCode();

                if (code.equals(QNHLEnum.REQUEST_SUCCESS.getCode())) {

                    record.setState(StatusEnum.SUCCESS.getSign());
                    record.setStateContent(responseVo.getRetMsg());

                    list.forEach(f -> f.setBackTime(LocalDateTime.now()));

                    try {
                        backForeUsForecastDataService.saveOrUpdateBatch(list);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    record.setState(code);
                    record.setStateContent(responseVo.getRetMsg());
                }
            }
        } else {
            record.setState(StatusEnum.BACK_DATA_GEN_FAIL.getSign());
            record.setStateContent("token失效或请求体为null");
        }

        recordService.save(record);
    }

    /**
     * 回传短期数据
     *
     * @param qnhlEnum 类型
     * @param signTime 标记时间
     */

    public void requestBackForeData(QNHLEnum qnhlEnum, long signTime) {

        LocalDateTime localDateTime = LocalDateTimeUtil.of(signTime);
        LocalDateTime beginDay = LocalDateTimeUtil.beginOfDay(localDateTime);
        Record record = new Record();
        record.setType(CommonStant.RECORD_TYPE_BACK_DATA);
        record.setTime(beginDay);
        record.setCreateTime(LocalDateTime.now());
        record.setContent(qnhlEnum.getCode());

        Station station = stationService.findThis();

        List<BackForeForecastData> list = backForeForecastDataService.findBySignTime(beginDay);

        RequestVo requestVo = null;
        try {
            requestVo = backDataGenService.genForeForecast(station.getSignCode(), signTime, list);

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (requestVo != null && comPermisson.isToken(station)) {
            JSONObject json = JSONUtil.parseObj(requestVo, true);
            String body = json.toString();
            System.out.println("----------------------------------------------------------------------");
            System.out.println(body);
            System.out.println("----------------------------------------------------------------------");
            String response = RequestUtils.post(station.getBackurl(), body, station.getComKey());

            if (RequestUtils.checkResponse(response, record)) {
                ResponseVo responseVo = JSONUtil.toBean(response, ResponseVo.class);
                String code = responseVo.getRetCode();

                if (code.equals(QNHLEnum.REQUEST_SUCCESS.getCode())) {

                    record.setState(StatusEnum.SUCCESS.getSign());
                    record.setStateContent(responseVo.getRetMsg());

                    list.forEach(f -> f.setBackTime(LocalDateTime.now()));

                    try {
                        backForeForecastDataService.saveOrUpdateBatch(list);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    record.setState(code);
                    record.setStateContent(responseVo.getRetMsg());
                }
            }
        } else {
            record.setState(StatusEnum.BACK_DATA_GEN_FAIL.getSign());
            record.setStateContent("token失效或请求体为null");
        }
        recordService.save(record);
    }

}
