定义一个枚举类
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;
}