package com.gyee.alarm.service.initialpoint;



import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.service.auto.*;
import com.gyee.alarm.util.ExcelExport;
import com.gyee.alarm.util.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
public class InitialPointGoldenXkByEqService implements IinitialPoint {


    @Resource
    private IProBasicRegionService proBasicRegionService;
    @Resource
    private IProBasicCompanyService proBasicCompanyService;

    @Resource
    private IProEconTestingPointService proEconTestingPointService;

    private final String FJ = "fj";
    private final String FJJSFW = "fjc";
    private final String GFJSFW = "gfc";
    private final String GF = "gf";

    private final String GCJSFW = "gcc";
    private final String FCJSFW = "fcc";

    private final String AGC = "agcc";
    private final String FGL = "fggl";

    private final String CFT = "cft";

    @Override
    public void initalFc() throws IOException {


        //   windPowerstationTestingPointService.deleteAll();


        List<ProEconTestingPoint> pointls = proEconTestingPointService.list().stream()
                .filter(i->i.getTypeId().equals(FCJSFW) ).collect(Collectors.toList());

        List<ProEconTestingPoint> fdpointls = proEconTestingPointService.list().stream()
                .filter(i->i.getTypeId().equals(FCJSFW) ).collect(Collectors.toList());
        List<ProEconTestingPoint> gfpointls = proEconTestingPointService.list().stream()
                .filter(i->i.getTypeId().equals(GCJSFW)).collect(Collectors.toList());


        List<ProBasicRegion> regions = proBasicRegionService.list();


        List<ProBasicPowerstationPoint> allpoints = new ArrayList<>();

        if(!regions.isEmpty())
        {


            for(ProBasicRegion re:regions)
            {

                StringBuilder sba=new StringBuilder();
                sba.append(re.getId().substring(0,re.getId().indexOf("_"))).append("_");
/********************************************区域公司测点**************************************************/


                List<ProBasicPowerstationPoint>  points=new ArrayList<>();
                for(int i=0;i<pointls.size();i++)
                {

                    ProEconTestingPoint stp=pointls.get(i);
                    StringBuilder sb =new StringBuilder();
                    StringBuilder sbtable =new StringBuilder();
                    sbtable.append(re.getId().substring(0,re.getId().indexOf("_")));
                    sb.append(String.valueOf(sba));
                    sb.append("XXXX_XXX_X_XX_XX_XXXX_");
                    sb.append("CI");
                    sb.append(stp.getNemCode());

                    sbtable.append("JSFW.");
                    sbtable.append(sb);

                    ProBasicPowerstationPoint po=new ProBasicPowerstationPoint();
                    po.setId(StringUtils.getUUID());
//                            po.setNemCode(V2Config.getInitialcode());
                    po.setNemCode(String.valueOf(sbtable));
                    sb =new StringBuilder();
                    sb.append(re.getName()).append(stp.getName());
                    po.setName(String.valueOf(sb));
                    po.setModel(null);
                    po.setModelId(null);

                    StringBuilder tempsb=new StringBuilder();

                    if(StringUtils.notEmp(re.getNemCode()))
                    {
                        tempsb.append(re.getNemCode()).append("0");
                    }else
                    {
                        tempsb.append("0");
                    }

                    po.setWindpowerstationId(String.valueOf(tempsb));
                    po.setUniformCode(stp.getUniformCode());



                    sb =new StringBuilder();
                    sb.append(re.getNemCode()).append(".JSFW");
                    po.setRealtimeId(String.valueOf(sb));


                    points.add(po);
                }

                allpoints.addAll(points);
                // windPowerstationTestingPointService.insertBatch(points);

/*******************************************区域公司测点*******************************************************/
/********************************************风电场公司测点**************************************************/
                points=new ArrayList<>();

                for(int i=0;i<fdpointls.size();i++)
                {
                     sba=new StringBuilder();

                    ProEconTestingPoint stp=fdpointls.get(i);
                    StringBuilder sb =new StringBuilder();
                    StringBuilder sbtable =new StringBuilder();
                    sbtable.append(re.getId().substring(0,re.getId().indexOf("_")));
                    sb.append(String.valueOf(sba));
                    sb.append(re.getId().substring(0,re.getId().indexOf("_"))).append("_XXXX_XXX_F_XX_XX_XXXX_");
                    sb.append("CI");
                    sb.append(stp.getNemCode());

                    sbtable.append("JSFW.");
                    sbtable.append(sb);

                    ProBasicPowerstationPoint po=new ProBasicPowerstationPoint();
                    po.setId(StringUtils.getUUID());
//                            po.setNemCode(V2Config.getInitialcode());
                    po.setNemCode(String.valueOf(sbtable));
                    sb =new StringBuilder();
                    sb.append(re.getName()).append("风电").append(stp.getName());
                    po.setName(String.valueOf(sb));
                    po.setModel(null);
                    po.setModelId(null);

                    StringBuilder tempsb=new StringBuilder();
                    if(StringUtils.notEmp(re.getNemCode()))
                    {
                        tempsb.append(re.getNemCode()).append("-1");
                    }else
                    {
                        tempsb.append("-1");
                    }
                    po.setWindpowerstationId(String.valueOf(tempsb));
                    po.setUniformCode(stp.getUniformCode());



                    sb =new StringBuilder();
                    sb.append(re.getNemCode()).append(".JSFW");
                    po.setRealtimeId(String.valueOf(sb));
//                            else
//                            {
//                                sb =new StringBuilder();
//                                sb.append(re.getNemCode()).append(cs.getId()).append(".JSFWDD");
//                                po.setRealtimeId(String.valueOf(sb));
//                            }



                    points.add(po);
                }

                allpoints.addAll(points);
                // windPowerstationTestingPointService.insertBatch(points);

//*******************************************风电测点生成*******************************************************/


//*********************************************光伏测点生成*****************************************************/

                points=new ArrayList<>();
                for(int i=0;i<gfpointls.size();i++)
                {


                    ProEconTestingPoint stp=gfpointls.get(i);
                    StringBuilder sb =new StringBuilder();
                    StringBuilder sbtable =new StringBuilder();
                    sbtable.append(re.getId().substring(0,re.getId().indexOf("_")));
                    sb.append(String.valueOf(sba));
                    sb.append(re.getId().substring(0,re.getId().indexOf("_"))).append("_XXXX_XXX_G_XX_XX_XXXX_");
                    sb.append("CI");
                    sb.append(stp.getNemCode());

                    sbtable.append("JSFW.");
                    sbtable.append(sb);

                    ProBasicPowerstationPoint po=new ProBasicPowerstationPoint();
                    po.setId(StringUtils.getUUID());
//                            po.setNemCode(V2Config.getInitialcode());
                    po.setNemCode(String.valueOf(sbtable));
                    sb =new StringBuilder();
                    sb.append(re.getName()).append("光伏").append(stp.getName());
                    po.setName(String.valueOf(sb));
                    po.setModel(null);
                    po.setModelId(null);
                    StringBuilder tempsb=new StringBuilder();

                    if(StringUtils.notEmp(re.getNemCode()))
                    {
                        tempsb.append(re.getNemCode()).append("-2");
                    }else
                    {
                        tempsb.append("-2");
                    }

                    po.setWindpowerstationId(String.valueOf(tempsb));
                    po.setUniformCode(stp.getUniformCode());

                    sb =new StringBuilder();
                    sb.append(re.getNemCode()).append(".JSFW");
                    po.setRealtimeId(String.valueOf(sb));


                    points.add(po);
                }

                allpoints.addAll(points);
                List<ProBasicCompany> companys= proBasicCompanyService.list().stream().filter(i->i.getRegionId().equals(re.getId())).collect(Collectors.toList());
                if(!companys.isEmpty())
                {
                    for(ProBasicCompany cs:companys)
                    {
                        sba=new StringBuilder();
                        sba.append(cs.getId().substring(0,cs.getId().lastIndexOf("_"))).append("_");
/********************************************区域公司测点**************************************************/
                         points=new ArrayList<>();
                        for(int i=0;i<pointls.size();i++)
                        {


                            ProEconTestingPoint stp=pointls.get(i);
                            StringBuilder sb =new StringBuilder();
                            StringBuilder sbtable =new StringBuilder();
                            sbtable.append(re.getId().substring(0,re.getId().indexOf("_")));

                            sb.append(String.valueOf(sba));
                            sb.append("XXX_X_XX_XX_XXXX_");
                            sb.append("CI");
                            sb.append(stp.getNemCode());

                            sbtable.append("JSFW.");
                            sbtable.append(sb);

                            ProBasicPowerstationPoint po=new ProBasicPowerstationPoint();
                            po.setId(StringUtils.getUUID());
//                            po.setNemCode(V2Config.getInitialcode());
                            po.setNemCode(String.valueOf(sbtable));
                            sb =new StringBuilder();
                            sb.append(cs.getName()).append(stp.getName());
                            po.setName(String.valueOf(sb));
                            po.setModel(null);
                            po.setModelId(null);

                            StringBuilder tempsb=new StringBuilder();

                            if(StringUtils.notEmp(cs.getNemCode()))
                            {
                                tempsb.append(cs.getNemCode()).append("0");
                            }else
                            {
                                tempsb.append("0");
                            }

                            po.setWindpowerstationId(String.valueOf(tempsb));
                            po.setUniformCode(stp.getUniformCode());

                            sb =new StringBuilder();
                            sb.append(cs.getId()).append(".JSFW");
                            po.setRealtimeId(String.valueOf(sb));


                            points.add(po);
                        }

                        allpoints.addAll(points);
                        // windPowerstationTestingPointService.insertBatch(points);

/*******************************************区域公司测点*******************************************************/
/********************************************风电场公司测点**************************************************/
                        points=new ArrayList<>();

                        sba=new StringBuilder();
                        sba.append(cs.getId().substring(0,cs.getId().lastIndexOf("_"))).append("_");
                        for(int i=0;i<fdpointls.size();i++)
                        {

                            ProEconTestingPoint stp=fdpointls.get(i);
                            StringBuilder sb =new StringBuilder();
                            StringBuilder sbtable =new StringBuilder();
                            sbtable.append(re.getId().substring(0,re.getId().indexOf("_")));
                            sb.append(String.valueOf(sba));
                            sb.append("XXX_F_XX_XX_XXXX_");
                            sb.append("CI");
                            sb.append(stp.getNemCode());

                            sbtable.append("JSFW.");
                            sbtable.append(sb);

                            ProBasicPowerstationPoint po=new ProBasicPowerstationPoint();
                            po.setId(StringUtils.getUUID());
//                            po.setNemCode(V2Config.getInitialcode());
                            po.setNemCode(String.valueOf(sbtable));
                            sb =new StringBuilder();
                            sb.append(cs.getName()).append("风电").append(stp.getName());
                            po.setName(String.valueOf(sb));
                            po.setModel(null);
                            po.setModelId(null);

                            StringBuilder tempsb=new StringBuilder();
                            if(StringUtils.notEmp(cs.getNemCode()))
                            {
                                tempsb.append(cs.getNemCode()).append("-1");
                            }else
                            {
                                tempsb.append("-1");
                            }
                            po.setWindpowerstationId(String.valueOf(tempsb));
                            po.setUniformCode(stp.getUniformCode());

                            sb =new StringBuilder();
                            sb.append(cs.getId()).append(".JSFW");
                            po.setRealtimeId(String.valueOf(sb));



                            points.add(po);
                        }

                        allpoints.addAll(points);
                        // windPowerstationTestingPointService.insertBatch(points);

                        /*******************************************风电测点生成*******************************************************/


/*********************************************光伏测点生成*****************************************************/
                        sba=new StringBuilder();
                        sba.append(cs.getId().substring(0,cs.getId().lastIndexOf("_"))).append("_");
                        points=new ArrayList<>();
                        for(int i=0;i<gfpointls.size();i++)
                        {

                            ProEconTestingPoint stp=gfpointls.get(i);
                            StringBuilder sb =new StringBuilder();
                            StringBuilder sbtable =new StringBuilder();
                            sbtable.append(re.getId().substring(0,re.getId().indexOf("_")));
                            sb.append(String.valueOf(sba));
                            sb.append("XXX_G_XX_XX_XXXX_");
                            sb.append("CI");
                            sb.append(stp.getNemCode());

                            sbtable.append("JSFW.");
                            sbtable.append(sb);

                            ProBasicPowerstationPoint po=new ProBasicPowerstationPoint();
                            po.setId(StringUtils.getUUID());
//                            po.setNemCode(V2Config.getInitialcode());
                            po.setNemCode(String.valueOf(sbtable));
                            sb =new StringBuilder();
                            sb.append(cs.getName()).append("光伏").append(stp.getName());
                            po.setName(String.valueOf(sb));
                            po.setModel(null);
                            po.setModelId(null);
                            StringBuilder tempsb=new StringBuilder();

                            if(StringUtils.notEmp(cs.getNemCode()))
                            {
                                tempsb.append(cs.getNemCode()).append("-2");
                            }else
                            {
                                tempsb.append("-2");
                            }

                            po.setWindpowerstationId(String.valueOf(tempsb));
                            po.setUniformCode(stp.getUniformCode());

                            sb =new StringBuilder();
                            sb.append(cs.getId()).append(".JSFW");
                            po.setRealtimeId(String.valueOf(sb));

                            points.add(po);
                        }

                        allpoints.addAll(points);
                        // windPowerstationTestingPointService.insertBatch(points);
                    }
                }
            }
        }


//        }


        /*********************************************场站、项目、线路测点生成*****************************************************/


        for (ProBasicPowerstation wp : CacheContext.wpls) {
            List<ProEconTestingPoint> ls=null;
            if(wp.getId().indexOf("FDC")>=0)
            {
                ls=fdpointls;

            }else
            {
                ls=gfpointls;

            }
            createWpPoints(wp, ls, allpoints);
            createPjPoints(wp, ls, allpoints);
            createLnPoints(wp, ls, allpoints);

        }

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

    @Override
    public void initalZb() throws IOException {

    }

    @Override
    public void initalQx() throws IOException {

        List<ProBasicPowerstationPoint> allpoints = new ArrayList<>();

        List<ProEconTestingPoint> fcs = proEconTestingPointService.list().stream().filter(i ->  i.getTypeId().equals("cft")).collect(Collectors.toList());
        List<ProEconTestingPoint> gcs = proEconTestingPointService.list().stream().filter(i ->  i.getTypeId().equals("qxz")).collect(Collectors.toList());
        //获取场站
        for (ProBasicPowerstation postation : CacheContext.wpls) {

            List<ProEconTestingPoint> ls = null;
            if (postation.getId().indexOf("FDC") >= 0) {
                ls = fcs;
            } else {
                ls = gcs;
            }

            //获取场站下的气象站
            List<ProBasicWeatherStation> subStations = CacheContext.poWsStationMap.get(postation.getId());
            //循环气象站生成测点code
            for (ProBasicWeatherStation ws : subStations) {
                List<ProBasicPowerstationPoint> points = new ArrayList<>();

                for (ProEconTestingPoint p : ls) {

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

                    sb.append("XX_XX_XXXX_");
                    sb.append(p.getTypeId().toUpperCase()).append("_");
                    if (p.getUniformCode().indexOf("AI") >= 0) {
                        sb.append("AI");

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

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


                    StringBuilder sba = new StringBuilder();

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

                    String temp3 = postation.getId().substring(0, postation.getId().lastIndexOf("_"));
                    temp3 = temp3.substring(0, temp3.lastIndexOf("_"));
                    temp3 = temp3.substring(temp3.lastIndexOf("_") + 1);
                    if (postation.getId().indexOf("FDC") >= 0) {
                        sba.append(temp2).append(temp3).append("FC");
                    } else if (postation.getId().indexOf("GDC") >= 0) {
                        sba.append(temp2).append(temp3).append("GC");
                    }

                    StringBuilder sbtable = new StringBuilder();


                    sbtable.append(sba).append(sb);
                    ProBasicPowerstationPoint po = new ProBasicPowerstationPoint();
                    po.setId(com.gyee.common.model.StringUtils.getUUID());
//                    if (p.getTypeId().indexOf("agcjsfw") >= 0 ) {
//                        po.setNemCode(String.valueOf(sbtable));
//                    } else {
                    po.setNemCode(String.valueOf(sbtable));
//                    }
                    sb = new StringBuilder();
                    sb.append(ws.getName()).append(p.getName());
                    po.setName(String.valueOf(sb));
                    po.setWindpowerstationId(ws.getId());
                    po.setUniformCode(p.getUniformCode());
                    po.setTypeId(p.getTypeId());
                    po.setRealtimeId(String.valueOf(sbtable).substring(0,sbtable.indexOf(".")));

                    points.add(po);
                }
                allpoints.addAll(points);
            }
        }
        //  编号,编码,名称,实时库名称,型号,单位,英文名称,类型编号,所属型号,最大值,最小值,合理最大值,合理最小值,统一编码,短ID,长ID,场站编号,实时配置编号,集团测点编号,具体名称,系数
        String[] arr = new String[]{"编号", "编码", "名称","实时库名称", "型号", "单位", "英文名称", "类型编号", "所属型号", "最大值", "最小值", "合理最大值",
                "合理最小值", "统一编码", "短ID", "长ID", "风场编号", "实时配置编号", "集团测点编号", "具体名称", "系数"};
        String heardName = "气象测点表";
        //调用Excel导出工具类
        ExcelExport.exportToPath(allpoints, arr, heardName, 6, "气象测点表");
    }

    @Override
    public void initalFgl() throws IOException {


        List<ProBasicPowerstationPoint> allpoints = new ArrayList<>();

        List<ProEconTestingPoint>  ls=proEconTestingPointService.list().stream().filter(i -> i.getTypeId().equals(FGL) ||  i.getTypeId().equals(AGC) ).collect(Collectors.toList());

        //循环变电所生成测点code
        for (ProBasicSubStation sub : CacheContext.subwpls) {
            List<ProBasicPowerstationPoint> points = new ArrayList<>();

            if(CacheContext.wpmap.containsKey(sub.getWindpowerstationId()))
            {
                ProBasicPowerstation postation=CacheContext.wpmap.get(sub.getWindpowerstationId());

                for (ProEconTestingPoint p : ls) {

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

                    sb.append("XX_XX_XXXX_");
                    sb.append(p.getTypeId().toUpperCase()).append("_");
                    if (p.getUniformCode().indexOf("AI") >= 0) {
                        sb.append("AI");

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

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


                    StringBuilder sba = new StringBuilder();

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

                    String temp3 = postation.getId().substring(0, postation.getId().lastIndexOf("_"));
                    temp3 = temp3.substring(0, temp3.lastIndexOf("_"));
                    temp3 = temp3.substring(temp3.lastIndexOf("_") + 1);
                    if (postation.getId().indexOf("FDC") >= 0) {
                        sba.append(temp2).append(temp3).append("FC");
                    } else if (postation.getId().indexOf("GDC") >= 0) {
                        sba.append(temp2).append(temp3).append("GC");
                    }

                    StringBuilder sbtable = new StringBuilder();

                    sbtable.append(sba).append(".");
                    sbtable.append(sb);


                    ProBasicPowerstationPoint po = new ProBasicPowerstationPoint();
                    po.setId(com.gyee.common.model.StringUtils.getUUID());

                    po.setNemCode(String.valueOf(sbtable));

                    sb = new StringBuilder();
                    sb.append(sub.getName()).append(p.getName());
                    po.setName(String.valueOf(sb));
                    po.setWindpowerstationId(sub.getId());
                    po.setUniformCode(p.getUniformCode());
                    po.setTypeId(p.getTypeId());
                    po.setRealtimeId(String.valueOf(sbtable).substring(0,sbtable.indexOf(".")));
                    points.add(po);
                }
                allpoints.addAll(points);
            }


        }

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

    private void createWpPoints(ProBasicPowerstation wp, List<ProEconTestingPoint> stpointls, List<ProBasicPowerstationPoint> allpoints) {
        List<ProBasicPowerstationPoint> points = new ArrayList<>();
        for (int i = 0; i < stpointls.size(); i++) {



            ProEconTestingPoint stp = stpointls.get(i);
            StringBuilder sb = new StringBuilder();
//            sb.append(wp.getPhoto());
            String temp = wp.getId().substring(0, wp.getId().lastIndexOf("_"));
            temp = temp.substring(0, temp.lastIndexOf("_"));
            temp = temp.substring(temp.lastIndexOf("_")+1);
            StringBuilder sba = new StringBuilder();


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

            String temp3=wp.getId().substring(0,wp.getId().lastIndexOf("_"));
            temp3=temp3.substring(0,temp3.lastIndexOf("_"));
            temp3=temp3.substring(temp3.lastIndexOf("_")+1);

            sb.append(wp.getCompanyId().substring(0,wp.getCompanyId().lastIndexOf("_"))).append("_");
            if (wp.getId().indexOf("FDC")>=0) {


                sba.append(temp2).append(temp3).append("FC");
                sb.append(temp).append("_F").append("_XX_XX_XXXX_");
            } else if (wp.getId().indexOf("GDC")>=0) {

                sba.append(temp2).append(temp3).append("GC");
                sb.append(temp).append("_G").append("_XX_XX_XXXX_");
            }

            sb.append("CI");
            sb.append(stp.getNemCode());
            StringBuilder sbtable = new StringBuilder();


            sbtable.append(sba).append("JSFW.");
            sbtable.append(sb);

            ProBasicPowerstationPoint po = new ProBasicPowerstationPoint();
            po.setId(StringUtils.getUUID());
//            po.setNemCode(V2Config.getInitialcode());
            po.setNemCode(String.valueOf(sbtable));
            sb = new StringBuilder();
            sb.append(wp.getName()).append(stp.getName());
            po.setName(String.valueOf(sb));
            po.setModel(null);
            po.setModelId(null);
            po.setWindpowerstationId(wp.getId());
            po.setUniformCode(stp.getUniformCode());



            sb = new StringBuilder();

            if (StringUtils.notEmp(wp.getPhoto())) {
                sb.append(wp.getPhoto().replace("_", "")).append(".JSFW").append(sba);
            }

            po.setRealtimeId(String.valueOf(sb));

            points.add(po);
        }

        allpoints.addAll(points);
        //windPowerstationTestingPointService.insertBatch(points);
    }




    private void createPjPoints(ProBasicPowerstation wp, List<ProEconTestingPoint> stpointls, List<ProBasicPowerstationPoint> allpoints) {


        for (ProBasicProject pj : CacheContext.pjls) {
            if (pj.getWindpowerstationId().equals(wp.getId())) {
                List<ProBasicPowerstationPoint> points = new ArrayList<>();
                for (int i = 0; i < stpointls.size(); i++) {


                    ProEconTestingPoint stp = stpointls.get(i);
                    StringBuilder sb = new StringBuilder();

                    String temp = wp.getId().substring(0, wp.getId().lastIndexOf("_"));
                    temp = temp.substring(0, temp.lastIndexOf("_"));
                    temp = temp.substring(temp.lastIndexOf("_")+1);
                    sb.append(wp.getCompanyId().substring(0,wp.getCompanyId().lastIndexOf("_"))).append("_");


                    StringBuilder sba = new StringBuilder();

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

                    String temp3=wp.getId().substring(0,wp.getId().lastIndexOf("_"));
                    temp3=temp3.substring(0,temp3.lastIndexOf("_"));
                    temp3=temp3.substring(temp3.lastIndexOf("_")+1);

                    if (wp.getId().indexOf("FDC")>=0) {

                        sba.append(temp2).append(temp3).append("FC");
                        sb.append(temp).append("_F_");
                        temp = pj.getId().substring(0, pj.getId().lastIndexOf("_"));
                        sb.append("P").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                        sb.append("XX_XXXX_");
                    } else if (wp.getId().indexOf("GDC")>=0) {

                        sba.append(temp2).append(temp3).append("GC");
                        sb.append(temp).append("_G_");
                        temp = pj.getId().substring(0, pj.getId().lastIndexOf("_"));
                        sb.append("P").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                        sb.append("XX_XXXX_");
                    }

                    sb.append("CI");
                    sb.append(stp.getNemCode());

                    StringBuilder sbtable = new StringBuilder();


                    sbtable.append(sba).append("JSFW.");
                    sbtable.append(sb);

                    ProBasicPowerstationPoint po = new ProBasicPowerstationPoint();

                    po.setId(StringUtils.getUUID());

                    po.setNemCode(String.valueOf(sbtable));
                    sb = new StringBuilder();
                    sb.append(pj.getName()).append(stp.getName());
                    po.setName(String.valueOf(sb));
                    po.setModel(null);
                    po.setModelId(null);
                    po.setWindpowerstationId(pj.getId());
                    po.setUniformCode(stp.getUniformCode());

                    sb = new StringBuilder();

                    if (StringUtils.notEmp(wp.getPhoto())) {
                        sb.append(wp.getPhoto().replace("_", "")).append(".JSFW").append(sba);
                    }

                    po.setRealtimeId(String.valueOf(sb));

                    points.add(po);
                }
                allpoints.addAll(points);
            }
        }

    }



    private void createLnPoints(ProBasicPowerstation wp, List<ProEconTestingPoint> stpointls, List<ProBasicPowerstationPoint> allpoints) {

        for (ProBasicLine ln : CacheContext.lnls) {
            if (CacheContext.pjmap.containsKey(ln.getProjectId())) {
                ProBasicProject pj = CacheContext.pjmap.get(ln.getProjectId());

                if (pj.getWindpowerstationId().equals(wp.getId())) {
                    List<ProBasicPowerstationPoint> points = new ArrayList<>();
                    for (int i = 0; i < stpointls.size(); i++) {


                        ProEconTestingPoint stp = stpointls.get(i);
                        StringBuilder sb = new StringBuilder();
//                        sb.append(wp.getPhoto());
                        String temp = wp.getId().substring(0, wp.getId().lastIndexOf("_"));
                        temp = temp.substring(0, temp.lastIndexOf("_"));
                        temp = temp.substring(temp.lastIndexOf("_")+1);
                        sb.append(wp.getCompanyId().substring(0,wp.getCompanyId().lastIndexOf("_"))).append("_");


                        StringBuilder sba = new StringBuilder();

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

                        String temp3=wp.getId().substring(0,wp.getId().lastIndexOf("_"));
                        temp3=temp3.substring(0,temp3.lastIndexOf("_"));
                        temp3=temp3.substring(temp3.lastIndexOf("_")+1);

                        if (wp.getId().indexOf("FDC")>=0) {

                            sba.append(temp2).append(temp3).append("FC");
                            sb.append(temp).append("_F_");
                            temp = pj.getId().substring(0, pj.getId().lastIndexOf("_"));
                            sb.append("P").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                            temp = ln.getId().substring(0, ln.getId().lastIndexOf("_"));
                            sb.append("L").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                            sb.append("XXXX_");
//                            sb.append(CI);
                        } else if (wp.getId().indexOf("GDC")>=0) {

                            sba.append(temp2).append(temp3).append("GC");
                            sb.append(temp).append("G_");
                            temp = pj.getId().substring(0, pj.getId().lastIndexOf("_"));
                            sb.append("P").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                            temp = ln.getId().substring(0, ln.getId().lastIndexOf("_"));
                            sb.append("L").append(Integer.valueOf(temp.substring(temp.length() - 2))).append("_");
                            sb.append("XXXX_");
//                            sb.append(CI);
                        }

                        sb.append("CI");
                        sb.append(stp.getNemCode());

                        StringBuilder sbtable = new StringBuilder();

                        sbtable.append(sba).append("JSFW.");
                        sbtable.append(sb);

                        ProBasicPowerstationPoint po = new ProBasicPowerstationPoint();
                        po.setId(StringUtils.getUUID());

                        po.setNemCode(String.valueOf(sbtable));
                        sb = new StringBuilder();
                        sb.append(ln.getName()).append(stp.getName());
                        po.setName(String.valueOf(sb));
                        po.setModel(null);
                        po.setModelId(null);
                        po.setWindpowerstationId(ln.getId());
                        po.setUniformCode(stp.getUniformCode());

                        sb = new StringBuilder();

                        if (StringUtils.notEmp(wp.getPhoto())) {
                            sb.append(wp.getPhoto().replace("_", "")).append(".JSFW").append(sba);
                        }

                        po.setRealtimeId(String.valueOf(sb));


                        points.add(po);
                    }
                    allpoints.addAll(points);


                }
            }

        }

    }



    public void initalFj() throws IOException {

        // windturbinetestingpointService.deleteAll();





        List<ProBasicEquipmentPoint> allpoints = new ArrayList<>();

        List<ProEconTestingPoint>  fdls=proEconTestingPointService.list().stream().filter(i->i.getTypeId().equals("fj") || i.getTypeId().equals(FJJSFW)).collect(Collectors.toList());

        List<ProEconTestingPoint>  gfls=proEconTestingPointService.list().stream().filter(i->i.getTypeId().equals("gf") || i.getTypeId().equals(GFJSFW)).collect(Collectors.toList());

        for (ProBasicPowerstation wp : CacheContext.wpls) {

            List<ProEconTestingPoint> ls=null;
            if(wp.getId().contains("FDC"))
            {
                ls=fdls;

            }else
            {
                ls=gfls;

            }
            List<ProBasicEquipment> wtls = CacheContext.wpwtmap.get(wp.getId());


            if (!ls.isEmpty() ) {
                for (ProBasicEquipment wt : wtls) {

                    List<ProBasicEquipmentPoint> points = new ArrayList<>();
                    for (int i = 0; i < ls.size(); i++) {



                        ProEconTestingPoint stp = ls.get(i);
//                        if(!stp.getTypeId().contains("jsfw") && !stp.getModel().equals(wt.getModelId()))
//                        {
//                            continue;
//                        }
                        StringBuilder sb = new StringBuilder();
//                        sb.append(wp.getPhoto());

                        String temp = wp.getId().substring(0, wp.getId().lastIndexOf("_"));
                        temp = temp.substring(0, temp.lastIndexOf("_"));
                        temp = temp.substring(temp.lastIndexOf("_")+1);
                        if(wp.getId().contains("FDC"))
                        {
                            sb.append(wp.getCompanyId().substring(0,wp.getCompanyId().lastIndexOf("_"))).append("_").append(temp).append("_F_");
                        }else
                        {
                            sb.append(wp.getCompanyId().substring(0,wp.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.getProjectId().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(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.getNemCode());

                        StringBuilder sba = new StringBuilder();

                        temp = wp.getId().substring(0, wp.getId().lastIndexOf("_"));
                        temp = temp.substring(0, temp.lastIndexOf("_"));
                        temp = temp.substring(temp.lastIndexOf("_")+1);

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

                        String temp3=wp.getId().substring(0,wp.getId().lastIndexOf("_"));
                        temp3=temp3.substring(0,temp3.lastIndexOf("_"));
                        temp3=temp3.substring(temp3.lastIndexOf("_")+1);
                        if (wp.getId().indexOf("FDC")>=0) {
                            sba.append(temp2).append(temp3).append("FJ");
                        } else if (wp.getId().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(sba).append("JSFW.");
                            sbtable.append(sb);
                        } else {
                            sbtable.append(sba).append(".");
                            sbtable.append(sb);
                        }

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

                        if (stp.getUniformCode().indexOf("AI")<0 && stp.getUniformCode().indexOf("DI")<0)
                        {
                            po.setNemCode(String.valueOf(sbtable));
                        }else
                        {
                            po.setNemCode(String.valueOf(sbtable));
                        }

                        sb = new StringBuilder();
                        sb.append(wt.getName()).append(stp.getName());
                        po.setName(String.valueOf(sb));
                        po.setModel(wt.getModelId());
                        po.setModelId(wt.getModelId());
                        po.setWindturbineId(wt.getId());
                        po.setUniformCode(stp.getUniformCode());
                        po.setWindpowerstationId(wt.getWindpowerstationId());
                        po.setLogicalUnitId(ls.get(i).getLogicalUnitId());

                        if (stp.getUniformCode().indexOf("AI")<0 && stp.getUniformCode().indexOf("DI")<0) {
                            sb = new StringBuilder();

                            sb.append(wp.getId().substring(0,wp.getId().lastIndexOf("_")).replace("_", "")).append(".JSFW").append(sba);

                            po.setRealtimeId(String.valueOf(sb));
                        } else {
                            sb = new StringBuilder();

                            sb.append(wp.getId().substring(0,wp.getId().lastIndexOf("_")).replace("_", "")).append(".").append(sba);

                            po.setRealtimeId(String.valueOf(sb));
                        }

                        points.add(po);


//                               allpoints.addAll(points);
                    }
                    allpoints.addAll(points);
                }

            }
//            }


        }


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

    }



}
