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

import org.apache.commons.lang.time.DateUtils;
import org.springblade.common.constant.NhConstants;
import org.springblade.modules.nh.factory.aiswei.AisweiApi;
import org.springblade.modules.nh.factory.aiswei.AisweiPage;
import org.springblade.modules.nh.factory.aiswei.AisweiRet;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterdatapagepro.GetInverterDataPageProData;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterdatapagepro.GetInverterDataPageProR;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterdatapagepro.GetInverterDataPageProReq;
import org.springblade.modules.nh.factory.common.PageAction;
import org.springblade.modules.nh.factory.common.PageInfo;
import org.springblade.modules.nh.pojo.dto.sync.InverterHourlyEnergyDTO;
import org.springblade.modules.nh.service.IInverterDataService;
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 java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AisweiInverterDetailPageAction extends PageAction<AisweiPage<GetInverterDataPageProData>, GetInverterDataPageProR> {

    @Autowired
    AisweiApi aisweiApi;

	@Autowired
	IInverterDataService inverterDataService;

    @Autowired
	IMonitorInverterDailyStatService monitorInverterDailyStatService;


    @Override
    public PageInfo<AisweiPage<GetInverterDataPageProData>, GetInverterDataPageProR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        GetInverterDataPageProReq req = new GetInverterDataPageProReq();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        req.setApiKey((String) ctx.get("stationId"));
        req.setToken("");
        req.setIsnos(null); // TA004095N2410077
        Date now = new Date();
        Date yesterday = DateUtils.addDays(now, -1);
        req.setStartDate(df.format(DateUtils.addHours(yesterday, 1)));
        req.setEndDate(df.format(now));
        req.setPageNum(page);
        req.setPageSize(pageSize);
        AisweiRet<AisweiPage<GetInverterDataPageProData>> resp = aisweiApi.getInverterDataPagePro(req);
        if (!isAisOk(resp.getStatus())) {
            return null;
        }
        PageInfo<AisweiPage<GetInverterDataPageProData>, GetInverterDataPageProR> ret = new PageInfo<>();
        ret.setData(resp.getData());
        ret.setPages(resp.getData().getTotalPages());
        List<GetInverterDataPageProData> result = resp.getData().getResult();
        List<GetInverterDataPageProR> dataList = new ArrayList<>();
        for (GetInverterDataPageProData getInverterDataPageProData : result) {
            dataList.addAll(getInverterDataPageProData.getDataList());
        }
        ret.setList(dataList);
        return ret;
    }

    @Override
    public void handlePageInfo(PageInfo<AisweiPage<GetInverterDataPageProData>, GetInverterDataPageProR> pageInfo, Map<String, Object> ctx) {
        List<GetInverterDataPageProR> list = pageInfo.getList();
        List<InverterHourlyEnergyDTO> energyList = new ArrayList<>();
        for (GetInverterDataPageProR getInverterDataPageProR : list) {
            InverterHourlyEnergyDTO val = new InverterHourlyEnergyDTO();
            val.setSn(getInverterDataPageProR.getSn());
            val.setEnergy(Double.parseDouble(getInverterDataPageProR.getEtd()) * 0.1);
            val.setJlgl(Double.parseDouble(getInverterDataPageProR.getPac()) / 1000);
            Date syncTime = new Date();
            syncTime.setTime(Long.parseLong(getInverterDataPageProR.getTmstp()));
            val.setSyncTime(syncTime);
            energyList.add(val);
        }
        monitorInverterDailyStatService.syncHourlyData(NhConstants.SYS_TYPE_AISWEI, energyList);
    }
}
