package com.yfd.platform.component.nettyclient;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.config.HttpServerConfig;
import com.yfd.platform.constant.SystemCode;
import com.yfd.platform.modules.basedata.domain.AlgorithmModel;
import com.yfd.platform.modules.basedata.domain.Substation;
import com.yfd.platform.modules.basedata.mapper.WeatherLogMapper;
import com.yfd.platform.modules.basedata.service.IAlgorithmModelService;
import com.yfd.platform.modules.basedata.service.ISubstationService;
import com.yfd.platform.utils.HttpRESTfulUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 心跳保持器
 */
@Component
public class BootNettyHeartTimer {

    @Resource
    private WeatherLogMapper weatherLogMapper;
    @Resource
    private HttpRESTfulUtils httpRESTfulUtils;
    @Resource
    private HttpServerConfig httpServerConfig;
    @Resource
    private ISubstationService substationService;
    @Resource
    private IAlgorithmModelService algorithmModelService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void timeTask() {
        // 删除一天以前的温度数据
        weatherLogMapper.deleteWeatherLog();
    }


    /**********************************
     * 用途说明: 发送请求获取系统自检信息/资源上报
     * 参数说明
     * 返回值说明: void
     ***********************************/
    //    @Scheduled(cron = "0/30 * * * * ?")
    public void algorithmResource() throws IOException {
        // 调用分析主机获取设备资源信息
        String api1 = "algorithmResource";
        Map<String, Object> param1 = new HashMap<>();
        JSONObject jsonObject = httpRESTfulUtils.sendHttpPost("json", httpServerConfig.getAnalyseIp(),
                httpServerConfig.getAnalysePort13(),
                "", api1, param1, null);
        if (SystemCode.SUCCESS_STATUS_CODE.getCode().equals(jsonObject.getString("code"))) {
            JSONObject data = jsonObject.getJSONObject("data");
            List<Substation> list =
                    substationService.list(new LambdaQueryWrapper<Substation>().eq(Substation::getStationIp,
                            httpServerConfig.getPatrolIp()));
            List<AlgorithmModel> list1 = algorithmModelService.list();
            if (list1.size() > 0) {
                AlgorithmModel algorithmModel = list1.get(0);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("type", 0);
                jsonObject2.put("version", algorithmModel.getVersion());
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(jsonObject2);
                data.put("version_info", jsonArray.toString());
            }
            if (list.size() > 0) {
                Substation substation = list.get(0);
                data.put("province_name", substation.getProvinceName());
                data.put("city_name", substation.getCityName());
                data.put("volt_level", substation.getVoltLevel());
                data.put("section_id", substation.getSectionId());
                data.put("section name", substation.getSectionName());
                //                data.put("station_id", substation.getStationId());
                data.put("station_name", substation.getStationName());

            }
            // TODO 缺少变电站信息
            // 发送资源到上级系统
            //            this.sendTaskData("313", "", "", data.toString());

        }
    }

}
