package com.gyee.wisdom.xfai.net3.service;

import com.alibaba.fastjson.JSON;
import com.gyee.wisdom.xfai.net3.entity.*;
import com.gyee.wisdom.xfai.net3.mapper.MtMapper;
import com.gyee.wisdom.xfai.net3.mapper.WindPowerInfoDayMapper;
import com.gyee.wisdom.xfai.net3.mapper.WindTablesMapper;
import com.gyee.wisdom.xfai.net3.model.XFCommand;
import com.gyee.wisdom.xfai.net3.transport.DataAdapterClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class XFCommandService {
    @Autowired
    CacheService cacheService;

    @Autowired
    DataAdapterClient adapterClient;

    @Autowired
    CaculateTotalCapacityService capacityService;

    @Autowired
    WindPowerInfoDayMapper powerInfoDayMapper;

    @Autowired
    WeatherService weatherService;

    @Autowired
    WindTablesMapper windTablesMapper;

    @Autowired
    MtMapper mtMapper;

    public String phraseXFCommand(XFCommand xfCommand){
        String result;
        switch (xfCommand.getBusiness()) {
            case "RFDL":
                result = getRFDL(xfCommand);
                break;
            case "YFDL":
                result = getYFDL(xfCommand);
                break;
            case "NFDL":
                result = getNFDL(xfCommand);
                break;
            case "NFDLWC":
                result = getNFDLWC(xfCommand);
                break;
            case "FWJSL":
                result = getFWJSL(xfCommand);
                break;
            case "XQJSL":
                result = getXQJSL(xfCommand);
                break;
            case "ZTZHL":
                result = getZTZHL(xfCommand);
                break;
            case "YXQK":
                result = getYXQK(xfCommand);
                break;
            case "TQ":
                result = getTQ(xfCommand);
                break;
            case "FZY":
                result = getFZY(xfCommand);
                break;
            case "GZY":
                result = getGZY(xfCommand);
                break;
            case "SBKLYL":
                result = getSBKLYL(xfCommand);
                break;
            case "FNLYL":
                result = getFNLYL(xfCommand);
                break;
            case "MTBF":
                result = getMTBF(xfCommand);
                break;
            case "MTTR":
                result = getMTTR(xfCommand);
                break;
            case "YCGL":
                result = getMTTR(xfCommand);
                break;
            default:
                result = "";
        }
        return result;
    }
    //风资源数据获取
    private String getFZY(XFCommand xfCommand){
        String name = cacheService.getOrgName(xfCommand.getOrg());
        Object wind = null;
        WeatherReal weatherInfo = null;
        try {
            String weatherStr=weatherService.redisget(weatherService.getStation(xfCommand.getOrg()));
            if(weatherStr==null) return "";
            weatherInfo = JSON.parseObject(weatherStr, WeatherReal.class);
            wind = adapterClient.getLatest(weatherService.getWindSpeeds(xfCommand.getOrg()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String formatWind;
        if(wind==null){
            formatWind=weatherInfo.getWind().getSpeed()+"";
        }else {
            String s1 = String.valueOf(wind);
            formatWind = s1.substring(0, s1.indexOf(".") + 2);
        }
        if (xfCommand.isToday()) {
            return "今天"+name+"天气"+weatherService.getWeather(weatherInfo.getWeather().get(0).getIcon())+
                    "，当前风速"+formatWind+"m/s,"+weatherService.getWindDirection(weatherInfo.getWind().getDeg())+
                    "风,湿度"+weatherInfo.getMain().getHumidity()+"%。";
        } else {
            String result ="今天"+name+"天气"+weatherService.getWeather(weatherInfo.getWeather().get(0).getIcon())+
                    "，当前风速"+formatWind+"m/s,"+weatherService.getWindDirection(weatherInfo.getWind().getDeg())+
                    "风,湿度"+weatherInfo.getMain().getHumidity()+"%。";

            String weatherStr7=weatherService.redisget(weatherService.getStation7(xfCommand.getOrg()));
            if(weatherStr7==null) return "";
            WeatherReal7 weatherInfo7 = JSON.parseObject(weatherStr7, WeatherReal7.class);

            int ttime=getApartDate(xfCommand.getCmddayint(),xfCommand.getRi());
            if(ttime>8) {
                ttime=8;
            }else if(ttime==2){
                result=name;//问明天信息的时候不报今天的信息
            }
            for(int i=1;i<ttime;i++){
                result+=getForecastDay(i)+"天气"+
                        weatherService.getWeather(weatherInfo7.getDaily().get(i).getWeather().get(0).getIcon())+"，平均风速"+
                        weatherInfo7.getDaily().get(i).getWind_speed()+"m/s,"+
                        weatherService.getWindDirection(weatherInfo7.getCurrent().getWind_deg())+
                        "风,湿度"+weatherInfo7.getDaily().get(i).getHumidity()+"%。";
            }
            return result;
        }
    }
    //光资源数据获取
    private String getGZY(XFCommand xfCommand){
        String name = cacheService.getOrgName(xfCommand.getOrg());
        Object wind = null;
        WeatherReal weatherInfo = null;
        try {
            String weatherStr=weatherService.redisget(weatherService.getStation(xfCommand.getOrg()));
            if(weatherStr==null) return "";
            weatherInfo = JSON.parseObject(weatherStr, WeatherReal.class);
            wind = adapterClient.getLatest(weatherService.getIrradiance(xfCommand.getOrg()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String s1 = String.valueOf(wind);
        String formatWind = s1.substring(0, s1.indexOf(".") + 2);
        if(wind==null||s1.equals("")){
            return "今天"+name+"天气"+weatherService.getWeather(weatherInfo.getWeather().get(0).getIcon())+
                    ","+name+"暂无光照数据,温度"+weatherInfo.getMain().getTemp()+
                    "°C,湿度"+weatherInfo.getMain().getHumidity()+"%。";
        }else {
            return "今天"+name+"天气"+weatherService.getWeather(weatherInfo.getWeather().get(0).getIcon())+
                    ",光照"+formatWind+"瓦每平方米,温度"+weatherInfo.getMain().getTemp()+
                    "°C,湿度"+weatherInfo.getMain().getHumidity()+"%。";
        }
    }
    //天气数据获取
    private String getTQ(XFCommand xfCommand) {
        String name = cacheService.getOrgName(xfCommand.getOrg());
        String weatherStr=weatherService.redisget(weatherService.getStation(xfCommand.getOrg()));
        if(weatherStr==null) return "";
        WeatherReal weatherInfo = JSON.parseObject(weatherStr, WeatherReal.class);
        if (xfCommand.isToday()) {
            return "今天"+name+"天气"+weatherService.getWeather(weatherInfo.getWeather().get(0).getIcon())+
                    "，平均风速"+weatherInfo.getWind().getSpeed()+""+"m/s,当前温度"+weatherInfo.getMain().getTemp()+
                    "°C,湿度"+weatherInfo.getMain().getHumidity()+"%。";
        } else {
            String result ="今天"+name+"天气"+weatherService.getWeather(weatherInfo.getWeather().get(0).getIcon())+
                    "，平均风速"+weatherInfo.getWind().getSpeed()+""+"m/s,当前温度"+weatherInfo.getMain().getTemp()+
                    "°C,湿度"+weatherInfo.getMain().getHumidity()+"%。";

            String weatherStr7=weatherService.redisget(weatherService.getStation7(xfCommand.getOrg()));
            if(weatherStr7==null) return "";
            WeatherReal7 weatherInfo7 = JSON.parseObject(weatherStr7, WeatherReal7.class);

            int ttime=getApartDate(xfCommand.getCmddayint(),xfCommand.getRi());
            if(ttime>8) {
                ttime=8;
            }else if(ttime==2){
                result=name;//问明天信息的时候不报今天的信息
            }
            for(int i=1;i<ttime;i++){
                result=result+getForecastDay(i)+"天气"+
                        weatherService.getWeather(weatherInfo7.getDaily().get(i).getWeather().get(0).getIcon())+"，平均风速"+
                        weatherInfo7.getDaily().get(i).getWind_speed()+"m/s,最高温度"+
                        weatherInfo7.getDaily().get(i).getTemp().getMax()+"°C,最低温度"+
                        weatherInfo7.getDaily().get(i).getTemp().getMin()+"°C,湿度"+weatherInfo7.getDaily().get(i).getHumidity()+"%。";
            }
            return result;
        }
    }
    //计算传入日期与当天日期间隔几天
    private int getApartDate(int m,int n){
        if(m-n>0){
            return m-n+1;
        }else {
            SimpleDateFormat apartFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar=Calendar.getInstance();
            Date d1=null;
            Date d2=null;
            try {
                String mm=""+calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+2)+"-"+m;
                String nn=""+calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+n;
                d1=apartFormat.parse(mm);
                d2=apartFormat.parse(nn);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long diff = d1.getTime() - d2.getTime();
            int diffDays = (int)diff / (24 * 60 * 60 * 1000);
            return diffDays+1;
        }
    }
    //计算日期，因为跨月的话日期不好确定
    private String getForecastDay(int k){
        switch (k){
            case 1:
                return "明天";
            case 2:
                return "后天";
            case 3:
                return "大后天";
            default:
                Calendar calendar=Calendar.getInstance();
                int huancun=calendar.get(Calendar.MONTH)+1;
                calendar.add(Calendar.DAY_OF_MONTH,k);
                int xianzai=calendar.get(Calendar.MONTH)+1;
                if(huancun==xianzai){
                    return calendar.get(Calendar.DAY_OF_MONTH)+"日";
                }else {
                    return xianzai+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日";
                }
        }
    }
    //获取年发电量
    private String getNFDL(XFCommand xfCommand) {
        try {
            if (xfCommand.isToday()){
                String tagId = cacheService.getNFDLTagId(xfCommand.getOrg());
                Object obj = adapterClient.getLatest(tagId);
                double nfdl=Double.parseDouble(obj.toString());
                BigDecimal bd = new BigDecimal(nfdl);
                nfdl = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                String orgName = cacheService.getOrgName(xfCommand.getOrg());
                if (xfCommand.getOrg().equals("GS")){
                    Object wind = adapterClient.getLatest("JSFW.NX_GD_FDC_XX_XX_XXX_XXX_CI0037");
                    double nfdlfc=Double.parseDouble(wind.toString());
                    BigDecimal bd1 = new BigDecimal(nfdlfc);
                    nfdlfc = bd1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    Object photo = adapterClient.getLatest("JSFW.NX_GD_GDC_XX_XX_XXX_XXX_CI0037");
                    double nfdlgf=Double.parseDouble(photo.toString());
                    BigDecimal bd2 = new BigDecimal(nfdlgf);
                    nfdlgf = bd2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    return "公司今年的发电量是" + nfdl + "万千瓦时，风场发电" + nfdlfc + "万千瓦时，光伏发电" + nfdlgf +"万千瓦时";
                }
                String result = orgName+"今年的发电量是"+nfdl+"万千瓦时。";
                log.info("获取年发电量返参返参"+result);
                return result;
            } else {
                //取历史发电量
                String hisCapacity;
                String orgId = cacheService.getOrgId(xfCommand.getOrg());
                String name = cacheService.getOrgName(xfCommand.getOrg());
                String format = xfCommand.getYear()+"-"+xfCommand.getMonth()+"-"+xfCommand.getDay();
                WindPowerInfoDay windPowerInfoDay = powerInfoDayMapper.selectByOrgAndDate(orgId, format);
                if (windPowerInfoDay == null){
                    return "没有查到相关数据";
                }
                double nfdl = windPowerInfoDay.getYeargc2();
                String[] split = format.split("-");
                if (xfCommand.getOrg().equals("GS")){
                    WindPowerInfoDay windPowerInfoDay1 = powerInfoDayMapper.selectByOrgAndDate("-1", format);
                    WindPowerInfoDay windPowerInfoDay2 = powerInfoDayMapper.selectByOrgAndDate("-2", format);
                    double nfdlfc = windPowerInfoDay1.getYeargc2();
                    double nfdlgf = windPowerInfoDay2.getYeargc2();
                    //hisCapacity = name + split[0] +"年的发电量是"+ yearCapacity+ "万千瓦时，风场发电量" + String.format("%.2f",yearCapacity * percent[0])+"万千瓦时，光伏发电"+String.format("%.2f",yearCapacity*percent[1])+"万千瓦时";
                    hisCapacity = name + split[0] +"年的发电量是"+ nfdl+ "万千瓦时，风场发电" + nfdlfc+"万千瓦时，光伏发电"+nfdlgf+"万千瓦时";
                } else{
                    hisCapacity = name + split[0] +"年的发电量是"+ nfdl+ "万千瓦时";
                }
                log.info("获取历史日发电量返参" + hisCapacity);
                return hisCapacity;
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return "没有查到相关数据";
    }
    //获取月发电量
    private String getRFDL(XFCommand xfCommand) {
        try {
            if (xfCommand.isToday()) {
                //取实时发电量
                String tagId = cacheService.getRFDLTagId(xfCommand.getOrg());
                Object object = adapterClient.getLatest(tagId);
                String s = String.valueOf(object);
                String value = s.substring(0, s.indexOf(".") + 2);
                String orgName = cacheService.getOrgName(xfCommand.getOrg());
                if (xfCommand.getOrg().equals("GS")){
                    Object wind = adapterClient.getLatest("JSFW.NX_GD_FDC_XX_XX_XXX_XXX_CI0088");
                    String s1 = String.valueOf(wind);
                    String formatWind = s1.substring(0, s1.indexOf(".") + 2);
                    Object photo = adapterClient.getLatest("JSFW.NX_GD_GDC_XX_XX_XXX_XXX_CI0088");
                    String s2 = String.valueOf(photo);
                    String formatPhoto = s2.substring(0, s2.indexOf(".") + 2);
                    return "公司今日的发电量是" + value + "万千瓦时，风场发电" + formatWind + "万千瓦时，光伏发电" + formatPhoto +"万千瓦时";
                }
                String result = String.format("%s今日的发电量是%s万千瓦时",orgName, value);
                log.info("获取实时日发电量返参"+result);
                return result;
            } else {
                //取历史发电量
                String hisCapacity = null;
                String orgId = cacheService.getOrgId(xfCommand.getOrg());
                String name = cacheService.getOrgName(xfCommand.getOrg());
                String format = xfCommand.getYear()+"-"+xfCommand.getMonth()+"-"+xfCommand.getDay();
                WindPowerInfoDay windPowerInfoDay = powerInfoDayMapper.selectByOrgAndDate(orgId, format);
                if(windPowerInfoDay == null){
                    return "没有查到相关数据";
                }
                double dayCapacity = windPowerInfoDay.getGeneratingcapacity2();
                String[] split = format.split("-");
                if (xfCommand.getOrg().equals("GS")){
                    WindPowerInfoDay windPowerInfoDay1 = powerInfoDayMapper.selectByOrgAndDate("-1", format);
                    WindPowerInfoDay windPowerInfoDay2 = powerInfoDayMapper.selectByOrgAndDate("-2", format);
                    double fdr = windPowerInfoDay1.getGeneratingcapacity2();
                    double gfr = windPowerInfoDay2.getGeneratingcapacity2();
                    //hisCapacity = name + split[0] +"年"+ split[1] +"月"+ split[2] +"日的发电量是"+ dayCapacity+ "万千瓦时，风场发电" +
                    //String.format("%.2f",dayCapacity * percent[0])+"万千瓦时，光伏发电"+String.format("%.2f",dayCapacity * percent[1])+"万千瓦时";
                    hisCapacity = name + split[0] +"年"+ split[1] +"月"+ split[2] +"日的发电量是"+ dayCapacity+ "万千瓦时，风场发电" +
                            fdr+"万千瓦时，光伏发电"+gfr+"万千瓦时";
                } else {
                    hisCapacity = name + split[0] +"年"+ split[1] +"月"+ split[2] +"日的发电量是"+ dayCapacity+ "万千瓦时";
                }
                log.info("获取历史日发电量返参" + hisCapacity);
                return hisCapacity;
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return "";
    }
    //获取日发电量
    private String getYFDL(XFCommand xfCommand){
        try {
            if (xfCommand.isToday()){
                //获取本月截至当前时刻的发电量
                String tagId = cacheService.getYFDLTagId(xfCommand.getOrg());
                String orgName = cacheService.getOrgName(xfCommand.getOrg());
                String s = String.valueOf(adapterClient.getLatest(tagId));
                String value = s.substring(0, s.indexOf(".") + 2);
                if (xfCommand.getOrg().equals("GS")){
                    Object wind = adapterClient.getLatest("JSFW.NX_GD_FDC_XX_XX_XXX_XXX_CI0146");
                    String s1 = String.valueOf(wind);
                    String formatWind = s1.substring(0, s1.indexOf(".") + 2);
                    Object photo = adapterClient.getLatest("JSFW.NX_GD_GDC_XX_XX_XXX_XXX_CI0146");
                    String s2 = String.valueOf(photo);
                    String formatPhoto = s2.substring(0, s2.indexOf(".") + 2);
                    return "本月公司的发电量是" + value + "万千瓦时，风场发电" + formatWind + "万千瓦时，光伏发电" + formatPhoto +"万千瓦时";
                }
                String result = String.format("%s本月的发电量是%s万千瓦时",orgName, value);
                log.info("获取本月发电量返参"+result);
                return result;
            } else{
                String hisCapacity = null;
                String orgId = cacheService.getOrgId(xfCommand.getOrg());
                String name = cacheService.getOrgName(xfCommand.getOrg());
                String format = xfCommand.getYear()+"-"+xfCommand.getMonth()+"-"+xfCommand.getDay();
                WindPowerInfoDay windPowerInfoDay = powerInfoDayMapper.selectByOrgAndDate(orgId, format);
                if (windPowerInfoDay == null){
                    return "没有查询到该月信息";
                }
                double monthlyCapacity = windPowerInfoDay.getMonthgc2();
                String[] split = format.split("-");
                int month = Integer.parseInt(split[1]);
                int year = Integer.parseInt(split[0]);
                if (month == 0){
                    month = 12;
                    year = Integer.parseInt(split[0]) - 1;
                }
                if(xfCommand.getOrg().equals("GS")){
                    WindPowerInfoDay windPowerInfoDay1 = powerInfoDayMapper.selectByOrgAndDate("-1", format);
                    WindPowerInfoDay windPowerInfoDay2 = powerInfoDayMapper.selectByOrgAndDate("-2", format);
                    double fdy = windPowerInfoDay1.getMonthgc2();
                    double gfy = windPowerInfoDay2.getMonthgc2();
                    //hisCapacity = name + year +"年"+  month +"月份的发电量是"+ monthlyCapacity+ "万千瓦时，风场发电"+String.format("%.2f",monthlyCapacity * percent[0])+
                    //"万千瓦时，光伏发电" + String.format("%.2f",monthlyCapacity * percent[1])+"万千瓦时";
                    hisCapacity = name + year +"年"+  month +"月份的发电量是"+ monthlyCapacity+ "万千瓦时，风场发电"+fdy+
                            "万千瓦时，光伏发电" + gfy+"万千瓦时";
                } else {
                    hisCapacity = name + year +"年"+  month +"月份的发电量是"+ monthlyCapacity+ "万千瓦时";
                }
                log.info("获取历史月发电量返参" + hisCapacity);
                return hisCapacity;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    //获取年发电量完成
    private String getNFDLWC(XFCommand xfCommand) {
        double totalPlan = 0.0;
        double windPlan = 0.0;
        double photovoltaic = 0.0;
        String yearPlanSt=null;
        String hqnfdl=getNFDL(xfCommand);
        if(hqnfdl.equals("没有查到相关数据")) return "没有查到相关数据";
        String nsjfdl=hqnfdl.substring(hqnfdl.indexOf("发电量是")+4,hqnfdl.indexOf("万千瓦时"));
        if (xfCommand.getOrg().equals("GS")){
            List<String> yearPlan = capacityService.getYearPlan(xfCommand);
            for (String year:yearPlan){
                totalPlan += Double.parseDouble(year);
            }
            //年实际发电量
            double finishRate = Double.parseDouble(nsjfdl)/totalPlan * 100.00;
            //风场年发电计划
            List<String> windCapacity = capacityService.getWindPower(xfCommand);
            for (String win : windCapacity){
                windPlan += Double.parseDouble(win);
            }
            //风场实际发电量
            int intfc=hqnfdl.indexOf("风场发电")+4;
            String fcsjfdl=hqnfdl.substring(intfc,hqnfdl.indexOf("万千瓦时",intfc));
            double windRate = Double.parseDouble(fcsjfdl)/windPlan * 100.0;
            //光伏计划发电量
            List<String> photoList = capacityService.getPhotovoltaic(xfCommand);
            for (String photo:photoList){
                photovoltaic += Double.parseDouble(photo);
            }
            //光伏实际发电量
            int intgf=hqnfdl.indexOf("光伏发电")+4;
            String gfsjfdl=hqnfdl.substring(intgf,hqnfdl.indexOf("万千瓦时",intgf));
            double photoRate = Double.parseDouble(gfsjfdl)/photovoltaic * 100.0;
            return "公司"+ xfCommand.getYear() +"年度计划发电量"+ totalPlan +"万千瓦时，已完成发电量"+ nsjfdl +"万千瓦时，完成率是"+ String.format("%.2f",finishRate) +"%，" +
                    "风场年计划发电量"+ windPlan +"万千瓦时，实际发电量"+ fcsjfdl +"万千瓦时，完成率是"+String.format("%.2f", windRate)+"%,光伏年计划发电量"
                    +photovoltaic+"万千瓦时，实际发电量"+gfsjfdl+"万千瓦时，完成率是"+String.format("%.2f", photoRate)+"%";
        }else {
            String orgName = cacheService.getOrgName(xfCommand.getOrg());
            switch (xfCommand.getOrg()){
                case "QS":
                    yearPlanSt = capacityService.getQSPlan(xfCommand);
                    break;
                case "NSS":
                    yearPlanSt = capacityService.getNSSPlan(xfCommand);
                    break;
                case "MHS":
                    yearPlanSt = capacityService.getMHSPlan(xfCommand);
                    break;
                case "XS":
                    yearPlanSt = capacityService.getXSPlan(xfCommand);
                    break;
                case "SBQ":
                    yearPlanSt = capacityService.getSBQPlan(xfCommand);
                    break;
                case "DWK":
                    yearPlanSt = capacityService.getDWKPlan(xfCommand);
                    break;
                case "PL":
                    yearPlanSt = capacityService.getPLPlan(xfCommand);
                    break;
                case "XH":
                    yearPlanSt = capacityService.getXHPlan(xfCommand);
                    break;
            }
            totalPlan = Double.parseDouble(yearPlanSt);
            //年实际发电量
            double finishRate = Double.parseDouble(nsjfdl)/totalPlan * 100.00;
            return orgName+ xfCommand.getYear() +"年度计划发电量"+ totalPlan +"万千瓦时，已完成发电量"+ nsjfdl +"万千瓦时，完成率是"+ String.format("%.2f",finishRate) +"%。";
        }
    }
    //查看运行情况
    public String getYXQK(XFCommand xfCommand){
        String code = "";
        HashMap<String,String> uCode=new HashMap<>();
        if (xfCommand.getOrg().equals("GS")){
            xfCommand.setOrg("FD");
        }
        try {
            List<WindTables> windtable = windTablesMapper.selectByUD(cacheService.getOrgId(xfCommand.getOrg()));
            for(WindTables v:windtable){
                uCode.put(v.getUniformCode(),v.getCode());
                code+=v.getCode()+",";
            }
            HashMap<String,Integer> objmap=adapterClient.getRun(code);
            //待机台数DJTS
            int djts=objmap.get(uCode.get("DJTS"));
            //运行台数YXTS
            int yxts=objmap.get(uCode.get("YXTS"));
            //维护台数WHTJ
            int whts=objmap.get(uCode.get("WHTJ"));
            //故障台数GZTJ
            int gzts=objmap.get(uCode.get("GZTJ"));
            //限电台数XDTS
            int xdts=objmap.get(uCode.get("XDTS"));
            //离线台数LXNUM
            int lxts=objmap.get(uCode.get("LXNUM"));
            //总共的台数
            int zgts = yxts+djts+whts+gzts+xdts+lxts;
            if (xfCommand.getOrg().equals("FD")){
                //全省离线台数取不到，只能取风场离线台数
                String obj = adapterClient.getLatest("JSFW.NX_GD_XXX_XX_XX_XXX_XXX_CI0035").toString();
                lxts=Integer.parseInt(obj.substring(0,obj.indexOf(".")));
                zgts = yxts+djts+whts+gzts+xdts+lxts;
                return "公司共投运" + zgts + "台风机，其中故障"+gzts+"台,维护"+whts+"台,离线"+lxts+"台,限电"+xdts+"台";
            } else{
                String name = cacheService.getOrgName(xfCommand.getOrg());
                String kk="风机";
                if(xfCommand.getOrg().equals("DWK")||xfCommand.getOrg().equals("PL")||xfCommand.getOrg().equals("MCH")||xfCommand.getOrg().equals("XH")){
                    kk="光伏机";
                }
                return name + "共投运" + zgts + "台"+kk+"，其中故障"+gzts+"台,维护"+whts+"台,离线"+lxts+"台,限电"+xdts+"台";
            }
        } catch (Exception e){
            e.getMessage();
        }
        return "该项服务暂无数据";
    }
    //公司状态转换率
    private String getZTZHL(XFCommand xfCommand) {
        HttpClient httpClient=new HttpClient();
        String result=httpClient.loginBaidu();
        int res=result.indexOf("ztzhjsl")+9;
        return "公司本年度状态转换率为"+result.substring(res,result.indexOf(",",res))+"%。";
    }
    //公司消缺及时率 capacityService.getXQRate
    private String getXQJSL(XFCommand xfCommand) {
        HttpClient httpClient=new HttpClient();
        String result=httpClient.loginBaidu();
        int res=result.indexOf("xqjsl")+7;
        return "公司本年度消缺及时率为"+result.substring(res,result.indexOf(",",res))+"%。";
    }
    //公司复位及时率 capacityService.getFWJSRate
    private String getFWJSL(XFCommand xfCommand) {
        HttpClient httpClient=new HttpClient();
        String result=httpClient.loginBaidu();
        int res=result.indexOf("fwjsl")+7;
        return "公司本年度复位及时率为"+result.substring(res,result.indexOf(",",res))+"%。";
    }
    //公司风能利用率
    private String getFNLYL(XFCommand xfCommand) {
        HttpClient httpClient=new HttpClient();
        String result=httpClient.loginBaidu();
        int res=result.indexOf("fnlyl")+7;
        return "公司本年度风能利用率为"+result.substring(res,result.indexOf(",",res))+"%。";
    }
    //公司设备可利用率
    private String getSBKLYL(XFCommand xfCommand) {
        HttpClient httpClient=new HttpClient();
        String result=httpClient.loginBaidu();
        int res=result.indexOf("fcsbklyl")+10;
        return "公司本年度设备可利用率为"+result.substring(res,result.indexOf(",",res))+"%。";
    }
    //公司MTTR一般指平均修复时间。
    private String getMTTR(XFCommand xfCommand) {
        try {
            String orgId = cacheService.getOrgId(xfCommand.getOrg());
            String name = cacheService.getOrgName(xfCommand.getOrg());
            String format = xfCommand.getNian()+"";
            Mt mt = mtMapper.selectByX(orgId, format);
            if (mt == null){
                return "没有查到相关数据";
            }
            double sth = mt.getStopHours();
            double stt=mt.getStoprTimes();
            BigDecimal bd=new BigDecimal(sth/stt);
            double mttr=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            return name+"本年度MTTR为"+mttr+"小时。";
        } catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    //公司MTBF，即平均无故障工作时间
    private String getMTBF(XFCommand xfCommand) {
        try {
            String orgId = cacheService.getOrgId(xfCommand.getOrg());
            String name = cacheService.getOrgName(xfCommand.getOrg());
            String format = xfCommand.getNian()+"";
            Mt mt = mtMapper.selectByX(orgId, format);
            if (mt == null){
                return "没有查到相关数据";
            }
            double wth = mt.getWtHours();
            double stt=mt.getStoprTimes();
            BigDecimal bd=new BigDecimal(wth/stt);
            double mtbf=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            return name+"本年度MTBF为"+mtbf+"小时。";
        } catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
