package com.gyee.frame.service.app.windpowerstationdetail;

import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.WindPowerstationTestingPoint;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.enumeration.Messagetype;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Windspeedpowermodule extends SocketToolService {
    @Autowired
    private InitialRunner initialRunner;
    private static int decimal_digit = 0;

    public void index_windSpeed_power_target_detail() throws Exception{
        Map<String, Map<String, WindPowerstationTestingPoint>> stationPointmap = InitialRunner.stationPointmap;
        List<Windpowerstation> wpls = new ArrayList<>();
        for(Windpowerstation wp:InitialRunner.wpls){
            wpls.add(wp);
        }
        Windpowerstation windpowerstationAll = new Windpowerstation();
        windpowerstationAll.setId("0");
        wpls.add(windpowerstationAll);
        Map<String,Map<String,Double>> windpowerstationpointDataMap = new HashMap();
        IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();

        for(Windpowerstation windpowerstation:wpls){
            Map<String,Double> pointDataMap = new HashMap();
            double pjfs = 0;
            double pjfsMin = 0;
            double pjfsMax = 0;
            double ycfs = 0;
            double ycfsMin = 0;
            double ycfsMax = 0;
            double sjgl = 0;
            double sjglMin = 0;
            double sjglMax = 0;
            double llgl = 0;
            double llglMax = 0;
            double llglMin = 0;

            try {
                //平均风速
                PointData rfdl_real = mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_PJFS).getCode());
                pjfs = new BigDecimal(rfdl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

            //平均风速min
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        if(pjfs==0){
                            pjfsMin=pjfs;
                        }else{
                            pjfsMin = pjfs - 2;
                        }
                    }
                }else{
                    if(pjfs==0){
                        pjfsMin=pjfs;
                    }else{
                        pjfsMin = pjfs - 2;
                    }
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //平均风速max
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        pjfsMax = pjfs + 2;
                    }
                }else{
                    pjfsMax = pjfs + 2;
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //预测风速
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        ycfs = pjfs + 2;
                    }
                }else{
                    ycfs = pjfs + 2;
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //预测风速min
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        if(ycfs==0){
                            ycfsMin = ycfs;
                        }else{
                            ycfsMin = ycfs - 2;
                        }
                    }
                }else{
                    if(ycfs==0){
                        ycfsMin = ycfs;
                    }else{
                        ycfsMin = ycfs - 2;
                    }
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //预测风速max
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        ycfsMax = ycfs + 2;
                    }
                }else{
                    ycfsMax = ycfs + 2;
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            try{
                //实际功率
                PointData swdl_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_SJGL).getCode());
                sjgl = new BigDecimal(swdl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

            //实际功率min
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        if(sjgl==0){
                            sjglMin = sjgl;
                        }else{
                            sjglMin = sjgl - 2;
                        }
                    }
                }else{
                    if(sjgl==0){
                        sjglMin = sjgl;
                    }else{
                        sjglMin = sjgl - 2;
                    }
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //实际功率max
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        sjglMax = sjgl + 2;
                    }
                }else{
                    sjglMax = sjgl + 2;
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            try{
                //理论功率
                PointData swdl_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_ZLLGL).getCode());
                llgl = new BigDecimal(swdl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

            //理论功率min
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        if(llgl==0){
                            llglMin = llgl;
                        }else{
                            llglMin = llgl - 2;
                        }
                    }
                }else{
                    if(llgl==0){
                        llglMin = llgl;
                    }else{
                        llglMin = llgl - 2;
                    }
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //理论功率max
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        llglMax = llgl + 2;
                    }
                }else{
                    llglMax = llgl + 2;
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }


            pointDataMap.put("pjfs",pjfs);
            pointDataMap.put("pjfsMin",pjfsMin);
            pointDataMap.put("pjfsMax",pjfsMax);
            pointDataMap.put("ycfs",ycfs);
            pointDataMap.put("ycfsMin",ycfsMin);
            pointDataMap.put("ycfsMax",ycfsMax);
            pointDataMap.put("sjgl",sjgl);
            pointDataMap.put("sjglMin",sjglMin);
            pointDataMap.put("sjglMax",sjglMax);
            pointDataMap.put("llgl",llgl);
            pointDataMap.put("llglMin",llglMin);
            pointDataMap.put("llglMax",llglMax);
            windpowerstationpointDataMap.put(windpowerstation.getId(),pointDataMap);
        }
        this.pushToWeb("pageNumber_7","functionNumber_3",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }
}
