package org.springblade.modules.nh.factory.aiswei.scheduler;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springblade.common.constant.NhConstants;
import org.springblade.modules.nh.factory.aiswei.AisweiApi;
import org.springblade.modules.nh.factory.aiswei.AisweiPage;
import org.springblade.modules.nh.factory.aiswei.AisweiRet;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterhiserrorpagepro.GetInverterHisErrorPageProR;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterhiserrorpagepro.GetInverterHisErrorPageProReq;
import org.springblade.modules.nh.factory.common.PageAction;
import org.springblade.modules.nh.factory.common.PageInfo;
import org.springblade.modules.nh.factory.ginlong.dto.alarmlist.GetAlarmListR;
import org.springblade.modules.nh.service.IAlarmCodeService;
import org.springblade.modules.nh.service.IAlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class AisweiAlarmHistoryPageAction extends PageAction<AisweiPage<GetInverterHisErrorPageProR>, GetInverterHisErrorPageProR> {

    @Autowired
    AisweiApi aisweiApi;
    @Autowired
	IAlarmInfoService monitorAlarmInfoService;
    @Autowired
	IAlarmCodeService monitorFaultCodeService;

    @Override
    public PageInfo<AisweiPage<GetInverterHisErrorPageProR>, GetInverterHisErrorPageProR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        GetInverterHisErrorPageProReq req = new GetInverterHisErrorPageProReq();
        req.setToken("");
        req.setIsno(null);
        req.setApikey((String) ctx.get("stationId"));
        Date end = new Date();
        Date yesterday = DateUtils.addDays(end, -1);
        Date start = DateUtils.addMinutes(yesterday, 1);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        req.setStartDate(df.format(start));
        req.setEndDate(df.format(end));
        req.setPageNum(page);
        req.setPageSize(pageSize);
        AisweiRet<AisweiPage<GetInverterHisErrorPageProR>> resp = aisweiApi.getInverterHisErrorPagePro(req);
        if (!Objects.equals(resp.getStatus(), 200)) {
            log.error("aiswei 获取历史报警信息失败，stationId:{},info:{}", ctx.get("stationId"), resp.getInfo());
            return null;
        }
        PageInfo<AisweiPage<GetInverterHisErrorPageProR>, GetInverterHisErrorPageProR> ret = new PageInfo<>();
        ret.setPages(resp.getData().getTotalPages());
        ret.setData(resp.getData());
        AisweiPage<GetInverterHisErrorPageProR> respData = resp.getData();
        ret.setList(respData.getResult());
        return ret;
    }

    @Override
    public void handlePageInfo(PageInfo<AisweiPage<GetInverterHisErrorPageProR>, GetInverterHisErrorPageProR> pageInfo, Map<String, Object> ctx) {
        List<GetInverterHisErrorPageProR> list = pageInfo.getList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<GetAlarmListR> alarmList = convList(list, (String) ctx.get("stationId"));
        try {
            monitorAlarmInfoService.syncAlarmInfo(alarmList, NhConstants.SYS_TYPE_AISWEI);
        } catch (Exception ex) {
            log.error("aiswei sync alarm list error", ex);
        }
        try {
            monitorFaultCodeService.syncFaultCode(NhConstants.SYS_TYPE_AISWEI, alarmList);
        } catch (Exception ex) {
            log.error("aiswei sync fault code error", ex);
        }
    }

    private List<GetAlarmListR> convList(List<GetInverterHisErrorPageProR> list, String stationId) {
        List<GetAlarmListR> ret = new ArrayList<>();
        for (GetInverterHisErrorPageProR r : list) {
            ret.add(convR(r, stationId));
        }
        return ret;
    }

    private GetAlarmListR convR(GetInverterHisErrorPageProR r, String stationId) {
        GetAlarmListR ret = new GetAlarmListR();
        ret.setStationId(stationId);
        ret.setStationName(null);
        ret.setAlarmDeviceSn(r.getIsno());
        ret.setAlarmCode(String.valueOf(r.getErrorCode()));
        ret.setAlarmType(null); // 用不到
        ret.setAlarmLevel("2"); // aiswei全部当成报警好了
        ret.setAlarmBeginTime(r.getErrorDate());
        ret.setAlarmEndTime(null);
        ret.setAlarmMsg(r.getContext());
        ret.setState(null);
        ret.setWarningInfoData(null);
        ret.setAdvice(null);
        ret.setMachine(null);
        ret.setModel(null);
        return ret;
    }
}
