数据库唯一校验

This commit is contained in:
潘荣晟
2026-01-14 17:45:15 +08:00
parent af6b6342c1
commit b5f142cc3e
2 changed files with 13 additions and 8 deletions

View File

@@ -35,8 +35,8 @@ public interface InternalWarehouseMapper extends BaseMapperX<InternalWarehouseDO
.eqIfPresent(InternalWarehouseDO::getCompanyNameCustom, reqVO.getCompanyNameCustom()) .eqIfPresent(InternalWarehouseDO::getCompanyNameCustom, reqVO.getCompanyNameCustom())
.orderByDesc(InternalWarehouseDO::getId)); .orderByDesc(InternalWarehouseDO::getId));
} }
@Select("SELECT COUNT(*) AS total FROM bse_intl_wrh WHERE NUM = #{number}") @Select("SELECT * FROM bse_intl_wrh WHERE NUM = #{number}")
@TenantIgnore @TenantIgnore
Long selectCountByNumber(String number); InternalWarehouseDO selectInternalWarehouseDOByNumber(String number);
} }

View File

@@ -21,7 +21,6 @@ import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils; import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList; import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.diffList; import static com.zt.plat.framework.common.util.collection.CollectionUtils.diffList;
@@ -46,23 +45,29 @@ public class InternalWarehouseServiceImpl implements InternalWarehouseService {
// 插入 // 插入
InternalWarehouseDO internalWarehouse = BeanUtils.toBean(createReqVO, InternalWarehouseDO.class); InternalWarehouseDO internalWarehouse = BeanUtils.toBean(createReqVO, InternalWarehouseDO.class);
//校验所绑定的库位是否已经存在 //校验所绑定的库位是否已经存在
validateInternalWarehouseExists(createReqVO.getNumber()); validateInternalWarehouseExists(createReqVO.getNumber(), "insert", null);
internalWarehouseMapper.insert(internalWarehouse); internalWarehouseMapper.insert(internalWarehouse);
// 返回 // 返回
return BeanUtils.toBean(internalWarehouse, InternalWarehouseRespVO.class); return BeanUtils.toBean(internalWarehouse, InternalWarehouseRespVO.class);
} }
public void validateInternalWarehouseExists(String number){ public void validateInternalWarehouseExists(String number, String type, Long id) {
if (internalWarehouseMapper.selectCountByNumber(number)>0) { InternalWarehouseDO internalWarehouseDO = internalWarehouseMapper.selectInternalWarehouseDOByNumber(number);
if (internalWarehouseDO != null && "insert".equals(type)) {
throw exception(INTERNAL_WAREHOUSE_EXISTS);
} else if (internalWarehouseDO != null && "update".equals(type) && !Objects.equals(id, internalWarehouseDO.getId())) {
throw exception(INTERNAL_WAREHOUSE_EXISTS); throw exception(INTERNAL_WAREHOUSE_EXISTS);
} }
} }
@Override @Override
public void updateInternalWarehouse(InternalWarehouseSaveReqVO updateReqVO) { public void updateInternalWarehouse(InternalWarehouseSaveReqVO updateReqVO) {
// 校验存在 // 校验存在
validateInternalWarehouseExists(updateReqVO.getId()); validateInternalWarehouseExists(updateReqVO.getId());
// 校验所绑定的库位是否已经存在
validateInternalWarehouseExists(updateReqVO.getNumber(), "update", updateReqVO.getId());
// 更新 // 更新
InternalWarehouseDO updateObj = BeanUtils.toBean(updateReqVO, InternalWarehouseDO.class); InternalWarehouseDO updateObj = BeanUtils.toBean(updateReqVO, InternalWarehouseDO.class);
internalWarehouseMapper.updateById(updateObj); internalWarehouseMapper.updateById(updateObj);
@@ -77,12 +82,12 @@ public class InternalWarehouseServiceImpl implements InternalWarehouseService {
} }
@Override @Override
public void deleteInternalWarehouseListByIds(List<Long> ids) { public void deleteInternalWarehouseListByIds(List<Long> ids) {
// 校验存在 // 校验存在
validateInternalWarehouseExists(ids); validateInternalWarehouseExists(ids);
// 删除 // 删除
internalWarehouseMapper.deleteByIds(ids); internalWarehouseMapper.deleteByIds(ids);
} }
private void validateInternalWarehouseExists(List<Long> ids) { private void validateInternalWarehouseExists(List<Long> ids) {
List<InternalWarehouseDO> list = internalWarehouseMapper.selectByIds(ids); List<InternalWarehouseDO> list = internalWarehouseMapper.selectByIds(ids);