枚举案例二 BILL_TYPE_BJ("BILL_TYPE", "bj", "布机单"),

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  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"
  }
]

转载于:https://my.oschina.net/u/3621360/blog/2056268

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值