Commit 6984eb5db1fc32649042980fed89e6b9eea8a8cc
1 parent
3580b416
完成错误码的后端代码
Showing
31 changed files
with
846 additions
and
525 deletions
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/InfErrorCodeController.http
0 → 100644
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/InfErrorCodeController.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.common.pojo.CommonResult; | |
| 4 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils; | |
| 6 | +import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog; | |
| 7 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.*; | |
| 8 | +import cn.iocoder.dashboard.modules.infra.convert.errorcode.InfErrorCodeConvert; | |
| 9 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 10 | +import cn.iocoder.dashboard.modules.infra.service.errorcode.InfErrorCodeService; | |
| 11 | +import io.swagger.annotations.Api; | |
| 12 | +import io.swagger.annotations.ApiImplicitParam; | |
| 13 | +import io.swagger.annotations.ApiOperation; | |
| 14 | +import org.springframework.security.access.prepost.PreAuthorize; | |
| 15 | +import org.springframework.validation.annotation.Validated; | |
| 16 | +import org.springframework.web.bind.annotation.*; | |
| 17 | + | |
| 18 | +import javax.annotation.Resource; | |
| 19 | +import javax.servlet.http.HttpServletResponse; | |
| 20 | +import javax.validation.Valid; | |
| 21 | +import java.io.IOException; | |
| 22 | +import java.util.List; | |
| 23 | + | |
| 24 | +import static cn.iocoder.dashboard.common.pojo.CommonResult.success; | |
| 25 | +import static cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum.EXPORT; | |
| 26 | + | |
| 27 | +@Api(tags = "错误码") | |
| 28 | +@RestController | |
| 29 | +@RequestMapping("/infra/error-code") | |
| 30 | +@Validated | |
| 31 | +public class InfErrorCodeController { | |
| 32 | + | |
| 33 | + @Resource | |
| 34 | + private InfErrorCodeService errorCodeService; | |
| 35 | + | |
| 36 | + @PostMapping("/create") | |
| 37 | + @ApiOperation("创建错误码") | |
| 38 | + @PreAuthorize("@ss.hasPermission('infra:error-code:create')") | |
| 39 | + public CommonResult<Long> createErrorCode(@Valid @RequestBody InfErrorCodeCreateReqVO createReqVO) { | |
| 40 | + return success(errorCodeService.createErrorCode(createReqVO)); | |
| 41 | + } | |
| 42 | + | |
| 43 | + @PutMapping("/update") | |
| 44 | + @ApiOperation("更新错误码") | |
| 45 | + @PreAuthorize("@ss.hasPermission('infra:error-code:update')") | |
| 46 | + public CommonResult<Boolean> updateErrorCode(@Valid @RequestBody InfErrorCodeUpdateReqVO updateReqVO) { | |
| 47 | + errorCodeService.updateErrorCode(updateReqVO); | |
| 48 | + return success(true); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @DeleteMapping("/delete") | |
| 52 | + @ApiOperation("删除错误码") | |
| 53 | + @ApiImplicitParam(name = "id", value = "编号", required = true) | |
| 54 | + @PreAuthorize("@ss.hasPermission('infra:error-code:delete')") | |
| 55 | + public CommonResult<Boolean> deleteErrorCode(@RequestParam("id") Long id) { | |
| 56 | + errorCodeService.deleteErrorCode(id); | |
| 57 | + return success(true); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @GetMapping("/get") | |
| 61 | + @ApiOperation("获得错误码") | |
| 62 | + @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) | |
| 63 | + @PreAuthorize("@ss.hasPermission('infra:error-code:query')") | |
| 64 | + public CommonResult<InfErrorCodeRespVO> getErrorCode(@RequestParam("id") Long id) { | |
| 65 | + InfErrorCodeDO errorCode = errorCodeService.getErrorCode(id); | |
| 66 | + return success(InfErrorCodeConvert.INSTANCE.convert(errorCode)); | |
| 67 | + } | |
| 68 | + | |
| 69 | + @GetMapping("/page") | |
| 70 | + @ApiOperation("获得错误码分页") | |
| 71 | + @PreAuthorize("@ss.hasPermission('infra:error-code:query')") | |
| 72 | + public CommonResult<PageResult<InfErrorCodeRespVO>> getErrorCodePage(@Valid InfErrorCodePageReqVO pageVO) { | |
| 73 | + PageResult<InfErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(pageVO); | |
| 74 | + return success(InfErrorCodeConvert.INSTANCE.convertPage(pageResult)); | |
| 75 | + } | |
| 76 | + | |
| 77 | + @GetMapping("/export-excel") | |
| 78 | + @ApiOperation("导出错误码 Excel") | |
| 79 | + @PreAuthorize("@ss.hasPermission('infra:error-code:export')") | |
| 80 | + @OperateLog(type = EXPORT) | |
| 81 | + public void exportErrorCodeExcel(@Valid InfErrorCodeExportReqVO exportReqVO, | |
| 82 | + HttpServletResponse response) throws IOException { | |
| 83 | + List<InfErrorCodeDO> list = errorCodeService.getErrorCodeList(exportReqVO); | |
| 84 | + // 导出 Excel | |
| 85 | + List<InfErrorCodeExcelVO> datas = InfErrorCodeConvert.INSTANCE.convertList02(list); | |
| 86 | + ExcelUtils.write(response, "错误码.xls", "数据", InfErrorCodeExcelVO.class, datas); | |
| 87 | + } | |
| 88 | + | |
| 89 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodeBaseVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModelProperty; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +import javax.validation.constraints.NotNull; | |
| 7 | + | |
| 8 | +/** | |
| 9 | +* 错误码 Base VO,提供给添加、修改、详细的子 VO 使用 | |
| 10 | +* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 | |
| 11 | +*/ | |
| 12 | +@Data | |
| 13 | +public class InfErrorCodeBaseVO { | |
| 14 | + | |
| 15 | + @ApiModelProperty(value = "应用名", required = true, example = "dashboard") | |
| 16 | + @NotNull(message = "应用名不能为空") | |
| 17 | + private String applicationName; | |
| 18 | + | |
| 19 | + @ApiModelProperty(value = "错误码编码", required = true, example = "1234") | |
| 20 | + @NotNull(message = "错误码编码不能为空") | |
| 21 | + private Integer code; | |
| 22 | + | |
| 23 | + @ApiModelProperty(value = "错误码错误提示", required = true, example = "帅气") | |
| 24 | + @NotNull(message = "错误码错误提示不能为空") | |
| 25 | + private String message; | |
| 26 | + | |
| 27 | + @ApiModelProperty(value = "备注", example = "哈哈哈") | |
| 28 | + private String memo; | |
| 29 | + | |
| 30 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodeCreateReqVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModel; | |
| 4 | +import lombok.Data; | |
| 5 | +import lombok.EqualsAndHashCode; | |
| 6 | +import lombok.ToString; | |
| 7 | + | |
| 8 | +@ApiModel("错误码创建 Request VO") | |
| 9 | +@Data | |
| 10 | +@EqualsAndHashCode(callSuper = true) | |
| 11 | +@ToString(callSuper = true) | |
| 12 | +public class InfErrorCodeCreateReqVO extends InfErrorCodeBaseVO { | |
| 13 | + | |
| 14 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodeExcelVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat; | |
| 4 | +import cn.iocoder.dashboard.framework.excel.core.convert.DictConvert; | |
| 5 | +import com.alibaba.excel.annotation.ExcelProperty; | |
| 6 | +import lombok.Data; | |
| 7 | + | |
| 8 | +import java.util.Date; | |
| 9 | + | |
| 10 | +import static cn.iocoder.dashboard.modules.system.enums.dict.SysDictTypeEnum.INF_ERROR_CODE_TYPE; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * 错误码 Excel VO | |
| 14 | + * | |
| 15 | + * @author 芋道源码 | |
| 16 | + */ | |
| 17 | +@Data | |
| 18 | +public class InfErrorCodeExcelVO { | |
| 19 | + | |
| 20 | + @ExcelProperty("错误码编号") | |
| 21 | + private Long id; | |
| 22 | + | |
| 23 | + @ExcelProperty(value = "错误码类型", converter = DictConvert.class) | |
| 24 | + @DictFormat(INF_ERROR_CODE_TYPE) | |
| 25 | + private Integer type; | |
| 26 | + | |
| 27 | + @ExcelProperty("应用名") | |
| 28 | + private String applicationName; | |
| 29 | + | |
| 30 | + @ExcelProperty("错误码编码") | |
| 31 | + private Integer code; | |
| 32 | + | |
| 33 | + @ExcelProperty("错误码错误提示") | |
| 34 | + private String message; | |
| 35 | + | |
| 36 | + @ExcelProperty("备注") | |
| 37 | + private String memo; | |
| 38 | + | |
| 39 | + @ExcelProperty("创建时间") | |
| 40 | + private Date createTime; | |
| 41 | + | |
| 42 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodeExportReqVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModel; | |
| 4 | +import io.swagger.annotations.ApiModelProperty; | |
| 5 | +import lombok.Data; | |
| 6 | +import org.springframework.format.annotation.DateTimeFormat; | |
| 7 | + | |
| 8 | +import java.util.Date; | |
| 9 | + | |
| 10 | +import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | |
| 11 | + | |
| 12 | +@ApiModel(value = "错误码 Excel 导出 Request VO", description = "参数和 InfErrorCodePageReqVO 是一致的") | |
| 13 | +@Data | |
| 14 | +public class InfErrorCodeExportReqVO { | |
| 15 | + | |
| 16 | + @ApiModelProperty(value = "错误码类型", example = "1") | |
| 17 | + private Integer type; | |
| 18 | + | |
| 19 | + @ApiModelProperty(value = "应用名", example = "dashboard") | |
| 20 | + private String applicationName; | |
| 21 | + | |
| 22 | + @ApiModelProperty(value = "错误码编码", example = "1234") | |
| 23 | + private Integer code; | |
| 24 | + | |
| 25 | + @ApiModelProperty(value = "错误码错误提示", example = "帅气") | |
| 26 | + private String message; | |
| 27 | + | |
| 28 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | |
| 29 | + @ApiModelProperty(value = "开始创建时间") | |
| 30 | + private Date beginCreateTime; | |
| 31 | + | |
| 32 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | |
| 33 | + @ApiModelProperty(value = "结束创建时间") | |
| 34 | + private Date endCreateTime; | |
| 35 | + | |
| 36 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodePageReqVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.common.pojo.PageParam; | |
| 4 | +import io.swagger.annotations.ApiModel; | |
| 5 | +import io.swagger.annotations.ApiModelProperty; | |
| 6 | +import lombok.Data; | |
| 7 | +import lombok.EqualsAndHashCode; | |
| 8 | +import lombok.ToString; | |
| 9 | +import org.springframework.format.annotation.DateTimeFormat; | |
| 10 | + | |
| 11 | +import java.util.Date; | |
| 12 | + | |
| 13 | +import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | |
| 14 | + | |
| 15 | +@ApiModel("错误码分页 Request VO") | |
| 16 | +@Data | |
| 17 | +@EqualsAndHashCode(callSuper = true) | |
| 18 | +@ToString(callSuper = true) | |
| 19 | +public class InfErrorCodePageReqVO extends PageParam { | |
| 20 | + | |
| 21 | + @ApiModelProperty(value = "错误码类型", example = "1", notes = "参见 SysErrorCodeTypeEnum 枚举类") | |
| 22 | + private Integer type; | |
| 23 | + | |
| 24 | + @ApiModelProperty(value = "应用名", example = "dashboard") | |
| 25 | + private String applicationName; | |
| 26 | + | |
| 27 | + @ApiModelProperty(value = "错误码编码", example = "1234") | |
| 28 | + private Integer code; | |
| 29 | + | |
| 30 | + @ApiModelProperty(value = "错误码错误提示", example = "帅气") | |
| 31 | + private String message; | |
| 32 | + | |
| 33 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | |
| 34 | + @ApiModelProperty(value = "开始创建时间") | |
| 35 | + private Date beginCreateTime; | |
| 36 | + | |
| 37 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | |
| 38 | + @ApiModelProperty(value = "结束创建时间") | |
| 39 | + private Date endCreateTime; | |
| 40 | + | |
| 41 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodeRespVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModel; | |
| 4 | +import io.swagger.annotations.ApiModelProperty; | |
| 5 | +import lombok.Data; | |
| 6 | +import lombok.EqualsAndHashCode; | |
| 7 | +import lombok.ToString; | |
| 8 | + | |
| 9 | +import java.util.Date; | |
| 10 | + | |
| 11 | +@ApiModel("错误码 Response VO") | |
| 12 | +@Data | |
| 13 | +@EqualsAndHashCode(callSuper = true) | |
| 14 | +@ToString(callSuper = true) | |
| 15 | +public class InfErrorCodeRespVO extends InfErrorCodeBaseVO { | |
| 16 | + | |
| 17 | + @ApiModelProperty(value = "错误码编号", required = true, example = "1024") | |
| 18 | + private Long id; | |
| 19 | + | |
| 20 | + @ApiModelProperty(value = "错误码类型", required = true, example = "1", notes = "参见 SysErrorCodeTypeEnum 枚举类") | |
| 21 | + private Integer type; | |
| 22 | + | |
| 23 | + @ApiModelProperty(value = "创建时间", required = true) | |
| 24 | + private Date createTime; | |
| 25 | + | |
| 26 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/controller/errorcode/vo/InfErrorCodeUpdateReqVO.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.controller.errorcode.vo; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModel; | |
| 4 | +import io.swagger.annotations.ApiModelProperty; | |
| 5 | +import lombok.Data; | |
| 6 | +import lombok.EqualsAndHashCode; | |
| 7 | +import lombok.ToString; | |
| 8 | + | |
| 9 | +import javax.validation.constraints.NotNull; | |
| 10 | + | |
| 11 | +@ApiModel("错误码更新 Request VO") | |
| 12 | +@Data | |
| 13 | +@EqualsAndHashCode(callSuper = true) | |
| 14 | +@ToString(callSuper = true) | |
| 15 | +public class InfErrorCodeUpdateReqVO extends InfErrorCodeBaseVO { | |
| 16 | + | |
| 17 | + @ApiModelProperty(value = "错误码编号", required = true, example = "1024") | |
| 18 | + @NotNull(message = "错误码编号不能为空") | |
| 19 | + private Long id; | |
| 20 | + | |
| 21 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/convert/errorcode/InfErrorCodeConvert.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.convert.errorcode; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 4 | +import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; | |
| 5 | +import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeRespDTO; | |
| 6 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeCreateReqVO; | |
| 7 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeExcelVO; | |
| 8 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeRespVO; | |
| 9 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeUpdateReqVO; | |
| 10 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 11 | +import org.mapstruct.Mapper; | |
| 12 | +import org.mapstruct.factory.Mappers; | |
| 13 | + | |
| 14 | +import java.util.List; | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * 错误码 Convert | |
| 18 | + * | |
| 19 | + * @author 芋道源码 | |
| 20 | + */ | |
| 21 | +@Mapper | |
| 22 | +public interface InfErrorCodeConvert { | |
| 23 | + | |
| 24 | + InfErrorCodeConvert INSTANCE = Mappers.getMapper(InfErrorCodeConvert.class); | |
| 25 | + | |
| 26 | + InfErrorCodeDO convert(InfErrorCodeCreateReqVO bean); | |
| 27 | + | |
| 28 | + InfErrorCodeDO convert(InfErrorCodeUpdateReqVO bean); | |
| 29 | + | |
| 30 | + InfErrorCodeRespVO convert(InfErrorCodeDO bean); | |
| 31 | + | |
| 32 | + List<InfErrorCodeRespVO> convertList(List<InfErrorCodeDO> list); | |
| 33 | + | |
| 34 | + PageResult<InfErrorCodeRespVO> convertPage(PageResult<InfErrorCodeDO> page); | |
| 35 | + | |
| 36 | + List<InfErrorCodeExcelVO> convertList02(List<InfErrorCodeDO> list); | |
| 37 | + | |
| 38 | + InfErrorCodeDO convert(ErrorCodeAutoGenerateReqDTO bean); | |
| 39 | + | |
| 40 | + List<ErrorCodeRespDTO> convertList03(List<InfErrorCodeDO> list); | |
| 41 | + | |
| 42 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/convert/errorcode/SysErrorCodeConvert.java deleted
| 1 | -package cn.iocoder.dashboard.modules.infra.convert.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 4 | -import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; | |
| 5 | -import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeRespDTO; | |
| 6 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeCreateDTO; | |
| 7 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeUpdateDTO; | |
| 8 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO; | |
| 9 | -import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 10 | -import com.baomidou.mybatisplus.core.metadata.IPage; | |
| 11 | -import org.mapstruct.Mapper; | |
| 12 | -import org.mapstruct.Mapping; | |
| 13 | -import org.mapstruct.ReportingPolicy; | |
| 14 | -import org.mapstruct.factory.Mappers; | |
| 15 | - | |
| 16 | -import java.util.List; | |
| 17 | - | |
| 18 | -@Mapper(unmappedSourcePolicy = ReportingPolicy.IGNORE) | |
| 19 | -public interface SysErrorCodeConvert { | |
| 20 | - | |
| 21 | - SysErrorCodeConvert INSTANCE = Mappers.getMapper(SysErrorCodeConvert.class); | |
| 22 | - | |
| 23 | - ErrorCodeVO convert (InfErrorCodeDO bean); | |
| 24 | - | |
| 25 | - List<ErrorCodeVO> convertList(List<InfErrorCodeDO> list); | |
| 26 | - | |
| 27 | - @Mapping(source = "records", target = "list") | |
| 28 | - PageResult<ErrorCodeVO> convertPage(IPage<InfErrorCodeDO> page); | |
| 29 | - | |
| 30 | - InfErrorCodeDO convert (ErrorCodeCreateDTO bean); | |
| 31 | - | |
| 32 | - InfErrorCodeDO convert (ErrorCodeUpdateDTO bean); | |
| 33 | - | |
| 34 | - InfErrorCodeDO convert(ErrorCodeAutoGenerateReqDTO bean); | |
| 35 | - | |
| 36 | - List<ErrorCodeRespDTO> convertList02(List<InfErrorCodeDO> list); | |
| 37 | - | |
| 38 | -} |
src/main/java/cn/iocoder/dashboard/modules/infra/dal/dataobject/errorcode/InfErrorCodeDO.java
| 1 | 1 | package cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; |
| 4 | -import cn.iocoder.dashboard.modules.system.enums.errorcode.SysErrorCodeTypeEnum; | |
| 4 | +import cn.iocoder.dashboard.modules.infra.enums.errorcode.InfErrorCodeTypeEnum; | |
| 5 | 5 | import com.baomidou.mybatisplus.annotation.TableId; |
| 6 | 6 | import com.baomidou.mybatisplus.annotation.TableName; |
| 7 | 7 | import lombok.Data; |
| ... | ... | @@ -27,7 +27,7 @@ public class InfErrorCodeDO extends BaseDO { |
| 27 | 27 | /** |
| 28 | 28 | * 错误码类型 |
| 29 | 29 | * |
| 30 | - * 枚举 {@link SysErrorCodeTypeEnum} | |
| 30 | + * 枚举 {@link InfErrorCodeTypeEnum} | |
| 31 | 31 | */ |
| 32 | 32 | private Integer type; |
| 33 | 33 | /** | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/dal/mysql/errorcode/InfErrorCodeMapper.java
| 1 | 1 | package cn.iocoder.dashboard.modules.infra.dal.mysql.errorcode; |
| 2 | 2 | |
| 3 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 4 | +import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; | |
| 3 | 5 | import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; |
| 4 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodePageDTO; | |
| 6 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeExportReqVO; | |
| 7 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodePageReqVO; | |
| 5 | 8 | import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; |
| 6 | 9 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 7 | -import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |
| 8 | -import com.baomidou.mybatisplus.core.metadata.IPage; | |
| 9 | -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |
| 10 | 10 | import org.apache.ibatis.annotations.Mapper; |
| 11 | 11 | |
| 12 | 12 | import java.util.Collection; |
| ... | ... | @@ -14,12 +14,26 @@ import java.util.Date; |
| 14 | 14 | import java.util.List; |
| 15 | 15 | |
| 16 | 16 | @Mapper |
| 17 | -public interface InfErrorCodeMapper extends BaseMapper<InfErrorCodeDO> { | |
| 17 | +public interface InfErrorCodeMapper extends BaseMapperX<InfErrorCodeDO> { | |
| 18 | + | |
| 19 | + default PageResult<InfErrorCodeDO> selectPage(InfErrorCodePageReqVO reqVO) { | |
| 20 | + return selectPage(reqVO, new QueryWrapperX<InfErrorCodeDO>() | |
| 21 | + .eqIfPresent("type", reqVO.getType()) | |
| 22 | + .likeIfPresent("application_name", reqVO.getApplicationName()) | |
| 23 | + .eqIfPresent("code", reqVO.getCode()) | |
| 24 | + .likeIfPresent("message", reqVO.getMessage()) | |
| 25 | + .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) | |
| 26 | + .orderByAsc("application_name", "code")); | |
| 27 | + } | |
| 18 | 28 | |
| 19 | - default IPage<InfErrorCodeDO> selectPage(ErrorCodePageDTO pageDTO) { | |
| 20 | - return selectPage(new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize()), | |
| 21 | - new QueryWrapperX<InfErrorCodeDO>().likeIfPresent("`group`", pageDTO.getGroup()) | |
| 22 | - .eqIfPresent("code", pageDTO.getCode()).likeIfPresent("message", pageDTO.getMessage())); | |
| 29 | + default List<InfErrorCodeDO> selectList(InfErrorCodeExportReqVO reqVO) { | |
| 30 | + return selectList(new QueryWrapperX<InfErrorCodeDO>() | |
| 31 | + .eqIfPresent("type", reqVO.getType()) | |
| 32 | + .likeIfPresent("application_name", reqVO.getApplicationName()) | |
| 33 | + .eqIfPresent("code", reqVO.getCode()) | |
| 34 | + .likeIfPresent("message", reqVO.getMessage()) | |
| 35 | + .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) | |
| 36 | + .orderByAsc("application_name", "code")); | |
| 23 | 37 | } |
| 24 | 38 | |
| 25 | 39 | default List<InfErrorCodeDO> selectListByCodes(Collection<Integer> codes) { | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/enums/InfErrorCodeConstants.java
| ... | ... | @@ -30,4 +30,9 @@ public interface InfErrorCodeConstants { |
| 30 | 30 | // ========== 文件 1001003000 ========== |
| 31 | 31 | ErrorCode FILE_NOT_EXISTS = new ErrorCode(1001003000, "文件不存在"); |
| 32 | 32 | |
| 33 | + // ========== 错误码模块 1001004000 ========== | |
| 34 | + ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1001004000, "错误码不存在"); | |
| 35 | + ErrorCode ERROR_CODE_DUPLICATE = new ErrorCode(1001004001, "已经存在编码为【{}}】的错误码"); | |
| 36 | + ErrorCode ERROR_CAN_NOT_UPDATE_SYSTEM_TYPE_ERROR = new ErrorCode(1001004002, "不能修改类型为系统内置的错误码"); | |
| 37 | + | |
| 33 | 38 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/enums/errorcode/SysErrorCodeTypeEnum.java renamed to src/main/java/cn/iocoder/dashboard/modules/infra/enums/errorcode/InfErrorCodeTypeEnum.java
| 1 | -package cn.iocoder.dashboard.modules.system.enums.errorcode; | |
| 1 | +package cn.iocoder.dashboard.modules.infra.enums.errorcode; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.common.core.IntArrayValuable; |
| 4 | 4 | import lombok.AllArgsConstructor; |
| ... | ... | @@ -13,7 +13,7 @@ import java.util.Arrays; |
| 13 | 13 | */ |
| 14 | 14 | @AllArgsConstructor |
| 15 | 15 | @Getter |
| 16 | -public enum SysErrorCodeTypeEnum implements IntArrayValuable { | |
| 16 | +public enum InfErrorCodeTypeEnum implements IntArrayValuable { | |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * 自动生成 |
| ... | ... | @@ -24,7 +24,7 @@ public enum SysErrorCodeTypeEnum implements IntArrayValuable { |
| 24 | 24 | */ |
| 25 | 25 | MANUAL_OPERATION(2); |
| 26 | 26 | |
| 27 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysErrorCodeTypeEnum::getType).toArray(); | |
| 27 | + public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(InfErrorCodeTypeEnum::getType).toArray(); | |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * 类型 | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/service/errorcode/InfErrorCodeService.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.service.errorcode; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 4 | +import cn.iocoder.dashboard.framework.errorcode.core.service.ErrorCodeFrameworkService; | |
| 5 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeCreateReqVO; | |
| 6 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeExportReqVO; | |
| 7 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodePageReqVO; | |
| 8 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeUpdateReqVO; | |
| 9 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 10 | + | |
| 11 | +import javax.validation.Valid; | |
| 12 | +import java.util.List; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * 错误码 Service 接口 | |
| 16 | + * | |
| 17 | + * @author 芋道源码 | |
| 18 | + */ | |
| 19 | +public interface InfErrorCodeService extends ErrorCodeFrameworkService { | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * 创建错误码 | |
| 23 | + * | |
| 24 | + * @param createReqVO 创建信息 | |
| 25 | + * @return 编号 | |
| 26 | + */ | |
| 27 | + Long createErrorCode(@Valid InfErrorCodeCreateReqVO createReqVO); | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * 更新错误码 | |
| 31 | + * | |
| 32 | + * @param updateReqVO 更新信息 | |
| 33 | + */ | |
| 34 | + void updateErrorCode(@Valid InfErrorCodeUpdateReqVO updateReqVO); | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * 删除错误码 | |
| 38 | + * | |
| 39 | + * @param id 编号 | |
| 40 | + */ | |
| 41 | + void deleteErrorCode(Long id); | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * 获得错误码 | |
| 45 | + * | |
| 46 | + * @param id 编号 | |
| 47 | + * @return 错误码 | |
| 48 | + */ | |
| 49 | + InfErrorCodeDO getErrorCode(Long id); | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * 获得错误码分页 | |
| 53 | + * | |
| 54 | + * @param pageReqVO 分页查询 | |
| 55 | + * @return 错误码分页 | |
| 56 | + */ | |
| 57 | + PageResult<InfErrorCodeDO> getErrorCodePage(InfErrorCodePageReqVO pageReqVO); | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * 获得错误码列表, 用于 Excel 导出 | |
| 61 | + * | |
| 62 | + * @param exportReqVO 查询条件 | |
| 63 | + * @return 错误码列表 | |
| 64 | + */ | |
| 65 | + List<InfErrorCodeDO> getErrorCodeList(InfErrorCodeExportReqVO exportReqVO); | |
| 66 | + | |
| 67 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/infra/service/errorcode/impl/InfErrorCodeServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.service.errorcode.impl; | |
| 2 | + | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 4 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; | |
| 6 | +import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeRespDTO; | |
| 7 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeCreateReqVO; | |
| 8 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeExportReqVO; | |
| 9 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodePageReqVO; | |
| 10 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeUpdateReqVO; | |
| 11 | +import cn.iocoder.dashboard.modules.infra.convert.errorcode.InfErrorCodeConvert; | |
| 12 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 13 | +import cn.iocoder.dashboard.modules.infra.dal.mysql.errorcode.InfErrorCodeMapper; | |
| 14 | +import cn.iocoder.dashboard.modules.infra.enums.errorcode.InfErrorCodeTypeEnum; | |
| 15 | +import cn.iocoder.dashboard.modules.infra.service.errorcode.InfErrorCodeService; | |
| 16 | +import com.google.common.annotations.VisibleForTesting; | |
| 17 | +import lombok.extern.slf4j.Slf4j; | |
| 18 | +import org.springframework.stereotype.Service; | |
| 19 | +import org.springframework.transaction.annotation.Transactional; | |
| 20 | +import org.springframework.validation.annotation.Validated; | |
| 21 | + | |
| 22 | +import javax.annotation.Resource; | |
| 23 | +import java.util.Date; | |
| 24 | +import java.util.List; | |
| 25 | +import java.util.Map; | |
| 26 | + | |
| 27 | +import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception; | |
| 28 | +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.ERROR_CODE_DUPLICATE; | |
| 29 | +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.ERROR_CODE_NOT_EXISTS; | |
| 30 | +import static cn.iocoder.dashboard.util.collection.CollectionUtils.convertMap; | |
| 31 | +import static cn.iocoder.dashboard.util.collection.CollectionUtils.convertSet; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * 错误码 Service 实现类 | |
| 35 | + */ | |
| 36 | +@Service | |
| 37 | +@Validated | |
| 38 | +@Slf4j | |
| 39 | +public class InfErrorCodeServiceImpl implements InfErrorCodeService { | |
| 40 | + | |
| 41 | + @Resource | |
| 42 | + private InfErrorCodeMapper errorCodeMapper; | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + public Long createErrorCode(InfErrorCodeCreateReqVO createReqVO) { | |
| 46 | + // 校验 code 重复 | |
| 47 | + validateCodeDuplicate(createReqVO.getCode(), null); | |
| 48 | + | |
| 49 | + // 插入 | |
| 50 | + InfErrorCodeDO errorCode = InfErrorCodeConvert.INSTANCE.convert(createReqVO) | |
| 51 | + .setType(InfErrorCodeTypeEnum.MANUAL_OPERATION.getType()); | |
| 52 | + errorCodeMapper.insert(errorCode); | |
| 53 | + // 返回 | |
| 54 | + return errorCode.getId(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Override | |
| 58 | + public void updateErrorCode(InfErrorCodeUpdateReqVO updateReqVO) { | |
| 59 | + // 校验存在 | |
| 60 | + this.validateErrorCodeExists(updateReqVO.getId()); | |
| 61 | + // 校验 code 重复 | |
| 62 | + validateCodeDuplicate(updateReqVO.getCode(), updateReqVO.getId()); | |
| 63 | + | |
| 64 | + // 更新 | |
| 65 | + InfErrorCodeDO updateObj = InfErrorCodeConvert.INSTANCE.convert(updateReqVO) | |
| 66 | + .setType(InfErrorCodeTypeEnum.MANUAL_OPERATION.getType()); | |
| 67 | + errorCodeMapper.updateById(updateObj); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public void deleteErrorCode(Long id) { | |
| 72 | + // 校验存在 | |
| 73 | + this.validateErrorCodeExists(id); | |
| 74 | + // 删除 | |
| 75 | + errorCodeMapper.deleteById(id); | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * 校验错误码的唯一字段是否重复 | |
| 80 | + * | |
| 81 | + * 是否存在相同编码的错误码 | |
| 82 | + * | |
| 83 | + * @param code 错误码编码 | |
| 84 | + * @param id 错误码编号 | |
| 85 | + */ | |
| 86 | + @VisibleForTesting | |
| 87 | + public void validateCodeDuplicate(Integer code, Long id) { | |
| 88 | + InfErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code); | |
| 89 | + if (errorCodeDO == null) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + // 如果 id 为空,说明不用比较是否为相同 id 的错误码 | |
| 93 | + if (id == null) { | |
| 94 | + throw exception(ERROR_CODE_DUPLICATE); | |
| 95 | + } | |
| 96 | + if (!errorCodeDO.getId().equals(id)) { | |
| 97 | + throw exception(ERROR_CODE_DUPLICATE); | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + @VisibleForTesting | |
| 102 | + public void validateErrorCodeExists(Long id) { | |
| 103 | + if (errorCodeMapper.selectById(id) == null) { | |
| 104 | + throw exception(ERROR_CODE_NOT_EXISTS); | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + @Override | |
| 109 | + public InfErrorCodeDO getErrorCode(Long id) { | |
| 110 | + return errorCodeMapper.selectById(id); | |
| 111 | + } | |
| 112 | + | |
| 113 | + @Override | |
| 114 | + public PageResult<InfErrorCodeDO> getErrorCodePage(InfErrorCodePageReqVO pageReqVO) { | |
| 115 | + return errorCodeMapper.selectPage(pageReqVO); | |
| 116 | + } | |
| 117 | + | |
| 118 | + @Override | |
| 119 | + public List<InfErrorCodeDO> getErrorCodeList(InfErrorCodeExportReqVO exportReqVO) { | |
| 120 | + return errorCodeMapper.selectList(exportReqVO); | |
| 121 | + } | |
| 122 | + | |
| 123 | + @Override | |
| 124 | + @Transactional | |
| 125 | + public void autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) { | |
| 126 | + if (CollUtil.isEmpty(autoGenerateDTOs)) { | |
| 127 | + return; | |
| 128 | + } | |
| 129 | + // 获得错误码 | |
| 130 | + List<InfErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByCodes( | |
| 131 | + convertSet(autoGenerateDTOs, ErrorCodeAutoGenerateReqDTO::getCode)); | |
| 132 | + Map<Integer, InfErrorCodeDO> errorCodeDOMap = convertMap(errorCodeDOs, InfErrorCodeDO::getCode); | |
| 133 | + | |
| 134 | + // 遍历 autoGenerateBOs 数组,逐个插入或更新。考虑到每次量级不大,就不走批量了 | |
| 135 | + autoGenerateDTOs.forEach(autoGenerateDTO -> { | |
| 136 | + InfErrorCodeDO errorCodeDO = errorCodeDOMap.get(autoGenerateDTO.getCode()); | |
| 137 | + // 不存在,则进行新增 | |
| 138 | + if (errorCodeDO == null) { | |
| 139 | + errorCodeDO = InfErrorCodeConvert.INSTANCE.convert(autoGenerateDTO) | |
| 140 | + .setType(InfErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 141 | + errorCodeMapper.insert(errorCodeDO); | |
| 142 | + return; | |
| 143 | + } | |
| 144 | + // 存在,则进行更新。更新有三个前置条件: | |
| 145 | + // 条件 1. 只更新自动生成的错误码,即 Type 为 ErrorCodeTypeEnum.AUTO_GENERATION | |
| 146 | + if (!InfErrorCodeTypeEnum.AUTO_GENERATION.getType().equals(errorCodeDO.getType())) { | |
| 147 | + return; | |
| 148 | + } | |
| 149 | + // 条件 2. 分组 group 必须匹配,避免存在错误码冲突的情况 | |
| 150 | + if (!autoGenerateDTO.getApplicationName().equals(errorCodeDO.getApplicationName())) { | |
| 151 | + log.error("[autoGenerateErrorCodes][自动创建({}/{}) 错误码失败,数据库中已经存在({}/{})]", | |
| 152 | + autoGenerateDTO.getCode(), autoGenerateDTO.getApplicationName(), | |
| 153 | + errorCodeDO.getCode(), errorCodeDO.getApplicationName()); | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + // 条件 3. 错误提示语存在差异 | |
| 157 | + if (autoGenerateDTO.getMessage().equals(errorCodeDO.getMessage())) { | |
| 158 | + return; | |
| 159 | + } | |
| 160 | + // 最终匹配,进行更新 | |
| 161 | + errorCodeMapper.updateById(new InfErrorCodeDO().setId(errorCodeDO.getId()).setMessage(autoGenerateDTO.getMessage())); | |
| 162 | + }); | |
| 163 | + } | |
| 164 | + | |
| 165 | + @Override | |
| 166 | + public List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, Date minUpdateTime) { | |
| 167 | + List<InfErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByApplicationNameAndUpdateTimeGt( | |
| 168 | + applicationName, minUpdateTime); | |
| 169 | + return InfErrorCodeConvert.INSTANCE.convertList03(errorCodeDOs); | |
| 170 | + } | |
| 171 | + | |
| 172 | +} | |
| 173 | + | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/controller/errorcode/ErrorCodeController.http deleted
| 1 | -### list-group | |
| 2 | -GET http://localhost:48080/api/system/error-code/list-group | |
| 3 | -Authorization: Bearer {{token}} | |
| 4 | -Content-Type: application/x-www-form-urlencoded | |
| 5 | - | |
| 6 | -group=test&minUpdateTime=2021-03-14 14:37:47 | |
| 7 | - | |
| 8 | - | |
| 9 | - | |
| 10 | -### | |
| 11 | -GET http://localhost:48080/api/system/error-code/query | |
| 12 | -Authorization: Bearer {{token}} | |
| 13 | -Content-Type: application/x-www-form-urlencoded | |
| 14 | - | |
| 15 | -errorCodeId=350 | |
| 16 | - | |
| 17 | -### | |
| 18 | -POST http://localhost:48080/api/system/error-code/generate | |
| 19 | -Authorization: Bearer {{token}} | |
| 20 | - | |
| 21 | -[ | |
| 22 | -{ | |
| 23 | - "code": 202, | |
| 24 | - "message": "成功1", | |
| 25 | - "group": "test1" | |
| 26 | -} | |
| 27 | -] | |
| 28 | - | |
| 29 | -### | |
| 30 | -POST http://localhost:48080/api/system/error-code/create | |
| 31 | -Authorization: Bearer {{token}} | |
| 32 | -Content-Type:application/json | |
| 33 | - | |
| 34 | -{ | |
| 35 | - "code": 200, | |
| 36 | - "message": "成功", | |
| 37 | - "group": "test", | |
| 38 | - "type": 1 | |
| 39 | -} | |
| 40 | - | |
| 41 | - | |
| 42 | - |
src/main/java/cn/iocoder/dashboard/modules/system/controller/errorcode/ErrorCodeController.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.controller.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.dashboard.common.pojo.CommonResult; | |
| 4 | -import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 5 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeCreateDTO; | |
| 6 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodePageDTO; | |
| 7 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeUpdateDTO; | |
| 8 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO; | |
| 9 | -import cn.iocoder.dashboard.modules.system.service.errorcode.impl.ErrorCodeServiceImpl; | |
| 10 | -import io.swagger.annotations.Api; | |
| 11 | -import io.swagger.annotations.ApiOperation; | |
| 12 | -import org.springframework.web.bind.annotation.*; | |
| 13 | - | |
| 14 | -import javax.annotation.Resource; | |
| 15 | -import java.util.List; | |
| 16 | - | |
| 17 | -import static cn.iocoder.dashboard.common.pojo.CommonResult.success; | |
| 18 | - | |
| 19 | -@Api(tags = "错误码") | |
| 20 | -@RestController | |
| 21 | -@RequestMapping("/system/error-code") | |
| 22 | -public class ErrorCodeController { | |
| 23 | - | |
| 24 | - @Resource | |
| 25 | - private ErrorCodeServiceImpl errorCodeService; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * 创建错误码 | |
| 29 | - * | |
| 30 | - * @param createDTO 创建错误码 DTO | |
| 31 | - * @return 错误码编号 | |
| 32 | - */ | |
| 33 | - @ApiOperation("创建错误码") | |
| 34 | - @PostMapping("/create") | |
| 35 | - public CommonResult<Integer> createErrorCode(@RequestBody ErrorCodeCreateDTO createDTO) { | |
| 36 | - return success(errorCodeService.createErrorCode(createDTO).getId()); | |
| 37 | - } | |
| 38 | - | |
| 39 | - /** | |
| 40 | - * 更新错误码 | |
| 41 | - * | |
| 42 | - * @param updateDTO 更新错误码 DTO | |
| 43 | - */ | |
| 44 | - @ApiOperation("更新错误码") | |
| 45 | - @PatchMapping("/update") | |
| 46 | - public CommonResult<Boolean> updateErrorCode(@RequestBody ErrorCodeUpdateDTO updateDTO) { | |
| 47 | - errorCodeService.updateErrorCode(updateDTO); | |
| 48 | - return success(Boolean.TRUE); | |
| 49 | - } | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * 删除错误码 | |
| 53 | - * | |
| 54 | - * @param errorCodeId 错误码编号 | |
| 55 | - */ | |
| 56 | - @ApiOperation("删除错误码") | |
| 57 | - @DeleteMapping("delete") | |
| 58 | - public CommonResult<Boolean> deleteErrorCode(Integer errorCodeId) { | |
| 59 | - errorCodeService.deleteErrorCode(errorCodeId); | |
| 60 | - return success(Boolean.TRUE); | |
| 61 | - } | |
| 62 | - | |
| 63 | - /** | |
| 64 | - * 获得错误码 | |
| 65 | - * | |
| 66 | - * @param errorCodeId 错误码编号 | |
| 67 | - * @return 错误码 | |
| 68 | - */ | |
| 69 | - @ApiOperation("获取错误码") | |
| 70 | - @GetMapping("/query") | |
| 71 | - public CommonResult<ErrorCodeVO> getErrorCode(Integer errorCodeId) { | |
| 72 | - return success(errorCodeService.getErrorCode(errorCodeId)); | |
| 73 | - } | |
| 74 | - | |
| 75 | - /** | |
| 76 | - * 获得错误码列表 | |
| 77 | - * | |
| 78 | - * @param errorCodeIds 错误码编号列表 | |
| 79 | - * @return 错误码列表 | |
| 80 | - */ | |
| 81 | - @ApiOperation("获取错误码列表") | |
| 82 | - @GetMapping("/query-ids") | |
| 83 | - public CommonResult<List<ErrorCodeVO>> listErrorCodes(@RequestBody List<Integer> errorCodeIds) { | |
| 84 | - return success(errorCodeService.listErrorCodes(errorCodeIds)); | |
| 85 | - } | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * 获得错误码分页 | |
| 89 | - * | |
| 90 | - * @param pageDTO 错误码分页查询 | |
| 91 | - * @return 错误码分页结果 | |
| 92 | - */ | |
| 93 | - @ApiOperation("获取错误码分页列表") | |
| 94 | - @GetMapping("/page") | |
| 95 | - public CommonResult<PageResult<ErrorCodeVO>> pageErrorCode(ErrorCodePageDTO pageDTO) { | |
| 96 | - return success(errorCodeService.pageErrorCode(pageDTO)); | |
| 97 | - } | |
| 98 | - | |
| 99 | -} |
src/main/java/cn/iocoder/dashboard/modules/system/controller/errorcode/dto/ErrorCodeCreateDTO.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.controller.errorcode.dto; | |
| 2 | - | |
| 3 | -import io.swagger.annotations.ApiModel; | |
| 4 | -import io.swagger.annotations.ApiModelProperty; | |
| 5 | -import lombok.Data; | |
| 6 | - | |
| 7 | -import javax.validation.constraints.NotEmpty; | |
| 8 | -import javax.validation.constraints.NotNull; | |
| 9 | - | |
| 10 | -@ApiModel("错误码创建 DTO") | |
| 11 | -@Data | |
| 12 | -public class ErrorCodeCreateDTO { | |
| 13 | - | |
| 14 | - @ApiModelProperty(value = "错误码编码", required = true, example = "10086") | |
| 15 | - @NotNull(message = "错误码编码不能为空") | |
| 16 | - private Integer code; | |
| 17 | - @ApiModelProperty(value = "错误码错误提示", required = true, example = "艿艿长的丑") | |
| 18 | - @NotEmpty(message = "错误码错误提示不能为空") | |
| 19 | - private String message; | |
| 20 | - @ApiModelProperty(value = "错误码分组", required = true, example = "user-service") | |
| 21 | - @NotEmpty(message = "错误码分组不能为空") | |
| 22 | - private String group; | |
| 23 | - @ApiModelProperty(value = "错误码备注", example = "我就是一个备注") | |
| 24 | - private String memo; | |
| 25 | - | |
| 26 | -} | |
| 27 | - |
src/main/java/cn/iocoder/dashboard/modules/system/controller/errorcode/dto/ErrorCodePageDTO.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.controller.errorcode.dto; | |
| 2 | - | |
| 3 | -import cn.iocoder.dashboard.common.pojo.PageParam; | |
| 4 | -import io.swagger.annotations.ApiModel; | |
| 5 | -import io.swagger.annotations.ApiModelProperty; | |
| 6 | -import lombok.Data; | |
| 7 | -import lombok.EqualsAndHashCode; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * 错误码分页 DTO | |
| 11 | - */ | |
| 12 | -@ApiModel("错误码分页 DTO") | |
| 13 | -@Data | |
| 14 | -@EqualsAndHashCode(callSuper = true) | |
| 15 | -public class ErrorCodePageDTO extends PageParam { | |
| 16 | - | |
| 17 | - @ApiModelProperty(value = "错误码编码", required = true) | |
| 18 | - private Integer code; | |
| 19 | - @ApiModelProperty(value = "错误码错误提示", required = true) | |
| 20 | - private String message; | |
| 21 | - @ApiModelProperty(value = "错误码分组", required = true) | |
| 22 | - private String group; | |
| 23 | - | |
| 24 | -} |
src/main/java/cn/iocoder/dashboard/modules/system/controller/errorcode/dto/ErrorCodeUpdateDTO.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.controller.errorcode.dto; | |
| 2 | - | |
| 3 | -import io.swagger.annotations.ApiModel; | |
| 4 | -import io.swagger.annotations.ApiModelProperty; | |
| 5 | -import lombok.Data; | |
| 6 | - | |
| 7 | -import javax.validation.constraints.NotEmpty; | |
| 8 | -import javax.validation.constraints.NotNull; | |
| 9 | - | |
| 10 | -@ApiModel("错误码更新 DTO") | |
| 11 | -@Data | |
| 12 | -public class ErrorCodeUpdateDTO { | |
| 13 | - | |
| 14 | - @ApiModelProperty(value = "错误码编号", required = true, example = "1") | |
| 15 | - @NotNull(message = "错误码编号不能为空") | |
| 16 | - private Integer id; | |
| 17 | - @ApiModelProperty(value = "错误码编码", required = true, example = "10086") | |
| 18 | - @NotNull(message = "错误码编码不能为空") | |
| 19 | - private Integer code; | |
| 20 | - @ApiModelProperty(value = "错误码错误提示", required = true, example = "艿艿长的丑") | |
| 21 | - @NotEmpty(message = "错误码错误提示不能为空") | |
| 22 | - private String message; | |
| 23 | - @ApiModelProperty(value = "错误码分组", required = true, example = "user-service") | |
| 24 | - @NotEmpty(message = "错误码分组不能为空") | |
| 25 | - private String group; | |
| 26 | - @ApiModelProperty(value = "错误码备注", example = "我就是一个备注") | |
| 27 | - private String memo; | |
| 28 | - | |
| 29 | -} | |
| 30 | - |
src/main/java/cn/iocoder/dashboard/modules/system/controller/errorcode/vo/ErrorCodeVO.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.controller.errorcode.vo; | |
| 2 | - | |
| 3 | -import io.swagger.annotations.ApiModel; | |
| 4 | -import io.swagger.annotations.ApiModelProperty; | |
| 5 | -import lombok.Data; | |
| 6 | -import lombok.experimental.Accessors; | |
| 7 | - | |
| 8 | -import java.io.Serializable; | |
| 9 | -import java.util.Date; | |
| 10 | - | |
| 11 | -/** | |
| 12 | - * 错误码 | |
| 13 | - */ | |
| 14 | -@ApiModel("错误码 VO") | |
| 15 | -@Data | |
| 16 | -public class ErrorCodeVO { | |
| 17 | - | |
| 18 | - @ApiModelProperty(value = "错误码编号", required = true, example = "1") | |
| 19 | - private Integer id; | |
| 20 | - @ApiModelProperty(value = "错误码编码", required = true, example = "10086") | |
| 21 | - private Integer code; | |
| 22 | - @ApiModelProperty(value = "错误码错误提示", required = true, example = "艿艿长的丑") | |
| 23 | - private String message; | |
| 24 | - @ApiModelProperty(value = "错误码类型", required = true, notes = "见 ErrorCodeTypeEnum 枚举", example = "1") | |
| 25 | - private Integer type; | |
| 26 | - @ApiModelProperty(value = "错误码分组", required = true, example = "user-service") | |
| 27 | - private String group; | |
| 28 | - @ApiModelProperty(value = "错误码备注", example = "我就是一个备注") | |
| 29 | - private String memo; | |
| 30 | - @ApiModelProperty(value = "创建时间", required = true) | |
| 31 | - private Date createTime; | |
| 32 | - @ApiModelProperty(value = "更新时间", required = false) | |
| 33 | - private Date updateTime; | |
| 34 | - | |
| 35 | -} |
src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -91,10 +91,4 @@ public interface SysErrorCodeConstants { |
| 91 | 91 | ErrorCode SMS_SEND_MOBILE_NOT_EXISTS = new ErrorCode(1002012000, "手机号不存在"); |
| 92 | 92 | ErrorCode SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS = new ErrorCode(1002012001, "模板参数({})缺失"); |
| 93 | 93 | |
| 94 | - | |
| 95 | - // ========== 错误码模块 1002009000 ========== | |
| 96 | - ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1002009000, "错误码不存在"); | |
| 97 | - ErrorCode ERROR_CODE_DUPLICATE = new ErrorCode(1002009001, "已经存在编码为【{}}】的错误码"); | |
| 98 | - ErrorCode ERROR_CAN_NOT_UPDATE_SYSTEM_TYPE_ERROR = new ErrorCode(1002004003, "不能修改类型为系统内置的错误码"); | |
| 99 | - | |
| 100 | 94 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/enums/dict/SysDictTypeEnum.java
| ... | ... | @@ -27,7 +27,7 @@ public enum SysDictTypeEnum { |
| 27 | 27 | INF_JOB_STATUS("inf_job_status"), // 定时任务状态的枚举 |
| 28 | 28 | INF_JOB_LOG_STATUS("inf_job_log_status"), // 定时任务日志状态的枚举 |
| 29 | 29 | INF_API_ERROR_LOG_PROCESS_STATUS("inf_api_error_log_process_status"), // API 错误日志的处理状态的枚举 |
| 30 | - | |
| 30 | + INF_ERROR_CODE_TYPE("inf_error_code_type"), // 错误码的类型枚举 | |
| 31 | 31 | ; |
| 32 | 32 | |
| 33 | 33 | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/errorcode/SysErrorCodeService.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.service.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.dashboard.framework.errorcode.core.service.ErrorCodeFrameworkService; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * 错误码 Service 接口 | |
| 7 | - * | |
| 8 | - * @author 芋道源码 | |
| 9 | - */ | |
| 10 | -public interface SysErrorCodeService extends ErrorCodeFrameworkService { | |
| 11 | - | |
| 12 | -} |
src/main/java/cn/iocoder/dashboard/modules/system/service/errorcode/impl/ErrorCodeServiceImpl.java deleted
| 1 | -package cn.iocoder.dashboard.modules.system.service.errorcode.impl; | |
| 2 | - | |
| 3 | -import cn.hutool.core.collection.CollUtil; | |
| 4 | -import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; | |
| 5 | -import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 6 | -import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; | |
| 7 | -import cn.iocoder.dashboard.framework.errorcode.core.dto.ErrorCodeRespDTO; | |
| 8 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeCreateDTO; | |
| 9 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodePageDTO; | |
| 10 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeUpdateDTO; | |
| 11 | -import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO; | |
| 12 | -import cn.iocoder.dashboard.modules.infra.convert.errorcode.SysErrorCodeConvert; | |
| 13 | -import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 14 | -import cn.iocoder.dashboard.modules.infra.dal.mysql.errorcode.InfErrorCodeMapper; | |
| 15 | -import cn.iocoder.dashboard.modules.system.enums.errorcode.SysErrorCodeTypeEnum; | |
| 16 | -import cn.iocoder.dashboard.modules.system.service.errorcode.SysErrorCodeService; | |
| 17 | -import com.baomidou.mybatisplus.core.metadata.IPage; | |
| 18 | -import lombok.extern.slf4j.Slf4j; | |
| 19 | -import org.springframework.stereotype.Service; | |
| 20 | -import org.springframework.transaction.annotation.Transactional; | |
| 21 | -import org.springframework.validation.annotation.Validated; | |
| 22 | - | |
| 23 | -import javax.annotation.Resource; | |
| 24 | -import java.util.Date; | |
| 25 | -import java.util.List; | |
| 26 | -import java.util.Map; | |
| 27 | - | |
| 28 | -import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.ERROR_CODE_DUPLICATE; | |
| 29 | -import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.ERROR_CODE_NOT_EXISTS; | |
| 30 | -import static cn.iocoder.dashboard.util.collection.CollectionUtils.convertMap; | |
| 31 | -import static cn.iocoder.dashboard.util.collection.CollectionUtils.convertSet; | |
| 32 | - | |
| 33 | -/** | |
| 34 | - * 错误码 Service 实现类 | |
| 35 | - */ | |
| 36 | -@Service | |
| 37 | -@Validated | |
| 38 | -@Slf4j | |
| 39 | -public class ErrorCodeServiceImpl implements SysErrorCodeService { | |
| 40 | - | |
| 41 | - @Resource | |
| 42 | - private InfErrorCodeMapper errorCodeMapper; | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * 创建错误码 | |
| 46 | - * | |
| 47 | - * @param createDTO 创建错误码 DTO | |
| 48 | - * @return 错误码 | |
| 49 | - */ | |
| 50 | - public ErrorCodeVO createErrorCode(ErrorCodeCreateDTO createDTO) { | |
| 51 | - checkDuplicateErrorCode(createDTO.getCode(), null); | |
| 52 | - // 插入到数据库 | |
| 53 | - InfErrorCodeDO errorCodeDO = SysErrorCodeConvert.INSTANCE.convert(createDTO); | |
| 54 | - errorCodeMapper.insert(errorCodeDO); | |
| 55 | - // 返回 | |
| 56 | - return SysErrorCodeConvert.INSTANCE.convert(errorCodeDO); | |
| 57 | - } | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * 更新错误码 | |
| 61 | - * | |
| 62 | - * @param updateDTO 更新错误码 DTO | |
| 63 | - */ | |
| 64 | - public void updateErrorCode(ErrorCodeUpdateDTO updateDTO) { | |
| 65 | - checkDuplicateErrorCode(updateDTO.getCode(), updateDTO.getId()); | |
| 66 | - // 校验更新的错误码是否存在 | |
| 67 | - if (errorCodeMapper.selectById(updateDTO.getId()) == null) { | |
| 68 | - throw ServiceExceptionUtil.exception(ERROR_CODE_NOT_EXISTS); | |
| 69 | - } | |
| 70 | - // 更新到数据库 | |
| 71 | - InfErrorCodeDO updateObject = SysErrorCodeConvert.INSTANCE.convert(updateDTO); | |
| 72 | - errorCodeMapper.updateById(updateObject); | |
| 73 | - } | |
| 74 | - | |
| 75 | - /** | |
| 76 | - * 删除错误码 | |
| 77 | - * | |
| 78 | - * @param errorCodeId 错误码编号 | |
| 79 | - */ | |
| 80 | - public void deleteErrorCode(Integer errorCodeId) { | |
| 81 | - // 校验删除的错误码是否存在 | |
| 82 | - if (errorCodeMapper.selectById(errorCodeId) == null) { | |
| 83 | - throw ServiceExceptionUtil.exception(ERROR_CODE_NOT_EXISTS); | |
| 84 | - } | |
| 85 | - // 标记删除 | |
| 86 | - errorCodeMapper.deleteById(errorCodeId); | |
| 87 | - } | |
| 88 | - | |
| 89 | - /** | |
| 90 | - * 获得错误码 | |
| 91 | - * | |
| 92 | - * @param errorCodeId 错误码编号 | |
| 93 | - * @return 错误码 | |
| 94 | - */ | |
| 95 | - public ErrorCodeVO getErrorCode(Integer errorCodeId) { | |
| 96 | - InfErrorCodeDO errorCodeDO = errorCodeMapper.selectById(errorCodeId); | |
| 97 | - return SysErrorCodeConvert.INSTANCE.convert(errorCodeDO); | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * 获得错误码列表 | |
| 102 | - * | |
| 103 | - * @param errorCodeIds 错误码编号列表 | |
| 104 | - * @return 错误码列表 | |
| 105 | - */ | |
| 106 | - public List<ErrorCodeVO> listErrorCodes(List<Integer> errorCodeIds) { | |
| 107 | - List<InfErrorCodeDO> errorCodeDOs = errorCodeMapper.selectBatchIds(errorCodeIds); | |
| 108 | - return SysErrorCodeConvert.INSTANCE.convertList(errorCodeDOs); | |
| 109 | - } | |
| 110 | - | |
| 111 | - /** | |
| 112 | - * 获得错误码分页 | |
| 113 | - * | |
| 114 | - * @param pageDTO 错误码分页查询 | |
| 115 | - * @return 错误码分页结果 | |
| 116 | - */ | |
| 117 | - public PageResult<ErrorCodeVO> pageErrorCode(ErrorCodePageDTO pageDTO) { | |
| 118 | - IPage<InfErrorCodeDO> errorCodeDOPage = errorCodeMapper.selectPage(pageDTO); | |
| 119 | - return SysErrorCodeConvert.INSTANCE.convertPage(errorCodeDOPage); | |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * 校验错误码的唯一字段是否重复 | |
| 124 | - * | |
| 125 | - * 是否存在相同编码的错误码 | |
| 126 | - * | |
| 127 | - * @param code 错误码编码 | |
| 128 | - * @param id 错误码编号 | |
| 129 | - */ | |
| 130 | - private void checkDuplicateErrorCode(Integer code, Integer id) { | |
| 131 | - InfErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code); | |
| 132 | - if (errorCodeDO == null) { | |
| 133 | - return; | |
| 134 | - } | |
| 135 | - // 如果 id 为空,说明不用比较是否为相同 id 的错误码 | |
| 136 | - if (id == null) { | |
| 137 | - throw ServiceExceptionUtil.exception(ERROR_CODE_DUPLICATE); | |
| 138 | - } | |
| 139 | - if (!errorCodeDO.getId().equals(id)) { | |
| 140 | - throw ServiceExceptionUtil.exception(ERROR_CODE_DUPLICATE); | |
| 141 | - } | |
| 142 | - } | |
| 143 | - | |
| 144 | - @Override | |
| 145 | - @Transactional | |
| 146 | - public void autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) { | |
| 147 | - if (CollUtil.isEmpty(autoGenerateDTOs)) { | |
| 148 | - return; | |
| 149 | - } | |
| 150 | - // 获得错误码 | |
| 151 | - List<InfErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByCodes( | |
| 152 | - convertSet(autoGenerateDTOs, ErrorCodeAutoGenerateReqDTO::getCode)); | |
| 153 | - Map<Integer, InfErrorCodeDO> errorCodeDOMap = convertMap(errorCodeDOs, InfErrorCodeDO::getCode); | |
| 154 | - | |
| 155 | - // 遍历 autoGenerateBOs 数组,逐个插入或更新。考虑到每次量级不大,就不走批量了 | |
| 156 | - autoGenerateDTOs.forEach(autoGenerateDTO -> { | |
| 157 | - InfErrorCodeDO errorCodeDO = errorCodeDOMap.get(autoGenerateDTO.getCode()); | |
| 158 | - // 不存在,则进行新增 | |
| 159 | - if (errorCodeDO == null) { | |
| 160 | - errorCodeDO = SysErrorCodeConvert.INSTANCE.convert(autoGenerateDTO) | |
| 161 | - .setType(SysErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 162 | - errorCodeMapper.insert(errorCodeDO); | |
| 163 | - return; | |
| 164 | - } | |
| 165 | - // 存在,则进行更新。更新有三个前置条件: | |
| 166 | - // 条件 1. 只更新自动生成的错误码,即 Type 为 ErrorCodeTypeEnum.AUTO_GENERATION | |
| 167 | - if (!SysErrorCodeTypeEnum.AUTO_GENERATION.getType().equals(errorCodeDO.getType())) { | |
| 168 | - return; | |
| 169 | - } | |
| 170 | - // 条件 2. 分组 group 必须匹配,避免存在错误码冲突的情况 | |
| 171 | - if (!autoGenerateDTO.getApplicationName().equals(errorCodeDO.getApplicationName())) { | |
| 172 | - log.error("[autoGenerateErrorCodes][自动创建({}/{}) 错误码失败,数据库中已经存在({}/{})]", | |
| 173 | - autoGenerateDTO.getCode(), autoGenerateDTO.getApplicationName(), | |
| 174 | - errorCodeDO.getCode(), errorCodeDO.getApplicationName()); | |
| 175 | - return; | |
| 176 | - } | |
| 177 | - // 条件 3. 错误提示语存在差异 | |
| 178 | - if (autoGenerateDTO.getMessage().equals(errorCodeDO.getMessage())) { | |
| 179 | - return; | |
| 180 | - } | |
| 181 | - // 最终匹配,进行更新 | |
| 182 | - errorCodeMapper.updateById(new InfErrorCodeDO().setId(errorCodeDO.getId()).setMessage(autoGenerateDTO.getMessage())); | |
| 183 | - }); | |
| 184 | - } | |
| 185 | - | |
| 186 | - @Override | |
| 187 | - public List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, Date minUpdateTime) { | |
| 188 | - List<InfErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByApplicationNameAndUpdateTimeGt( | |
| 189 | - applicationName, minUpdateTime); | |
| 190 | - return SysErrorCodeConvert.INSTANCE.convertList02(errorCodeDOs); | |
| 191 | - } | |
| 192 | - | |
| 193 | -} | |
| 194 | - |
src/main/resources/codegen/java/service/serviceImpl.vm
| ... | ... | @@ -53,7 +53,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service |
| 53 | 53 | public void delete${simpleClassName}(${primaryColumn.javaType} id) { |
| 54 | 54 | // 校验存在 |
| 55 | 55 | this.validate${simpleClassName}Exists(id); |
| 56 | - // 更新 | |
| 56 | + // 删除 | |
| 57 | 57 | ${classNameVar}Mapper.deleteById(id); |
| 58 | 58 | } |
| 59 | 59 | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/infra/service/errorcode/InfErrorCodeServiceTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.service.errorcode; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.BaseDbUnitTest; | |
| 4 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeCreateReqVO; | |
| 6 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeExportReqVO; | |
| 7 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodePageReqVO; | |
| 8 | +import cn.iocoder.dashboard.modules.infra.controller.errorcode.vo.InfErrorCodeUpdateReqVO; | |
| 9 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.errorcode.InfErrorCodeDO; | |
| 10 | +import cn.iocoder.dashboard.modules.infra.dal.mysql.errorcode.InfErrorCodeMapper; | |
| 11 | +import cn.iocoder.dashboard.modules.infra.enums.config.InfConfigTypeEnum; | |
| 12 | +import cn.iocoder.dashboard.modules.infra.enums.errorcode.InfErrorCodeTypeEnum; | |
| 13 | +import cn.iocoder.dashboard.modules.infra.service.errorcode.impl.InfErrorCodeServiceImpl; | |
| 14 | +import cn.iocoder.dashboard.util.collection.ArrayUtils; | |
| 15 | +import cn.iocoder.dashboard.util.object.ObjectUtils; | |
| 16 | +import org.junit.jupiter.api.Test; | |
| 17 | +import org.springframework.context.annotation.Import; | |
| 18 | + | |
| 19 | +import javax.annotation.Resource; | |
| 20 | +import java.util.List; | |
| 21 | +import java.util.function.Consumer; | |
| 22 | + | |
| 23 | +import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 24 | +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.ERROR_CODE_DUPLICATE; | |
| 25 | +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.ERROR_CODE_NOT_EXISTS; | |
| 26 | +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | |
| 27 | +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | |
| 28 | +import static cn.iocoder.dashboard.util.RandomUtils.*; | |
| 29 | +import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; | |
| 30 | +import static org.junit.jupiter.api.Assertions.*; | |
| 31 | + | |
| 32 | +/** | |
| 33 | +* {@link InfErrorCodeServiceImpl} 的单元测试类 | |
| 34 | +* | |
| 35 | +* @author 芋道源码 | |
| 36 | +*/ | |
| 37 | +@Import(InfErrorCodeServiceImpl.class) | |
| 38 | +public class InfErrorCodeServiceTest extends BaseDbUnitTest { | |
| 39 | + | |
| 40 | + @Resource | |
| 41 | + private InfErrorCodeServiceImpl errorCodeService; | |
| 42 | + | |
| 43 | + @Resource | |
| 44 | + private InfErrorCodeMapper errorCodeMapper; | |
| 45 | + | |
| 46 | + @Test | |
| 47 | + public void testCreateErrorCode_success() { | |
| 48 | + // 准备参数 | |
| 49 | + InfErrorCodeCreateReqVO reqVO = randomPojo(InfErrorCodeCreateReqVO.class); | |
| 50 | + | |
| 51 | + // 调用 | |
| 52 | + Long errorCodeId = errorCodeService.createErrorCode(reqVO); | |
| 53 | + // 断言 | |
| 54 | + assertNotNull(errorCodeId); | |
| 55 | + // 校验记录的属性是否正确 | |
| 56 | + InfErrorCodeDO errorCode = errorCodeMapper.selectById(errorCodeId); | |
| 57 | + assertPojoEquals(reqVO, errorCode); | |
| 58 | + assertEquals(InfErrorCodeTypeEnum.MANUAL_OPERATION.getType(), errorCode.getType()); | |
| 59 | + } | |
| 60 | + | |
| 61 | + @Test | |
| 62 | + public void testUpdateErrorCode_success() { | |
| 63 | + // mock 数据 | |
| 64 | + InfErrorCodeDO dbErrorCode = randomInfErrorCodeDO(); | |
| 65 | + errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 | |
| 66 | + // 准备参数 | |
| 67 | + InfErrorCodeUpdateReqVO reqVO = randomPojo(InfErrorCodeUpdateReqVO.class, o -> { | |
| 68 | + o.setId(dbErrorCode.getId()); // 设置更新的 ID | |
| 69 | + }); | |
| 70 | + | |
| 71 | + // 调用 | |
| 72 | + errorCodeService.updateErrorCode(reqVO); | |
| 73 | + // 校验是否更新正确 | |
| 74 | + InfErrorCodeDO errorCode = errorCodeMapper.selectById(reqVO.getId()); // 获取最新的 | |
| 75 | + assertPojoEquals(reqVO, errorCode); | |
| 76 | + assertEquals(InfErrorCodeTypeEnum.MANUAL_OPERATION.getType(), errorCode.getType()); | |
| 77 | + } | |
| 78 | + | |
| 79 | + @Test | |
| 80 | + public void testDeleteErrorCode_success() { | |
| 81 | + // mock 数据 | |
| 82 | + InfErrorCodeDO dbErrorCode = randomInfErrorCodeDO(); | |
| 83 | + errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 | |
| 84 | + // 准备参数 | |
| 85 | + Long id = dbErrorCode.getId(); | |
| 86 | + | |
| 87 | + // 调用 | |
| 88 | + errorCodeService.deleteErrorCode(id); | |
| 89 | + // 校验数据不存在了 | |
| 90 | + assertNull(errorCodeMapper.selectById(id)); | |
| 91 | + } | |
| 92 | + | |
| 93 | + @Test | |
| 94 | + public void testGetErrorCodePage() { | |
| 95 | + // mock 数据 | |
| 96 | + InfErrorCodeDO dbErrorCode = initGetErrorCodePage(); | |
| 97 | + // 准备参数 | |
| 98 | + InfErrorCodePageReqVO reqVO = new InfErrorCodePageReqVO(); | |
| 99 | + reqVO.setType(InfErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 100 | + reqVO.setApplicationName("yudao"); | |
| 101 | + reqVO.setCode(1); | |
| 102 | + reqVO.setMessage("yu"); | |
| 103 | + reqVO.setBeginCreateTime(buildTime(2020, 11, 1)); | |
| 104 | + reqVO.setEndCreateTime(buildTime(2020, 11, 30)); | |
| 105 | + | |
| 106 | + // 调用 | |
| 107 | + PageResult<InfErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO); | |
| 108 | + // 断言 | |
| 109 | + assertEquals(1, pageResult.getTotal()); | |
| 110 | + assertEquals(1, pageResult.getList().size()); | |
| 111 | + assertPojoEquals(dbErrorCode, pageResult.getList().get(0)); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * 初始化 getErrorCodePage 方法的测试数据 | |
| 116 | + */ | |
| 117 | + private InfErrorCodeDO initGetErrorCodePage() { | |
| 118 | + InfErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> { // 等会查询到 | |
| 119 | + o.setType(InfErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 120 | + o.setApplicationName("yudaoyuanma"); | |
| 121 | + o.setCode(1); | |
| 122 | + o.setMessage("yudao"); | |
| 123 | + o.setCreateTime(buildTime(2020, 11, 11)); | |
| 124 | + }); | |
| 125 | + errorCodeMapper.insert(dbErrorCode); | |
| 126 | + // 测试 type 不匹配 | |
| 127 | + errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setType(InfErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); | |
| 128 | + // 测试 applicationName 不匹配 | |
| 129 | + errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setApplicationName("yunai"))); | |
| 130 | + // 测试 code 不匹配 | |
| 131 | + errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setCode(2))); | |
| 132 | + // 测试 message 不匹配 | |
| 133 | + errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setMessage("nai"))); | |
| 134 | + // 测试 createTime 不匹配 | |
| 135 | + errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); | |
| 136 | + return dbErrorCode; | |
| 137 | + } | |
| 138 | + | |
| 139 | + @Test | |
| 140 | + public void testGetErrorCodeList() { | |
| 141 | + // mock 数据 | |
| 142 | + InfErrorCodeDO dbErrorCode = initGetErrorCodePage(); | |
| 143 | + // 准备参数 | |
| 144 | + InfErrorCodeExportReqVO reqVO = new InfErrorCodeExportReqVO(); | |
| 145 | + reqVO.setType(InfErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 146 | + reqVO.setApplicationName("yudao"); | |
| 147 | + reqVO.setCode(1); | |
| 148 | + reqVO.setMessage("yu"); | |
| 149 | + reqVO.setBeginCreateTime(buildTime(2020, 11, 1)); | |
| 150 | + reqVO.setEndCreateTime(buildTime(2020, 11, 30)); | |
| 151 | + | |
| 152 | + // 调用 | |
| 153 | + List<InfErrorCodeDO> list = errorCodeService.getErrorCodeList(reqVO); | |
| 154 | + // 断言 | |
| 155 | + assertEquals(1, list.size()); | |
| 156 | + assertPojoEquals(dbErrorCode, list.get(0)); | |
| 157 | + } | |
| 158 | + | |
| 159 | + @Test | |
| 160 | + public void testValidateCodeDuplicate_codeDuplicateForCreate() { | |
| 161 | + // 准备参数 | |
| 162 | + Integer code = randomInteger(); | |
| 163 | + // mock 数据 | |
| 164 | + errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code))); | |
| 165 | + | |
| 166 | + // 调用,校验异常 | |
| 167 | + assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null), | |
| 168 | + ERROR_CODE_DUPLICATE); | |
| 169 | + } | |
| 170 | + | |
| 171 | + @Test | |
| 172 | + public void testValidateCodeDuplicate_codeDuplicateForUpdate() { | |
| 173 | + // 准备参数 | |
| 174 | + Long id = randomLongId(); | |
| 175 | + Integer code = randomInteger(); | |
| 176 | + // mock 数据 | |
| 177 | + errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code))); | |
| 178 | + | |
| 179 | + // 调用,校验异常 | |
| 180 | + assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id), | |
| 181 | + ERROR_CODE_DUPLICATE); | |
| 182 | + } | |
| 183 | + | |
| 184 | + @Test | |
| 185 | + public void testValidateErrorCodeExists_notExists() { | |
| 186 | + assertServiceException(() -> errorCodeService.validateErrorCodeExists(null), | |
| 187 | + ERROR_CODE_NOT_EXISTS); | |
| 188 | + } | |
| 189 | + | |
| 190 | + // ========== 随机对象 ========== | |
| 191 | + | |
| 192 | + @SafeVarargs | |
| 193 | + private static InfErrorCodeDO randomInfErrorCodeDO(Consumer<InfErrorCodeDO>... consumers) { | |
| 194 | + Consumer<InfErrorCodeDO> consumer = (o) -> { | |
| 195 | + o.setType(randomEle(InfConfigTypeEnum.values()).getType()); // 保证 key 的范围 | |
| 196 | + }; | |
| 197 | + return randomPojo(InfErrorCodeDO.class, ArrayUtils.append(consumer, consumers)); | |
| 198 | + } | |
| 199 | + | |
| 200 | +} | ... | ... |
src/test/resources/sql/clean.sql
src/test/resources/sql/create_tables.sql
| ... | ... | @@ -359,7 +359,7 @@ CREATE TABLE IF NOT EXISTS "sys_sms_channel" ( |
| 359 | 359 | PRIMARY KEY ("id") |
| 360 | 360 | ) COMMENT '短信渠道'; |
| 361 | 361 | |
| 362 | -CREATE TABLE "sys_sms_template" ( | |
| 362 | +CREATE TABLE IF NOT EXISTS "sys_sms_template" ( | |
| 363 | 363 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, |
| 364 | 364 | "type" tinyint NOT NULL, |
| 365 | 365 | "status" tinyint NOT NULL, |
| ... | ... | @@ -379,7 +379,7 @@ CREATE TABLE "sys_sms_template" ( |
| 379 | 379 | PRIMARY KEY ("id") |
| 380 | 380 | ) COMMENT '短信模板'; |
| 381 | 381 | |
| 382 | -CREATE TABLE "sys_sms_log" ( | |
| 382 | +CREATE TABLE IF NOT EXISTS "sys_sms_log" ( | |
| 383 | 383 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, |
| 384 | 384 | "channel_id" bigint NOT NULL, |
| 385 | 385 | "channel_code" varchar(63) NOT NULL, |
| ... | ... | @@ -411,3 +411,18 @@ CREATE TABLE "sys_sms_log" ( |
| 411 | 411 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 412 | 412 | PRIMARY KEY ("id") |
| 413 | 413 | ) COMMENT '短信日志'; |
| 414 | + | |
| 415 | +CREATE TABLE IF NOT EXISTS "inf_error_code" ( | |
| 416 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 417 | + "type" tinyint NOT NULL DEFAULT '0', | |
| 418 | + "application_name" varchar(50) NOT NULL, | |
| 419 | + "code" int NOT NULL DEFAULT '0', | |
| 420 | + "message" varchar(512) NOT NULL DEFAULT '', | |
| 421 | + "memo" varchar(512) DEFAULT '', | |
| 422 | + "creator" varchar(64) DEFAULT '', | |
| 423 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 424 | + "updater" varchar(64) DEFAULT '', | |
| 425 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 426 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 427 | + PRIMARY KEY ("id") | |
| 428 | +) COMMENT '错误码表'; | ... | ... |