新增-erp库位重复编码优化异常抛出

This commit is contained in:
liss
2025-11-03 17:12:17 +08:00
parent 79fa52f39c
commit 94313a6f57
3 changed files with 39 additions and 23 deletions

View File

@@ -10,16 +10,17 @@ import com.zt.plat.framework.common.exception.ErrorCode;
public interface ErrorCodeConstants {
// ========== 示例模块 1-001-000-000 ==========
ErrorCode MATERIAL_OTHER_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode ELEMENT_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode CONTACT_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode ACCOUNT_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode MATERIAL_DESTROY_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode MATERIAL_INFOMATION_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode COMPANY_RELATIVITY_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode WAREHOUSE_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode FACTORY_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode TAX_NOT_EXISTS = new ErrorCode(1_001_000_001, "示例不存在");
ErrorCode MATERIAL_OTHER_NOT_EXISTS = new ErrorCode(1_001_000_001, "物料不存在");
ErrorCode ELEMENT_NOT_EXISTS = new ErrorCode(1_001_000_001, "金属元素不存在");
ErrorCode CONTACT_NOT_EXISTS = new ErrorCode(1_001_000_001, "联系人不存在");
ErrorCode ACCOUNT_NOT_EXISTS = new ErrorCode(1_001_000_001, "账户条款不存在");
ErrorCode MATERIAL_DESTROY_NOT_EXISTS = new ErrorCode(1_001_000_001, "物料回收率不存在");
ErrorCode MATERIAL_INFOMATION_NOT_EXISTS = new ErrorCode(1_001_000_001, "物料信息不存在");
ErrorCode COMPANY_RELATIVITY_NOT_EXISTS = new ErrorCode(1_001_000_001, "公司关系不存在");
ErrorCode WAREHOUSE_NOT_EXISTS = new ErrorCode(1_001_000_001, "库位不存在");
ErrorCode WAREHOUSE_CODE_EXISTS = new ErrorCode(1_001_000_002, "库位编码已存在");
ErrorCode FACTORY_NOT_EXISTS = new ErrorCode(1_001_000_001, "工厂不存在");
ErrorCode TAX_NOT_EXISTS = new ErrorCode(1_001_000_001, "公司关系不存在");
ErrorCode BUSINESS_RULE_NOT_EXISTS = new ErrorCode(1_027_100_001, "规则模型不存在");

View File

@@ -33,4 +33,10 @@ public interface WarehouseMapper extends BaseMapperX<WarehouseDO> {
}
String selectMaxCode();
default WarehouseDO selectByCode(String code){
return selectOne(new LambdaQueryWrapperX<WarehouseDO>()
.eq(WarehouseDO::getCoding, code)
.last("LIMIT 1"));
};
}

View File

@@ -16,6 +16,7 @@ import org.springframework.validation.annotation.Validated;
import java.util.List;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.WAREHOUSE_CODE_EXISTS;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;
/**
@@ -35,16 +36,17 @@ public class WarehouseServiceImpl implements WarehouseService {
// 插入
WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
// 库位编码自动生成,格式 KW-0001,依次新增
String maxCode = warehouseMapper.selectMaxCode();
if (maxCode == null) {
warehouse.setCoding("KW-0001");
} else {
String prefix = "KW-";
String numberPart = maxCode.substring(prefix.length());
int nextNumber = Integer.parseInt(numberPart) + 1;
String nextCode = prefix + String.format("%04d", nextNumber);
warehouse.setCoding(nextCode);
}
// String maxCode = warehouseMapper.selectMaxCode();
// if (maxCode == null) {
// warehouse.setCoding("KW-0001");
// } else {
// String prefix = "KW-";
// String numberPart = maxCode.substring(prefix.length());
// int nextNumber = Integer.parseInt(numberPart) + 1;
// String nextCode = prefix + String.format("%04d", nextNumber);
// warehouse.setCoding(nextCode);
// }
validateWarehouseCodeExists(warehouse.getCoding());
warehouseMapper.insert(warehouse);
// 返回
return BeanUtils.toBean(warehouse, WarehouseRespVO.class);
@@ -68,12 +70,12 @@ public class WarehouseServiceImpl implements WarehouseService {
}
@Override
public void deleteWarehouseListByIds(List<Long> ids) {
public void deleteWarehouseListByIds(List<Long> ids) {
// 校验存在
validateWarehouseExists(ids);
// 删除
warehouseMapper.deleteByIds(ids);
}
}
private void validateWarehouseExists(List<Long> ids) {
List<WarehouseDO> list = warehouseMapper.selectByIds(ids);
@@ -88,6 +90,13 @@ public class WarehouseServiceImpl implements WarehouseService {
}
}
private void validateWarehouseCodeExists(String code) {
WarehouseDO warehouse = warehouseMapper.selectByCode(code);
if (warehouse != null) {
throw exception(WAREHOUSE_CODE_EXISTS);
}
}
@Override
public WarehouseDO getWarehouse(Long id) {
return warehouseMapper.selectById(id);
@@ -101,7 +110,7 @@ public class WarehouseServiceImpl implements WarehouseService {
@Override
public void enableWarehouseList(List<WarehouseRespVO> saveReqVOS) {
List<WarehouseDO> updateObj = BeanUtils.toBean(saveReqVOS, WarehouseDO.class);
List<BatchResult> count = warehouseMapper.updateById(updateObj);
List<BatchResult> count = warehouseMapper.updateById(updateObj);
if (CollUtil.isEmpty(count)) {
throw exception(WAREHOUSE_NOT_EXISTS);
}