feat: 校验客户名称是否存在

This commit is contained in:
caiyuchao
2025-05-24 16:11:44 +08:00
parent 7505395fd3
commit 047490c0b9
5 changed files with 96 additions and 36 deletions

View File

@@ -1,33 +1,38 @@
package org.agt.module.license.controller.admin.customer;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.util.object.BeanUtils;
import static org.agt.framework.common.pojo.CommonResult.success;
import org.agt.framework.excel.core.util.ExcelUtils;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import static org.agt.framework.apilog.core.enums.OperateTypeEnum.*;
import org.agt.module.license.controller.admin.customer.vo.*;
import org.agt.framework.ip.core.utils.AreaUtils;
import org.agt.module.license.controller.admin.customer.vo.CustomerPageReqVO;
import org.agt.module.license.controller.admin.customer.vo.CustomerRespVO;
import org.agt.module.license.controller.admin.customer.vo.CustomerSaveReqVO;
import org.agt.module.license.dal.dataobject.customer.CustomerDO;
import org.agt.module.license.service.customer.CustomerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 客户")
@RestController
@@ -68,7 +73,18 @@ public class CustomerController {
@PreAuthorize("@ss.hasPermission('license:customer:query')")
public CommonResult<CustomerRespVO> getCustomer(@RequestParam("id") Long id) {
CustomerDO customer = customerService.getCustomer(id);
return success(BeanUtils.toBean(customer, CustomerRespVO.class));
return success(BeanUtils.toBean(customer, CustomerRespVO.class, customerRespVO -> {
if (customerRespVO.getAreaId() != null) {
customerRespVO.setAreaIds(AreaUtils.formatToIdList(customerRespVO.getAreaId().intValue()));
}
}));
}
@GetMapping("/name-exists")
@Operation(summary = "校验客户名称是否存在")
public CommonResult<Boolean> validateCustomerNameExists(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "id", required = false) Long id) {
Boolean isExists = customerService.validateCustomerNameExists(name, id);
return success(isExists);
}
@GetMapping("/page")
@@ -76,7 +92,7 @@ public class CustomerController {
@PreAuthorize("@ss.hasPermission('license:customer:query')")
public CommonResult<PageResult<CustomerRespVO>> getCustomerPage(@Valid CustomerPageReqVO pageReqVO) {
PageResult<CustomerDO> pageResult = customerService.getCustomerPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, CustomerRespVO.class));
return success(buildCustomerVOList(pageResult));
}
@GetMapping("/export-excel")
@@ -84,12 +100,19 @@ public class CustomerController {
@PreAuthorize("@ss.hasPermission('license:customer:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportCustomerExcel(@Valid CustomerPageReqVO pageReqVO,
HttpServletResponse response) throws IOException {
HttpServletResponse response) throws IOException {
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<CustomerDO> list = customerService.getCustomerPage(pageReqVO).getList();
PageResult<CustomerDO> pageResult = customerService.getCustomerPage(pageReqVO);
// 导出 Excel
ExcelUtils.write(response, "客户.xls", "数据", CustomerRespVO.class,
BeanUtils.toBean(list, CustomerRespVO.class));
buildCustomerVOList(pageResult).getList());
}
private static PageResult<CustomerRespVO> buildCustomerVOList(PageResult<CustomerDO> pageResult) {
return BeanUtils.toBean(pageResult, CustomerRespVO.class, customerRespVO -> {
if (customerRespVO.getAreaId() != null) {
customerRespVO.setAreaNames(AreaUtils.format(customerRespVO.getAreaId().intValue()));
}
});
}
}

View File

@@ -8,6 +8,7 @@ import org.agt.framework.excel.core.annotations.DictFormat;
import org.agt.framework.excel.core.convert.DictConvert;
import java.time.LocalDateTime;
import java.util.List;
@Schema(description = "管理后台 - 客户 Response VO")
@Data
@@ -32,9 +33,15 @@ public class CustomerRespVO {
private Integer type;
@Schema(description = "地区", requiredMode = Schema.RequiredMode.REQUIRED, example = "5186")
@ExcelProperty("地区")
private Long areaId;
@Schema(description = "地区编号列表", example = "5186")
private List<Integer> areaIds;
@Schema(description = "地区名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "广东省")
@ExcelProperty("地区")
private String areaNames;
@Schema(description = "联系人", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("联系人")
private String contacts;

View File

@@ -1,13 +1,11 @@
package org.agt.module.license.dal.mysql.customer;
import java.util.*;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.agt.framework.mybatis.core.mapper.BaseMapperX;
import org.agt.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.agt.module.license.controller.admin.customer.vo.CustomerPageReqVO;
import org.agt.module.license.dal.dataobject.customer.CustomerDO;
import org.apache.ibatis.annotations.Mapper;
import org.agt.module.license.controller.admin.customer.vo.*;
/**
* 客户 Mapper
@@ -28,4 +26,8 @@ public interface CustomerMapper extends BaseMapperX<CustomerDO> {
.orderByDesc(CustomerDO::getId));
}
default CustomerDO selectByName(String name) {
return selectOne(CustomerDO::getName, name);
}
}

View File

@@ -1,11 +1,10 @@
package org.agt.module.license.service.customer;
import java.util.*;
import jakarta.validation.*;
import org.agt.module.license.controller.admin.customer.vo.*;
import org.agt.module.license.dal.dataobject.customer.CustomerDO;
import jakarta.validation.Valid;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.module.license.controller.admin.customer.vo.CustomerPageReqVO;
import org.agt.module.license.controller.admin.customer.vo.CustomerSaveReqVO;
import org.agt.module.license.dal.dataobject.customer.CustomerDO;
/**
* 客户 Service 接口
@@ -52,4 +51,13 @@ public interface CustomerService {
*/
PageResult<CustomerDO> getCustomerPage(CustomerPageReqVO pageReqVO);
/**
* 校验客户名称是否存在
*
* @param name 客户名称
* @param id 客户id
* @return 是否存在
*/
Boolean validateCustomerNameExists(String name, Long id);
}

View File

@@ -1,5 +1,6 @@
package org.agt.module.license.service.customer;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.util.object.BeanUtils;
@@ -67,4 +68,23 @@ public class CustomerServiceImpl implements CustomerService {
return customerMapper.selectPage(pageReqVO);
}
@Override
public Boolean validateCustomerNameExists(String name, Long id) {
if (StrUtil.isBlank(name)) {
return false;
}
CustomerDO customer = customerMapper.selectByName(name);
if (customer == null) {
return false;
}
// 如果 id 为空,说明不用比较是否为相同 id 的客户
if (id == null) {
return true;
}
if (!customer.getId().equals(id)) {
return true;
}
return false;
}
}