package com.gyee.generation.service;

import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicModelPower;
import com.gyee.generation.model.auto.ProBasicModelPowerRd;
import com.gyee.generation.model.vo.PointVo;
import com.gyee.generation.service.auto.IProBasicModelPowerRdService;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.math.LineUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Service
public class PowerCurveFittingModelService {

    //    private static final Logger logger = LoggerFactory.getLogger(PowerCurveFittingModelService.class);

    @Value("${curvefitting.dimension}")
    private Integer dimension;
    @Value("${curvefitting.scale}")
    private Double scale;


    @Resource
    private IProBasicModelPowerRdService proBasicModelPowerRdService;


    public void cureFittingModel() {


        Map<String, List<ProBasicModelPower>> modelpmap = new TreeMap<>();

        for (ProBasicModelPower mp : CacheContext.mpls) {
            if (modelpmap.containsKey(mp.getModelId())) {
                List<ProBasicModelPower> ls = modelpmap.get(mp.getModelId());
                ls.add(mp);

            } else {
                List<ProBasicModelPower> ls = new ArrayList<>();
                ls.add(mp);
                modelpmap.put(mp.getModelId(), ls);
            }
        }


        for (String key : modelpmap.keySet()) {
            List<ProBasicModelPower> ls = modelpmap.get(key);
            if (!ls.isEmpty()) {
                ls = ls.stream().sorted(Comparator.comparing(ProBasicModelPower::getSpeed)).collect(Collectors.toList());
                //理论功率拟合
                List<PointVo> llls = LineUtil.buildLine(ls.stream().mapToDouble(ProBasicModelPower::getSpeed).toArray(), ls.stream().mapToDouble(ProBasicModelPower::getTheoryPower).toArray(), ls.size(), dimension, scale);
                //保证功率拟合
                List<PointVo> bzls = LineUtil.buildLine(ls.stream().mapToDouble(ProBasicModelPower::getSpeed).toArray(), ls.stream().mapToDouble(ProBasicModelPower::getEnsurePower).toArray(), ls.size(), dimension, scale);


                if (!llls.isEmpty() && !bzls.isEmpty() && llls.size() == bzls.size()) {

                    double value = 0.00;
                    for (int i = 0; i < 100; i++) {

                        ProBasicModelPowerRd rd = new ProBasicModelPowerRd();
                        rd.setModelId(ls.get(0).getModelId());
                        rd.setWindpowerstationId(ls.get(0).getWindpowerstationId());
                        rd.setEnsurePower(0.0);
                        rd.setTheoryPower(0.0);
                        rd.setSpeed(value);
                        proBasicModelPowerRdService.save(rd);
                        value = value + 0.01;

                    }
                    for (int i = 0; i < llls.size(); i++) {
                        ProBasicModelPowerRd rd = new ProBasicModelPowerRd();
                        rd.setModelId(ls.get(0).getModelId());
                        rd.setWindpowerstationId(ls.get(0).getWindpowerstationId());
                        rd.setEnsurePower(StringUtils.round(bzls.get(i).getY(), 2));
                        rd.setTheoryPower(StringUtils.round(llls.get(i).getY(), 2));
                        rd.setSpeed(StringUtils.round(llls.get(i).getX(), 2));
                        proBasicModelPowerRdService.save(rd);
                    }

                }


            }

        }


    }

    public void cureFittingModel(List<String> modells) {

        if (!modells.isEmpty()) {
            Map<String, String> keymap = new HashMap<>();
            Map<String, List<ProBasicModelPower>> modelpmap = new TreeMap<>();

            for (String key : modells) {
                keymap.put(key, key);
            }

            for (ProBasicModelPower mp : CacheContext.mpls) {

                if (keymap.containsKey(mp.getModelId())) {
                    if (modelpmap.containsKey(mp.getModelId())) {
                        List<ProBasicModelPower> ls = modelpmap.get(mp.getModelId());
                        ls.add(mp);

                    } else {
                        List<ProBasicModelPower> ls = new ArrayList<>();
                        ls.add(mp);
                        modelpmap.put(mp.getModelId(), ls);
                    }
                }

            }


            for (String key : modelpmap.keySet()) {
                List<ProBasicModelPower> ls = modelpmap.get(key);
                if (!ls.isEmpty()) {
                    ls = ls.stream().sorted(Comparator.comparing(ProBasicModelPower::getSpeed)).collect(Collectors.toList());
                    //理论功率拟合
                    List<PointVo> llls = LineUtil.buildLine(ls.stream().mapToDouble(ProBasicModelPower::getSpeed).toArray(), ls.stream().mapToDouble(ProBasicModelPower::getTheoryPower).toArray(), ls.size(), dimension, scale);
                    //保证功率拟合
                    List<PointVo> bzls = LineUtil.buildLine(ls.stream().mapToDouble(ProBasicModelPower::getSpeed).toArray(), ls.stream().mapToDouble(ProBasicModelPower::getEnsurePower).toArray(), ls.size(), dimension, scale);


                    if (!llls.isEmpty() && !bzls.isEmpty() && llls.size() == bzls.size()) {

                        double value = 0.00;
                        for (int i = 0; i < 100; i++) {

                            ProBasicModelPowerRd rd = new ProBasicModelPowerRd();
                            rd.setModelId(ls.get(0).getModelId());
                            rd.setWindpowerstationId(ls.get(0).getWindpowerstationId());
                            rd.setEnsurePower(0.0);
                            rd.setTheoryPower(0.0);
                            rd.setSpeed(value);
                            proBasicModelPowerRdService.save(rd);
                            value = value + 0.01;

                        }
                        for (int i = 0; i < llls.size(); i++) {
                            ProBasicModelPowerRd rd = new ProBasicModelPowerRd();
                            rd.setModelId(ls.get(0).getModelId());
                            rd.setWindpowerstationId(ls.get(0).getWindpowerstationId());
                            rd.setEnsurePower(StringUtils.round(bzls.get(i).getY(), 2));
                            rd.setTheoryPower(StringUtils.round(llls.get(i).getY(), 2));
                            rd.setSpeed(StringUtils.round(llls.get(i).getX(), 2));
                            proBasicModelPowerRdService.save(rd);
                        }

                    }


                }

            }

        }


    }

}
