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

import org.springblade.common.constant.NhConstants;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
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.getplanlistpro.GetPlanListProR;
import org.springblade.modules.nh.factory.aiswei.dto.getplanlistpro.GetPlanListProReq;
import org.springblade.modules.nh.factory.common.PageAction;
import org.springblade.modules.nh.factory.common.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.nh.service.IMonitorEntryDailyStatService;
import org.springblade.modules.nh.service.IMonitorEntryMonthStatService;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class AisweiUserStationListPageAction extends PageAction<AisweiPage<GetPlanListProR>, GetPlanListProR> {

    @Autowired
    AisweiApi aisweiApi;
//    @Autowired
//    IMonitorEntryOverviewStatService monitorEntryOverviewStatService;
    @Autowired
    IMonitorEntryStatService monitorEntryStatService;
    @Autowired
	IMonitorEntryDailyStatService monitorEntryDailyStatService;
    @Autowired
	IMonitorEntryMonthStatService monitorEntryMonthStatService;

    @Override
    public PageInfo<AisweiPage<GetPlanListProR>, GetPlanListProR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        GetPlanListProReq req = new GetPlanListProReq();
        req.setPageSize(pageSize);
        req.setPageNum(page);
        req.setOrder(0);
        AisweiRet<AisweiPage<GetPlanListProR>> resp = aisweiApi.getPlanListPro(req);
        if (isAisOk(resp.getStatus())) {
            PageInfo<AisweiPage<GetPlanListProR>, GetPlanListProR> ret = new PageInfo<>();
            AisweiPage<GetPlanListProR> data = resp.getData();
            ret.setPages(data.getTotalPages());
            ret.setData(data);
            ret.setList(data.getResult());
            return ret;
        }
        log.error("aiswei query power station monitor data error: {}, code: {}", resp.getInfo(), resp.getStatus());
        return null;
    }

    @Override
    public void handlePageInfo(PageInfo<AisweiPage<GetPlanListProR>, GetPlanListProR> pageInfo, Map<String, Object> ctx) {
        List<GetPlanListProR> list = pageInfo.getList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<GetStationR> convList = conv(pageInfo.getList());
        try {
            monitorEntryStatService.syncEntryStat(convList, NhConstants.SYS_TYPE_AISWEI);
        } catch (Exception ex) {
            log.error("sync entry stat error", ex);
        }
        try {
            monitorEntryDailyStatService.syncEntryDailyStat(convList, NhConstants.SYS_TYPE_AISWEI);
        } catch (Exception ex) {
            log.error("sync entry daily stat error", ex);
        }
    }

    private List<GetStationR> conv(List<GetPlanListProR> list) {
        List<GetStationR> ret = new ArrayList<>();
        for (GetPlanListProR r : list) {
            ret.add(convR(r));
        }
        return ret;
    }

    private GetStationR convR(GetPlanListProR r) {
        GetStationR ret = new GetStationR();
        Date lastUpdate = DateUtil.parse(r.getLudt(), "yyyy-MM-dd HH:mm:ss");
        ret.setDataTimestamp(lastUpdate.getTime());
        ret.setFullHour(null);
        ret.setStationName(r.getName());
        ret.setId(r.getApikey());
        ret.setUserId(null);
        ret.setUserName(null);
        ret.setUserMobile(null);
        ret.setSno(null);
        ret.setAddr(r.getPosition());
        ret.setState(convState(r.getStatus()));
        ret.setPower(0); // aiswei 该接口没有提供电站功率
        ret.setPowerStr(null);
        ret.setCapacity(r.getTotalpower());
        ret.setCapacityStr("kwp");
        ret.setDayEnergy(r.getEtoday());
        ret.setDayEnergyStr("kwh");
        ret.setMonthEnergy(null);
        ret.setMonthEnergyStr(null);
        ret.setYearEnergy(null);
        ret.setYearEnergyStr(null);
        ret.setAllEnergy(r.getEtotal());
        ret.setAllEnergyStr("kwh");
        ret.setNmiCode(null);
        return ret;
    }

    /**
     * 0-离线 1-正常 2-警告 3-错误
     */
    private int convState(Integer status) {
        if (Objects.equals(0, status)) {
            return 2; // 离线
        } else if (Objects.equals(1, status)) {
            return 1;
        } else if (Objects.equals(2, status)) {
            return 3;
        } else if (Objects.equals(3, status)) {
            return 3;
        }
        return 1; // 在线
    }

}
