【移除】错误码的管理,简化项目的复杂度
parent
ff5276998c
commit
8093ef3b96
@ -1,30 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.errorcode.config;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码的配置属性类
|
|
||||||
*
|
|
||||||
* @author dlyan
|
|
||||||
*/
|
|
||||||
@ConfigurationProperties("yudao.error-code")
|
|
||||||
@Data
|
|
||||||
@Validated
|
|
||||||
public class ErrorCodeProperties {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否开启
|
|
||||||
*/
|
|
||||||
private Boolean enable = true;
|
|
||||||
/**
|
|
||||||
* 错误码枚举类
|
|
||||||
*/
|
|
||||||
@NotNull(message = "错误码枚举类不能为空")
|
|
||||||
private List<String> constantsClassList;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.errorcode.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGenerator;
|
|
||||||
import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGeneratorImpl;
|
|
||||||
import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoader;
|
|
||||||
import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoaderImpl;
|
|
||||||
import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码配置类
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@AutoConfiguration
|
|
||||||
@ConditionalOnProperty(prefix = "yudao.error-code", value = "enable", matchIfMissing = true) // 允许使用 yudao.error-code.enable=false 禁用访问日志
|
|
||||||
@EnableConfigurationProperties(ErrorCodeProperties.class)
|
|
||||||
@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码
|
|
||||||
public class YudaoErrorCodeAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ErrorCodeAutoGenerator errorCodeAutoGenerator(@Value("${spring.application.name}") String applicationName,
|
|
||||||
ErrorCodeProperties errorCodeProperties,
|
|
||||||
ErrorCodeApi errorCodeApi) {
|
|
||||||
return new ErrorCodeAutoGeneratorImpl(applicationName, errorCodeProperties.getConstantsClassList(), errorCodeApi);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ErrorCodeLoader errorCodeLoader(@Value("${spring.application.name}") String applicationName,
|
|
||||||
ErrorCodeApi errorCodeApi) {
|
|
||||||
return new ErrorCodeLoaderImpl(applicationName, errorCodeApi);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.errorcode.core.generator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码的自动生成器
|
|
||||||
*
|
|
||||||
* @author dylan
|
|
||||||
*/
|
|
||||||
public interface ErrorCodeAutoGenerator {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将配置类到错误码写入数据库
|
|
||||||
*/
|
|
||||||
void execute();
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.api.errorcode.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.experimental.Accessors;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码自动生成 DTO
|
|
||||||
*
|
|
||||||
* @author dylan
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
@Accessors(chain = true)
|
|
||||||
public class ErrorCodeAutoGenerateReqDTO {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 应用名
|
|
||||||
*/
|
|
||||||
@NotNull(message = "应用名不能为空")
|
|
||||||
private String applicationName;
|
|
||||||
/**
|
|
||||||
* 错误码编码
|
|
||||||
*/
|
|
||||||
@NotNull(message = "错误码编码不能为空")
|
|
||||||
private Integer code;
|
|
||||||
/**
|
|
||||||
* 错误码错误提示
|
|
||||||
*/
|
|
||||||
@NotEmpty(message = "错误码错误提示不能为空")
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.api.errorcode.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码的 Response DTO
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class ErrorCodeRespDTO {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码编码
|
|
||||||
*/
|
|
||||||
private Integer code;
|
|
||||||
/**
|
|
||||||
* 错误码错误提示
|
|
||||||
*/
|
|
||||||
private String message;
|
|
||||||
/**
|
|
||||||
* 更新时间
|
|
||||||
*/
|
|
||||||
private LocalDateTime updateTime;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.enums.errorcode;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码的类型枚举
|
|
||||||
*
|
|
||||||
* @author dylan
|
|
||||||
*/
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Getter
|
|
||||||
public enum ErrorCodeTypeEnum implements IntArrayValuable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自动生成
|
|
||||||
*/
|
|
||||||
AUTO_GENERATION(1),
|
|
||||||
/**
|
|
||||||
* 手动编辑
|
|
||||||
*/
|
|
||||||
MANUAL_OPERATION(2);
|
|
||||||
|
|
||||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(ErrorCodeTypeEnum::getType).toArray();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 类型
|
|
||||||
*/
|
|
||||||
private final Integer type;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int[] array() {
|
|
||||||
return ARRAYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.api.errorcode;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO;
|
|
||||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
|
|
||||||
import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码 Api 实现类
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ErrorCodeApiImpl implements ErrorCodeApi {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ErrorCodeService errorCodeService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void autoGenerateErrorCodeList(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) {
|
|
||||||
errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, LocalDateTime minUpdateTime) {
|
|
||||||
return errorCodeService.getErrorCodeList(applicationName, minUpdateTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,13 +0,0 @@
|
|||||||
### 创建错误码
|
|
||||||
POST {{baseUrl}}/inra/error-code/create
|
|
||||||
Authorization: Bearer {{token}}
|
|
||||||
Content-Type: application/json
|
|
||||||
tenant-id: {{adminTenentId}}
|
|
||||||
|
|
||||||
{
|
|
||||||
"code": 200,
|
|
||||||
"message": "成功",
|
|
||||||
"group": "test",
|
|
||||||
"type": 1
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,93 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.controller.admin.errorcode;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
||||||
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeRespVO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO;
|
|
||||||
import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
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.springframework.security.access.prepost.PreAuthorize;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
|
|
||||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
|
||||||
|
|
||||||
@Tag(name = "管理后台 - 错误码")
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/system/error-code")
|
|
||||||
@Validated
|
|
||||||
public class ErrorCodeController {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ErrorCodeService errorCodeService;
|
|
||||||
|
|
||||||
@PostMapping("/create")
|
|
||||||
@Operation(summary = "创建错误码")
|
|
||||||
@PreAuthorize("@ss.hasPermission('system:error-code:create')")
|
|
||||||
public CommonResult<Long> createErrorCode(@Valid @RequestBody ErrorCodeSaveReqVO createReqVO) {
|
|
||||||
return success(errorCodeService.createErrorCode(createReqVO));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/update")
|
|
||||||
@Operation(summary = "更新错误码")
|
|
||||||
@PreAuthorize("@ss.hasPermission('system:error-code:update')")
|
|
||||||
public CommonResult<Boolean> updateErrorCode(@Valid @RequestBody ErrorCodeSaveReqVO updateReqVO) {
|
|
||||||
errorCodeService.updateErrorCode(updateReqVO);
|
|
||||||
return success(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/delete")
|
|
||||||
@Operation(summary = "删除错误码")
|
|
||||||
@Parameter(name = "id", description = "编号", required = true)
|
|
||||||
@PreAuthorize("@ss.hasPermission('system:error-code:delete')")
|
|
||||||
public CommonResult<Boolean> deleteErrorCode(@RequestParam("id") Long id) {
|
|
||||||
errorCodeService.deleteErrorCode(id);
|
|
||||||
return success(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/get")
|
|
||||||
@Operation(summary = "获得错误码")
|
|
||||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
|
||||||
@PreAuthorize("@ss.hasPermission('system:error-code:query')")
|
|
||||||
public CommonResult<ErrorCodeRespVO> getErrorCode(@RequestParam("id") Long id) {
|
|
||||||
ErrorCodeDO errorCode = errorCodeService.getErrorCode(id);
|
|
||||||
return success(BeanUtils.toBean(errorCode, ErrorCodeRespVO.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/page")
|
|
||||||
@Operation(summary = "获得错误码分页")
|
|
||||||
@PreAuthorize("@ss.hasPermission('system:error-code:query')")
|
|
||||||
public CommonResult<PageResult<ErrorCodeRespVO>> getErrorCodePage(@Valid ErrorCodePageReqVO pageVO) {
|
|
||||||
PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(pageVO);
|
|
||||||
return success(BeanUtils.toBean(pageResult, ErrorCodeRespVO.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/export-excel")
|
|
||||||
@Operation(summary = "导出错误码 Excel")
|
|
||||||
@PreAuthorize("@ss.hasPermission('system:error-code:export')")
|
|
||||||
@ApiAccessLog(operateType = EXPORT)
|
|
||||||
public void exportErrorCodeExcel(@Valid ErrorCodePageReqVO exportReqVO,
|
|
||||||
HttpServletResponse response) throws IOException {
|
|
||||||
exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
|
||||||
List<ErrorCodeDO> list = errorCodeService.getErrorCodePage(exportReqVO).getList();
|
|
||||||
// 导出 Excel
|
|
||||||
ExcelUtils.write(response, "错误码.xls", "数据", ErrorCodeRespVO.class,
|
|
||||||
BeanUtils.toBean(list, ErrorCodeRespVO.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,36 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import lombok.ToString;
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
|
||||||
|
|
||||||
@Schema(description = "管理后台 - 错误码分页 Request VO")
|
|
||||||
@Data
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@ToString(callSuper = true)
|
|
||||||
public class ErrorCodePageReqVO extends PageParam {
|
|
||||||
|
|
||||||
@Schema(description = "错误码类型,参见 ErrorCodeTypeEnum 枚举类", example = "1")
|
|
||||||
private Integer type;
|
|
||||||
|
|
||||||
@Schema(description = "应用名", example = "dashboard")
|
|
||||||
private String applicationName;
|
|
||||||
|
|
||||||
@Schema(description = "错误码编码", example = "1234")
|
|
||||||
private Integer code;
|
|
||||||
|
|
||||||
@Schema(description = "错误码错误提示", example = "帅气")
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
|
||||||
@Schema(description = "创建时间")
|
|
||||||
private LocalDateTime[] createTime;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,47 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
|
|
||||||
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
|
|
||||||
import cn.iocoder.yudao.module.system.enums.DictTypeConstants;
|
|
||||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
|
||||||
import com.alibaba.excel.annotation.ExcelProperty;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
@Schema(description = "管理后台 - 错误码 Response VO")
|
|
||||||
@Data
|
|
||||||
@ExcelIgnoreUnannotated
|
|
||||||
public class ErrorCodeRespVO {
|
|
||||||
|
|
||||||
@Schema(description = "错误码编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
|
||||||
@ExcelProperty("错误码编号")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Schema(description = "错误码类型,参见 ErrorCodeTypeEnum 枚举类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
|
||||||
@ExcelProperty(value = "错误码类型", converter = DictConvert.class)
|
|
||||||
@DictFormat(DictTypeConstants.ERROR_CODE_TYPE)
|
|
||||||
private Integer type;
|
|
||||||
|
|
||||||
@Schema(description = "应用名", requiredMode = Schema.RequiredMode.REQUIRED, example = "dashboard")
|
|
||||||
@ExcelProperty("应用名")
|
|
||||||
private String applicationName;
|
|
||||||
|
|
||||||
@Schema(description = "错误码编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1234")
|
|
||||||
@ExcelProperty("错误码编码")
|
|
||||||
private Integer code;
|
|
||||||
|
|
||||||
@Schema(description = "错误码错误提示", requiredMode = Schema.RequiredMode.REQUIRED, example = "帅气")
|
|
||||||
@ExcelProperty("错误码错误提示")
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
@Schema(description = "备注", example = "哈哈哈")
|
|
||||||
@ExcelProperty("备注")
|
|
||||||
private String memo;
|
|
||||||
|
|
||||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
|
||||||
@ExcelProperty("创建时间")
|
|
||||||
private LocalDateTime createTime;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,30 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
@Schema(description = "管理后台 - 错误码创建/修改 Request VO")
|
|
||||||
@Data
|
|
||||||
public class ErrorCodeSaveReqVO {
|
|
||||||
|
|
||||||
@Schema(description = "错误码编号", example = "1024")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Schema(description = "应用名", requiredMode = Schema.RequiredMode.REQUIRED, example = "dashboard")
|
|
||||||
@NotNull(message = "应用名不能为空")
|
|
||||||
private String applicationName;
|
|
||||||
|
|
||||||
@Schema(description = "错误码编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1234")
|
|
||||||
@NotNull(message = "错误码编码不能为空")
|
|
||||||
private Integer code;
|
|
||||||
|
|
||||||
@Schema(description = "错误码错误提示", requiredMode = Schema.RequiredMode.REQUIRED, example = "帅气")
|
|
||||||
@NotNull(message = "错误码错误提示不能为空")
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
@Schema(description = "备注", example = "哈哈哈")
|
|
||||||
private String memo;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.dal.mysql.errorcode;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface ErrorCodeMapper extends BaseMapperX<ErrorCodeDO> {
|
|
||||||
|
|
||||||
default PageResult<ErrorCodeDO> selectPage(ErrorCodePageReqVO reqVO) {
|
|
||||||
return selectPage(reqVO, new LambdaQueryWrapperX<ErrorCodeDO>()
|
|
||||||
.eqIfPresent(ErrorCodeDO::getType, reqVO.getType())
|
|
||||||
.likeIfPresent(ErrorCodeDO::getApplicationName, reqVO.getApplicationName())
|
|
||||||
.eqIfPresent(ErrorCodeDO::getCode, reqVO.getCode())
|
|
||||||
.likeIfPresent(ErrorCodeDO::getMessage, reqVO.getMessage())
|
|
||||||
.betweenIfPresent(ErrorCodeDO::getCreateTime, reqVO.getCreateTime())
|
|
||||||
.orderByDesc(ErrorCodeDO::getCode));
|
|
||||||
}
|
|
||||||
|
|
||||||
default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) {
|
|
||||||
return selectList(ErrorCodeDO::getCode, codes);
|
|
||||||
}
|
|
||||||
|
|
||||||
default ErrorCodeDO selectByCode(Integer code) {
|
|
||||||
return selectOne(ErrorCodeDO::getCode, code);
|
|
||||||
}
|
|
||||||
|
|
||||||
default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) {
|
|
||||||
return selectList(new LambdaQueryWrapperX<ErrorCodeDO>().eq(ErrorCodeDO::getApplicationName, applicationName)
|
|
||||||
.gtIfPresent(ErrorCodeDO::getUpdateTime, minUpdateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,167 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.service.errorcode;
|
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
||||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO;
|
|
||||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.mysql.errorcode.ErrorCodeMapper;
|
|
||||||
import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum;
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
|
||||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_DUPLICATE;
|
|
||||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_NOT_EXISTS;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 错误码 Service 实现类
|
|
||||||
*
|
|
||||||
* @author dlyan
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
@Validated
|
|
||||||
@Slf4j
|
|
||||||
public class ErrorCodeServiceImpl implements ErrorCodeService {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ErrorCodeMapper errorCodeMapper;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Long createErrorCode(ErrorCodeSaveReqVO createReqVO) {
|
|
||||||
// 校验 code 重复
|
|
||||||
validateCodeDuplicate(createReqVO.getCode(), null);
|
|
||||||
|
|
||||||
// 插入
|
|
||||||
ErrorCodeDO errorCode = BeanUtils.toBean(createReqVO, ErrorCodeDO.class)
|
|
||||||
.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType());
|
|
||||||
errorCodeMapper.insert(errorCode);
|
|
||||||
// 返回
|
|
||||||
return errorCode.getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateErrorCode(ErrorCodeSaveReqVO updateReqVO) {
|
|
||||||
// 校验存在
|
|
||||||
validateErrorCodeExists(updateReqVO.getId());
|
|
||||||
// 校验 code 重复
|
|
||||||
validateCodeDuplicate(updateReqVO.getCode(), updateReqVO.getId());
|
|
||||||
|
|
||||||
// 更新
|
|
||||||
ErrorCodeDO updateObj = BeanUtils.toBean(updateReqVO, ErrorCodeDO.class)
|
|
||||||
.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType());
|
|
||||||
errorCodeMapper.updateById(updateObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deleteErrorCode(Long id) {
|
|
||||||
// 校验存在
|
|
||||||
validateErrorCodeExists(id);
|
|
||||||
// 删除
|
|
||||||
errorCodeMapper.deleteById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 校验错误码的唯一字段是否重复
|
|
||||||
*
|
|
||||||
* 是否存在相同编码的错误码
|
|
||||||
*
|
|
||||||
* @param code 错误码编码
|
|
||||||
* @param id 错误码编号
|
|
||||||
*/
|
|
||||||
@VisibleForTesting
|
|
||||||
public void validateCodeDuplicate(Integer code, Long id) {
|
|
||||||
ErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code);
|
|
||||||
if (errorCodeDO == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 如果 id 为空,说明不用比较是否为相同 id 的错误码
|
|
||||||
if (id == null) {
|
|
||||||
throw exception(ERROR_CODE_DUPLICATE);
|
|
||||||
}
|
|
||||||
if (!errorCodeDO.getId().equals(id)) {
|
|
||||||
throw exception(ERROR_CODE_DUPLICATE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
void validateErrorCodeExists(Long id) {
|
|
||||||
if (errorCodeMapper.selectById(id) == null) {
|
|
||||||
throw exception(ERROR_CODE_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ErrorCodeDO getErrorCode(Long id) {
|
|
||||||
return errorCodeMapper.selectById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PageResult<ErrorCodeDO> getErrorCodePage(ErrorCodePageReqVO pageReqVO) {
|
|
||||||
return errorCodeMapper.selectPage(pageReqVO);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional
|
|
||||||
public void autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) {
|
|
||||||
if (CollUtil.isEmpty(autoGenerateDTOs)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 获得错误码
|
|
||||||
List<ErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByCodes(
|
|
||||||
convertSet(autoGenerateDTOs, ErrorCodeAutoGenerateReqDTO::getCode));
|
|
||||||
Map<Integer, ErrorCodeDO> errorCodeDOMap = convertMap(errorCodeDOs, ErrorCodeDO::getCode);
|
|
||||||
|
|
||||||
// 遍历 autoGenerateBOs 数组,逐个插入或更新。考虑到每次量级不大,就不走批量了
|
|
||||||
autoGenerateDTOs.forEach(autoGenerateDTO -> {
|
|
||||||
ErrorCodeDO errorCode = errorCodeDOMap.get(autoGenerateDTO.getCode());
|
|
||||||
// 不存在,则进行新增
|
|
||||||
if (errorCode == null) {
|
|
||||||
errorCode = BeanUtils.toBean(autoGenerateDTO, ErrorCodeDO.class)
|
|
||||||
.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
|
|
||||||
errorCodeMapper.insert(errorCode);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 存在,则进行更新。更新有三个前置条件:
|
|
||||||
// 条件 1. 只更新自动生成的错误码,即 Type 为 ErrorCodeTypeEnum.AUTO_GENERATION
|
|
||||||
if (!ErrorCodeTypeEnum.AUTO_GENERATION.getType().equals(errorCode.getType())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 条件 2. 分组 applicationName 必须匹配,避免存在错误码冲突的情况
|
|
||||||
if (!autoGenerateDTO.getApplicationName().equals(errorCode.getApplicationName())) {
|
|
||||||
log.error("[autoGenerateErrorCodes][自动创建({}/{}) 错误码失败,数据库中已经存在({}/{})]",
|
|
||||||
autoGenerateDTO.getCode(), autoGenerateDTO.getApplicationName(),
|
|
||||||
errorCode.getCode(), errorCode.getApplicationName());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 条件 3. 错误提示语存在差异
|
|
||||||
if (autoGenerateDTO.getMessage().equals(errorCode.getMessage())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 最终匹配,进行更新
|
|
||||||
errorCodeMapper.updateById(new ErrorCodeDO().setId(errorCode.getId()).setMessage(autoGenerateDTO.getMessage()));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, LocalDateTime minUpdateTime) {
|
|
||||||
List<ErrorCodeDO> list = errorCodeMapper.selectListByApplicationNameAndUpdateTimeGt(
|
|
||||||
applicationName, minUpdateTime);
|
|
||||||
return BeanUtils.toBean(list, ErrorCodeRespDTO.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue