package org.springblade.modules.nh.factory.ginlong.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.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.userstationlist.GetStationListReq;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationListResp;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.StationStatusVo;
import org.springblade.modules.nh.service.IMonitorEntryDailyStatService;
import org.springblade.modules.nh.service.IMonitorEntryMonthStatService;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springblade.modules.nh.service.impl.MonitorEntryStatServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Slf4j
@Service
@AllArgsConstructor
public class GinlongUserStationListPageAction extends PageAction<GetStationListResp, GetStationR> {

	private final  GinlongApi ginlongApi;
//	private final  IMonitorEntryOverviewStatService monitorEntryOverviewStatService;
	private final IMonitorEntryStatService monitorEntryStatService;
	private final IMonitorEntryDailyStatService monitorEntryDailyStatService;
	private final IMonitorEntryMonthStatService monitorEntryMonthStatService;

    @Override
    public PageInfo<GetStationListResp, GetStationR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        PageInfo<GetStationListResp, GetStationR> ret = new PageInfo<>();
        GetStationListReq req = new GetStationListReq();
        req.setPageNo(page);
        req.setPageSize(100);
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        IRet<GetStationListResp> resp = ginlongApi.userStationList(req);
        if (isOk(resp.getSuccess()) && resp.getData() != null) {
            Page<GetStationR> pagedData = resp.getData().getPage();
            ret.setData(resp.getData());
            ret.setPages(pagedData.getPages());
            ret.setList(pagedData.getRecords());
            return ret;
        }
        return null;
    }

    @Override
    public void handlePageInfo(PageInfo<GetStationListResp, GetStationR> pageInfo, Map<String, Object> ctx) {
        GetStationListResp data = pageInfo.getData();
        if (data == null) {
            return;
        }
        StationStatusVo stationStatusVo = data.getStationStatusVo();
        handleEntryOverviewInfo(stationStatusVo);
		pageInfo.getList().forEach(item->{
			item.setVendorIndex(CommonUtil.getVendorIndex(ctx));
		});
        if (!CollectionUtils.isEmpty(pageInfo.getList())) {
            try {
                monitorEntryStatService.syncEntryStat(pageInfo.getList(), NhConstants.SYS_TYPE_GINLONG);
            } catch (Exception ex) {
                log.error("sync entry stat error", ex);
            }
            try {
                monitorEntryDailyStatService.syncEntryDailyStat(pageInfo.getList(), NhConstants.SYS_TYPE_GINLONG);
            } catch (Exception ex) {
                log.error("sync entry daily stat error", ex);
            }
        }
    }

    private void handleEntryOverviewInfo(StationStatusVo stationStatusVo) {
        if (stationStatusVo != null) {
            try {
//                monitorEntryOverviewStatService.updateMonitorEntryOverviewStat(stationStatusVo);
            } catch (Exception ex) {
                log.info("handle update monitor entry overview stat error", ex);
            }
        }
    }
}
