package com.gyee.runeconomy.service.auto.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.contant.Contant;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.mapper.auto.ProEconBenchmarkingBetweenMapper;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.model.auto.ProBasicRegion;
import com.gyee.runeconomy.model.auto.ProEconBenchmarkingBetween;
import com.gyee.runeconomy.model.auto.ProEconDutySchedule;
import com.gyee.runeconomy.service.auto.IProEconBenchmarkingBetweenService;
import com.gyee.runeconomy.service.auto.IProEconDutyScheduleService;
import com.gyee.runeconomy.util.realtimesource.EdosUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-02-03
 */
@Service
public class ProEconBenchmarkingBetweenServiceImpl extends ServiceImpl<ProEconBenchmarkingBetweenMapper, ProEconBenchmarkingBetween> implements IProEconBenchmarkingBetweenService {


    @Autowired
    private IProEconDutyScheduleService proEconDutyScheduleService;

    @Resource
    private ProEconBenchmarkingBetweenMapper proEconBenchmarkingBetweenMapper;

    @Autowired
    private EdosUtil edosUtil;


    @Override
    public void saveBeanchmarkList(String beginDate, String endDate) throws Exception {
        List<String> days = getDays(beginDate, endDate);

        for (String day : days) {
            Date date = DateUtils.parseDate(day);
            Map<String, Object> params = new HashMap<>();
            params.put("theday", date);
            this.removeByMap(params);
            Map<String, Object> params1 = new HashMap<>();
            params1.put("create_date", date);
            List<ProEconDutySchedule> dutyList = proEconDutyScheduleService.listByMap(params1);

            String[] points = Contant.fiveLossPoints.split(",");
            if (null == CacheContext.rgls || CacheContext.rgls.size() <= 0) {
                return;
            }
            ProBasicRegion region = CacheContext.rgls.get(0);
            Map<String, ProBasicPowerstationPoint> wpmap = CacheContext.regionpointmap.get(region.getNemCode() + "0");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String, Map<String, Double>> remap = new HashMap<>();
            for (ProEconDutySchedule dutyschedule : dutyList) {
                String[] begins = dutyschedule.getBeginDate().split(",");
                String[] ends = dutyschedule.getEndDate().split(",");
                Map<String, Double> map = new HashMap<>();
                for (int i = 0; i < begins.length; i++) {
                    Date begin = df.parse(day + " " + begins[i] + ":00");
                    Date end = df.parse(day + " " + ends[i] + ":00");
                    for (String point : points) {
                        ProBasicPowerstationPoint proBasicPowerstationPoint = wpmap.get(point);
                        List<PointData> maxs = edosUtil.getHistStat(proBasicPowerstationPoint.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000, (long) 1, null, 0);
                        List<PointData> mins = edosUtil.getHistStat(proBasicPowerstationPoint.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000, (long) 1, null, 1);
                        double ssdl = 0;
                        if (StringUtils.isNotEmpty(maxs) && StringUtils.isNotEmpty(mins)) {
                            ssdl = (maxs.get(0).getPointValueInDouble() - mins.get(0).getPointValueInDouble()) / 10000;
                        }
                        if (!map.containsKey(point)) {
                            map.put(point, ssdl);
                        } else {
                            map.put(point, map.get(point) + ssdl);
                        }
                    }
                    ProBasicPowerstationPoint proBasicPowerstationPoint = wpmap.get("RFDL");
                    List<PointData> maxs = edosUtil.getHistStat(proBasicPowerstationPoint.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000, (long) 1, null, 0);
                    List<PointData> mins = edosUtil.getHistStat(proBasicPowerstationPoint.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000, (long) 1, null, 1);
                    double fdl = 0;
                    if (StringUtils.isNotEmpty(maxs) && StringUtils.isNotEmpty(mins)) {
                        fdl = maxs.get(0).getPointValueInDouble() - mins.get(0).getPointValueInDouble();
                    }
                    if (!map.containsKey("RFDL")) {
                        map.put("RFDL", fdl);
                    } else {
                        map.put("RFDL", map.get("RFDL") + fdl);
                    }
                    String[] name = dutyschedule.getName().split(",");
                    remap.put(name[i], map);
                }
            }

            Set<String> dutyset = remap.keySet();
            for (String duty : dutyset) {
                ProEconBenchmarkingBetween bench = new ProEconBenchmarkingBetween();
                bench.setDutyName(duty);
                bench.setTheday(date);
                Map<String, Double> pointmap = remap.get(duty);
                bench.setPerformanceLoss(pointmap.get("RXNSSDL") + pointmap.get("RDJSSDL") + pointmap.get("RQXJCLSSDL") + pointmap.get("RSDTJSSDL"));
                bench.setFaultLoss(pointmap.get("RGZSSDL") + pointmap.get("RCNSLGZSSDL"));
                bench.setMainLoss(pointmap.get("RJXSSDL") + pointmap.get("RCNSLJXSSDL"));
                bench.setRationingLoss(pointmap.get("RXDTJSSDL") + pointmap.get("RQFDL"));
                bench.setInvolvesLoss(pointmap.get("RCWSLTQSSDL") + pointmap.get("RCWSLDWSSDL"));
                bench.setGeneratity(pointmap.get("RFDL"));
                bench.setTheoreticalGeneratity(bench.getPerformanceLoss() + bench.getFaultLoss() + bench.getMainLoss() + bench.getRationingLoss() + bench.getInvolvesLoss() + bench.getGeneratity());
                this.save(bench);
            }
        }
    }

    /**
     * 值际五项损失
     */
    @Override
    public List<ProEconBenchmarkingBetween> findByBeginAndEnd(Date begin, Date end) {

        return proEconBenchmarkingBetweenMapper.findByBeginAndEnd(begin, end);
    }

    /**
     * 获取两日期间日期list
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static List<String> getDays(String beginDate, String endDate) {
        List<String> days = null;
        if (StringUtils.isNotEmpty(beginDate) && StringUtils.isNotEmpty(endDate)) {
            days = DateUtils.getDays(beginDate, endDate);
        } else {
            days = new ArrayList<>();
            days.add(DateUtils.getYesterdayStr("yyyy-MM-dd"));
        }
        return days;
    }
}
