feat: 校验客户编号是否唯一

This commit is contained in:
caiyuchao
2025-05-26 10:18:53 +08:00
parent 1a9ad560f6
commit 98b6ec61a1
4 changed files with 39 additions and 0 deletions

View File

@@ -87,6 +87,13 @@ public class CustomerController {
return success(isExists);
}
@GetMapping("/code-unique")
@Operation(summary = "校验客户编号是否唯一")
public CommonResult<Boolean> validateCustomerCodeUnique(@RequestParam(value = "code", required = false) String code, @RequestParam(value = "id", required = false) Long id) {
Boolean isExists = customerService.validateCustomerCodeUnique(code, id);
return success(isExists);
}
@GetMapping("/max-sn")
@Operation(summary = "返回当前最大客户sn")
public CommonResult<Integer> selectMaxCode() {

View File

@@ -30,5 +30,9 @@ public interface CustomerMapper extends BaseMapperX<CustomerDO> {
return selectOne(CustomerDO::getName, name);
}
default CustomerDO selectByCode(String code) {
return selectOne(CustomerDO::getCode, code);
}
Integer selectMaxCode();
}

View File

@@ -60,6 +60,15 @@ public interface CustomerService {
*/
Boolean validateCustomerNameExists(String name, Long id);
/**
* 校验客户编号是否唯一
*
* @param code 客户编号
* @param id 客户id
* @return 是否存在
*/
Boolean validateCustomerCodeUnique(String code, Long id);
/**
* 查询当前最大的sn号
*

View File

@@ -87,6 +87,25 @@ public class CustomerServiceImpl implements CustomerService {
return false;
}
@Override
public Boolean validateCustomerCodeUnique(String code, Long id) {
if (StrUtil.isBlank(code)) {
return true;
}
CustomerDO customer = customerMapper.selectByCode(code);
if (customer == null) {
return true;
}
// 如果 id 为空,说明不用比较是否为相同 id 的客户
if (id == null) {
return false;
}
if (!customer.getId().equals(id)) {
return false;
}
return true;
}
@Override
public Integer selectMaxCode() {
Integer maxCode = customerMapper.selectMaxCode();