diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/DateUtil.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/DateUtil.java index 67f06e5ad..a8666ef52 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/DateUtil.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/DateUtil.java @@ -133,4 +133,14 @@ public class DateUtil { && now.getTime() <= endTime.getTime(); } + public static Date max(Date a, Date b) { + if (a == null) { + return b; + } + if (b == null) { + return a; + } + return a.compareTo(b) > 0 ? a : b; + } + } diff --git a/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java b/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java index 2796bfbda..7f6328ed6 100644 --- a/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java +++ b/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java @@ -97,6 +97,10 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener { private GlobalException defaultExceptionHandler(Throwable exception, Invocation invocation) { logger.error("[defaultExceptionHandler][service({}) method({}) params({}) 执行异常]", invocation.getServiceName(), invocation.getServiceName(), invocation.getArguments(), exception); + // 如果已经是 GlobalException 全局异常,直接返回即可 + if (exception instanceof GlobalException) { + return (GlobalException) exception; + } return new GlobalException(INTERNAL_SERVER_ERROR) .setDetailMessage(this.buildDetailMessage(exception, invocation)); } diff --git a/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/dataobject/DeletableDO.java b/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/dataobject/DeletableDO.java index 457a1ec2d..9e63947c3 100644 --- a/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/dataobject/DeletableDO.java +++ b/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/dataobject/DeletableDO.java @@ -31,4 +31,5 @@ public class DeletableDO extends BaseDO { this.deleted = deleted; return this; } + } diff --git a/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/query/QueryWrapperX.java b/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/query/QueryWrapperX.java index e38336966..2def0b130 100644 --- a/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/query/QueryWrapperX.java +++ b/common/mall-spring-boot-starter-mybatis/src/main/java/cn/iocoder/mall/mybatis/core/query/QueryWrapperX.java @@ -44,6 +44,13 @@ public class QueryWrapperX extends QueryWrapper { return this; } + public QueryWrapperX gtIfPresent(String column, Object val) { + if (val != null) { + return (QueryWrapperX) super.gt(column, val); + } + return this; + } + // ========== 重写父类方法,方便链式调用 ========== @Override diff --git a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java index 1596c2209..3917bb73c 100644 --- a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java +++ b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java @@ -1,12 +1,15 @@ package cn.iocoder.mall.system.errorcode.config; import cn.iocoder.mall.system.errorcode.core.ErrorCodeAutoGenerator; +import cn.iocoder.mall.system.errorcode.core.ErrorCodeRemoteLoader; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @EnableConfigurationProperties(ErrorCodeProperties.class) +@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码 public class ErrorCodeAutoConfiguration { @Bean @@ -15,4 +18,9 @@ public class ErrorCodeAutoConfiguration { .setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass()); } + @Bean + public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) { + return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup()); + } + } diff --git a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java index cc72ab15b..7e9aef0b1 100644 --- a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java +++ b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java @@ -1,13 +1,18 @@ package cn.iocoder.mall.system.errorcode.config; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.NotNull; @ConfigurationProperties("mall.error-code") +@Validated public class ErrorCodeProperties { /** * 应用分组 */ + @NotNull(message = "应用分组不能为空,请设置 mall.error-code.group 配置项,推荐直接使用 spring. application.name 配置项") private String group; /** * 错误码枚举类 diff --git a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java index d8a36dcb9..49a3bb6df 100644 --- a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java +++ b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java @@ -2,18 +2,19 @@ package cn.iocoder.mall.system.errorcode.core; import cn.iocoder.common.framework.exception.ErrorCode; import cn.iocoder.common.framework.util.StringUtils; +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeRpc; import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO; +import org.apache.dubbo.config.annotation.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.function.Consumer; public class ErrorCodeAutoGenerator { @@ -28,6 +29,9 @@ public class ErrorCodeAutoGenerator { */ private String errorCodeConstantsClass; + @Reference(version = "${dubbo.consumer.ErrorCodeRpc.version}") + private ErrorCodeRpc errorCodeRpc; + public ErrorCodeAutoGenerator(String group) { this.group = group; } @@ -54,23 +58,26 @@ public class ErrorCodeAutoGenerator { } // 写入 system-service 服务 logger.info("[execute][自动将 ({}) 类的错误码,准备写入到 system-service 服务]", errorCodeConstantsClass); - List autoGenerateDTO = new ArrayList<>(); - Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(new Consumer() { - @Override - public void accept(Field field) { - if (field.getType() != ErrorCode.class) { - return; - } - try { - ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz); - autoGenerateDTO.add(new ErrorCodeAutoGenerateDTO().setGroup(group) - .setCode(errorCode.getCode()).setMessage(errorCode.getMessage())); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } + List autoGenerateDTOs = new ArrayList<>(); + Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> { + if (field.getType() != ErrorCode.class) { + return; + } + try { + ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz); + autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group) + .setCode(errorCode.getCode()).setMessage(errorCode.getMessage())); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); } }); - logger.info("[execute][自动将 ({}) 类的错误码,完成写入到 system-service 服务]", errorCodeConstantsClass); + CommonResult autoGenerateErrorCodesResult = errorCodeRpc.autoGenerateErrorCodes(autoGenerateDTOs); + if (autoGenerateErrorCodesResult.isSuccess()) { + logger.info("[execute][自动将 ({}) 类的错误码,成功写入到 system-service 服务]", errorCodeConstantsClass); + } else { + logger.error("[execute][自动将 ({}) 类的错误码,失败写入到 system-service 服务,原因为 ({}/{}/{})]", errorCodeConstantsClass, + autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMessage(), autoGenerateErrorCodesResult.getDetailMessage()); + } } } diff --git a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java index 83be579ed..5a0fa5fac 100644 --- a/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java +++ b/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java @@ -1,5 +1,7 @@ package cn.iocoder.mall.system.errorcode.core; +import cn.iocoder.common.framework.util.CollectionUtils; +import cn.iocoder.common.framework.util.DateUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeRpc; @@ -44,32 +46,25 @@ public class ErrorCodeRemoteLoader { listErrorCodesResult.getData().forEach(errorCodeVO -> { ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage()); // 记录下更新时间,方便增量更新 - maxUpdateTime = max(maxUpdateTime, errorCodeVO.getUpdateTime()); + maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime()); }); } - @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD) + @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) public void refreshErrorCodes() { // 从 ErrorCodeRpc 加载 ErrorCode 错误码 CommonResult> listErrorCodesResult = errorCodeRpc.listErrorCodes(group, maxUpdateTime); listErrorCodesResult.checkError(); + if (CollectionUtils.isEmpty(listErrorCodesResult.getData())) { + return; + } logger.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size()); // 写入到 ServiceExceptionUtil 到 listErrorCodesResult.getData().forEach(errorCodeVO -> { ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage()); // 记录下更新时间,方便增量更新 - maxUpdateTime = max(maxUpdateTime, errorCodeVO.getUpdateTime()); + maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime()); }); } - private static Date max(Date a, Date b) { - if (a == null) { - return b; - } - if (b == null) { - return a; - } - return a.compareTo(b) > 0 ? a : b; - } - } diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/ErrorCodeController.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/ErrorCodeController.java new file mode 100644 index 000000000..e81cf72e9 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/ErrorCodeController.java @@ -0,0 +1,67 @@ +package cn.iocoder.mall.managementweb.controller.errorcode; + +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodeUpdateDTO; +import cn.iocoder.mall.managementweb.controller.errorcode.vo.ErrorCodeVO; +import cn.iocoder.mall.managementweb.manager.errorcode.ErrorCodeManager; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +import static cn.iocoder.common.framework.vo.CommonResult.success; + +/** +* 错误码 Controller +*/ +@RestController +@RequestMapping("/error-code") +@Api(tags = "错误码") +@Validated +public class ErrorCodeController { + + @Autowired + private ErrorCodeManager errorCodeManager; + + @PostMapping("/create") + @ApiOperation("创建错误码") + public CommonResult createErrorCode(@Valid ErrorCodeCreateDTO createDTO) { + return success(errorCodeManager.createErrorCode(createDTO)); + } + + @PostMapping("/update") + @ApiOperation("更新错误码") + public CommonResult updateErrorCode(@Valid ErrorCodeUpdateDTO updateDTO) { + errorCodeManager.updateErrorCode(updateDTO); + return success(true); + } + + @PostMapping("/delete") + @ApiOperation("删除错误码") + @ApiImplicitParam(name = "errorCodeId", value = "错误码编号", required = true) + public CommonResult deleteErrorCode(@RequestParam("errorCodeId") Integer errorCodeId) { + errorCodeManager.deleteErrorCode(errorCodeId); + return success(true); + } + + @GetMapping("/get") + @ApiOperation("获得错误码") + @ApiImplicitParam(name = "errorCodeId", value = "错误码编号", required = true) + public CommonResult getErrorCode(@RequestParam("errorCodeId") Integer errorCodeId) { + return success(errorCodeManager.getErrorCode(errorCodeId)); + } + + @GetMapping("/page") + @ApiOperation("获得错误码分页") + public CommonResult> pageErrorCode(ErrorCodePageDTO pageDTO) { + return success(errorCodeManager.pageErrorCode(pageDTO)); + } + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodeCreateDTO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodeCreateDTO.java new file mode 100644 index 000000000..4abcdba2d --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodeCreateDTO.java @@ -0,0 +1,26 @@ +package cn.iocoder.mall.managementweb.controller.errorcode.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +@ApiModel("错误码创建 DTO") +@Data +public class ErrorCodeCreateDTO { + + @ApiModelProperty(value = "错误码编码", required = true, example = "10086") + @NotNull(message = "错误码编码不能为空") + private Integer code; + @ApiModelProperty(value = "错误码错误提示", required = true, example = "艿艿长的丑") + @NotEmpty(message = "错误码错误提示不能为空") + private String message; + @ApiModelProperty(value = "错误码分组", required = true, example = "user-service") + @NotEmpty(message = "错误码分组不能为空") + private String group; + @ApiModelProperty(value = "错误码备注", example = "我就是一个备注") + private String memo; + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodePageDTO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodePageDTO.java new file mode 100644 index 000000000..23fbc4046 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodePageDTO.java @@ -0,0 +1,21 @@ +package cn.iocoder.mall.managementweb.controller.errorcode.dto; + +import cn.iocoder.common.framework.vo.PageParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@ApiModel("错误码分页 DTO") +@Data +@EqualsAndHashCode(callSuper = true) +public class ErrorCodePageDTO extends PageParam { + + @ApiModelProperty(value = "错误码编码", required = true) + private Integer code; + @ApiModelProperty(value = "错误码错误提示", required = true) + private String message; + @ApiModelProperty(value = "错误码分组", required = true) + private String group; + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodeUpdateDTO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodeUpdateDTO.java new file mode 100644 index 000000000..1ba866760 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/dto/ErrorCodeUpdateDTO.java @@ -0,0 +1,29 @@ +package cn.iocoder.mall.managementweb.controller.errorcode.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +@ApiModel("错误码更新 DTO") +@Data +public class ErrorCodeUpdateDTO { + + @ApiModelProperty(value = "错误码编号", required = true, example = "1") + @NotNull(message = "错误码编号不能为空") + private Integer id; + @ApiModelProperty(value = "错误码编码", required = true, example = "10086") + @NotNull(message = "错误码编码不能为空") + private Integer code; + @ApiModelProperty(value = "错误码错误提示", required = true, example = "艿艿长的丑") + @NotEmpty(message = "错误码错误提示不能为空") + private String message; + @ApiModelProperty(value = "错误码分组", required = true, example = "user-service") + @NotEmpty(message = "错误码分组不能为空") + private String group; + @ApiModelProperty(value = "错误码备注", example = "我就是一个备注") + private String memo; + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/vo/ErrorCodeVO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/vo/ErrorCodeVO.java new file mode 100644 index 000000000..c86795cb8 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/errorcode/vo/ErrorCodeVO.java @@ -0,0 +1,28 @@ +package cn.iocoder.mall.managementweb.controller.errorcode.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.Date; + +@ApiModel("错误码 VO") +@Data +public class ErrorCodeVO { + + @ApiModelProperty(value = "错误码编号", required = true, example = "1") + private Integer id; + @ApiModelProperty(value = "错误码编码", required = true, example = "10086") + private Integer code; + @ApiModelProperty(value = "错误码错误提示", required = true, example = "艿艿长的丑") + private String message; + @ApiModelProperty(value = "错误码类型", required = true, notes = "见 ErrorCodeTypeEnum 枚举", example = "1") + private Integer type; + @ApiModelProperty(value = "错误码分组", required = true, example = "user-service") + private String group; + @ApiModelProperty(value = "错误码备注", example = "我就是一个备注") + private String memo; + @ApiModelProperty(value = "创建时间", required = true) + private Date createTime; + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/errorcode/ErrorCodeConvert.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/errorcode/ErrorCodeConvert.java new file mode 100644 index 000000000..a364e3090 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/errorcode/ErrorCodeConvert.java @@ -0,0 +1,30 @@ +package cn.iocoder.mall.managementweb.convert.errorcode; + +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.managementweb.controller.errorcode.vo.ErrorCodeVO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeUpdateDTO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper +public interface ErrorCodeConvert { + + ErrorCodeConvert INSTANCE = Mappers.getMapper(ErrorCodeConvert.class); + + ErrorCodeCreateDTO convert(cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodeCreateDTO bean); + + ErrorCodeUpdateDTO convert(cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodeUpdateDTO bean); + + ErrorCodeVO convert(cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO bean); + + List convertList(List list); + + PageResult convertPage(PageResult page); + + ErrorCodePageDTO convert(cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodePageDTO bean); + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/errorcode/ErrorCodeManager.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/errorcode/ErrorCodeManager.java new file mode 100644 index 000000000..2eccd05ba --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/errorcode/ErrorCodeManager.java @@ -0,0 +1,94 @@ +package cn.iocoder.mall.managementweb.manager.errorcode; + +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodeUpdateDTO; +import cn.iocoder.mall.managementweb.controller.errorcode.vo.ErrorCodeVO; +import cn.iocoder.mall.managementweb.convert.errorcode.ErrorCodeConvert; +import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeRpc; +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** +* 错误码 Manager +*/ +@Service +public class ErrorCodeManager { + + @Reference(version = "${dubbo.consumer.ErrorCodeRpc.version}", validation = "false") + private ErrorCodeRpc errorCodeRpc; + + /** + * 创建错误码 + * + * @param createDTO 创建错误码 DTO + * @return 错误码 + */ + public Integer createErrorCode(ErrorCodeCreateDTO createDTO) { + CommonResult createErrorCodeResult = errorCodeRpc.createErrorCode(ErrorCodeConvert.INSTANCE.convert(createDTO)); + createErrorCodeResult.checkError(); + return createErrorCodeResult.getData(); + } + + /** + * 更新错误码 + * + * @param updateDTO 更新错误码 DTO + */ + public void updateErrorCode(ErrorCodeUpdateDTO updateDTO) { + CommonResult updateErrorCodeResult = errorCodeRpc.updateErrorCode(ErrorCodeConvert.INSTANCE.convert(updateDTO)); + updateErrorCodeResult.checkError(); + } + + /** + * 删除错误码 + * + * @param errorCodeId 错误码编号 + */ + public void deleteErrorCode(Integer errorCodeId) { + CommonResult deleteErrorCodeResult = errorCodeRpc.deleteErrorCode(errorCodeId); + deleteErrorCodeResult.checkError(); + } + + /** + * 获得错误码 + * + * @param errorCodeId 错误码编号 + * @return 错误码 + */ + public ErrorCodeVO getErrorCode(Integer errorCodeId) { + CommonResult getErrorCodeResult = errorCodeRpc.getErrorCode(errorCodeId); + getErrorCodeResult.checkError(); + return ErrorCodeConvert.INSTANCE.convert(getErrorCodeResult.getData()); + } + + /** + * 获得错误码列表 + * + * @param errorCodeIds 错误码编号列表 + * @return 错误码列表 + */ + public List listErrorCodes(List errorCodeIds) { + CommonResult> listErrorCodeResult = errorCodeRpc.listErrorCodes(errorCodeIds); + listErrorCodeResult.checkError(); + return ErrorCodeConvert.INSTANCE.convertList(listErrorCodeResult.getData()); + } + + /** + * 获得错误码分页 + * + * @param pageDTO 错误码分页查询 + * @return 错误码分页结果 + */ + public PageResult pageErrorCode(ErrorCodePageDTO pageDTO) { + CommonResult> pageErrorCodeResult + = errorCodeRpc.pageErrorCode(ErrorCodeConvert.INSTANCE.convert(pageDTO)); + pageErrorCodeResult.checkError(); + return ErrorCodeConvert.INSTANCE.convertPage(pageErrorCodeResult.getData()); + } + +} diff --git a/management-web-app/src/main/resources/application.yml b/management-web-app/src/main/resources/application.yml index a45906dae..0a24c2fd0 100644 --- a/management-web-app/src/main/resources/application.yml +++ b/management-web-app/src/main/resources/application.yml @@ -47,6 +47,8 @@ dubbo: version: 1.0.0 SystemExceptionLogRpc: version: 1.0.0 + ErrorCodeRpc: + version: 1.0.0 # Swagger 配置项 swagger: diff --git a/pom.xml b/pom.xml index 657fa5bbb..a2f94469e 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,6 @@ - mall-dependencies user-service-project @@ -34,4 +33,50 @@ pom + + + 1.8 + + 1.16.14 + 1.3.0.Final + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java.version} + ${java.version} + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + + org.projectlombok + lombok + ${org.projectlombok.version} + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeConstants.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeConstants.java index 5f67f80f8..fd38b36ef 100644 --- a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeConstants.java +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeConstants.java @@ -25,13 +25,12 @@ public interface SystemErrorCodeConstants { ErrorCode ADMIN_NOT_FOUND = new ErrorCode(1002002000, "管理员不存在"); ErrorCode ADMIN_PASSWORD_ERROR = new ErrorCode(1002002001, "密码不正确"); ErrorCode ADMIN_IS_DISABLE = new ErrorCode(1002002002, "账号被禁用"); - ErrorCode ADMIN_USERNAME_EXISTS = new ErrorCode(1002002002, "账号已经存在"); - ErrorCode ADMIN_STATUS_EQUALS = new ErrorCode(1002002003, "账号已经是该状态"); -// ErrorCode ADMIN_DELETE_ONLY_DISABLE = new ErrorCode(1002002004, "只有关闭的账号才可以删除"); + ErrorCode ADMIN_USERNAME_EXISTS = new ErrorCode(1002002003, "账号已经存在"); + ErrorCode ADMIN_STATUS_EQUALS = new ErrorCode(1002002004, "账号已经是该状态"); ErrorCode ADMIN_ADMIN_STATUS_CAN_NOT_UPDATE = new ErrorCode(1002002005, "管理员的账号状态不允许变更"); ErrorCode ADMIN_ASSIGN_ROLE_NOT_EXISTS = new ErrorCode(1002002006, "分配员工角色时,有角色不存在"); ErrorCode ADMIN_ADMIN_CAN_NOT_UPDATE = new ErrorCode(1002002008, "管理员的账号不允许变更"); - ErrorCode ADMIN_USERNAME_NOT_EXISTS = new ErrorCode(1002002008, "账号不存在"); + ErrorCode ADMIN_USERNAME_NOT_EXISTS = new ErrorCode(1002002009, "账号不存在"); // ========== 资源模块 1002003000 ========== ErrorCode RESOURCE_NAME_DUPLICATE = new ErrorCode(1002003000, "已经存在该名字的资源"); @@ -49,8 +48,8 @@ public interface SystemErrorCodeConstants { ErrorCode ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE = new ErrorCode(1002004005, "不能删除类型为系统内置的角色"); // ========== 数据字典模块 1002005000 ========== - ErrorCode DATA_DICT_EXISTS = new ErrorCode(1002005000, "该数据字典已经存在"); - ErrorCode DATA_DICT_NOT_EXISTS = new ErrorCode(1002005001, "该数据字典不存在"); + ErrorCode DATA_DICT_EXISTS = new ErrorCode(1002005000, "数据字典已经存在"); + ErrorCode DATA_DICT_NOT_EXISTS = new ErrorCode(1002005001, "数据字典不存在"); // ========== 短信模板 1002006000 ========== ErrorCode SMS_PLATFORM_FAIL = new ErrorCode(1002006000, "短信平台调用失败【具体错误会动态替换】"); diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/errorcode/ErrorCodeTypeEnum.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/errorcode/ErrorCodeTypeEnum.java index a7b32eeee..9c40b38fe 100644 --- a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/errorcode/ErrorCodeTypeEnum.java +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/errorcode/ErrorCodeTypeEnum.java @@ -1,5 +1,9 @@ package cn.iocoder.mall.systemservice.enums.errorcode; +import cn.iocoder.common.framework.core.IntArrayValuable; + +import java.util.Arrays; + /** * 错误码的类型枚举 * @@ -8,7 +12,7 @@ package cn.iocoder.mall.systemservice.enums.errorcode; * * @author ding */ -public enum ErrorCodeTypeEnum { +public enum ErrorCodeTypeEnum implements IntArrayValuable { /** * 自动生成 @@ -19,6 +23,8 @@ public enum ErrorCodeTypeEnum { */ MANUAL_OPERATION(2); + public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(ErrorCodeTypeEnum::getType).toArray(); + private final Integer type; ErrorCodeTypeEnum(Integer type) { @@ -29,4 +35,9 @@ public enum ErrorCodeTypeEnum { return type; } + @Override + public int[] array() { + return ARRAYS; + } + } diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpc.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpc.java index 1509562bc..837e12dcc 100644 --- a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpc.java +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpc.java @@ -1,16 +1,80 @@ package cn.iocoder.mall.systemservice.rpc.errorcode; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeUpdateDTO; import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO; +import javax.validation.constraints.NotNull; import java.util.Date; import java.util.List; public interface ErrorCodeRpc { - CommonResult> listErrorCodes(String group, Date minUpdateTime); + /** + * 获得指定分组下的错误码列表 + * + * @param group 错误码分组 + * @param minUpdateTime 最小更新时间,允许为空。 + * 通过该参数,我们可以增量获取超过 minUpdateTime 时间的错误码 + * @return 错误码列表 + */ + CommonResult> listErrorCodes(@NotNull(message = "错误码分组不能为空") String group, Date minUpdateTime); - CommonResult autoGenerateErrorCodes(ErrorCodeAutoGenerateDTO autoGenerateDTO); + /** + * 自动生成错误码 + * + * @param autoGenerateDTOs 自动生成信息 DTO + */ + CommonResult autoGenerateErrorCodes(List autoGenerateDTOs); + + /** + * 创建错误码 + * + * @param createDTO 创建错误码 DTO + * @return 错误码编号 + */ + CommonResult createErrorCode(ErrorCodeCreateDTO createDTO); + + /** + * 更新错误码 + * + * @param updateDTO 更新错误码 DTO + */ + CommonResult updateErrorCode(ErrorCodeUpdateDTO updateDTO); + + /** + * 删除错误码 + * + * @param errorCodeId 错误码编号 + */ + CommonResult deleteErrorCode(Integer errorCodeId); + + /** + * 获得错误码 + * + * @param errorCodeId 错误码编号 + * @return 错误码 + */ + CommonResult getErrorCode(Integer errorCodeId); + + /** + * 获得错误码列表 + * + * @param errorCodeIds 错误码编号列表 + * @return 错误码列表 + */ + CommonResult> listErrorCodes(List errorCodeIds); + + /** + * 获得错误码分页 + * + * @param pageDTO 错误码分页查询 + * @return 错误码分页结果 + */ + CommonResult> pageErrorCode(ErrorCodePageDTO pageDTO); } diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeAutoGenerateDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeAutoGenerateDTO.java index e280bcb73..155c7af86 100644 --- a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeAutoGenerateDTO.java +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeAutoGenerateDTO.java @@ -3,8 +3,13 @@ package cn.iocoder.mall.systemservice.rpc.errorcode.dto; import lombok.Data; import lombok.experimental.Accessors; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; import java.io.Serializable; +/** + * 错误码自动生成 DTO + */ @Data @Accessors(chain = true) public class ErrorCodeAutoGenerateDTO implements Serializable { @@ -12,14 +17,17 @@ public class ErrorCodeAutoGenerateDTO implements Serializable { /** * 错误码编码 */ + @NotNull(message = "错误码编码不能为空") private Integer code; /** * 错误码错误提示 */ + @NotEmpty(message = "错误码错误提示不能为空") private String message; /** * 错误码分组 */ + @NotNull(message = "错误码分组不能为空") private String group; } diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeCreateDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeCreateDTO.java new file mode 100644 index 000000000..116853802 --- /dev/null +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeCreateDTO.java @@ -0,0 +1,45 @@ +package cn.iocoder.mall.systemservice.rpc.errorcode.dto; + +import cn.iocoder.common.framework.validator.InEnum; +import cn.iocoder.mall.systemservice.enums.errorcode.ErrorCodeTypeEnum; +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** +* 错误码创建 DTO +*/ +@Data +@Accessors(chain = true) +public class ErrorCodeCreateDTO implements Serializable { + + /** + * 错误码编码 + */ + @NotNull(message = "错误码编码不能为空") + private Integer code; + /** + * 错误码错误提示 + */ + @NotEmpty(message = "错误码错误提示不能为空") + private String message; + /** + * 错误码类型 + */ + @NotNull(message = "错误码类型不能为空") + @InEnum(value = ErrorCodeTypeEnum.class, message = "错误码类型必须是 {value}") + private Integer type; + /** + * 错误码分组 + */ + @NotNull(message = "错误码分组不能为空") + private String group; + /** + * 错误码备注 + */ + private String memo; + +} diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodePageDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodePageDTO.java new file mode 100644 index 000000000..b071c6bf8 --- /dev/null +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodePageDTO.java @@ -0,0 +1,33 @@ +package cn.iocoder.mall.systemservice.rpc.errorcode.dto; + +import cn.iocoder.common.framework.vo.PageParam; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** +* 错误码分页 DTO +*/ +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +public class ErrorCodePageDTO extends PageParam { + + /** + * 错误码编码 + */ + private Integer code; + /** + * 错误码错误提示 + * + * 模糊匹配 + */ + private String message; + /** + * 错误码分组 + * + * 模糊匹配 + */ + private String group; + +} diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeUpdateDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeUpdateDTO.java new file mode 100644 index 000000000..293784904 --- /dev/null +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/dto/ErrorCodeUpdateDTO.java @@ -0,0 +1,46 @@ +package cn.iocoder.mall.systemservice.rpc.errorcode.dto; + +import cn.iocoder.common.framework.validator.InEnum; +import cn.iocoder.mall.systemservice.enums.errorcode.ErrorCodeTypeEnum; +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** +* 错误码更新 DTO +*/ +@Data +@Accessors(chain = true) +public class ErrorCodeUpdateDTO implements Serializable { + + /** + * 错误码编号 + */ + @NotNull(message = "错误码编号不能为空") + private Integer id; + /** + * 错误码编码 + */ + @NotNull(message = "错误码编码不能为空") + private Integer code; + /** + * 错误码错误提示 + */ + private String message; + /** + * 错误码类型 + */ + @InEnum(value = ErrorCodeTypeEnum.class, message = "错误码类型必须是 {value}") + private Integer type; + /** + * 错误码分组 + */ + private String group; + /** + * 错误码备注 + */ + private String memo; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/errorcode/ErrorCodeConvert.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/errorcode/ErrorCodeConvert.java new file mode 100644 index 000000000..e80f818f2 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/errorcode/ErrorCodeConvert.java @@ -0,0 +1,50 @@ +package cn.iocoder.mall.systemservice.convert.errorcode; + +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.systemservice.dal.mysql.dataobject.errorcode.ErrorCodeDO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeUpdateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO; +import cn.iocoder.mall.systemservice.service.errorcode.bo.*; +import com.baomidou.mybatisplus.core.metadata.IPage; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper +public interface ErrorCodeConvert { + + ErrorCodeConvert INSTANCE = Mappers.getMapper(ErrorCodeConvert.class); + + ErrorCodeDO convert(ErrorCodeCreateBO bean); + + ErrorCodeBO convert(ErrorCodeDO bean); + + List convertList(List list); + + @Mapping(source = "records", target = "list") + PageResult convertPage(IPage page); + + ErrorCodeDO convert(ErrorCodeUpdateBO bean); + + ErrorCodeCreateBO convert(ErrorCodeCreateDTO bean); + + ErrorCodeUpdateBO convert(ErrorCodeUpdateDTO bean); + + ErrorCodeVO convert(ErrorCodeBO bean); + + List convertList02(List list); + + PageResult convertPage(PageResult page); + + ErrorCodePageBO convert(ErrorCodePageDTO bean); + + ErrorCodeDO convert(ErrorCodeAutoGenerateBO bean); + + List convertList03(List list); + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/errorcode/ErrorCodeDO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/errorcode/ErrorCodeDO.java index 5f43daf64..97d40085a 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/errorcode/ErrorCodeDO.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/errorcode/ErrorCodeDO.java @@ -2,6 +2,7 @@ package cn.iocoder.mall.systemservice.dal.mysql.dataobject.errorcode; import cn.iocoder.mall.mybatis.core.dataobject.DeletableDO; import cn.iocoder.mall.systemservice.enums.errorcode.ErrorCodeTypeEnum; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import lombok.EqualsAndHashCode; @@ -10,7 +11,7 @@ import lombok.experimental.Accessors; /** * 错误码实体 */ -@TableName(value = "error_code") +@TableName(value = "system_error_code") @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) @@ -39,7 +40,8 @@ public class ErrorCodeDO extends DeletableDO { * * 一般情况下,可以采用应用名 */ - private Integer group; + @TableField("`group`") // 避免和数据库关键字冲突 + private String group; /** * 错误码备注 */ diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/errorcode/ErrorCodeMapper.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/errorcode/ErrorCodeMapper.java new file mode 100644 index 000000000..c1ff0b55f --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/errorcode/ErrorCodeMapper.java @@ -0,0 +1,38 @@ +package cn.iocoder.mall.systemservice.dal.mysql.mapper.errorcode; + +import cn.iocoder.mall.mybatis.core.query.QueryWrapperX; +import cn.iocoder.mall.systemservice.dal.mysql.dataobject.errorcode.ErrorCodeDO; +import cn.iocoder.mall.systemservice.service.errorcode.bo.ErrorCodePageBO; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.springframework.stereotype.Repository; + +import java.util.Collection; +import java.util.Date; +import java.util.List; + +@Repository +public interface ErrorCodeMapper extends BaseMapper { + + default IPage selectPage(ErrorCodePageBO pageBO) { + return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()), + new QueryWrapperX().likeIfPresent("`group`", pageBO.getGroup()) + .eqIfPresent("code", pageBO.getCode()).likeIfPresent("message", pageBO.getMessage())); + } + + default List selectListByCodes(Collection codes) { + return selectList(new QueryWrapper().in("code", codes)); + } + + default ErrorCodeDO selectByCode(Integer code) { + return selectOne(new QueryWrapper().eq("code", code)); + } + + default List selectListByGroup(String group, Date minUpdateTime) { + return selectList(new QueryWrapperX().eq("`group`", group) + .gtIfPresent("update_time", minUpdateTime)); + } + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/errorcode/ErrorCodeManager.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/errorcode/ErrorCodeManager.java new file mode 100644 index 000000000..3a0924c75 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/errorcode/ErrorCodeManager.java @@ -0,0 +1,103 @@ +package cn.iocoder.mall.systemservice.manager.errorcode; + +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.systemservice.convert.errorcode.ErrorCodeConvert; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeUpdateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO; +import cn.iocoder.mall.systemservice.service.errorcode.ErrorCodeService; +import cn.iocoder.mall.systemservice.service.errorcode.bo.ErrorCodeBO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +/** +* 错误码 Manager +*/ +@Service +public class ErrorCodeManager { + + @Autowired + private ErrorCodeService errorCodeService; + + /** + * 创建错误码 + * + * @param createDTO 创建错误码 DTO + * @return 错误码 + */ + public Integer createErrorCode(ErrorCodeCreateDTO createDTO) { + ErrorCodeBO errorCodeBO = errorCodeService.createErrorCode(ErrorCodeConvert.INSTANCE.convert(createDTO)); + return errorCodeBO.getId(); + } + + /** + * 更新错误码 + * + * @param updateDTO 更新错误码 DTO + */ + public void updateErrorCode(ErrorCodeUpdateDTO updateDTO) { + errorCodeService.updateErrorCode(ErrorCodeConvert.INSTANCE.convert(updateDTO)); + } + + /** + * 自动生成错误码 + * + * @param autoGenerateDTOs 自动生成信息 DTO + */ + public void autoGenerateErrorCodes(List autoGenerateDTOs) { + errorCodeService.autoGenerateErrorCodes(ErrorCodeConvert.INSTANCE.convertList03(autoGenerateDTOs)); + } + + /** + * 删除错误码 + * + * @param errorCodeId 错误码编号 + */ + public void deleteErrorCode(Integer errorCodeId) { + errorCodeService.deleteErrorCode(errorCodeId); + } + + /** + * 获得错误码 + * + * @param errorCodeId 错误码编号 + * @return 错误码 + */ + public ErrorCodeVO getErrorCode(Integer errorCodeId) { + ErrorCodeBO errorCodeBO = errorCodeService.getErrorCode(errorCodeId); + return ErrorCodeConvert.INSTANCE.convert(errorCodeBO); + } + + /** + * 获得错误码列表 + * + * @param errorCodeIds 错误码编号列表 + * @return 错误码列表 + */ + public List listErrorCodes(List errorCodeIds) { + List errorCodeBOs = errorCodeService.listErrorCodes(errorCodeIds); + return ErrorCodeConvert.INSTANCE.convertList02(errorCodeBOs); + } + + /** + * 获得错误码分页 + * + * @param pageDTO 错误码分页查询 + * @return 错误码分页结果 + */ + public PageResult pageErrorCode(ErrorCodePageDTO pageDTO) { + PageResult pageResultBO = errorCodeService.pageErrorCode(ErrorCodeConvert.INSTANCE.convert(pageDTO)); + return ErrorCodeConvert.INSTANCE.convertPage(pageResultBO); + } + + public List listErrorCodes(String group, Date minUpdateTime) { + List errorCodeBOs = errorCodeService.listErrorCodes(group, minUpdateTime); + return ErrorCodeConvert.INSTANCE.convertList02(errorCodeBOs); + } + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpcImpl.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpcImpl.java index 8115411d5..a9add651e 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpcImpl.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/errorcode/ErrorCodeRpcImpl.java @@ -1,24 +1,68 @@ package cn.iocoder.mall.systemservice.rpc.errorcode; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.systemservice.manager.errorcode.ErrorCodeManager; import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeCreateDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodePageDTO; +import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeUpdateDTO; import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO; import org.apache.dubbo.config.annotation.Service; +import org.springframework.beans.factory.annotation.Autowired; import java.util.Date; import java.util.List; +import static cn.iocoder.common.framework.vo.CommonResult.success; + @Service(version = "${dubbo.provider.ErrorCodeRpc.version}") public class ErrorCodeRpcImpl implements ErrorCodeRpc { + @Autowired + private ErrorCodeManager errorCodeManager; + @Override public CommonResult> listErrorCodes(String group, Date minUpdateTime) { - return null; + return success(errorCodeManager.listErrorCodes(group, minUpdateTime)); } @Override - public CommonResult autoGenerateErrorCodes(ErrorCodeAutoGenerateDTO autoGenerateDTO) { - return null; + public CommonResult autoGenerateErrorCodes(List autoGenerateDTOs) { + errorCodeManager.autoGenerateErrorCodes(autoGenerateDTOs); + return success(true); + } + + @Override + public CommonResult createErrorCode(ErrorCodeCreateDTO createDTO) { + return success(errorCodeManager.createErrorCode(createDTO)); + } + + @Override + public CommonResult updateErrorCode(ErrorCodeUpdateDTO updateDTO) { + errorCodeManager.updateErrorCode(updateDTO); + return success(true); + } + + @Override + public CommonResult deleteErrorCode(Integer errorCodeId) { + errorCodeManager.deleteErrorCode(errorCodeId); + return success(true); + } + + @Override + public CommonResult getErrorCode(Integer errorCodeId) { + return success(errorCodeManager.getErrorCode(errorCodeId)); + } + + @Override + public CommonResult> listErrorCodes(List errorCodeIds) { + return success(errorCodeManager.listErrorCodes(errorCodeIds)); + } + + @Override + public CommonResult> pageErrorCode(ErrorCodePageDTO pageDTO) { + return success(errorCodeManager.pageErrorCode(pageDTO)); } } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/ErrorCodeService.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/ErrorCodeService.java new file mode 100644 index 000000000..3616bf1de --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/ErrorCodeService.java @@ -0,0 +1,181 @@ +package cn.iocoder.mall.systemservice.service.errorcode; + +import cn.iocoder.common.framework.util.CollectionUtils; +import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.systemservice.convert.errorcode.ErrorCodeConvert; +import cn.iocoder.mall.systemservice.dal.mysql.dataobject.errorcode.ErrorCodeDO; +import cn.iocoder.mall.systemservice.dal.mysql.mapper.errorcode.ErrorCodeMapper; +import cn.iocoder.mall.systemservice.enums.errorcode.ErrorCodeTypeEnum; +import cn.iocoder.mall.systemservice.service.errorcode.bo.*; +import com.baomidou.mybatisplus.core.metadata.IPage; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; + +import javax.validation.Valid; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants.ERROR_CODE_DUPLICATE; +import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants.ERROR_CODE_NOT_EXISTS; + +/** +* 错误码 Service +*/ +@Service +@Validated +@Slf4j +public class ErrorCodeService { + + @Autowired + private ErrorCodeMapper errorCodeMapper; + + /** + * 创建错误码 + * + * @param createBO 创建错误码 BO + * @return 错误码 + */ + public ErrorCodeBO createErrorCode(@Valid ErrorCodeCreateBO createBO) { + checkDuplicateErrorCode(createBO.getCode(), null); + // 插入到数据库 + ErrorCodeDO errorCodeDO = ErrorCodeConvert.INSTANCE.convert(createBO); + errorCodeMapper.insert(errorCodeDO); + // 返回 + return ErrorCodeConvert.INSTANCE.convert(errorCodeDO); + } + + /** + * 更新错误码 + * + * @param updateBO 更新错误码 BO + */ + public void updateErrorCode(@Valid ErrorCodeUpdateBO updateBO) { + checkDuplicateErrorCode(updateBO.getCode(), updateBO.getId()); + // 校验更新的错误码是否存在 + if (errorCodeMapper.selectById(updateBO.getId()) == null) { + throw ServiceExceptionUtil.exception(ERROR_CODE_NOT_EXISTS); + } + // 更新到数据库 + ErrorCodeDO updateObject = ErrorCodeConvert.INSTANCE.convert(updateBO); + errorCodeMapper.updateById(updateObject); + } + + @Transactional + public void autoGenerateErrorCodes(@Valid List autoGenerateBOs) { + if (CollectionUtils.isEmpty(autoGenerateBOs)) { + return; + } + List errorCodeDOs = errorCodeMapper.selectListByCodes( + CollectionUtils.convertSet(autoGenerateBOs, ErrorCodeAutoGenerateBO::getCode)); + Map errorCodeDOMap = CollectionUtils.convertMap(errorCodeDOs, ErrorCodeDO::getCode); + // 遍历 autoGenerateBOs 数组,逐个插入或更新。考虑到每次量级不大,就不走批量了 + autoGenerateBOs.forEach(autoGenerateBO -> { + ErrorCodeDO errorCodeDO = errorCodeDOMap.get(autoGenerateBO.getCode()); + // 不存在,则进行新增 + if (errorCodeDO == null) { + errorCodeDO = ErrorCodeConvert.INSTANCE.convert(autoGenerateBO) + .setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); + errorCodeMapper.insert(errorCodeDO); + return; + } + // 存在,则进行更新。更新有三个前置条件: + // 条件 1. 只更新自动生成的错误码,即 Type 为 ErrorCodeTypeEnum.AUTO_GENERATION + if (!ErrorCodeTypeEnum.AUTO_GENERATION.getType().equals(errorCodeDO.getType())) { + return; + } + // 条件 2. 分组 group 必须匹配,避免存在错误码冲突的情况 + if (!autoGenerateBO.getGroup().equals(errorCodeDO.getGroup())) { + log.error("[autoGenerateErrorCodes][自动创建({}/{}) 错误码失败,数据库中已经存在({}/{})]", + autoGenerateBO.getCode(), autoGenerateBO.getGroup(), + errorCodeDO.getCode(), errorCodeDO.getGroup()); + return; + } + // 条件 3. 错误提示语存在差异 + if (autoGenerateBO.getMessage().equals(errorCodeDO.getMessage())) { + return; + } + // 最终匹配,进行更新 + errorCodeMapper.updateById(new ErrorCodeDO().setId(errorCodeDO.getId()).setMessage(autoGenerateBO.getMessage())); + }); + } + + /** + * 删除错误码 + * + * @param errorCodeId 错误码编号 + */ + public void deleteErrorCode(Integer errorCodeId) { + // 校验删除的错误码是否存在 + if (errorCodeMapper.selectById(errorCodeId) == null) { + throw ServiceExceptionUtil.exception(ERROR_CODE_NOT_EXISTS); + } + // 标记删除 + errorCodeMapper.deleteById(errorCodeId); + } + + /** + * 获得错误码 + * + * @param errorCodeId 错误码编号 + * @return 错误码 + */ + public ErrorCodeBO getErrorCode(Integer errorCodeId) { + ErrorCodeDO errorCodeDO = errorCodeMapper.selectById(errorCodeId); + return ErrorCodeConvert.INSTANCE.convert(errorCodeDO); + } + + /** + * 获得错误码列表 + * + * @param errorCodeIds 错误码编号列表 + * @return 错误码列表 + */ + public List listErrorCodes(List errorCodeIds) { + List errorCodeDOs = errorCodeMapper.selectBatchIds(errorCodeIds); + return ErrorCodeConvert.INSTANCE.convertList(errorCodeDOs); + } + + /** + * 获得错误码分页 + * + * @param pageBO 错误码分页查询 + * @return 错误码分页结果 + */ + public PageResult pageErrorCode(ErrorCodePageBO pageBO) { + IPage errorCodeDOPage = errorCodeMapper.selectPage(pageBO); + return ErrorCodeConvert.INSTANCE.convertPage(errorCodeDOPage); + } + + /** + * 校验错误码的唯一字段是否重复 + * + * 是否存在相同编码的错误码 + * + * @param code 错误码编码 + * @param id 错误码编号 + */ + private void checkDuplicateErrorCode(Integer code, Integer id) { + ErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code); + if (errorCodeDO == null) { + return; + } + // 如果 id 为空,说明不用比较是否为相同 id 的错误码 + if (id == null) { + throw ServiceExceptionUtil.exception(ERROR_CODE_DUPLICATE); + } + if (!errorCodeDO.getId().equals(id)) { + throw ServiceExceptionUtil.exception(ERROR_CODE_DUPLICATE); + } + } + + public List listErrorCodes(String group, Date minUpdateTime) { + List errorCodeDOs = errorCodeMapper.selectListByGroup(group, minUpdateTime); + return ErrorCodeConvert.INSTANCE.convertList(errorCodeDOs); + } + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeAutoGenerateBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeAutoGenerateBO.java new file mode 100644 index 000000000..795aff3e3 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeAutoGenerateBO.java @@ -0,0 +1,33 @@ +package cn.iocoder.mall.systemservice.service.errorcode.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * 错误码自动生成 BO + */ +@Data +@Accessors(chain = true) +public class ErrorCodeAutoGenerateBO implements Serializable { + + /** + * 错误码编码 + */ + @NotNull(message = "错误码编码不能为空") + private Integer code; + /** + * 错误码错误提示 + */ + @NotEmpty(message = "错误码错误提示不能为空") + private String message; + /** + * 错误码分组 + */ + @NotNull(message = "错误码分组不能为空") + private String group; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeBO.java new file mode 100644 index 000000000..b0fe3a5ea --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeBO.java @@ -0,0 +1,48 @@ +package cn.iocoder.mall.systemservice.service.errorcode.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.util.Date; + +/** +* 错误码 BO +*/ +@Data +@Accessors(chain = true) +public class ErrorCodeBO { + + /** + * 错误码编号 + */ + private Integer id; + /** + * 错误码编码 + */ + private Integer code; + /** + * 错误码错误提示 + */ + private String message; + /** + * 错误码类型 + */ + private Integer type; + /** + * 错误码分组 + */ + private String group; + /** + * 错误码备注 + */ + private String memo; + /** + * 创建时间 + */ + private Date createTime; + /** + * 最后更新时间 + */ + private Date updateTime; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeCreateBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeCreateBO.java new file mode 100644 index 000000000..ad412c6c8 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeCreateBO.java @@ -0,0 +1,40 @@ +package cn.iocoder.mall.systemservice.service.errorcode.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** +* 错误码创建 BO +*/ +@Data +@Accessors(chain = true) +public class ErrorCodeCreateBO { + + /** + * 错误码编码 + */ + @NotNull(message = "错误码编码不能为空") + private Integer code; + /** + * 错误码错误提示 + */ + @NotEmpty(message = "错误码错误提示不能为空") + private String message; + /** + * 错误码类型 + */ + @NotNull(message = "错误码类型不能为空") + private Integer type; + /** + * 错误码分组 + */ + private String group; + /** + * 错误码备注 + */ + private String memo; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodePageBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodePageBO.java new file mode 100644 index 000000000..f869e4b79 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodePageBO.java @@ -0,0 +1,29 @@ +package cn.iocoder.mall.systemservice.service.errorcode.bo; + +import cn.iocoder.common.framework.vo.PageParam; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** +* 错误码分页 BO +*/ +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +public class ErrorCodePageBO extends PageParam { + + /** + * 错误码编码 + */ + private Integer code; + /** + * 错误码错误提示 + */ + private String message; + /** + * 错误码分组 + */ + private String group; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeUpdateBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeUpdateBO.java new file mode 100644 index 000000000..0db5a1bb4 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/errorcode/bo/ErrorCodeUpdateBO.java @@ -0,0 +1,45 @@ +package cn.iocoder.mall.systemservice.service.errorcode.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** +* 错误码更新 BO +*/ +@Data +@Accessors(chain = true) +public class ErrorCodeUpdateBO { + + /** + * 错误码编号 + */ + @NotNull(message = "错误码编号不能为空") + private Integer id; + /** + * 错误码编码 + */ + @NotNull(message = "错误码编码不能为空") + private Integer code; + /** + * 错误码错误提示 + */ + @NotEmpty(message = "错误码错误提示不能为空") + private String message; + /** + * 错误码类型 + */ + @NotNull(message = "错误码类型不能为空") + private Integer type; + /** + * 错误码分组 + */ + private String group; + /** + * 错误码备注 + */ + private String memo; + +} diff --git a/system-service-project/system-service-app/src/main/resources/application.yaml b/system-service-project/system-service-app/src/main/resources/application.yaml index 44cf654f4..fb911809f 100644 --- a/system-service-project/system-service-app/src/main/resources/application.yaml +++ b/system-service-project/system-service-app/src/main/resources/application.yaml @@ -54,8 +54,14 @@ dubbo: version: 1.0.0 ErrorCodeRpc: version: 1.0.0 + # Dubbo 服务消费者的配置 + consumer: + ErrorCodeRpc: + version: 1.0.0 # Mall 配置项 mall: + # 错误码配置项对应 ErrorCodeProperties 配置类 error-code: + group: ${spring.application.name} constants-class: cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/bo/errorcode/ErrorCodeBO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/bo/errorcode/ErrorCodeBO.java deleted file mode 100644 index 0672e6142..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/bo/errorcode/ErrorCodeBO.java +++ /dev/null @@ -1,31 +0,0 @@ -package cn.iocoder.mall.system.biz.bo.errorcode; - -import lombok.Data; -import lombok.experimental.Accessors; - -import java.util.Date; - -/** - * 错误码模块 - 错误码信息 BO - * @author ding - */ -@Data -@Accessors(chain = true) -public class ErrorCodeBO { - /** - * 错误码编号 - */ - private Integer id; - /** - * 错误码编码 - */ - private Integer code; - /** - * 错误码错误信息 - */ - private String message; - /** - * 添加时间 - */ - private Date createTime; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/DatabaseConfiguration.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/DatabaseConfiguration.java deleted file mode 100644 index 95ced3e8a..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/DatabaseConfiguration.java +++ /dev/null @@ -1,28 +0,0 @@ -package cn.iocoder.mall.system.biz.config; - -import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; -import com.baomidou.mybatisplus.core.injector.ISqlInjector; -import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; -import org.mybatis.spring.annotation.MapperScan; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@Configuration -@MapperScan("cn.iocoder.mall.system.biz.dao") // 扫描对应的 Mapper 接口 -@EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600 -public class DatabaseConfiguration { - - // 数据库连接池 Druid - - @Bean - public ISqlInjector sqlInjector() { - return new DefaultSqlInjector(); // MyBatis Plus 逻辑删除 - } - - @Bean - public PaginationInterceptor paginationInterceptor() { - return new PaginationInterceptor(); // MyBatis Plus 分页插件 - } - -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/ServiceExceptionConfiguration.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/ServiceExceptionConfiguration.java deleted file mode 100644 index 7f57927ec..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/ServiceExceptionConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.iocoder.mall.system.biz.config; - -import cn.iocoder.common.framework.util.ServiceExceptionUtil; -import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum; -import cn.iocoder.mall.system.biz.service.errorcode.ErrorCodeService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.event.EventListener; - -@Configuration -public class ServiceExceptionConfiguration { - - // TODO FROM 芋艿 to 鱿鱼须:这块的实现,微信一起沟通下哈。大体是说,要调用 RPC 接口,不然别的模块无法使用哟。最终,我们是要做成 starter,提供给各个模块用。 - @Autowired - private ErrorCodeService errorCodeService; - - @EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html - public void initMessages() { - // TODO FROM 芋艿 to 芋艿:暂时注释掉,有问题的。 -// errorCodeService.deleteSyStemErrorCode(SystemErrorCodeEnum.ADMIN_NOT_FOUND.getGroup()); -// errorCodeService.addSystemErrorCodeList(SystemErrorCodeEnum.values()); - for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) { - ServiceExceptionUtil.put(item.getCode(), item.getMessage()); - } - // TODO FROM 芋艿 to 芋艿:暂时注释掉,有问题的。 -// for (ErrorCodeBO bo : errorCodeService.getErrorCodeByGroup(SystemErrorCodeEnum.ADMIN_NOT_FOUND.getGroup())) { -// ServiceExceptionUtil.put(bo.getCode(),bo.getMessage()); -// } - } - -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/convert/errorcode/ErrorCodeConvert.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/convert/errorcode/ErrorCodeConvert.java deleted file mode 100644 index 279cb2a84..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/convert/errorcode/ErrorCodeConvert.java +++ /dev/null @@ -1,37 +0,0 @@ -package cn.iocoder.mall.system.biz.convert.errorcode; - -import cn.iocoder.common.framework.vo.PageResult; -import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; -import cn.iocoder.mall.system.biz.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeAddDTO; -import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeDTO; -import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; -import com.baomidou.mybatisplus.core.metadata.IPage; -import org.mapstruct.Mapper; -import org.mapstruct.Mapping; -import org.mapstruct.factory.Mappers; - -import java.util.List; - -/** - * @author ding - */ -@Mapper -public interface ErrorCodeConvert { - - ErrorCodeConvert INSTANCE = Mappers.getMapper(ErrorCodeConvert.class); - - ErrorCodeDO convert(ErrorCodeDTO bean); - - ErrorCodeBO convert(ErrorCodeDO bean); - - List convertList(List beans); - - @Mapping(source = "records", target = "list") - PageResult convertPage(IPage page); - - ErrorCodeDO convert(ErrorCodeAddDTO bean); - - ErrorCodeDO convert(ErrorCodeUpdateDTO bean); - -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dao/errorcode/ErrorCodeMapper.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dao/errorcode/ErrorCodeMapper.java deleted file mode 100644 index 09531057d..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dao/errorcode/ErrorCodeMapper.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.iocoder.mall.system.biz.dao.errorcode; - -import cn.iocoder.mall.mybatis.core.query.QueryWrapperX; -import cn.iocoder.mall.system.biz.dataobject.authorization.RoleDO; -import cn.iocoder.mall.system.biz.dataobject.authorization.RoleResourceDO; -import cn.iocoder.mall.system.biz.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.mall.system.biz.dto.authorization.RolePageDTO; -import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO; -import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum; -import cn.iocoder.mall.system.biz.enums.errorcode.ErrorCodeTypeEnum; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import org.springframework.stereotype.Repository; - -import java.util.Collection; -import java.util.List; - -/** - * @author ding - */ -@Repository -public interface ErrorCodeMapper extends BaseMapper { - - default ErrorCodeDO selectByCode(Integer code){ - return selectOne(new QueryWrapperX().eqIfPresent("code", code)); - } - - default IPage selectPage(ErrorCodePageDTO errorCodePageDTO) { - return selectPage(new Page<>(errorCodePageDTO.getPageNo(), errorCodePageDTO.getPageSize()), - new QueryWrapperX().likeIfPresent("message", errorCodePageDTO.getMessage())); - } - - default List selectListByIds(Collection ids) { - return selectList(new QueryWrapperX().inIfPresent("id", ids)); - } - - default List selectByGroup(Integer group) { - return selectList(new QueryWrapperX().eqIfPresent("group", group)); - } - - - default int deleteSyStemErrorCode(int group){ - return delete(new QueryWrapper().eq("group", group)); - } -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeAddDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeAddDTO.java deleted file mode 100644 index 57618bd39..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeAddDTO.java +++ /dev/null @@ -1,29 +0,0 @@ -package cn.iocoder.mall.system.biz.dto.errorcode; - -import lombok.Data; -import lombok.experimental.Accessors; - -import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.NotNull; - -/** - * 角色模块 - 添加角色 DTO - */ -@Data -@Accessors(chain = true) -public class ErrorCodeAddDTO { - - @NotNull(message = "错误码编码") - private Integer code; - - @NotEmpty(message = "错误码错误信息") - private String message; - - @NotNull(message = "错误码分组id") - private Integer group; - - /** - * 错误码备注 - */ - private String remark; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDTO.java deleted file mode 100644 index 980d538ee..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDTO.java +++ /dev/null @@ -1,28 +0,0 @@ -package cn.iocoder.mall.system.biz.dto.errorcode; - -import lombok.Data; -import lombok.experimental.Accessors; - -/** - * @author ding - */ -@Data -@Accessors(chain = true) -public class ErrorCodeDTO { - /** - * 错误码编号 - */ - private Integer id; - /** - * 错误码编码 - */ - private Integer code; - /** - * 错误码错误信息 - */ - private String message; - /** - * 错误码类型 - */ - private Integer type; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDeleteDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDeleteDTO.java deleted file mode 100644 index 3db1f447c..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDeleteDTO.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.iocoder.mall.system.biz.dto.errorcode; - -import lombok.Data; -import lombok.experimental.Accessors; - -import javax.validation.constraints.NotNull; - -/** - * 只可以删除自定义错误码 - * @author ding - */ -@Data -@Accessors(chain = true) -public class ErrorCodeDeleteDTO { - @NotNull(message = "错误码编号不能为空") - private Integer id; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeGetListDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeGetListDTO.java deleted file mode 100644 index bf9f01550..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeGetListDTO.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.mall.system.biz.dto.errorcode; - -import lombok.Data; -import lombok.experimental.Accessors; - -import java.util.Collection; - -/** - * todo 考虑是否删除 - * @author ding - */ -@Data -@Accessors(chain = true) -public class ErrorCodeGetListDTO { - /** - * 错误码编号数组 - * - * 如果传入空,则不进行错误码编号的过滤 - */ - private Collection ids; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodePageDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodePageDTO.java deleted file mode 100644 index 6ab7671f7..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodePageDTO.java +++ /dev/null @@ -1,15 +0,0 @@ -package cn.iocoder.mall.system.biz.dto.errorcode; - -import cn.iocoder.common.framework.vo.PageParam; -import lombok.Data; - -/** - * @author ding - */ -@Data -public class ErrorCodePageDTO extends PageParam { - /** - * 错误码信息 - */ - private String message; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeUpdateDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeUpdateDTO.java deleted file mode 100644 index e07ecff0c..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeUpdateDTO.java +++ /dev/null @@ -1,40 +0,0 @@ -package cn.iocoder.mall.system.biz.dto.errorcode; - -import cn.iocoder.mall.system.biz.enums.errorcode.ErrorCodeTypeEnum; -import lombok.Data; -import lombok.experimental.Accessors; - -import javax.validation.constraints.NotNull; - -/** - * @author ding - */ -@Data -@Accessors(chain = true) -public class ErrorCodeUpdateDTO { - - // TODO FROM 芋艿 to 鱿鱼丝:必要的参数校验噢 - @NotNull(message = "错误码id不能为空") - private Integer id; - - @NotNull(message = "错误码编码不能为空") - private Integer code; - /** - * 错误码错误信息 - */ - private String message; - /** - * 错误码类型 {@link ErrorCodeTypeEnum} - */ - private Integer type; - - /** - * 错误码分组 - */ - private Integer group; - - /** - * 错误码备注 - */ - private String remark; -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorcode/ErrorCodeService.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorcode/ErrorCodeService.java deleted file mode 100644 index d69a5ee39..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorcode/ErrorCodeService.java +++ /dev/null @@ -1,81 +0,0 @@ -package cn.iocoder.mall.system.biz.service.errorcode; - -import cn.iocoder.common.framework.util.ServiceExceptionUtil; -import cn.iocoder.common.framework.vo.PageResult; -import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; -import cn.iocoder.mall.system.biz.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.mall.system.biz.dto.errorcode.*; -import com.baomidou.mybatisplus.extension.service.IService; - -import java.util.List; - -/** - * @author ding - */ -public interface ErrorCodeService extends IService{ - /** - * 根据code查询错误码 - * @param code code - * @return 错误码信息 - */ - ErrorCodeBO getErrorCode(Integer code); - - /** - * 从db取出错误码列表数据 - * @return db错误码列表 - */ - List getErrorCodeList(ErrorCodeGetListDTO errorCodeGetListDTO); - - /** - * 取出所有错误码列表数据 - * @param group 分组标示 - * @return 所有错误码列表 - */ - List getErrorCodeByGroup(Integer group); - - /** - * 分页取出所有错误码列表数据 - * @param pageDTO 分页数据 - * @return 错误码列表 - */ - PageResult getErrorCodePage(ErrorCodePageDTO pageDTO); - - /** - * 新增 - * @param errorCodeAddDTO 错误码信息,默认类型为自定义错误码 - * @return - */ - Integer addErrorCode(ErrorCodeAddDTO errorCodeAddDTO); - - /** - * 批量添加错误码信息 - * @param list 错误码集合 - * @return - */ - Boolean addErrorCodeList(List list); - - /** - * 批量添加错误码信息,项目启动时初始化错误码信息。 - * @param enumerable 错误码枚举类 - * @return 是否成功 - */ - Boolean addSystemErrorCodeList(ServiceExceptionUtil.Enumerable[] enumerable); - - /** - * 更新错误码,系统内置错误码是不允许更新 - * @param errorCodeUpdateDTO 错误码信息 - */ - void updateErrorCode(ErrorCodeUpdateDTO errorCodeUpdateDTO); - - /** - * 删除错误码 - * @param errorCodeDeleteDTO 只允许删除自定义错误码 - */ - void deleteErrorCode(ErrorCodeDeleteDTO errorCodeDeleteDTO); - - /** - * 删除分组下的错误码,只提供给服务初始化时候 - * @param group 分组 - */ - void deleteSyStemErrorCode(int group); -} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorcode/ErrorCodeServiceImpl.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorcode/ErrorCodeServiceImpl.java deleted file mode 100644 index 73b232b3d..000000000 --- a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorcode/ErrorCodeServiceImpl.java +++ /dev/null @@ -1,155 +0,0 @@ -package cn.iocoder.mall.system.biz.service.errorcode; - -import cn.iocoder.common.framework.util.ServiceExceptionUtil; -import cn.iocoder.common.framework.vo.PageResult; -import cn.iocoder.mall.mybatis.core.enums.DeletedStatusEnum; -import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; -import cn.iocoder.mall.system.biz.convert.errorcode.ErrorCodeConvert; -import cn.iocoder.mall.system.biz.dao.errorcode.ErrorCodeMapper; -import cn.iocoder.mall.system.biz.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.mall.system.biz.dto.errorcode.*; -import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum; -import cn.iocoder.mall.system.biz.enums.errorcode.ErrorCodeTypeEnum; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * @author ding - */ -@Service -public class ErrorCodeServiceImpl extends ServiceImpl implements ErrorCodeService { - - @Autowired - private ErrorCodeMapper errorCodeMapper; - - - @Override - public ErrorCodeBO getErrorCode(Integer code) { - return ErrorCodeConvert.INSTANCE.convert(errorCodeMapper.selectByCode(code)); - } - - @Override - public List getErrorCodeList(ErrorCodeGetListDTO errorCodeGetListDTO) { - return ErrorCodeConvert.INSTANCE.convertList(errorCodeMapper.selectListByIds(errorCodeGetListDTO.getIds())); - } - - @Override - public List getErrorCodeByGroup(Integer group) { - List list = errorCodeMapper.selectByGroup(group); - // TODO FROM 芋艿 to 鱿鱼丝:这块微信交流一波哈。 - return ErrorCodeConvert.INSTANCE.convertList(list); - } - - - @Override - public PageResult getErrorCodePage(ErrorCodePageDTO pageDTO) { - IPage list = errorCodeMapper.selectPage(pageDTO); - return ErrorCodeConvert.INSTANCE.convertPage(list); - } - - @Override - public Integer addErrorCode(ErrorCodeAddDTO errorCodeAddDTO) { - // 校验错误码 - checkDuplicateErrorCode(errorCodeAddDTO.getCode(), null); - // 保存到数据库 - ErrorCodeDO errorCode = ErrorCodeConvert.INSTANCE.convert(errorCodeAddDTO); - errorCode.setCreateTime(new Date()); - errorCode.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - errorCodeMapper.insert(errorCode); - // TODO 插入操作日志 - // 返回成功 - return errorCode.getId(); - } - - @Override - public Boolean addErrorCodeList(List list) { - List doList = new ArrayList<>(); - for (ErrorCodeAddDTO errorCodeAddDTO:list - ) { - // 校验错误码 - checkDuplicateErrorCode(errorCodeAddDTO.getCode(), null); - ErrorCodeDO errorCode = ErrorCodeConvert.INSTANCE.convert(errorCodeAddDTO); - errorCode.setCreateTime(new Date()); - errorCode.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - doList.add(errorCode); - } - // TODO 插入操作日志 - return this.saveBatch(doList); - } - - @Override - public Boolean addSystemErrorCodeList(ServiceExceptionUtil.Enumerable[] enumerable) { - List doList = new ArrayList<>(); - for (ServiceExceptionUtil.Enumerable errorCodeEnum : enumerable){ - ErrorCodeDO errorCode = new ErrorCodeDO().setCode(errorCodeEnum.getCode()). - setMessage(errorCodeEnum.getMessage()).setType(ErrorCodeTypeEnum.SYSTEM.getType()) - .setGroup(errorCodeEnum.getGroup()); - errorCode.setCreateTime(new Date()); - errorCode.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - doList.add(errorCode); - } - // TODO 插入操作日志 - return this.saveBatch(doList); - } - - @Override - public void updateErrorCode(ErrorCodeUpdateDTO errorCodeUpdateDTO) { - // 校验错误码是否存在 - ErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(errorCodeUpdateDTO.getCode()); - if (errorCodeDO == null) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_NOT_EXISTS); - } - // 内置错误码,写死在枚举类中,不允许修改 - if (ErrorCodeTypeEnum.SYSTEM.getType().equals(errorCodeDO.getType())) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CAN_NOT_UPDATE_SYSTEM_TYPE_ERROR); - } - // 校验角色的唯一字段是否重复 - checkDuplicateErrorCode(errorCodeDO.getCode(), errorCodeDO.getId()); - // 更新到数据库 - ErrorCodeDO updateRole = ErrorCodeConvert.INSTANCE.convert(errorCodeUpdateDTO); - errorCodeMapper.updateById(updateRole); - // TODO 插入操作日志 - } - - @Override - public void deleteErrorCode(ErrorCodeDeleteDTO errorCodeDeleteDTO) { - // 校验错误码是否存在 - ErrorCodeDO errorCodeDO = errorCodeMapper.selectById(errorCodeDeleteDTO.getId()); - if (errorCodeDO == null) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_NOT_EXISTS); - } - // 内置错误码,不允许删除 - if (ErrorCodeTypeEnum.SYSTEM.getType().equals(errorCodeDO.getType())) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CAN_NOT_UPDATE_SYSTEM_TYPE_ERROR); - } - // 更新到数据库,标记删除 - errorCodeMapper.deleteById(errorCodeDO.getId()); - ServiceExceptionUtil.delete(errorCodeDO.getCode(),errorCodeDO.getMessage()); - } - - @Override - public void deleteSyStemErrorCode(int group) { - errorCodeMapper.deleteSyStemErrorCode(group); - } - - /** - * 校验错误码的唯一字段是否重复 - * - * 是否存在相同编码的错误码 - * - * @param code 错误码编码 - * @param id 错误码编号 - */ - private void checkDuplicateErrorCode(Integer code, Integer id) { - ErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code); - if (errorCodeDO != null && !errorCodeDO.getId().equals(id)) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_DUPLICATE, errorCodeDO.getCode()); - } - } -} diff --git a/user/user-rpc-api/src/main/java/cn/iocoder/mall/user/rpc/request/package-info.java b/user/user-rpc-api/src/main/java/cn/iocoder/mall/user/rpc/request/package-info.java deleted file mode 100644 index 1d2ebd408..000000000 --- a/user/user-rpc-api/src/main/java/cn/iocoder/mall/user/rpc/request/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * author: sin - * time: 2020/5/3 8:41 下午 - */ -package cn.iocoder.mall.user.rpc.request; \ No newline at end of file