feat: license sn唯一性校验

This commit is contained in:
caiyuchao
2025-07-04 14:36:32 +08:00
parent a12131af17
commit f07fd94a72
5 changed files with 82 additions and 36 deletions

View File

@@ -15,4 +15,5 @@ public interface ErrorCodeConstants {
ErrorCode PROJECT_NAME_DUPLICATE = new ErrorCode(1_100_001_002, "项目名称`{}`已存在");
ErrorCode PROJECT_CODE_DUPLICATE = new ErrorCode(1_100_001_003, "项目编号`{}`已存在");
ErrorCode LICENSE_NOT_EXISTS = new ErrorCode(1_100_002_001, "License不存在");
ErrorCode LICENSE_SN_DUPLICATE = new ErrorCode(1_100_001_002, "License SN`{}`已存在");
}

View File

@@ -1,33 +1,38 @@
package org.agt.module.license.controller.admin.license;
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.license.vo.*;
import org.agt.module.license.controller.admin.license.vo.LicensePageReqVO;
import org.agt.module.license.controller.admin.license.vo.LicenseRespVO;
import org.agt.module.license.controller.admin.license.vo.LicenseSaveReqVO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.service.license.LicenseService;
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 java.util.List;
import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - License")
@RestController
@@ -92,4 +97,10 @@ public class LicenseController {
BeanUtils.toBean(list, LicenseRespVO.class));
}
@GetMapping("/sn-unique")
@Operation(summary = "校验License的sn是否唯一")
public CommonResult<Boolean> validateLicenseSnUnique(@RequestParam(value = "sn", required = false) String sn, @RequestParam(value = "id", required = false) Long id) {
Boolean isExists = licenseService.validateLicenseSnUnique(sn, id);
return success(isExists);
}
}

View File

@@ -36,4 +36,7 @@ public interface LicenseMapper extends BaseMapperX<LicenseDO> {
.orderByDesc(LicenseDO::getId));
}
default LicenseDO selectBySn(String sn) {
return selectOne(LicenseDO::getSn, sn);
}
}

View File

@@ -1,11 +1,10 @@
package org.agt.module.license.service.license;
import java.util.*;
import jakarta.validation.*;
import org.agt.module.license.controller.admin.license.vo.*;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
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.license.vo.LicensePageReqVO;
import org.agt.module.license.controller.admin.license.vo.LicenseSaveReqVO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
/**
* License Service 接口
@@ -52,4 +51,12 @@ public interface LicenseService {
*/
PageResult<LicenseDO> getLicensePage(LicensePageReqVO pageReqVO);
/**
* 校验License的sn是否唯一
*
* @param sn sn
* @param id id
* @return boolean
*/
Boolean validateLicenseSnUnique(String sn, Long id);
}

View File

@@ -1,21 +1,19 @@
package org.agt.module.license.service.license;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import org.agt.module.license.controller.admin.license.vo.*;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.module.license.controller.admin.license.vo.LicensePageReqVO;
import org.agt.module.license.controller.admin.license.vo.LicenseSaveReqVO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.dal.mysql.license.LicenseMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import static org.agt.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.agt.module.license.enums.ErrorCodeConstants.*;
import static org.agt.module.license.enums.ErrorCodeConstants.LICENSE_NOT_EXISTS;
import static org.agt.module.license.enums.ErrorCodeConstants.LICENSE_SN_DUPLICATE;
/**
* License Service 实现类
@@ -31,6 +29,10 @@ public class LicenseServiceImpl implements LicenseService {
@Override
public Long createLicense(LicenseSaveReqVO createReqVO) {
// 校验项目Sn是否唯一
if (!validateLicenseSnUnique(createReqVO.getSn(), createReqVO.getId())) {
throw exception(LICENSE_SN_DUPLICATE, createReqVO.getSn());
}
// 插入
LicenseDO license = BeanUtils.toBean(createReqVO, LicenseDO.class);
licenseMapper.insert(license);
@@ -40,6 +42,10 @@ public class LicenseServiceImpl implements LicenseService {
@Override
public void updateLicense(LicenseSaveReqVO updateReqVO) {
// 校验项目Sn是否唯一
if (!validateLicenseSnUnique(updateReqVO.getSn(), updateReqVO.getId())) {
throw exception(LICENSE_SN_DUPLICATE, updateReqVO.getSn());
}
// 校验存在
validateLicenseExists(updateReqVO.getId());
// 更新
@@ -71,4 +77,22 @@ public class LicenseServiceImpl implements LicenseService {
return licenseMapper.selectPage(pageReqVO);
}
@Override
public Boolean validateLicenseSnUnique(String sn, Long id) {
if (StrUtil.isBlank(sn)) {
return true;
}
LicenseDO license = licenseMapper.selectBySn(sn);
if (license == null) {
return true;
}
// 如果 id 为空,说明不用比较是否为相同 id 的License
if (id == null) {
return false;
}
if (!license.getId().equals(id)) {
return false;
}
return true;
}
}