关于java枚举类的简单使用

定义一个枚举类

public enum OperateTypeEnum {
    STORAGE(1,"入库"),
    IOT_REGISTER(2,"入网"),
    INSTALL(3,"安装"),
    SCRAP(4,"报废"),
    ORDER(5,"下令");

    @Getter
    private final Integer code;
    @Getter
    private final String desc;

    OperateTypeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static String valueOf(Integer code) {
        if (code == null) {
            return "";
        }
        for (OperateTypeEnum type : OperateTypeEnum.values()) {
            if (Objects.equals(type.getCode(), code)) {
                return type.getDesc();
            }
        }
        return "";
    }

    public static OperateTypeEnum ofByCode(Integer type){
        for (OperateTypeEnum value : values()) {
            if (value.getCode().equals(type)){
                return value;
            }
        }
        return null;
    }
}

ofByCode(Integer type)方法,根据类型码,找到那个枚举值

    @Override
    @PagingQuery
    public PageResult<DeviceEquipmentVO> getOperationEquipments(OperationQueryEquipmentVO query) {
        List<DeviceEquipment> list = this.getHistoryOperateEquipment(OperateTypeEnum.ofByCode(query.getOperationType()), query.getOperationBatch());
        PageInfo<DeviceEquipment> page = new PageInfo<>(list);
        return PageResult.<DeviceEquipmentVO>builder().data(this.dealManufacturerAndConver(list, DeviceEquipmentVO.class)).total(page.getTotal()).build();
    }
 /**
     * @param operateType    操作类型
     * @param operationBatch 操作批次
     * @return java.util.List<com.dclm.device.infrastructure.entity.DeviceEquipment>
     * @Author ljy
     * @Description 查询操作批次下的设备数据
     * @Date 17:48 2022/11/8
     **/
    private List<DeviceEquipment> getHistoryOperateEquipment(OperateTypeEnum operateType, String operationBatch) {
        Assert.notNull(operateType, "操作类型不能为空");
        Assert.notBlank(operationBatch, "操作批次不能为空");
        List<String> codes = deviceOperationListCrud.queryOperateDeviceId(operateType, operationBatch);
        // 审批通过的操作
        if (CollUtil.isNotEmpty(codes)) {
            return deviceEquipmentCrud.queryByCode(codes);
        }
        // 审批未通过
        switch (operateType) {
            case STORAGE:
                DeviceRepositoryRecord repositoryRecord = repositoryRecordCrud.findByRepositoryBatch(operationBatch);
                Assert.notNull(repositoryRecord, "操作批次 {} 不存在", operationBatch);
                return GsonUtil.strToList(repositoryRecord.getStaging(), DeviceEquipment.class);
            case IOT_REGISTER:
                DeviceIotRegisterRecord registerRecord = registerRecordServiceCrud.findByRegisterBatch(operationBatch);
                Assert.notNull(registerRecord, "操作批次 {} 不存在", operationBatch);
                codes = GsonUtil.strToList(registerRecord.getDeviceStorageJson(), String.class);
                break;
            case SCRAP:
                DeviceScrapRecord scrapRecord = scrapRecordServiceCrud.findByScrapBatch(operationBatch);
                Assert.notNull(scrapRecord, "操作批次 {} 不存在", operationBatch);
                codes = GsonUtil.strToList(scrapRecord.getStaging(), String.class);
                break;
            default:
                throw new RuntimeException("不支持的操作类型 " + operateType.getCode());
        }
        return CollUtil.isEmpty(codes) ? Collections.emptyList() : deviceEquipmentCrud.queryByCode(codes);
    }

OperationQueryEquipmentVO实体

@Data
public class OperationQueryEquipmentVO extends PageQueryVO {
    /**
     * 操作批次
     */
    private String operationBatch;
    /**
     * 操作类型
     */
    private Integer operationType;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值