package org.springblade.modules.nh.factory.ginlong.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.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.inverterdetaillist.GetInverterDetailListReq;
import org.springblade.modules.nh.factory.ginlong.dto.inverterdetaillist.GetInverterDetailR;
import org.springblade.modules.nh.service.IMonitorInverterDailyStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GinlongInverterDetailListPageAction extends PageAction<Page<GetInverterDetailR>, GetInverterDetailR> {

    @Autowired
    GinlongApi ginlongApi;
    @Autowired
	IMonitorInverterStatService monitorInverterStatService;
    @Autowired
	IMonitorInverterDailyStatService monitorInverterDailyStatService;

    @Override
    public PageInfo<Page<GetInverterDetailR>, GetInverterDetailR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        GetInverterDetailListReq r = new GetInverterDetailListReq();
        r.setPageNo(page);
        r.setPageSize(pageSize);
		r.setIndex(CommonUtil.getVendorIndex(ctx));
        IRet<Page<GetInverterDetailR>> resp = ginlongApi.inverterDetailList(r);
        if (isOk(resp.getSuccess())) {
            Page<GetInverterDetailR> data = resp.getData();
            if (data != null && !CollectionUtils.isEmpty(resp.getData().getRecords())) {
                PageInfo<Page<GetInverterDetailR>, GetInverterDetailR> ret = new PageInfo<>();
                ret.setData(data);
                ret.setPages(data.getPages());
                ret.setList(resp.getData().getRecords());
                return ret;
            }
        }
        return null;
    }

    @Override
    public void handlePageInfo(PageInfo<Page<GetInverterDetailR>, GetInverterDetailR> pageInfo, Map<String, Object> ctx) {
        List<GetInverterDetailR> list = pageInfo.getList();
		list.forEach(item->{
			item.setVendorIndex(CommonUtil.getVendorIndex(ctx));
		});
        if (!CollectionUtils.isEmpty(list)) {
            try {
                monitorInverterStatService.syncInverterDetails(list, NhConstants.SYS_TYPE_GINLONG, new HashSet<>());
            } catch (Exception ex) {
                log.error("sync inverter details error", ex);
            }
            try {
                monitorInverterDailyStatService.syncGinlongInverterForNowHour(list);
            } catch (Exception ex) {
                log.error("sync inverter daily error", ex);
            }
        }
    }

}
