package com.gyee.runeconomy.service.Irradiationinput;

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.model.auto.ProEconIrradiationInput;
import com.gyee.runeconomy.service.auto.IProEconIrradiationInputService;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IrradiationinputService {

    @Resource
    private IProEconIrradiationInputService inputService;

    @Resource
    private IEdosUtil edosUtil;

    public void getcockpit() throws Exception {

        List<ProEconIrradiationInput> qwtList = inputService.list();
        Date currentDate = DateUtils.getCurrentDate();
        List<String> wpPoints = new ArrayList<>();
        List<PointData> thls = new ArrayList<>();
        List<PointData> realData = new ArrayList<>();

        if (!qwtList.isEmpty() || qwtList.size() >= 1) {
            for (ProEconIrradiationInput lr : qwtList) {
                Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
                Map<String, ProBasicPowerstationPoint> pointMap = wppointmap.get(lr.getWindpowerstationId());
                if (pointMap.size() >= 1 || !pointMap.isEmpty()) {
                    wpPoints.add(pointMap.get(ContantXk.GZYC).getNemCode());
                    realData.addAll(edosUtil.getHistMatrix(wpPoints, currentDate.getTime() / 1000));
                    for (PointData data : realData) {
                        data.setEdnaId(lr.getNemCode());
                        //                        thls.add(data);
                        edosUtil.sendSinglePoint(data);
                    }
                    realData.clear(); // 清空realData
                    wpPoints.clear(); // 清空wpPoints,为下一次循环准备
                }
            }
            //            edosUtil.sendMultiPoint(thls);
        }
    }


    //数据补充
    public void getcockpitsjbc() throws Exception {

        List<ProEconIrradiationInput> qwtList = inputService.list();
        Date currentDate = DateUtils.getCurrentDate();
        List<String> wpPoints = new ArrayList<>();
        List<PointData> thls = new ArrayList<>();
        List<PointData> realData = new ArrayList<>();
        String Data = "2023-11-16";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(Data);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        Date startTime = calendar.getTime();

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(Calendar.HOUR_OF_DAY, 23);
        calendar1.set(Calendar.MINUTE, 59);
        calendar1.set(Calendar.SECOND, 59);

        Date endTime = calendar1.getTime();

        if (!qwtList.isEmpty() || qwtList.size() >= 1) {
            for (ProEconIrradiationInput lr : qwtList) {
                if (lr.getWindpowerstationId().equals("SXJ_KGDL_YG_GDC_STA")) {
                    Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
                    Map<String, ProBasicPowerstationPoint> pointMap = wppointmap.get(lr.getWindpowerstationId());
                    if (pointMap.size() >= 1 || !pointMap.isEmpty()) {
                        wpPoints.add(pointMap.get(ContantXk.GZYC).getNemCode());
                        realData.addAll(edosUtil.getHistMatrix(wpPoints, currentDate.getTime() / 1000));

                        List<PointData> ycglls = edosUtil.getHistoryDatasRaw(pointMap.get(ContantXk.GZYC), startTime.getTime() / 1000, endTime.getTime() / 1000);

                        for (PointData data : ycglls) {
                            data.setEdnaId(lr.getNemCode());
                            thls.add(data);
                            //                        edosUtil.sendSinglePoint(data);
                        }
                        realData.clear(); // 清空realData
                        wpPoints.clear(); // 清空wpPoints,为下一次循环准备
                    }
                }
            }
            edosUtil.updatePoint(thls);
        }
    }
}
