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

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

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

@Data
public class XFCommand {

    private String business;
    private String org;
    @TableField("year")
    //命令传入时间
    private String year;
    private String month;
    private String day;
    private int cmddayint;
    //当前时间
    private int nian;
    private int yue;
    private int ri;
    private Calendar ttoday;

    private String cmdStr;
    private boolean noData;
    private boolean noBusiness;
    private boolean badCommand;

    private Date date;
    private boolean isToday;

    private HashSet<String> orgSet;

    private HashSet<String> businessSet;
    public HashSet<String> getBusinessSet() {
        if (businessSet == null) {
            businessSet = new HashSet<>();
            businessSet.add("RFDL");
            businessSet.add("YFDL");
            businessSet.add("NFDL");
            businessSet.add("TQ");//天气
            businessSet.add("FZY");//风资源
            businessSet.add("GZY");//光资源
            businessSet.add("NFDLWC");//年发电量完成情况
            businessSet.add("YXQK");//运行情况
            businessSet.add("FWJSL");//复位及时率
            businessSet.add("XQJSL");//消缺及时率
            businessSet.add("ZTZHL");//状态转换率
            businessSet.add("SBKLYL");//设备可利用率
            businessSet.add("FNLYL");//风能利用率
            businessSet.add("MTBF");//MTBF
            businessSet.add("MTTR");//MTTR
        }

        return businessSet;
    }

    public HashSet<String> getOrgization() {
        if (orgSet == null) {
            orgSet = new HashSet<>();
            orgSet.add("GS");
            orgSet.add("MHS");
            orgSet.add("NSS");
            orgSet.add("QS");
            orgSet.add("SBQ");
            orgSet.add("XS");
            orgSet.add("DWK");
            orgSet.add("PL");
            orgSet.add("XH");
            orgSet.add("YC");
            orgSet.add("MCH");
        }
        return orgSet;
    }


    public XFCommand(String cmd) {
        //解析接收到的命令
        String[] arr= Arrays.copyOf(cmd.split("_"),5);
        //例麻黄山 转 MHS
        arr[1]=convertToEnglish(arr[1]);
        //RFDL_麻黄山_2021_08_02
        business = arr[0];
        org = arr[1];
        switch (business){
            case "YXQK":
                day="";
                break;
            default:
                //获取命令时间
                String[] cmdDate=getDate(arr);

                year = cmdDate[0];
                month = cmdDate[1];
                day = cmdDate[2];
                cmddayint=stringToInt(day);

                cmdStr = arr[0]+"_"+arr[1]+"_"+cmdDate[0]+"_"+cmdDate[1]+"_"+cmdDate[2];
        }
    }
    //获取命令日期
    public String[] getDate(String[] dateCmd){
        //RFDL_麻黄山_2021_08_02
        ttoday = Calendar.getInstance();
        nian= ttoday.get(Calendar.YEAR);
        yue= ttoday.get(Calendar.MONTH) + 1;
        ri= ttoday.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        if(dateCmd[2]==null||dateCmd[2].equals("XXXX")){
            dateCmd[2]=""+nian;
        }
        if(dateCmd[3]==null||dateCmd[3].equals("XX")){
            if(stringToInt(dateCmd[2])==nian){
                dateCmd[3]=""+yue;
            }else{
                dateCmd[3]="12";
            }
        }
        if(dateCmd[4]==null||dateCmd[4].equals("XX")){
            if(stringToInt(dateCmd[2])==nian&&stringToInt(dateCmd[3])==yue){
                dateCmd[4]=""+ri;
            }else{
                try {
                    Date dateri = dateFormat.parse(dateCmd[2]+"-"+dateCmd[3]+"-01");
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(dateri);
                    calendar.add(Calendar.MONTH,1);
                    calendar.add(Calendar.DAY_OF_MONTH,-1);
                    dateCmd[4]=calendar.get(Calendar.DAY_OF_MONTH)+"";
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        if((dateCmd[2]+dateCmd[3]+dateCmd[4]).equals(""+nian+yue+ri)){
            isToday=true;
        }
        String[] date={dateCmd[2],dateCmd[3],dateCmd[4]};
        return date;
    }

    public String convertToEnglish(String convert){
        HashMap<String,String> toEnglishMap = new HashMap<>();
        toEnglishMap.put("公司","GS");
        toEnglishMap.put("麻黄山","MHS");
        toEnglishMap.put("香山","XS");
        toEnglishMap.put("青山","QS");
        toEnglishMap.put("石板泉","SBQ");
        toEnglishMap.put("平罗","PL");
        toEnglishMap.put("牛首山","NSS");
        toEnglishMap.put("大武口","DWK");
        toEnglishMap.put("宣和","XH");
        toEnglishMap.put("马场湖","MCH");
        toEnglishMap.put("银川","YC");
        if(toEnglishMap.containsKey(convert)){
            return toEnglishMap.get(convert);
        }
        return convert;
    }

    private  int stringToInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch(Exception ex) {
            return 0;
        }
    }

    public boolean validate() {
        if (getBusinessSet().contains(business) == false) {
            noBusiness = true;
            return false;
        }
        if (getOrgization().contains(org) == false) {
            noData = true;
            return false;
        }
        if (day == null) {
            badCommand = true;
            return  false;
        }
        return true;
    }
}
