package com.gyee.frame.service.initialpoint;

import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class InitialPointGoldenService implements  IinitialPoint{

    @Resource
    private RegionService regionService;
    @Resource
    private CompanysService companysService;
    @Resource
    private WindturbinestandardpointService windturbinestandardpointService;
    @Resource
    private WindpowerstationstandardpointService windpowerstationstandardpointService;
    @Resource
    private WindturbinetestingpointService windturbinetestingpointService;
    @Resource
    private WindPowerstationTestingPointService windPowerstationTestingPointService;

    @Resource
    private ForecaststationtandardpointService forecaststationtandardpointService;
    @Resource
    private WindsubstationService windsubstationService;
    @Resource
    private WindsubstationstandardpointService windsubstationstandardpointService;
    @Resource
    private Windsubstationtestingpoint2Service windsubstationtestingpoint2Service;
    private final  String AI="AI";
    private final  String DI="DI";
    private final  String CI="CI";
    private final String CFT="CFT";

    @Override
    public void initalFc(boolean isGf) {



        windPowerstationTestingPointService.deleteAll();

        List<Windpowerstationstandardpoint>  stpointls=windpowerstationstandardpointService.findAllList();

        List<Region> regions=regionService.findAllList();

        if(!regions.isEmpty())
        {
            for(Region re:regions)
            {
                List<Companys> companys= companysService.getCompanysListByRId(re.getId());
                if(!companys.isEmpty())
                {
                    for(Companys cs:companys)
                    {
                        StringBuilder sba=new StringBuilder();
                        sba.append(re.getId()).append("_").append(cs.getId()).append("_");
/********************************************区域公司测点**************************************************/
                        List<WindPowerStationTestingPoint> points=new ArrayList<>();
                        for(int i=0;i<stpointls.size();i++)
                        {
                            StringBuilder sbtable =new StringBuilder();

                            Windpowerstationstandardpoint stp=stpointls.get(i);
                            StringBuilder sb =new StringBuilder();
                            sb.append(String.valueOf(sba));
                            sb.append("XXX_XX_XXX_XXX_XXX_");
                            sb.append(CI);
                            sb.append(stp.getOrdernum());
                            if(stp.getCode().equals(CI))
                            {
                                sbtable.append("JSFW.");
                                sbtable.append(sb);
                            }
                            WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
                            po.setCode(String.valueOf(sbtable));
                            sb =new StringBuilder();
                            sb.append(re.getName()).append(stp.getName());
                            po.setName(String.valueOf(sb));
                            po.setModel(null);
                            po.setModelid(null);
                            if(regions.size()==1)
                            {
                                po.setWindpowerstationid("0");
                            }else
                            {
                                StringBuilder tempsb=new StringBuilder();
                                tempsb.append(re.getId()).append(0);
                                po.setWindpowerstationid(String.valueOf(tempsb));
                            }
                            po.setUniformcode(stp.getUniformcode());

                            if(stp.getCode().equals(CI))
                            {
                                StringBuilder tempsb=new StringBuilder();
                                tempsb.append(cs.getId()).append(re.getId());
                                tempsb.append(".JSFW");
                                po.setRealtimeid(String.valueOf(tempsb));
                            }
                            points.add(po);
                        }
                        windPowerstationTestingPointService.insertBatch(points);

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

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

                            Windpowerstationstandardpoint stp=stpointls.get(i);
                            StringBuilder sb =new StringBuilder();
                            sb.append(String.valueOf(sba));
                            sb.append("FDC_XX_XXX_XXX_XXX_");
                            sb.append(CI);
                            sb.append(stp.getOrdernum());
                            if(stp.getCode().equals(CI))
                            {
                                sbtable.append("JSFW.");
                                sbtable.append(sb);
                            }
                            WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
                            po.setCode(String.valueOf(sbtable));
                            sb =new StringBuilder();
                            sb.append(re.getName()).append(stp.getName());
                            po.setName(String.valueOf(sb));
                            po.setModel(null);
                            po.setModelid(null);
                            if(regions.size()==1)
                            {
                                po.setWindpowerstationid("-1");
                            }else
                            {
                                StringBuilder tempsb=new StringBuilder();
                                tempsb.append(re.getId()).append(1);
                                po.setWindpowerstationid(String.valueOf(tempsb));
                            }
                            po.setUniformcode(stp.getUniformcode());

                            if(stp.getCode().equals(CI))
                            {
                                StringBuilder tempsb=new StringBuilder();
                                tempsb.append(cs.getId()).append(re.getId());
                                tempsb.append(".JSFW");
                                po.setRealtimeid(String.valueOf(tempsb));
                            }
                            points.add(po);
                        }
                        windPowerstationTestingPointService.insertBatch(points);

                        if(isGf)
                        {
                            /*********************************************光伏测点生成*****************************************************/

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

                                Windpowerstationstandardpoint stp=stpointls.get(i);
                                StringBuilder sb =new StringBuilder();
                                sb.append(String.valueOf(sba));
                                sb.append("GDC_XX_XXX_XXX_XXX_");
                                sb.append(CI);
                                sb.append(stp.getOrdernum());
                                if(stp.getCode().equals(CI))
                                {
                                    sbtable.append("JSFW.");
                                    sbtable.append(sb);
                                }
                                WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
                                po.setCode(String.valueOf(sbtable));
                                sb =new StringBuilder();
                                sb.append(re.getName()).append(stp.getName());
                                po.setName(String.valueOf(sb));
                                po.setModel(null);
                                po.setModelid(null);
                                if(regions.size()==1)
                                {
                                    po.setWindpowerstationid("-2");
                                }else
                                {
                                    StringBuilder tempsb=new StringBuilder();
                                    tempsb.append(re.getId()).append(2);
                                    po.setWindpowerstationid(String.valueOf(tempsb));
                                }
                                po.setUniformcode(stp.getUniformcode());

                                if(stp.getCode().equals(CI))
                                {
                                    StringBuilder tempsb=new StringBuilder();
                                    tempsb.append(cs.getId()).append(re.getId());
                                    tempsb.append(".JSFW");
                                    po.setRealtimeid(String.valueOf(tempsb));
                                }
                                points.add(po);
                            }
                            windPowerstationTestingPointService.insertBatch(points);
                        }
                    }
                }
            }
        }
        /*********************************************场站、项目、线路测点生成*****************************************************/

        for (Windpowerstation wp:InitialRunner.wpallls)
        {

            if(!stpointls.isEmpty())
            {

                createWpPoints(wp, stpointls);
                createPjPoints(wp, stpointls);
                createLnPoints(wp, stpointls);
            }
        }
    }

    private void createWpPoints(Windpowerstation wp, List<Windpowerstationstandardpoint> stpointls) {
        List<WindPowerStationTestingPoint> points=new ArrayList<>();
        for(int i=0;i<stpointls.size();i++)
        {
            StringBuilder sbtable =new StringBuilder();

            Windpowerstationstandardpoint stp=stpointls.get(i);
            StringBuilder sb =new StringBuilder();
            sb.append(wp.getPhoto());
            String temp=wp.getId().substring(0,wp.getId().indexOf("_"));

            if(wp.getId().endsWith("FDC"))
            {
                sbtable.append(temp).append("FC");
                sb.append(temp).append("F_").append("JS_XX_XX_XXX_");
            }else   if(wp.getId().endsWith("GDC"))
            {
                sbtable.append(temp).append("GC");
                sb.append(temp).append("G_").append("JS_XX_XX_XXX_");
            }



            sb.append(CI);

            sb.append(stp.getOrdernum());

            if(stp.getCode().equals(CI))
            {
                sbtable.append("JSFW.");
                sbtable.append(sb);
            }

            WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
            po.setCode(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());

            if(stp.getCode().equals(CI))
            {
                sb =new StringBuilder();
                temp=wp.getId().substring(0,wp.getId().indexOf("_"));
                sb.append(temp).append(".JSFW");
                po.setRealtimeid(String.valueOf(sb));
            }
            points.add(po);
        }
        windPowerstationTestingPointService.insertBatch(points);
    }

    private void createPjPoints(Windpowerstation wp, List<Windpowerstationstandardpoint> stpointls) {


        for(Project pj:InitialRunner.pjls)
        {
            if(pj.getWindpowerstationid().equals(wp.getId()))
            {
                List<WindPowerStationTestingPoint> points=new ArrayList<>();
                for(int i=0;i<stpointls.size();i++)
                {
                    StringBuilder sbtable =new StringBuilder();

                    Windpowerstationstandardpoint stp=stpointls.get(i);
                    StringBuilder sb =new StringBuilder();
                    sb.append(wp.getPhoto());
                    String temp=wp.getId().substring(0,wp.getId().indexOf("_"));

                    sbtable.append(temp).append("FC");
                    sb.append(temp).append("F_").append("FJ_");
                    temp=pj.getId().substring(0,pj.getId().lastIndexOf("_"));
                    sb.append("P").append(Integer.valueOf(temp.substring(temp.length()-2))).append("_");
                    sb.append("XX_XXX_");
                    sb.append(CI);

                    sb.append(stp.getOrdernum());

                    if(stp.getCode().equals(CI))
                    {
                        sbtable.append("JSFW.");
                        sbtable.append(sb);
                    }

                    WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
                    po.setCode(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());

                    if(stp.getCode().equals(CI))
                    {
                        sb =new StringBuilder();
                        temp=wp.getId().substring(0,wp.getId().indexOf("_"));
                        sb.append(temp).append(".JSFW");
                        po.setRealtimeid(String.valueOf(sb));
                    }
                    points.add(po);
                }
                windPowerstationTestingPointService.insertBatch(points);
            }
        }

    }

    private void createLnPoints(Windpowerstation wp, List<Windpowerstationstandardpoint> stpointls) {

        for(Line ln:InitialRunner.linels)
        {
            if(InitialRunner.pjmap.containsKey(ln.getProjectid()))
            {
                Project pj=InitialRunner.pjmap.get(ln.getProjectid());

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

                        Windpowerstationstandardpoint stp=stpointls.get(i);
                        StringBuilder sb =new StringBuilder();
                        sb.append(wp.getPhoto());
                        String temp=wp.getId().substring(0,wp.getId().indexOf("_"));

                        sbtable.append(temp).append("FC");
                        sb.append(temp).append("F_").append("FJ_");
                        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("XXX_");
                        sb.append(CI);

                        sb.append(stp.getOrdernum());

                        if(stp.getCode().equals(CI))
                        {
                            sbtable.append("JSFW.");
                            sbtable.append(sb);
                        }

                        WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
                        po.setCode(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());

                        if(stp.getCode().equals(CI))
                        {
                            sb =new StringBuilder();
                            temp=wp.getId().substring(0,wp.getId().indexOf("_"));
                            sb.append(temp).append(".JSFW");
                            po.setRealtimeid(String.valueOf(sb));
                        }
                        points.add(po);
                    }
                    windPowerstationTestingPointService.insertBatch(points);
                }
            }

        }

    }

    @Override
    public void initalSyz() {

    }

    @Override
    public void initalDd() {

        windsubstationtestingpoint2Service.deleteAll();

        List<Windsubstationtandardpoint> stpointls=windsubstationstandardpointService.findAllList();
        List<Windsubstation> wsubls=windsubstationService.findAllList();

        if(!wsubls.isEmpty() && !stpointls.isEmpty())
        {
            for(Windsubstation wsub:wsubls)
            {

                if(InitialRunner.wpmap.containsKey(wsub.getWindpowerstationid()))
                {
                    Windpowerstation wp=InitialRunner.wpmap.get(wsub.getWindpowerstationid());

                    List<Windsubstationtestingpoint2> points=new ArrayList<>();
                    for(int i=0;i<stpointls.size();i++)
                    {
                        StringBuilder sbtable =new StringBuilder();

                        Windsubstationtandardpoint stp=stpointls.get(i);
                        StringBuilder sb =new StringBuilder();
                        sb.append(wp.getPhoto());
                        String temp=wsub.getId().substring(0,wsub.getId().indexOf("_"));


                        if(stp.getCode().endsWith("CI"))
                        {
                            if(wp.getId().endsWith("FDC"))
                            {
                                sbtable.append(temp).append("FC");
                                sb.append(temp).append("F_").append("XX_XX_XX_XXX_");
                            }else   if(wp.getId().endsWith("GDC"))
                            {
                                sbtable.append(temp).append("GC");
                                sb.append(temp).append("G_").append("XX_XX_XX_XXX_");
                            }

                        }else
                        {
                            sbtable.append(temp).append("DQ");
                            if(wp.getId().endsWith("FDC"))
                            {

                                sb.append(temp).append("F_").append("DQ_P1_L1_001_");
                            }else   if(wp.getId().endsWith("GDC"))
                            {
                                sb.append(temp).append("G_").append("DQ_P1_L1_001_");
                            }
                        }



                        if(stp.getCode().equals(AI))
                        {
                            sb.append(AI);
                        }else if(stp.getCode().equals(DI))
                        {
                            sb.append(DI);
                        }else if(stp.getCode().equals(CI))
                        {
                            sb.append(CI);
                        }

                        sb.append(stp.getOrdernum());

                        if(stp.getCode().equals(CI))
                        {
                            sbtable.append("JSFW.");
                            sbtable.append(sb);
                        }else
                        {
                            sbtable.append(".").append(sb);
                        }
                        Windsubstationtestingpoint2 po=new Windsubstationtestingpoint2();
                        po.setCode(String.valueOf(sbtable));
                        sb =new StringBuilder();
                        sb.append(wsub.getAname()).append(stp.getName());
                        po.setName(String.valueOf(sb));
                        po.setModel(null);
                        po.setModelid(null);
                        po.setWindsubstationid(wsub.getId());
                        po.setUniformcode(stp.getUniformcode());
                        if(stp.getCode().equals(CI))
                        {
                            sb =new StringBuilder();
                            temp=wsub.getId().substring(0,wsub.getId().indexOf("_"));
                            sb.append(temp).append(".JSFW");
                            po.setRealtimeid(String.valueOf(sb));
                        }
                        points.add(po);
//                                windturbinetestingpointService.insertSelective(po);
                    }

                    windsubstationtestingpoint2Service.insertBatch(points);
                }

            }
        }



    }

    @Override
    public void initalFgl() {


        windPowerstationTestingPointService.deleteYc();

        List<Forecaststationtandardpoint>  stpointls=forecaststationtandardpointService.findAllList();


        /*********************************************风功率测点生成*****************************************************/

        for (Windpowerstation wp:InitialRunner.wpallls)
        {

            if(!stpointls.isEmpty())
            {

                List<WindPowerStationTestingPoint> points=new ArrayList<>();
                for(int i=0;i<stpointls.size();i++)
                {
                    StringBuilder sbtable =new StringBuilder();

                    Forecaststationtandardpoint stp=stpointls.get(i);
                    StringBuilder sb =new StringBuilder();
                    sb.append(wp.getPhoto());
                    String temp=wp.getId().substring(0,wp.getId().indexOf("_"));

                    if(wp.getId().endsWith("FDC"))
                    {
                        sbtable.append(temp).append("FGL");
                        sb.append(temp).append("F_").append("YC_XX_XX_XXX_");
                    }else   if(wp.getId().endsWith("GDC"))
                    {
                        sbtable.append(temp).append("GGL");
                        sb.append(temp).append("G_").append("YC_XX_XX_XXX_");
                    }



                   if(!stp.getCode().equals(CFT))
                    {
                        sb.append(stp.getCode());
                    }

                    sb.append(stp.getOrdernum());

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

                    WindPowerStationTestingPoint po=new WindPowerStationTestingPoint();
                    po.setCode(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());

                    if(stp.getCode().equals(CI))
                    {
                        sb =new StringBuilder();
                        temp=wp.getId().substring(0,wp.getId().indexOf("_"));


                        if(wp.getId().endsWith("FDC"))
                        {
                            sb.append(temp).append(".FGL");
                        }else   if(wp.getId().endsWith("GDC"))
                        {
                            sb.append(temp).append(".GGL");
                        }

                        po.setRealtimeid(String.valueOf(sb));
                    }
                    points.add(po);
                }
                windPowerstationTestingPointService.insertBatch(points);
            }
        }
    }


    public void initalFj()
    {

        windturbinetestingpointService.deleteAll();

        List<Windturbinestandardpoint>  stpointls=windturbinestandardpointService.findAllList();

            for (Windpowerstation wp:InitialRunner.wpls)
            {

                if(wp.getId().endsWith("FDC"))
                {
                    List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wp.getId());


                    if(!stpointls.isEmpty())
                    {
                        for(Windturbine wt:wtls)
                        {
                            List<Windturbinetestingpoint> points=new ArrayList<>();
                            for(int i=0;i<stpointls.size();i++)
                            {
                                StringBuilder sbtable =new StringBuilder();

                                Windturbinestandardpoint stp=stpointls.get(i);
                                StringBuilder sb =new StringBuilder();
                                sb.append(wp.getPhoto());
                                String temp=wp.getId().substring(0,wp.getId().indexOf("_"));



                                if(stp.getCode().equals(CI))
                                {
                                    sbtable.append(temp).append("JS");
                                    sb.append(temp).append("F_").append("JS_");
                                }else
                                {
                                    sbtable.append(temp).append("FJ");
                                    sb.append(temp).append("F_").append("FJ_");
                                }
                                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("_")+1);

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


                                sb.append(stp.getCode());

                                sb.append(stp.getOrdernum());

                                if(stp.getCode().equals(CI))
                                {
                                    sbtable.append("JSFW.");
                                    sbtable.append(sb);
                                }else
                                {
                                    sbtable.append(".").append(sb);
                                }
                                Windturbinetestingpoint po=new Windturbinetestingpoint();
                                po.setId(String.valueOf(sbtable));
                                po.setCode(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());

                                if(stp.getCode().equals(CI))
                                {
                                    sb =new StringBuilder();
                                    temp=wp.getId().substring(0,wp.getId().indexOf("_"));
                                    sb.append(temp).append(".JSFW");
                                    po.setRealtimeid(String.valueOf(sb));
                                }
                                points.add(po);
//                                windturbinetestingpointService.insertSelective(po);
                            }

                            windturbinetestingpointService.insertBatch(points);
                        }
                    }

                }
            }


    }

}
