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.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.inverterdetaillist.GetInverterDetailR;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
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.SajDevice;
import org.springblade.modules.nh.factory.saj.resp.SajDevice;
import org.springblade.modules.nh.factory.saj.resp.SajPageRet;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
@AllArgsConstructor
public class SajDeviceListPageAction extends PageAction<SajDevice, SajDevice> {

    private final SajApi sajApi;
    private final IMonitorInverterStatService inverterStatService;

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

		SajPageRet<SajDevice> resp = sajApi.queryDevicePage(req);
        if (resp.getCode()==200) {
			PageInfo<SajDevice, SajDevice> ret = new PageInfo<>();
			List<SajDevice> 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<SajDevice, SajDevice> pageInfo, Map<String, Object> ctx) {
        List<SajDevice> list = pageInfo.getList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
		List<GetInverterDetailR> convList = conv(list);
		try {
			Set<String> updateFields = new HashSet<>();
//			updateFields.add("name");
			updateFields.add("sn");
//			updateFields.add("power");
			inverterStatService.syncInverterDetails(convList, NhConstants.SYS_TYPE_SAJ, updateFields);
		} catch (Exception ex) {
			log.error("sync inverter details error", ex);
		}
    }

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

    private GetInverterDetailR convR(SajDevice r) {
		GetInverterDetailR ret = new GetInverterDetailR();
		ret.setVendorIndex(1);
		ret.setDataTimestamp(new Date().getTime());
		ret.setStationId(r.getPlantId());
		ret.setId(r.getDeviceSn());
		ret.setName(r.getDeviceType());
		ret.setSn(r.getDeviceSn());
		ret.setPower(0D);
		ret.setPowerStr("kw");
		ret.setVendorIndex(0);
		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));
	}

}
