枚舉案例二 BILL_TYPE_BJ("BILL_TYPE", "bj", "布機單"),

package yt.gk.woserviceapi.common;java


import com.alibaba.fastjson.JSONObject;json

import java.util.ArrayList;
import java.util.List;api

public enum ConstEnum {app

    /**
     * 工單類型
     */
    BILL_TYPE_BJ("BILL_TYPE", "bj", "布機單"),
    BILL_TYPE_YJ("BILL_TYPE","yj", "移機單"),
    BILL_TYPE_BH("BILL_TYPE","bh", "補貨單"),
    BILL_TYPE_PD("BILL_TYPE","pd", "盤點單"),
    BILL_TYPE_CGRK("BILL_TYPE","cgrk", "採購入庫單"),
    BILL_TYPE_LHCK("BILL_TYPE","lhck", "領貨出庫"),
    BILL_TYPE_SBXJ("BILL_TYPE","sbxj", "設備巡檢"),
    BILL_TYPE_SBYC("BILL_TYPE","sbyc", "設備異常"),this

    /**
     * 工單狀態
     */
    BILL_STATE_WYC("BILL_STATE", 0, "未完成"),
    BILL_STATE_YWC("BILL_STATE", 1, "已完成")spa

    ;字符串


    private String unique;get

    private String key;io

    private Integer state;ast

    private String value;

    ConstEnum(String unique, String key, String value) {
        this.unique = unique;
        this.key = key;
        this.value = value;
    }

    ConstEnum(String unique, Integer state, String value) {
        this.unique = unique;
        this.state = state;
        this.value = value;
    }

    public String getUnique() {
        return unique;
    }

    public void setUnique(String unique) {
        this.unique = unique;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    //key爲字符串
    public static String getValueByKey(String unique, String key){
        for (ConstEnum s : ConstEnum.values()) {
            if(s.getKey().equals(key) && s.getUnique().equals(unique)){
                return s.getValue();
            }
        }
        return "";
    }

    public static String getKeyByValue(String unique, String value){
        for (ConstEnum s : ConstEnum.values()) {
            if(value.equals(s.getValue()) && s.getUnique().equals(unique)){
                return s.getKey();
            }
        }
        return "";
    }

    //state爲Integer
    public static String getValueByState(String unique, Integer state){
        for (ConstEnum s : ConstEnum.values()) {
            if(s.getState()==state && s.getUnique().equals(unique)){
                return s.getValue();
            }
        }
        return "";
    }

    public static Integer getStateByValue(String unique, String value){
        for (ConstEnum s : ConstEnum.values()) {
            if(value.equals(s.getValue()) && s.getUnique().equals(unique)){
                return s.getState();
            }
        }
        return 0;
    }

    //所有枚舉值轉化位j'son
    public static List<JSONObject> getStateByValue(String unique){
        List<JSONObject> enumList= new ArrayList<>();
        for (ConstEnum s : ConstEnum.values()) {
            if(s.getUnique().equals(unique)){
                JSONObject json = new JSONObject();
                json.put("key", s.getKey());
                json.put("value", s.getValue());
                enumList.add(json);
            }
        }
        return enumList;
    }

}

 

 

枚舉action方法調用操做

@RequestMapping(value="json",method = RequestMethod.GET)
public  List<JSONObject> json(){
    List<JSONObject> bill_type = ConstEnum.getStateByValue("BILL_TYPE");
    return  bill_type;
}

執行結果:

[
  {
    "value": "布機單",
    "key": "bj"
  },
  {
    "value": "移機單",
    "key": "yj"
  },
  {
    "value": "補貨單",
    "key": "bh"
  },
  {
    "value": "盤點單",
    "key": "pd"
  },
  {
    "value": "採購入庫單",
    "key": "cgrk"
  },
  {
    "value": "領貨出庫",
    "key": "lhck"
  },
  {
    "value": "設備巡檢",
    "key": "sbxj"
  },
  {
    "value": "設備異常",
    "key": "sbyc"
  }
]
相關文章
相關標籤/搜索