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.modules.nh.factory.common.PageAction;
import org.springblade.modules.nh.factory.common.PageInfo;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
import org.springblade.modules.nh.factory.goodwe.GoodweApi;
import org.springblade.modules.nh.factory.goodwe.GoodweRet;
import org.springblade.modules.nh.factory.goodwe.dto.querypowerstationmonitor.QueryPowerStationMonitorData;
import org.springblade.modules.nh.factory.goodwe.dto.querypowerstationmonitor.QueryPowerStationMonitorR;
import org.springblade.modules.nh.factory.goodwe.dto.querypowerstationmonitor.QueryPowerStationMonitorReq;
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 GoodweUserStationListPageAction extends PageAction<QueryPowerStationMonitorData, QueryPowerStationMonitorR> {

    @Autowired
    GoodweApi goodweApi;
//    @Autowired
//    MonitorEntryOverviewStatServiceImpl monitorEntryOverviewStatService;
    @Autowired
	IMonitorEntryStatService monitorEntryStatService;
    @Autowired
	IMonitorEntryDailyStatService monitorEntryDailyStatService;
    @Autowired
	IMonitorEntryMonthStatService monitorEntryMonthStatService;

    @Override
    public PageInfo<QueryPowerStationMonitorData, QueryPowerStationMonitorR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        QueryPowerStationMonitorReq req = new QueryPowerStationMonitorReq();
        req.setPageSize(pageSize);
        req.setPageIndex(page);
		//账号
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        GoodweRet<QueryPowerStationMonitorData> resp = goodweApi.queryPowerStationMonitor(req);
        if (isOk(resp.getCode())) {
            PageInfo<QueryPowerStationMonitorData, QueryPowerStationMonitorR> ret = new PageInfo<>();
            QueryPowerStationMonitorData data = resp.getData();
            Long total = data.getRecord();
            long pages = total / req.getPageSize() + (total % req.getPageSize() == 0 ? 0 : 1);
            ret.setPages((int) pages);
            ret.setData(data);
            ret.setList(data.getList());
            return ret;
        }
        log.error("query power station monitor data error: {}, code: {}", resp.getMsg(), resp.getData());
        return null;
    }

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

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

    private GetStationR convR(int index,QueryPowerStationMonitorR r) {
        GetStationR ret = new GetStationR();
		ret.setVendorIndex(index);
        ret.setDataTimestamp(new Date().getTime());
        ret.setFullHour(null);
        ret.setStationName(r.getStationname());
        ret.setId(r.getPowerstationId());
        ret.setUserId(null);
        ret.setUserName(null);
        ret.setUserMobile(null);
        ret.setSno(null);
        ret.setAddr(r.getLocation());
        ret.setState(convState(r.getStatus()));
        ret.setPower(r.getPacKw());
        ret.setPowerStr("kw");
        ret.setCapacity(r.getCapacity());
        ret.setCapacityStr("kwp");
        ret.setDayEnergy(r.getEday());
        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;
    }

    /**
     * 电站状态(-1:离线 0:待机 1:正常 2:停机 -2:无设备)
     */
    private int convState(Integer status) {
        if (Objects.equals(-1, status)) {
            return 3; // 离线
        }
		if(Objects.equals(1, status)){
			return 1;
		}
        return 2; // 在线
    }

	public static void main(String[] args) {
		Integer status = 1;
		System.out.println(Objects.equals(-1, status));
		System.out.println(Objects.equals(1, status));
		System.out.println(Objects.equals(2, status));
	}

}
