package yt.gk.woserviceapi.common;
import com.alibaba.fastjson.JSONObject;import java.util.ArrayList;
import java.util.List;public enum ConstEnum {
/**
* 工单类型 */ 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", "设备异常"),/**
* 工单状态 */ BILL_STATE_WYC("BILL_STATE", 0, "未完成"), BILL_STATE_YWC("BILL_STATE", 1, "已完成");
private String unique;private String key;
private Integer state;
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 Listjson(){ List 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" }]