package com.ho.datacollect.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSONObject;
import com.ho.business.common.BusiTool;
import com.ho.business.feignclient.FlowFeignClient;
import com.ho.business.service.StationService;
import com.ho.business.service.TopologyService;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.service.RedisService;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.api.vo.req.add.DataSetParam;
import com.ho.datacollect.service.DataCollectService;
import com.ho.flow.vo.req.event.EventAddReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fancl
 * @desc: 事件采集处理类
 * @date 2022/8/31
 */
@Service(DataCollectConstant.KEY_MAP.EVENTS)
@Slf4j
public class EventDataListCollectServiceImpl implements DataCollectService {

    @Autowired
    FlowFeignClient flowFeignClient;

    @Autowired
    StationService stationService;

    @Autowired
    TopologyService topologyService;

    @Autowired
    BusiTool busiTool;

    @Autowired
    Snowflake snowflake;

    @Autowired
    RedisService redisService;

    //发送到告警中心匹配处理
    //异步调用方法,并指定 数据采集模块的线程池
    @Async("dataCollectThreadPoolExecutor")
    @Override
    public void doService(DataSetParam dataParam) {
        log.info("EventDataListCollectServiceImpl===============");
        try {
            if (dataParam == null ) {
                return;
            }
            List<DataSetParam> dataSetParamList = dataParam.getDataSetParamList();
            List<EventAddReq> eventAddReqList = new ArrayList<>();
            for (DataSetParam dataSetParam:dataSetParamList) {
                Integer groupId = dataSetParam.getGroupId();
                Integer stationId = dataSetParam.getStationId();
                Integer srcId = dataSetParam.getSrcId();
                String freezeTime = dataSetParam.getFreezeTime();
                String deviceType = dataSetParam.getDeviceType();
                Integer deviceTypeId =dataSetParam.getDeviceTypeId();
                //捕获异常,如果时间转换出错,就使用现在的时间
                DateTime ts = new DateTime();
                try {
                    ts = DateUtil.parse(freezeTime, CommonConstant.DATE_YMD_HMSS);
                } catch (Exception e) {
                    log.error("匹配告警freezeTime格式转换时出错,电站：{},设备类型：{},freezeTime:{}", stationId, deviceType, freezeTime);
                    return;
                }
                JSONObject dataSet = dataSetParam.getDataSet();
                //一次一个对象,但这个对象有多个属性
                EventAddReq eventAddReq = new EventAddReq();
                eventAddReq.setTs(ts);
                eventAddReq.setGroupId(groupId);
                eventAddReq.setStationId(stationId);
                eventAddReq.setSrcId(srcId);
                eventAddReq.setDeviceType(deviceType);
                eventAddReq.setDeviceTypeId(deviceTypeId);
                eventAddReq.setDeviceName(dataSetParam.getDeviceName());
                eventAddReq.setDataSet(dataSet);
                eventAddReqList.add(eventAddReq);
            }
            //发送,后续可以改为异步 或者 消息队列实现

            flowFeignClient.addEventsList(eventAddReqList);

        } catch (Exception e) {
            log.error("添加告警事件数据失败: " + e.getMessage());
        }
    }

}
