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

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.GinlongApi;
import org.springblade.modules.nh.factory.ginlong.IRet;
import org.springblade.modules.nh.factory.ginlong.Page;
import org.springblade.modules.nh.factory.ginlong.dto.alarmlist.GetAlarmListR;
import org.springblade.modules.nh.factory.ginlong.dto.alarmlist.GetAlarmListReq;
import lombok.extern.slf4j.Slf4j;
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.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GinlongAlarmListPageAction extends PageAction<Page<GetAlarmListR>, GetAlarmListR> {

    @Autowired
    GinlongApi ginlongApi;
    @Autowired
	IAlarmInfoService monitorAlarmInfoService;
    @Autowired
	IAlarmCodeService monitorFaultCodeService;

    @Override
    public PageInfo<Page<GetAlarmListR>, GetAlarmListR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        Date sevenDaysAgo = DateUtil.plusDays(new Date(), -7);
        GetAlarmListReq req = new GetAlarmListReq();
        req.setPageNo(page);
        req.setPageSize(pageSize);
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        req.setAlarmBeginTime(DateUtil.format(sevenDaysAgo, "yyyy-MM-dd"));
        IRet<Page<GetAlarmListR>> resp = ginlongApi.alarmList(req);
        if (!isOk(resp.getSuccess()) || resp.getData() == null) {
            return null;
        }
        List<GetAlarmListR> records = resp.getData().getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return null;
        }
        PageInfo<Page<GetAlarmListR>, GetAlarmListR> ret = new PageInfo<>();
        ret.setPages(resp.getData().getPages());
        ret.setData(resp.getData());
        ret.setList(resp.getData().getRecords());
        return ret;
    }

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

}
