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

import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.tool.utils.DateUtil;
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.factory.goodwe.GoodweApi;
import org.springblade.modules.nh.factory.goodwe.GoodweRet;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationwarininginfobymulticondition.GetPowerStationWariningInfoByMultiConditionData;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationwarininginfobymulticondition.GetPowerStationWariningInfoByMultiConditionR;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationwarininginfobymulticondition.GetPowerStationWariningInfoByMultiConditionReq;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GoodweAlarmPageAction extends PageAction<GetPowerStationWariningInfoByMultiConditionData, GetPowerStationWariningInfoByMultiConditionR> {

    @Autowired
    GoodweApi goodweApi;
    @Autowired
	IAlarmInfoService monitorAlarmInfoService;
    @Autowired
	IAlarmCodeService monitorFaultCodeService;

    @Override
    public PageInfo<GetPowerStationWariningInfoByMultiConditionData, GetPowerStationWariningInfoByMultiConditionR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        GetPowerStationWariningInfoByMultiConditionReq req = new GetPowerStationWariningInfoByMultiConditionReq();
        req.setPageIndex(page);
        req.setPageSize(pageSize);
        req.setStationid(getStationId(ctx));
        req.setStatus(3); // 全部
        req.setStarttime(DateUtil.format(DateUtil.plusDays(new Date(), -1), "yyyy-MM-dd HH:mm:ss"));
		req.setIndex(CommonUtil.getVendorIndex(ctx));
		GoodweRet<GetPowerStationWariningInfoByMultiConditionData> resp = goodweApi.getPowerStationWariningInfoByMultiCondition(req);
        if (isOk(resp.getCode())) {
            PageInfo<GetPowerStationWariningInfoByMultiConditionData, GetPowerStationWariningInfoByMultiConditionR> ret = new PageInfo<>();
            GetPowerStationWariningInfoByMultiConditionData data = resp.getData();
            ret.setPages(calcPage(data.getRecord(), pageSize));
            ret.setData(data);
            ret.setList(data.getList());
            return ret;
        }
        log.error("goodwe sync alarm info error: {}, code: {}", resp.getMsg(), resp.getCode());
        return null;
    }

    private String getStationId(Map<String, Object> ctx) {
        return (String) ctx.get("stationId");
    }

    @Override
    public void handlePageInfo(PageInfo<GetPowerStationWariningInfoByMultiConditionData, GetPowerStationWariningInfoByMultiConditionR> pageInfo, Map<String, Object> ctx) {
        List<GetAlarmListR> list = convList(pageInfo.getList());
        if (!CollectionUtils.isEmpty(list)) {
            try {
                monitorAlarmInfoService.syncAlarmInfo(list, NhConstants.SYS_TYPE_GOODWE);
            } catch (Exception ex) {
                log.error("goodwe sync alarm list error", ex);
            }
            try {
                monitorFaultCodeService.syncFaultCode(NhConstants.SYS_TYPE_GOODWE, list);
            } catch (Exception ex) {
                log.error("goodwe sync fault code error", ex);
            }
        }
    }

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

    private GetAlarmListR convR(GetPowerStationWariningInfoByMultiConditionR r) {
        GetAlarmListR ret = new GetAlarmListR();
        ret.setStationId(r.getStationId());
        ret.setStationName(r.getStationname());
        ret.setAlarmDeviceSn(r.getDevicesn());
        ret.setAlarmCode(r.getErrorCode());
        ret.setAlarmType(null);
        ret.setAlarmLevel("2");
        ret.setAlarmBeginTime(DateUtil.parse(r.getHappentime(), "yyyy-MM-dd HH:mm:ss").getTime());
        ret.setAlarmEndTime(null);
        ret.setAlarmMsg(r.getWarningname());
        ret.setState(String.valueOf(r.getStatus()));
        ret.setWarningInfoData(null);
        ret.setAdvice(null);
        ret.setMachine(null);
        ret.setModel(null);
        return ret;
    }
}
