package com.gyee.runeconomy.service.Pranalysis;

import com.gyee.common.model.StringUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.dto.PhotovoltaicLevel;
import com.gyee.runeconomy.dto.PrAnalysisVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.ProEconEquipmentmodel;
import com.gyee.runeconomy.model.auto.ProEconSystemEfficiencyEquipment;
import com.gyee.runeconomy.service.auto.impl.ProEconSystemEfficiencyEquipmentServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class PrAnalysisService {

    @Resource
    private ProEconSystemEfficiencyEquipmentServiceImpl efficiencyEquipmentService;

    public List<PrAnalysisVo> Comprehensive(String wpId, String month, String target, String sort) throws Exception {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = dateFormat.parse(month);


        List<PrAnalysisVo> vos = new ArrayList<PrAnalysisVo>();

        List<ProEconSystemEfficiencyEquipment> bqls = efficiencyEquipmentService.getbq(wpId, date);
        List<ProEconSystemEfficiencyEquipment> hbls = efficiencyEquipmentService.gethb(wpId, date);
        List<ProEconSystemEfficiencyEquipment> tbls = efficiencyEquipmentService.gettb(wpId, date);

        if (bqls != null && bqls.size() > 1) {
            for (ProEconSystemEfficiencyEquipment eq : bqls) {
                List<ProBasicEquipment> collect1 = CacheContext.wtls.stream().filter(wtt -> eq.getEquipmentId().equals(wtt.getId())).collect(Collectors.toList());
                List<ProEconEquipmentmodel> collect = CacheContext.equipmentmodels.stream().filter(w -> collect1.get(0).getModelId().equals(w.getId())).collect(Collectors.toList());

                PrAnalysisVo vo = new PrAnalysisVo();
                vo.setWtid(eq.getForeignKeyId());
                String level = determinePhotovoltaicLevel(eq.getEfficiency());
                vo.setLevel(level);
                vo.setPr(eq.getEfficiency());
                vo.setRl(collect.get(0).getPowerProduction());
                vo.setLjgzz(eq.getRzqd());
                vo.setFdl(eq.getPowerGeneration());
                vo.setLlfdlmax(eq.getLlfdlmax());
                vos.add(vo);
            }
        }
        if (hbls != null && hbls.size() > 1) {
            vos.stream().forEach(i -> {
                Optional<ProEconSystemEfficiencyEquipment> optional = hbls.stream()
                        .filter(j -> j.getForeignKeyId().equals(i.getWtid()))
                        .findFirst();
                if (optional.isPresent()) {
                    ProEconSystemEfficiencyEquipment tqinfoday = optional.get();
                    i.setPrhb(tqinfoday.getEfficiency());
                    if (i.getPrhb() != 0) {
                        i.setPrhbzzl((i.getPr() - i.getPrhb()) / i.getPrhb() * 100);
                    } else {
                        i.setPrhbzzl(0.0);
                    }
                } else {
                    i.setPrhb(0.0);
                }
            });
        }

        if (tbls != null && tbls.size() > 1) {
            vos.stream().forEach(i -> {
                Optional<ProEconSystemEfficiencyEquipment> optional = tbls.stream()
                        .filter(j -> j.getForeignKeyId().equals(i.getWtid()))
                        .findFirst();
                if (optional.isPresent()) {
                    ProEconSystemEfficiencyEquipment tqinfoday = optional.get();
                    i.setPrtb(tqinfoday.getEfficiency());
                    i.setPrtbzzl((i.getPr() - i.getPrtb()) / i.getPrtb() * 100);
                } else {
                    i.setPrtb(0.0);
                }
            });
        }


        SortUtils.sort(vos, "pr", SortUtils.DESC);
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setPrpm(i + 1);
        }

        SortUtils.sort(vos, "prhb", SortUtils.DESC);
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setPrhbpm(i + 1);
        }

        SortUtils.sort(vos, "prhbzzl", SortUtils.DESC);
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setPrhbzzlpm(i + 1);
        }

        SortUtils.sort(vos, "ljgzz", SortUtils.DESC);
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setLjgzzpm(i + 1);
        }

        SortUtils.sort(vos, "fdl", SortUtils.DESC);
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setFdlpm(i + 1);
        }

        SortUtils.sort(vos, "llfdlmax", SortUtils.DESC);
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setllfdlmaxpm(i + 1);
        }

        if (tbls.size() > 0) {
            SortUtils.sort(vos, "prtb", SortUtils.DESC);
            for (int i = 0; i < vos.size(); i++) {
                vos.get(i).setPrtbpm(i + 1);
            }

            SortUtils.sort(vos, "prtbzzl", SortUtils.DESC);
            for (int i = 0; i < vos.size(); i++) {
                vos.get(i).setPrtbzzlpm(i + 1);
            }
        }


        if (StringUtils.isNotEmpty(target) && StringUtils.isNotEmpty(sort)) {
            if (sort.equals("1")) {
                SortUtils.sort(vos, target, SortUtils.ASC);
            } else {
                SortUtils.sort(vos, target, SortUtils.DESC);
            }
        } else {
            SortUtils.sort(vos, "prpm", SortUtils.ASC);
        }
        return vos;
    }


    // 根据转换效率确定光伏运行水平
    private static String determinePhotovoltaicLevel(double efficiency) {
        if (efficiency >= 90.0) {
            return PhotovoltaicLevel.STABLE;
        } else if (efficiency >= 80.0) {
            return PhotovoltaicLevel.GOOD;
        } else if (efficiency >= 70.0) {
            return PhotovoltaicLevel.NEEDS_IMPROVEMENT;
        } else {
            return PhotovoltaicLevel.MUST_CORRECT;
        }

    }
}
