package com.gyee.runeconomy.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gyee.common.util.DateUtils;
import com.gyee.common.util.DoubleUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.common.vo.curve.CurveVo;
import com.gyee.runeconomy.dto.ZllglDTO;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.ProEconWtCurveFitting;
import com.gyee.runeconomy.model.auto.ProEconWtCurveFittingMonth;
import com.gyee.runeconomy.model.auto.ProEconWtCurveFittingYear;
import com.gyee.runeconomy.service.auto.IProEconWtCurveFittingMonthService;
import com.gyee.runeconomy.service.auto.IProEconWtCurveFittingService;
import com.gyee.runeconomy.service.auto.IProEconWtCurveFittingYearService;
import com.gyee.runeconomy.util.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PerformanceCurvefittingService {


    @Resource
    private RedisService redisService;

    @Autowired
    private IProEconWtCurveFittingService proEconWtCurveFittingService;


    @Autowired
    private IProEconWtCurveFittingMonthService proEconWtCurveFittingMonthService;

    @Autowired
    private IProEconWtCurveFittingYearService proEconWtCurveFittingYearService;


    public Map<String, List<CurveVo>> curveFittingList(String windturbineIds, String type, String dateType) {
        Map<String, List<CurveVo>> resultMap = new HashMap<>();
        Map<String, ProBasicEquipment> wtmap = CacheContext.wtmap;
        if (type.equals("-1")) {
            String zbzgl = redisService.get("ZLLGL");
            Map<String, Map<Double, ZllglDTO>> zbzglMap = JSONObject.parseObject(zbzgl, new TypeReference<Map<String, Map<Double, ZllglDTO>>>() {
            });
            Map<String, Map<Double, Double>> znhglMap = getWtCurveFitting(windturbineIds, dateType);

            List<CurveVo> tempList = new ArrayList<>();
            if (znhglMap.containsKey(windturbineIds)) {
                Map<Double, Double> zsglmap = znhglMap.get(windturbineIds);
                Map<Double, ZllglDTO> bzglmap = zbzglMap.get(wtmap.get(windturbineIds).getModelId());
                Set<Double> speeds = zsglmap.keySet();
                speeds.stream().forEach(speed -> {
                    CurveVo vo = new CurveVo();
                    vo.setWtid(windturbineIds);
                    vo.setNemCode(wtmap.get(windturbineIds).getAname());
                    vo.setSpeed(speed);
                    vo.setTheorypower(DoubleUtils.keepPrecision(zsglmap.get(speed), 2));
                    vo.setEnsurepower(DoubleUtils.keepPrecision(bzglmap.containsKey(speed) ? bzglmap.get(speed).getEnsurePower() : zsglmap.get(speed), 2));
                    tempList.add(vo);
                });
                SortUtils.sort(tempList, "speed", SortUtils.ASC);

                resultMap.put("list", tempList);
                resultMap.put("chanrt", tempList.stream().filter(i -> (i.getSpeed() % Double.valueOf(0.5)) == 0).collect(Collectors.toList()));
            }
        }

        return resultMap;
    }

    public Map<String, List<CurveVo>> curvefitting(String windturbineIds, String type, String dateType) {
        Map<String, List<CurveVo>> resultMap = new HashMap<>();
        Map<String, ProBasicEquipment> wtmap = CacheContext.wtmap;
        if (type.equals("-1")) {
            String zbzgl = redisService.get("ZLLGL");
            Map<String, Map<Double, ZllglDTO>> zbzglMap = JSONObject.parseObject(zbzgl, new TypeReference<Map<String, Map<Double, ZllglDTO>>>() {
            });
            Map<String, Map<Double, Double>> znhglMap = getWtCurveFitting(windturbineIds, dateType);

            List<CurveVo> tempList = new ArrayList<>();
            if (znhglMap.containsKey(windturbineIds)) {
                Map<Double, Double> zsglmap = znhglMap.get(windturbineIds);
                Map<Double, ZllglDTO> bzglmap = zbzglMap.get(wtmap.get(windturbineIds).getModelId());
                Set<Double> speeds = zsglmap.keySet();
                speeds.stream().forEach(speed -> {
                    CurveVo vo = new CurveVo();
                    vo.setWtid(windturbineIds);
                    vo.setNemCode(wtmap.get(windturbineIds).getAname());
                    vo.setSpeed(speed);
                    vo.setTheorypower(DoubleUtils.keepPrecision(zsglmap.get(speed), 2));
                    vo.setEnsurepower(DoubleUtils.keepPrecision(bzglmap.containsKey(speed) ? bzglmap.get(speed).getEnsurePower() : zsglmap.get(speed), 2));
                    tempList.add(vo);
                });
                SortUtils.sort(tempList, "speed", SortUtils.ASC);
                resultMap.put("chanrt", tempList.stream().filter(i -> (i.getSpeed() % Double.valueOf(0.5)) == 0).collect(Collectors.toList()));
            }
        }

        return resultMap;
    }

    /**
     * 获取风机拟合曲线
     *
     * @return
     */
    private Map<String, Map<Double, Double>> getWtCurveFitting(String windturbineIds, String dateType) {

        String[] wts = windturbineIds.split(",");
        Map<String, Map<Double, Double>> zzsglMap = new HashMap<>();
        String strdate = DateUtils.toDate1(new Date());
        Date date = DateUtils.parseDate(strdate);
        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        Arrays.stream(wts).forEach(wt -> {
            Map<Double, Double> doubleMap = new HashMap<>();
            switch (dateType) {

                case "1":
                    String dayCurve = redisService.get("dayCurve_" + wt);
                    List<ProEconWtCurveFitting> daylist = JSONObject.parseObject(dayCurve, new TypeReference<List<ProEconWtCurveFitting>>() {
                    });
                    //                    List<ProEconWtCurveFitting> daylist = proEconWtCurveFittingService.getProEconWtCurveFittingList(wt,date);

                    if (null != daylist) {
                        daylist.stream().forEach(w -> {
                            doubleMap.put(w.getSpeed(), w.getActualPower());
                        });
                    }
                    zzsglMap.put(wt, doubleMap);
                    break;
                case "2":

                    String monthCurve = redisService.get("monthCurve_" + wt);
                    List<ProEconWtCurveFittingMonth> monthlist = JSONObject.parseObject(monthCurve, new TypeReference<List<ProEconWtCurveFittingMonth>>() {
                    });
                    //                    List<ProEconWtCurveFittingMonth> monthlist = proEconWtCurveFittingMonthService.getProEconWtCurveFittingMonthList(wt,String.valueOf(month),String.valueOf(year));

                    if (null != monthlist) {
                        monthlist.stream().forEach(w -> {
                            doubleMap.put(w.getSpeed(), w.getActualPower());
                        });
                    }
                    zzsglMap.put(wt, doubleMap);
                    break;
                case "3":
                    String yearCurve = redisService.get("yearCurve_" + wt);
                    List<ProEconWtCurveFittingYear> yearslist = JSONObject.parseObject(yearCurve, new TypeReference<List<ProEconWtCurveFittingYear>>() {
                    });
                    //                    List<ProEconWtCurveFittingYear>  yearslist = proEconWtCurveFittingYearService.getProEconWtCurveFittingYearList(wt,String.valueOf(year));

                    if (null != yearslist) {
                        yearslist.stream().forEach(w -> {
                            doubleMap.put(w.getSpeed(), w.getActualPower());
                        });
                    }
                    zzsglMap.put(wt, doubleMap);
                    break;
            }
        });

        return zzsglMap;
    }


}
