package com.gyee.runeconomy.service.EarlyWarning;

import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.ProEconAlarmPlan;
import com.gyee.runeconomy.model.ProEconAlarmReal;
import com.gyee.runeconomy.model.ProEconAlarmRule;
import com.gyee.runeconomy.model.ReliabilityIssues;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.service.auto.*;
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 EarlyWarninggetService {

    @Autowired
    private IProBasicPowerstationService powerstationService;
    @Resource
    private ReliabilityIssuesService reliabilityIssuesService;
    @Resource
    private ProEconAlarmRuleService ruleService;
    @Resource
    private ProEconAlarmRealService realService;
    @Resource
    private ProEconAlarmPlanService planService;

    public Map Electricity(String time, String wpid) throws Exception {

        Map<String, Object> map = new HashMap<>();


        List<ProBasicPowerstation> selectwpid = powerstationService.selectwpid(wpid);
        ProBasicPowerstation proBasicPowerstation = null;
        if (selectwpid.size() > 0) {
            proBasicPowerstation = selectwpid.get(0);
        }
        List<ReliabilityIssues> iskkx = reliabilityIssuesService.getReliabilityIssues(wpid, time);

        /*************************************************************筛选结果***********************************************/

        // 筛选 reliabilityIssue 字段为 "是"，并且 reliabilityIs 字段为 1 的记录
        String wdl = iskkx.stream()
                .filter(issue -> "是".equals(issue.getReliabilityIssue()) && issue.getReliabilityIs() == 1)
                .sorted(Comparator.comparing(ReliabilityIssues::getTurbineId))  // 根据 turbineId 排序
                .map(ReliabilityIssues::getTurbineId)
                .collect(Collectors.joining(","));

        // 筛选 reliabilityIssue 字段为 "是"，并且 reliabilityIs 字段为 2 的记录
        String ztl = iskkx.stream()
                .filter(issue -> "是".equals(issue.getReliabilityIssue()) && issue.getReliabilityIs() == 2)
                .sorted(Comparator.comparing(ReliabilityIssues::getTurbineId))  // 根据 turbineId 排序
                .map(ReliabilityIssues::getTurbineId)
                .collect(Collectors.joining(","));

        // 筛选 reliabilityIssue 字段为 "是"，并且 reliabilityIs 字段为 3 的记录
        String ztwdl = iskkx.stream()
                .filter(issue -> "是".equals(issue.getReliabilityIssue()) && issue.getReliabilityIs() == 3)
                .sorted(Comparator.comparing(ReliabilityIssues::getTurbineId))  // 根据 turbineId 排序
                .map(ReliabilityIssues::getTurbineId)
                .collect(Collectors.joining(","));


        // 拼接相关字段，检查每个字段是否为空，并加入结果中
        StringBuilder sxjgBuilder = new StringBuilder();


        if (wdl != null && !wdl.isEmpty()) {
            sxjgBuilder.append("以下风机存在温度类隐患：").append(wdl).append("\n");
        }
        if (ztl != null && !ztl.isEmpty()) {
            sxjgBuilder.append("以下风机存在状态类隐患：").append(ztl).append("\n");
        }
        if (ztwdl != null && !ztwdl.isEmpty()) {
            sxjgBuilder.append("以下风机存在状态类及温度类隐患：").append(ztwdl).append("\n");
        }

        //不去掉换行符
        String sxjg = sxjgBuilder.toString();
        if (sxjg.endsWith("\n")) {
            sxjg = sxjg.substring(0, sxjg.length() - 1);  // 去掉最后一个换行符
        }
        //去掉换行符
//        String sxjg = sxjgBuilder.toString().trim();

        /*************************************************************筛选结果***********************************************/

        Map<String,List> jxjymap = new TreeMap<>();
        Map<String, List> finalReport = new TreeMap<>();

        for (ReliabilityIssues vo : iskkx) {
            List<ReliabilityIssues> collect = iskkx.stream().filter(v -> vo.getTurbineId().equals(v.getTurbineId())).collect(Collectors.toList());
            StringBuilder kkxBuilder = new StringBuilder();
            List<String> qksm = new ArrayList<>();
            List<String> jxjyls = new ArrayList<>();

            List<ProBasicEquipment> equipments = CacheContext.wtls.stream()
                    .filter(wt -> vo.getTurbineId().equals(wt.getAname())).collect(Collectors.toList());
            String modelId = equipments.get(0).getModelId();

            /*******************************************************添加检修建议*************************************************/
            if (collect.size() > 0) {
                if (collect.get(0).getMainShaftType() != null){
                    kkxBuilder.append(collect.get(0).getMainShaftType()).append(",");
                    String mainShaftType = collect.get(0).getMainShaftType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(mainShaftType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(mainShaftType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }

                }
                if (collect.get(0).getGeneratorType() != null){
                    kkxBuilder.append(collect.get(0).getGeneratorType()).append(",");
                    String generatorType = collect.get(0).getGeneratorType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(generatorType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add( generatorType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }

                }
                if (collect.get(0).getGearboxType() !=null){
                    kkxBuilder.append(collect.get(0).getGearboxType()).append(",");
                    String gearboxType = collect.get(0).getGearboxType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(gearboxType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(gearboxType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getPitchSystemType() !=null){
                    kkxBuilder.append(collect.get(0).getPitchSystemType()).append(",");
                    String pitchSystemType = collect.get(0).getPitchSystemType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(pitchSystemType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(pitchSystemType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getTemperatureOtherIssues() !=null) {
                    kkxBuilder.append(collect.get(0).getTemperatureOtherIssues()).append(",");

                    String temperatureOtherIssues = collect.get(0).getTemperatureOtherIssues();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(temperatureOtherIssues,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(temperatureOtherIssues + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getVibrationType() !=null){
                    kkxBuilder.append(collect.get(0).getVibrationType()).append(",");
                    String vibrationType = collect.get(0).getVibrationType();

                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(vibrationType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(vibrationType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getMechanicalType() !=null){
                    kkxBuilder.append(collect.get(0).getMechanicalType()).append(",");
                    String mechanicalType = collect.get(0).getMechanicalType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(mechanicalType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(mechanicalType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getElectricalType() !=null){
                    kkxBuilder.append(collect.get(0).getElectricalType()).append(",");
                    String electricalType = collect.get(0).getElectricalType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(electricalType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(electricalType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getHydraulicType() !=null){
                    kkxBuilder.append(collect.get(0).getHydraulicType()).append(",");
                    String hydraulicType = collect.get(0).getHydraulicType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(hydraulicType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(hydraulicType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getWindMeasurementType() !=null){
                    kkxBuilder.append(collect.get(0).getWindMeasurementType()).append(",");
                    String windMeasurementType = collect.get(0).getWindMeasurementType();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(windMeasurementType,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(windMeasurementType + ":" + plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
                if (collect.get(0).getOtherIssues() !=null){
                    kkxBuilder.append(collect.get(0).getOtherIssues()).append(",");
                    String otherIssues = collect.get(0).getOtherIssues();
                    List<ProEconAlarmRule> ruleList = ruleService.getProEconAlarmRuleList(otherIssues,modelId);
                    if (ruleList.size() > 0) {
                        String id = ruleList.get(0).getId();
                        List<ProEconAlarmReal> realid = realService.getProEconAlarmReal(id);
                        if (realid.size() > 0){
                            String alarmPlan = realid.get(0).getAlarmPlan();
                            List<ProEconAlarmPlan> plans = planService.selectAlarmPlanByProId(alarmPlan);
                            if (plans.size() > 0){
                                jxjyls.add(otherIssues + ":"+ plans.get(0).getProcessMethod());
                            }
                        }
                    }
                }
            }
            /*******************************************************添加检修建议*************************************************/


            String issue = "0";
            Integer issis = null;
            if (collect.size() > 0) {
                issue = collect.get(0).getReliabilityIssue();
                issis = collect.get(0).getReliabilityIs();
                if (Objects.isNull(issis)) {
                    issis = 3; // 如果为空则赋值为3
                }
            }

            if (kkxBuilder.length() > 0 && issue.equals("是")) {
                kkxBuilder.insert(0, "可靠性问题：");
                kkxBuilder.deleteCharAt(kkxBuilder.length() - 1);
                kkxBuilder.append("。"); // 添加句号，删除最后一个字符
                String kkxresult = kkxBuilder.toString();
                qksm.add(kkxresult);
            }

            if (qksm.size() > 0) {
                finalReport.put(vo.getTurbineId(), qksm);
            }

            if (jxjyls.size() > 0) {
                jxjymap.put(vo.getTurbineId(), jxjyls);
            }
        }

        map.put("stationid", proBasicPowerstation);
        map.put("reporttime", time);
        iskkx.sort(Comparator.comparing(ReliabilityIssues::getTurbineId));
        map.put("kkxwtls", iskkx);
        map.put("FilterResults", sxjg);
        map.put("qksm",finalReport);
        map.put("jxjy",jxjymap);
        return map;
    }
}
