package com.gyee.frame.service.health;

import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.mapper.auto.AlarmsnapMapper;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.DNAVal;
import com.gyee.frame.model.custom.HealthmodelrecordsVo;
import com.gyee.frame.model.custom.TotalNumVo;
import com.gyee.frame.model.custom.TotalNumsVo;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LossPowerRateHealthService {

    @Resource
    private AlarmsnapMapper alarmsnapMapper;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();
    private final int digit = 2;
    
    public List<HealthmodelrecordsVo> windTurbineHealthList(String wpId) throws Exception {

        List<String> codels = new ArrayList<String>();
        codels.add(Constant.FJJKZZ);
        codels.add(Constant.CLXJKZT);
        codels.add(Constant.FDJJKZT);
        codels.add(Constant.BJJKZT);
        codels.add(Constant.ZKJKZT);

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

     
        Date beginDate = cal.getTime();



        Map<String, List<String>> ahmap =getAlarmhistoryLMap();

        Map<String, String> altypemap = findAlertrulefailuresubMap();

        List<HealthmodelrecordsVo> vos = new ArrayList<HealthmodelrecordsVo>();
        int id = 0;
        for (Windturbine wt : InitialRunner.wp_wtmap.get(wpId)) {
            HealthmodelrecordsVo vo = new HealthmodelrecordsVo();
            vo.setId(id);
            vo.setRecodedate(beginDate);
            vo.setWpid(wt.getWindpowerstationid());
            vo.setWpname(InitialRunner.wpmap.get(wt.getWindpowerstationid()).getName());
            vo.setWtid(wt.getId());
            vo.setWtname(wt.getId());
            vo.setModel(wt.getModelid());

            String[] points = new String[codels.size()];

            for (int i = 0; i < codels.size(); i++) {

                WindTurbineTestingPointAi ai =windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(),codels.get(i));
                points[i] = ai.getId();
            }

            // 获得风机健康状态结果数组
            DNAVal[] jkvalues = realApiUtil.getRealtimeTagValues(points);

            double score = jkvalues[0].DValue;
            double clxzt = jkvalues[1].DValue;
            double fdjzt = jkvalues[2].DValue;
            double bjzt = jkvalues[3].DValue;
            double zkzt = jkvalues[4].DValue;

            vo.setScore(StringUtils.round(score, 2));

            vo.setClx(getStateValue(clxzt));
            vo.setFdj(getStateValue(fdjzt));
            vo.setBj(getStateValue(bjzt));
            vo.setZk(getStateValue(zkzt));

            vo.setBpq("优");
            vo.setPh("优");
            vo.setJc("优");
            vo.setZz("优");

            if(ahmap.containsKey(wt.getId()))
            {
                List<String> ahtypels=ahmap.get(wt.getId());

                if(!ahtypels.isEmpty())
                {
                    for(String type:ahtypels)
                    {
                        if(altypemap.containsKey(type))
                        {
                            String typetext=altypemap.get(type);

                            switch (typetext) {
                                case "CLX":
                                    vo.setClx("差");
                                    break;
                                case "FDJ":
                                    vo.setFdj("差");
                                    break;
                                case "BJ":
                                    vo.setBj("差");
                                    break;
                                case "ZK":
                                    vo.setZk("差");
                                    break;
                                case "PH":
                                    vo.setPh("差");
                                    break;
                                case "JC":
                                    vo.setJc("差");
                                    break;
                                case "ZZ":
                                    vo.setZz("差");
                                    break;
                                default:
                                    break;
                            }
                        }

                    }
                }

            }

            Integer[] futureHealth = new Integer[4];
            Random random = new Random();
            for (int i = 0; i < 4; i++) {
                futureHealth[i] = random.nextInt(10) + 1;
            }
            vo.setFutureHealth(futureHealth);

            int max = 100;
            int min = 80;

            double zxd = random.nextInt(max) % (max - min + 1) + min;
            vo.setZxd(StringUtils.round(zxd, 2));
            vos.add(vo);
            id++;
        }



        return vos;
    }

    private String getStateValue(double value) {
        String name;
        if (value == 1 || value == 2) {
            name = "优";
        } else if (value == 3) {
            name = "良";
        } else {
            name = "差";
        }
        return name;
    }

    private Map<String, List<String>> getAlarmhistoryLMap() {

        SimpleDateFormat sdf_no_underline = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

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

        Calendar cal = Calendar.getInstance();
        Date endDate = cal.getTime();
        cal.add(Calendar.MINUTE, -3);
        Date beginDate = cal.getTime();

        for (Windpowerstation wp : InitialRunner.wpls) {
            if (wp.getId().endsWith("_FDC")) {

                StringBuilder tablename = new StringBuilder();
                tablename.append("alarmhistory_");
                tablename.append(wp.getId().split("_")[0]);
                tablename.append("_");
                tablename.append(sdf_no_underline.format(endDate));



                List<TotalNumsVo> list = alarmsnapMapper.getAlarmhistoryList(String.valueOf(tablename),sdf.format(beginDate),sdf.format(endDate));

                if (!list.isEmpty()) {

                    for (TotalNumsVo vo : list) {

                        if(map.containsKey(vo.getWindturbineid()))
                        {
                            List<String> ls=map.get(vo.getWindturbineid());
                            ls.add(String.valueOf(vo.getAlertvalue()));
                            map.put(vo.getWindturbineid(), ls);
                        }else
                        {
                            List<String> ls=new ArrayList<>();
                            ls.add(String.valueOf(vo.getAlertvalue()));
                            map.put(vo.getWindturbineid(), ls);
                        }

                    }
                }
            }
        }

        return map;
    }

    private Map<String, String> findAlertrulefailuresubMap() {

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

        List<TotalNumVo> list =alarmsnapMapper.findAlertrulefailuresubMap();

        if (!list.isEmpty()) {
            for (TotalNumVo vo : list) {

                map.put(String.valueOf(vo.getNumbers()),vo.getTypeId());
            }
        }

        return map;
    }

}


