package com.gyee.alarm.service;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProBasicEquipmentPoint;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.service.auto.IProEconAlarmConfigurationService;

import com.gyee.alarm.util.ExcelExport;
import com.gyee.common.model.StringUtils;
import lombok.extern.log4j.Log4j2;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Log4j2
@Service
public class ProEconTestingPointService {





    private final String GFA = "gfa";

    private final String FJA = "fja";


    @Resource
    private IProEconAlarmConfigurationService proEconAlarmConfigurationService;


    public void generatePoint(String wpId) throws IOException {



        List<ProBasicEquipmentPoint> allpoints=initalFj(wpId);



                //创建一个数组用于设置表头
        String[] arr = new String[]{"编号","编码","名称","实时库名称","型号","单位","英文名称","类型编号","所属型号","最大值","最小值","合理最大值",
                "合理最小值","风机编号","统一编码","短ID","长ID","风场编号","实时配置编号","部件编号","集团测点编号","具体名称","系数"};
        String heardName="风功率测点表";
        //调用Excel导出工具类
        ExcelExport.exportToPath(allpoints,arr,heardName,6,"报警测点");
    }


    public List<ProBasicEquipmentPoint> initalFj(String wpId) {

        Map<String,List<ProEconAlarmConfiguration>> wtAlarmMap=CacheContext.wtAlarmMap;
        List<ProBasicEquipmentPoint> allpoints = new ArrayList<>();
        List<ProBasicEquipmentPoint> points = new ArrayList<>();

        if(StringUtils.notEmp(wpId) && CacheContext.wpwtmap.containsKey(wpId))
        {
            List<ProBasicEquipment> wtls=CacheContext.wpwtmap.get(wpId);

            if (StringUtils.notEmp(wtls) ) {

                for (ProBasicEquipment wt : wtls) {

                    StringBuilder tempsb=new StringBuilder();
                    tempsb.append(wt.getWindpowerstationId()).append(wt.getModelId());

                    if(wtAlarmMap.containsKey(String.valueOf(tempsb)))
                    {
                        List<ProEconAlarmConfiguration> ls=wtAlarmMap.get(String.valueOf(tempsb));

                        if(!ls.isEmpty())
                        {
                            for (int i = 0; i < ls.size(); i++) {

                                ProEconAlarmConfiguration stp = ls.get(i);

                                StringBuilder sb = new StringBuilder();
                                String temp = wt.getWindpowerstationId().substring(0, wt.getWindpowerstationId().lastIndexOf("_"));
                                temp = temp.substring(0, temp.lastIndexOf("_"));
                                temp = temp.substring(temp.lastIndexOf("_") + 1);
                                if (wt.getWindpowerstationId().indexOf("FDC") >= 0) {
                                    sb.append(wt.getCompanyId().substring(0, wt.getCompanyId().lastIndexOf("_"))).append("_").append(temp).append("_F_");
                                } else {
                                    sb.append(wt.getCompanyId().substring(0, wt.getCompanyId().lastIndexOf("_"))).append("_").append(temp).append("_G_");
                                }


                                temp = null;
                                temp = wt.getProjectId().substring(0, wt.getProjectId().lastIndexOf("_"));
                                sb.append("P").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                                temp = wt.getLineId().substring(0, wt.getLineId().lastIndexOf("_"));
                                sb.append("L").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");

                                temp = wt.getId().substring(wt.getId().lastIndexOf("_") - 4, wt.getId().lastIndexOf("_"));

                                if (temp.length() == 1) {
                                    sb.append("000").append(temp).append("_");
                                } else if (temp.length() == 2) {
                                    sb.append("00").append(temp).append("_");
                                } else if (temp.length() == 3) {
                                    sb.append("0").append(temp).append("_");
                                } else {
                                    sb.append(temp).append("_");
                                }

                                if(wt.getWindpowerstationId().contains("GDC"))
                                {
                                    sb.append(GFA.toUpperCase()).append("_");
                                }else
                                {
                                    sb.append(FJA.toUpperCase()).append("_");
                                }


                                if (ls.get(i).getUniformCode().indexOf("AI") >= 0) {
                                    sb.append("AI");

                                } else if (ls.get(i).getUniformCode().indexOf("DI") >= 0) {
                                    sb.append("DI");

                                } else {
                                    sb.append("CI");

                                }

                                sb.append(stp.getUniformCode());

                                StringBuilder sba = new StringBuilder();

                                temp = wt.getWindpowerstationId().substring(0, wt.getWindpowerstationId().lastIndexOf("_"));
                                temp = temp.substring(0, temp.lastIndexOf("_"));
                                temp = temp.substring(temp.lastIndexOf("_") + 1);

                                String temp2 = wt.getCompanyId().substring(0, wt.getCompanyId().lastIndexOf("_"));
                                temp2 = temp2.substring(temp2.indexOf("_") + 1);

                                String temp3 = wt.getWindpowerstationId().substring(0, wt.getWindpowerstationId().lastIndexOf("_"));
                                temp3 = temp3.substring(0, temp3.lastIndexOf("_"));
                                temp3 = temp3.substring(temp3.lastIndexOf("_") + 1);
                                if (wt.getWindpowerstationId().indexOf("FDC") >= 0) {
                                    sba.append(temp2).append(temp3).append("FJ");
                                } else if (wt.getWindpowerstationId().indexOf("GDC") >= 0) {
                                    sba.append(temp2).append(temp3).append("GF");
                                }

                                StringBuilder sbtable = new StringBuilder();


                                if (stp.getUniformCode().indexOf("AI") < 0 && stp.getUniformCode().indexOf("DI") < 0) {
                                    sbtable.append("NEM_");
                                    sbtable.append(sba).append("JSFW.");
                                    sbtable.append(sb);
                                } else {
                                    sbtable.append("NEM_");
                                    sbtable.append(sba).append(".");
                                    sbtable.append(sb);
                                }

                                ProBasicEquipmentPoint po = new ProBasicEquipmentPoint();
                                po.setId(StringUtils.getUUID());
//                        po.setNemCode(V2Config.getInitialcode());

                                po.setNemCode("INITIAL");

                                sb = new StringBuilder();
                                sb.append(wt.getName()).append(stp.getDescription());
//                        sb.append(wt.getName()).append(br.getName().substring(br.getName().length()-5)).append(stp.getName());
                                po.setName(String.valueOf(sb));
                                po.setModel(wt.getModelId());
                                po.setModelId(wt.getModelId());
                                po.setWindturbineId(wt.getId());
//                        po.setWindturbineId(br.getId());
                                po.setUniformCode(stp.getUniformCode());
                                if(wt.getWindpowerstationId().contains("GDC"))
                                {
                                    po.setTypeId(GFA.toUpperCase());

                                }else
                                {
                                    po.setTypeId(FJA.toUpperCase());

                                }

                                po.setWindpowerstationId(wt.getWindpowerstationId());
                                po.setLogicalUnitId(ls.get(i).getComponents());
                                po.setRealtimeId(String.valueOf(sbtable).substring(0,sbtable.indexOf(".")));
                                points.add(po);


                            }
                        }

                    }


                    allpoints.addAll(points);


                }
            }

        }


        return allpoints;

    }

}
