if (ObjectUtil.isNotEmpty(param.getIsOut())) { if (param.getIsOut()) { List<SkuSubHosp> stopsales = hospDao.findAllBySphStopSaleAndSphStateNot(1, 9); List<String> spsIds = new ArrayList<>(); for (SkuSubHosp sub : stopsales) { spsIds.add(sub.getSphCode()); } if (getStockDto.getReturnDtos().getSize() == 0) { AssertUtil.DeveloperException.throwException("无库存"); } else { for (StockReturnDto dto : getStockDto.getReturnDtos().getContent()) { if (spsIds.contains(dto.getSphCode() ) && param.getRfid() != null) { AssertUtil.DeveloperException.throwException("商品已停售,无法添加"); } } } if (param.getRourceType()!=null && param.getRourceType().equals(5)){ 这一行不走此上面的校验,直接通过,这一行应该怎么写 } } }
时间: 2024-02-14 08:26:34 浏览: 42
根据你提供的代码,如果`param.getRourceType()`不为空且等于5,你想要在此行代码之前跳过上面的校验逻辑并直接通过。你可以使用`return`关键字来实现这个目的。具体代码如下所示:
```java
if (param.getRourceType() != null && param.getRourceType().equals(5)) {
return; // 直接通过,不执行上面的校验逻辑
}
```
这样,当满足条件时,程序会直接返回,不会继续执行后面的代码。请注意,在使用`return`语句时要确保它处于合适的位置,以避免影响程序的逻辑流程。
相关问题
if (ObjectUtil.isNotEmpty(param.getIsOut())) { if (param.getIsOut()) { List<SkuSubHosp> stopsales = hospDao.findAllBySphStopSaleAndSphStateNot(1, 9); List<String> spsIds = new ArrayList<>(); for (SkuSubHosp sub : stopsales) { spsIds.add(sub.getSphCode()); } if (getStockDto.getReturnDtos().getSize() == 0) { AssertUtil.DeveloperException.throwException("无库存"); } else { for (StockReturnDto dto : getStockDto.getReturnDtos().getContent()) { if (spsIds.contains(dto.getSphCode() ) && param.getRfid() != null) { AssertUtil.DeveloperException.throwException("商品已停售,无法添加"); } } } if (param.getRourceType()!=null && param.getRourceType().equals(5)){ 这一行不走此上面的校验,直接通过,这一行应该怎么写,不使用return的方法 } } }
如果你不想使用`return`来跳过上级校验并直接通过,你可以使用一个标志变量来控制上级校验的执行。具体代码如下所示:
```java
boolean bypassValidation = false;
if (param.getRourceType() != null && param.getRourceType().equals(5)) {
bypassValidation = true; // 设置标志变量为true,表示直接通过
}
if (!bypassValidation) {
// 上级校验逻辑
if (ObjectUtil.isNotEmpty(param.getIsOut())) {
if (param.getIsOut()) {
List<SkuSubHosp> stopsales = hospDao.findAllBySphStopSaleAndSphStateNot(1, 9);
List<String> spsIds = new ArrayList<>();
for (SkuSubHosp sub : stopsales) {
spsIds.add(sub.getSphCode());
}
if (getStockDto.getReturnDtos().getSize() == 0) {
AssertUtil.DeveloperException.throwException("无库存");
} else {
for (StockReturnDto dto : getStockDto.getReturnDtos().getContent()) {
if (spsIds.contains(dto.getSphCode()) && param.getRfid() != null) {
AssertUtil.DeveloperException.throwException("商品已停售,无法添加");
}
}
}
}
}
}
// 继续执行其他逻辑
```
这样,如果`param.getRourceType()`不为空且等于5,标志变量`bypassValidation`会被设置为true,从而跳过上级校验的执行。否则,上级校验逻辑会继续执行。请注意,在使用标志变量时要确保逻辑的正确性和一致性。
帮我优化这段代码: //如果不存在组织信息,则禁用 if (ObjectUtil.isEmpty(empXwempInfo)) { flowProxyEntity.setStatus("1"); flowProxyEntity.setCloseReason(MessageFormat.format("原因:未查询到部门信息,权限关闭,时间:【{0}】", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))); } else if (ObjectUtil.isNotEmpty(proxyEntity)) { flowProxyEntity.setStatus("0"); } if(ObjectUtil.isNotEmpty(empXwempInfo)){ flowProxyEntity.setDeptCode(empXwempInfo.getDepartmentCode()); flowProxyEntity.setPositionCode(empXwempInfo.getPositioncode()); } if (ObjectUtil.isEmpty(proxyEntity)) { flowProxyService.save(flowProxyEntity); } else { flowProxyEntity.setId(proxyEntity.getId()); flowProxyService.updateById(flowProxyEntity); }
以下是对该段代码的优化建议:
1. 使用常量或枚举类型替代“1”和“0”这样的魔法数字,提高代码可读性和可维护性。
2. 避免多次判断 empXwempInfo 是否为空,可以在最开始对其进行一次判断后,将是否为空的结果保存为一个布尔值,后面直接使用这个布尔值即可。
3. 对于 save 和 updateById 操作,可以使用一个变量来保存要执行的方法,避免重复代码。
4. 在设置 closeReason 的时候使用 StringBuilder 类型的变量,避免多次使用字符串拼接操作。
改写后的代码如下:
```
if (ObjectUtil.isEmpty(empXwempInfo)) {
flowProxyEntity.setStatus(Status.DISABLE.getCode());
StringBuilder closeReason = new StringBuilder("原因:未查询到部门信息,权限关闭,时间:【")
.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("】");
flowProxyEntity.setCloseReason(closeReason.toString());
} else {
boolean isProxyNotEmpty = ObjectUtil.isNotEmpty(proxyEntity);
flowProxyEntity.setStatus(isProxyNotEmpty ? Status.ENABLE.getCode() : Status.DISABLE.getCode());
flowProxyEntity.setDeptCode(empXwempInfo.getDepartmentCode());
flowProxyEntity.setPositionCode(empXwempInfo.getPositioncode());
FlowProxyService.MethodToExecute methodToExecute = isProxyNotEmpty ?
FlowProxyService.MethodToExecute.UPDATE : FlowProxyService.MethodToExecute.SAVE;
if (methodToExecute == FlowProxyService.MethodToExecute.SAVE) {
flowProxyService.save(flowProxyEntity);
} else {
flowProxyEntity.setId(proxyEntity.getId());
flowProxyService.updateById(flowProxyEntity);
}
}
```
其中,Status 是一个枚举类型,表示状态码,MethodToExecute 是一个枚举类型,表示要执行的方法。这样,代码就更加简洁、易读、易维护了。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)