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

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.NhConstants;
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.dto.querypowerstationmonitor.QueryPowerStationMonitorR;
import org.springblade.modules.nh.factory.saj.SajApi;
import org.springblade.modules.nh.factory.saj.req.SajPageReq;
import org.springblade.modules.nh.factory.saj.resp.BasePlant;
import org.springblade.modules.nh.factory.saj.resp.SajPageRet;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
@AllArgsConstructor
public class SajStationListPageAction extends PageAction<BasePlant, BasePlant> {

    private final SajApi sajApi;
    private final IMonitorEntryStatService entryStatService;

    @Override
    public PageInfo<BasePlant, BasePlant> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
		SajPageReq req = new SajPageReq();
        req.setPageSize(pageSize);
        req.setPageNum(page);

		SajPageRet<BasePlant> resp = sajApi.queryPlantPage(req);
        if (resp.getCode()==200) {
			PageInfo<BasePlant, BasePlant> ret = new PageInfo<>();
			List<BasePlant> data = resp.getRows();
            long pages = resp.getTotalPage();
            ret.setPages((int) pages);
            ret.setData(null);
            ret.setList(data);
            return ret;
        }
        log.error("query power station monitor data error: {}, code: {}", resp.getMsg(), resp.getRows());
        return null;
    }

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

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

    private GetStationR convR(BasePlant r) {
        GetStationR ret = new GetStationR();
		ret.setVendorIndex(1);
        ret.setDataTimestamp(new Date().getTime());
        ret.setFullHour(null);
        ret.setStationName(r.getPlantName());
        ret.setId(r.getPlantId());
        ret.setUserId(null);
        ret.setUserName(null);
        ret.setUserMobile(null);
        ret.setSno(null);
        ret.setAddr(null);
        ret.setState(0);//0表示未同步基础数据
        ret.setPower(0);
        ret.setPowerStr("kw");
        ret.setCapacity(0);
        ret.setCapacityStr("kwp");
        ret.setDayEnergy(0);
        ret.setDayEnergyStr("kwh");
        ret.setMonthEnergy(0D);
        ret.setMonthEnergyStr("kwh");
        ret.setYearEnergy(0D);
        ret.setYearEnergyStr("kwh");
        ret.setAllEnergy(0D);
        ret.setAllEnergyStr("kwh");
        ret.setNmiCode(r.getNMI());
        return ret;
    }

	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));
	}

}
