错误码的 Starter 的初始化,完成

pull/3/head
YunaiV 2020-07-20 19:18:27 +08:00
parent 0df486a677
commit 187b17ed01
50 changed files with 1249 additions and 611 deletions

View File

@ -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;
}
}

View File

@ -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));
}

View File

@ -31,4 +31,5 @@ public class DeletableDO extends BaseDO {
this.deleted = deleted;
return this;
}
}

View File

@ -44,6 +44,13 @@ public class QueryWrapperX<T> extends QueryWrapper<T> {
return this;
}
public QueryWrapperX<T> gtIfPresent(String column, Object val) {
if (val != null) {
return (QueryWrapperX<T>) super.gt(column, val);
}
return this;
}
// ========== 重写父类方法,方便链式调用 ==========
@Override

View File

@ -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());
}
}

View File

@ -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;
/**
*

View File

@ -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<ErrorCodeAutoGenerateDTO> autoGenerateDTO = new ArrayList<>();
Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(new Consumer<Field>() {
@Override
public void accept(Field field) {
List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
if (field.getType() != ErrorCode.class) {
return;
}
try {
ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
autoGenerateDTO.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
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<Boolean> 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());
}
}
}

View File

@ -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<List<ErrorCodeVO>> 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;
}
}

View File

@ -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<Integer> createErrorCode(@Valid ErrorCodeCreateDTO createDTO) {
return success(errorCodeManager.createErrorCode(createDTO));
}
@PostMapping("/update")
@ApiOperation("更新错误码")
public CommonResult<Boolean> updateErrorCode(@Valid ErrorCodeUpdateDTO updateDTO) {
errorCodeManager.updateErrorCode(updateDTO);
return success(true);
}
@PostMapping("/delete")
@ApiOperation("删除错误码")
@ApiImplicitParam(name = "errorCodeId", value = "错误码编号", required = true)
public CommonResult<Boolean> deleteErrorCode(@RequestParam("errorCodeId") Integer errorCodeId) {
errorCodeManager.deleteErrorCode(errorCodeId);
return success(true);
}
@GetMapping("/get")
@ApiOperation("获得错误码")
@ApiImplicitParam(name = "errorCodeId", value = "错误码编号", required = true)
public CommonResult<ErrorCodeVO> getErrorCode(@RequestParam("errorCodeId") Integer errorCodeId) {
return success(errorCodeManager.getErrorCode(errorCodeId));
}
@GetMapping("/page")
@ApiOperation("获得错误码分页")
public CommonResult<PageResult<ErrorCodeVO>> pageErrorCode(ErrorCodePageDTO pageDTO) {
return success(errorCodeManager.pageErrorCode(pageDTO));
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<ErrorCodeVO> convertList(List<cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO> list);
PageResult<ErrorCodeVO> convertPage(PageResult<cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO> page);
ErrorCodePageDTO convert(cn.iocoder.mall.managementweb.controller.errorcode.dto.ErrorCodePageDTO bean);
}

View File

@ -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<Integer> createErrorCodeResult = errorCodeRpc.createErrorCode(ErrorCodeConvert.INSTANCE.convert(createDTO));
createErrorCodeResult.checkError();
return createErrorCodeResult.getData();
}
/**
*
*
* @param updateDTO DTO
*/
public void updateErrorCode(ErrorCodeUpdateDTO updateDTO) {
CommonResult<Boolean> updateErrorCodeResult = errorCodeRpc.updateErrorCode(ErrorCodeConvert.INSTANCE.convert(updateDTO));
updateErrorCodeResult.checkError();
}
/**
*
*
* @param errorCodeId
*/
public void deleteErrorCode(Integer errorCodeId) {
CommonResult<Boolean> deleteErrorCodeResult = errorCodeRpc.deleteErrorCode(errorCodeId);
deleteErrorCodeResult.checkError();
}
/**
*
*
* @param errorCodeId
* @return
*/
public ErrorCodeVO getErrorCode(Integer errorCodeId) {
CommonResult<cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO> getErrorCodeResult = errorCodeRpc.getErrorCode(errorCodeId);
getErrorCodeResult.checkError();
return ErrorCodeConvert.INSTANCE.convert(getErrorCodeResult.getData());
}
/**
*
*
* @param errorCodeIds
* @return
*/
public List<ErrorCodeVO> listErrorCodes(List<Integer> errorCodeIds) {
CommonResult<List<cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO>> listErrorCodeResult = errorCodeRpc.listErrorCodes(errorCodeIds);
listErrorCodeResult.checkError();
return ErrorCodeConvert.INSTANCE.convertList(listErrorCodeResult.getData());
}
/**
*
*
* @param pageDTO
* @return
*/
public PageResult<ErrorCodeVO> pageErrorCode(ErrorCodePageDTO pageDTO) {
CommonResult<PageResult<cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO>> pageErrorCodeResult
= errorCodeRpc.pageErrorCode(ErrorCodeConvert.INSTANCE.convert(pageDTO));
pageErrorCodeResult.checkError();
return ErrorCodeConvert.INSTANCE.convertPage(pageErrorCodeResult.getData());
}
}

View File

@ -47,6 +47,8 @@ dubbo:
version: 1.0.0
SystemExceptionLogRpc:
version: 1.0.0
ErrorCodeRpc:
version: 1.0.0
# Swagger 配置项
swagger:

47
pom.xml
View File

@ -23,7 +23,6 @@
<!-- <module>pay</module>-->
<!-- <module>promotion</module>-->
<!-- <module>search</module>-->
<!-- <module>demo</module>-->
<module>mall-dependencies</module>
<!-- <module>mall-spring-boot-starter-cache</module>-->
<module>user-service-project</module>
@ -34,4 +33,50 @@
</modules>
<packaging>pom</packaging>
<!-- 属性 -->
<properties>
<java.version>1.8</java.version>
<!-- 工具类相关 -->
<org.projectlombok.version>1.16.14</org.projectlombok.version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
</properties>
<build>
<pluginManagement>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source> <!-- or higher, depending on your project -->
<target>${java.version}</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -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, "短信平台调用失败【具体错误会动态替换】");

View File

@ -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;
}
}

View File

@ -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<List<ErrorCodeVO>> listErrorCodes(String group, Date minUpdateTime);
/**
*
*
* @param group
* @param minUpdateTime
* minUpdateTime
* @return
*/
CommonResult<List<ErrorCodeVO>> listErrorCodes(@NotNull(message = "错误码分组不能为空") String group, Date minUpdateTime);
CommonResult<Boolean> autoGenerateErrorCodes(ErrorCodeAutoGenerateDTO autoGenerateDTO);
/**
*
*
* @param autoGenerateDTOs DTO
*/
CommonResult<Boolean> autoGenerateErrorCodes(List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs);
/**
*
*
* @param createDTO DTO
* @return
*/
CommonResult<Integer> createErrorCode(ErrorCodeCreateDTO createDTO);
/**
*
*
* @param updateDTO DTO
*/
CommonResult<Boolean> updateErrorCode(ErrorCodeUpdateDTO updateDTO);
/**
*
*
* @param errorCodeId
*/
CommonResult<Boolean> deleteErrorCode(Integer errorCodeId);
/**
*
*
* @param errorCodeId
* @return
*/
CommonResult<ErrorCodeVO> getErrorCode(Integer errorCodeId);
/**
*
*
* @param errorCodeIds
* @return
*/
CommonResult<List<ErrorCodeVO>> listErrorCodes(List<Integer> errorCodeIds);
/**
*
*
* @param pageDTO
* @return
*/
CommonResult<PageResult<ErrorCodeVO>> pageErrorCode(ErrorCodePageDTO pageDTO);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<ErrorCodeBO> convertList(List<ErrorCodeDO> list);
@Mapping(source = "records", target = "list")
PageResult<ErrorCodeBO> convertPage(IPage<ErrorCodeDO> page);
ErrorCodeDO convert(ErrorCodeUpdateBO bean);
ErrorCodeCreateBO convert(ErrorCodeCreateDTO bean);
ErrorCodeUpdateBO convert(ErrorCodeUpdateDTO bean);
ErrorCodeVO convert(ErrorCodeBO bean);
List<ErrorCodeVO> convertList02(List<ErrorCodeBO> list);
PageResult<ErrorCodeVO> convertPage(PageResult<ErrorCodeBO> page);
ErrorCodePageBO convert(ErrorCodePageDTO bean);
ErrorCodeDO convert(ErrorCodeAutoGenerateBO bean);
List<ErrorCodeAutoGenerateBO> convertList03(List<ErrorCodeAutoGenerateDTO> list);
}

View File

@ -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;
/**
*
*/

View File

@ -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<ErrorCodeDO> {
default IPage<ErrorCodeDO> selectPage(ErrorCodePageBO pageBO) {
return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
new QueryWrapperX<ErrorCodeDO>().likeIfPresent("`group`", pageBO.getGroup())
.eqIfPresent("code", pageBO.getCode()).likeIfPresent("message", pageBO.getMessage()));
}
default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) {
return selectList(new QueryWrapper<ErrorCodeDO>().in("code", codes));
}
default ErrorCodeDO selectByCode(Integer code) {
return selectOne(new QueryWrapper<ErrorCodeDO>().eq("code", code));
}
default List<ErrorCodeDO> selectListByGroup(String group, Date minUpdateTime) {
return selectList(new QueryWrapperX<ErrorCodeDO>().eq("`group`", group)
.gtIfPresent("update_time", minUpdateTime));
}
}

View File

@ -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<ErrorCodeAutoGenerateDTO> 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<ErrorCodeVO> listErrorCodes(List<Integer> errorCodeIds) {
List<ErrorCodeBO> errorCodeBOs = errorCodeService.listErrorCodes(errorCodeIds);
return ErrorCodeConvert.INSTANCE.convertList02(errorCodeBOs);
}
/**
*
*
* @param pageDTO
* @return
*/
public PageResult<ErrorCodeVO> pageErrorCode(ErrorCodePageDTO pageDTO) {
PageResult<ErrorCodeBO> pageResultBO = errorCodeService.pageErrorCode(ErrorCodeConvert.INSTANCE.convert(pageDTO));
return ErrorCodeConvert.INSTANCE.convertPage(pageResultBO);
}
public List<ErrorCodeVO> listErrorCodes(String group, Date minUpdateTime) {
List<ErrorCodeBO> errorCodeBOs = errorCodeService.listErrorCodes(group, minUpdateTime);
return ErrorCodeConvert.INSTANCE.convertList02(errorCodeBOs);
}
}

View File

@ -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<List<ErrorCodeVO>> listErrorCodes(String group, Date minUpdateTime) {
return null;
return success(errorCodeManager.listErrorCodes(group, minUpdateTime));
}
@Override
public CommonResult<Boolean> autoGenerateErrorCodes(ErrorCodeAutoGenerateDTO autoGenerateDTO) {
return null;
public CommonResult<Boolean> autoGenerateErrorCodes(List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs) {
errorCodeManager.autoGenerateErrorCodes(autoGenerateDTOs);
return success(true);
}
@Override
public CommonResult<Integer> createErrorCode(ErrorCodeCreateDTO createDTO) {
return success(errorCodeManager.createErrorCode(createDTO));
}
@Override
public CommonResult<Boolean> updateErrorCode(ErrorCodeUpdateDTO updateDTO) {
errorCodeManager.updateErrorCode(updateDTO);
return success(true);
}
@Override
public CommonResult<Boolean> deleteErrorCode(Integer errorCodeId) {
errorCodeManager.deleteErrorCode(errorCodeId);
return success(true);
}
@Override
public CommonResult<ErrorCodeVO> getErrorCode(Integer errorCodeId) {
return success(errorCodeManager.getErrorCode(errorCodeId));
}
@Override
public CommonResult<List<ErrorCodeVO>> listErrorCodes(List<Integer> errorCodeIds) {
return success(errorCodeManager.listErrorCodes(errorCodeIds));
}
@Override
public CommonResult<PageResult<ErrorCodeVO>> pageErrorCode(ErrorCodePageDTO pageDTO) {
return success(errorCodeManager.pageErrorCode(pageDTO));
}
}

View File

@ -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<ErrorCodeAutoGenerateBO> autoGenerateBOs) {
if (CollectionUtils.isEmpty(autoGenerateBOs)) {
return;
}
List<ErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByCodes(
CollectionUtils.convertSet(autoGenerateBOs, ErrorCodeAutoGenerateBO::getCode));
Map<Integer, ErrorCodeDO> 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<ErrorCodeBO> listErrorCodes(List<Integer> errorCodeIds) {
List<ErrorCodeDO> errorCodeDOs = errorCodeMapper.selectBatchIds(errorCodeIds);
return ErrorCodeConvert.INSTANCE.convertList(errorCodeDOs);
}
/**
*
*
* @param pageBO
* @return
*/
public PageResult<ErrorCodeBO> pageErrorCode(ErrorCodePageBO pageBO) {
IPage<ErrorCodeDO> 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<ErrorCodeBO> listErrorCodes(String group, Date minUpdateTime) {
List<ErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByGroup(group, minUpdateTime);
return ErrorCodeConvert.INSTANCE.convertList(errorCodeDOs);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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 分页插件
}
}

View File

@ -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());
// }
}
}

View File

@ -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<ErrorCodeBO> convertList(List<ErrorCodeDO> beans);
@Mapping(source = "records", target = "list")
PageResult<ErrorCodeBO> convertPage(IPage<ErrorCodeDO> page);
ErrorCodeDO convert(ErrorCodeAddDTO bean);
ErrorCodeDO convert(ErrorCodeUpdateDTO bean);
}

View File

@ -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<ErrorCodeDO> {
default ErrorCodeDO selectByCode(Integer code){
return selectOne(new QueryWrapperX<ErrorCodeDO>().eqIfPresent("code", code));
}
default IPage<ErrorCodeDO> selectPage(ErrorCodePageDTO errorCodePageDTO) {
return selectPage(new Page<>(errorCodePageDTO.getPageNo(), errorCodePageDTO.getPageSize()),
new QueryWrapperX<ErrorCodeDO>().likeIfPresent("message", errorCodePageDTO.getMessage()));
}
default List<ErrorCodeDO> selectListByIds(Collection<Integer> ids) {
return selectList(new QueryWrapperX<ErrorCodeDO>().inIfPresent("id", ids));
}
default List<ErrorCodeDO> selectByGroup(Integer group) {
return selectList(new QueryWrapperX<ErrorCodeDO>().eqIfPresent("group", group));
}
default int deleteSyStemErrorCode(int group){
return delete(new QueryWrapper<ErrorCodeDO>().eq("group", group));
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<Integer> ids;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<ErrorCodeDO>{
/**
* code
* @param code code
* @return
*/
ErrorCodeBO getErrorCode(Integer code);
/**
* db
* @return db
*/
List<ErrorCodeBO> getErrorCodeList(ErrorCodeGetListDTO errorCodeGetListDTO);
/**
*
* @param group
* @return
*/
List<ErrorCodeBO> getErrorCodeByGroup(Integer group);
/**
*
* @param pageDTO
* @return
*/
PageResult<ErrorCodeBO> getErrorCodePage(ErrorCodePageDTO pageDTO);
/**
*
* @param errorCodeAddDTO
* @return
*/
Integer addErrorCode(ErrorCodeAddDTO errorCodeAddDTO);
/**
*
* @param list
* @return
*/
Boolean addErrorCodeList(List<ErrorCodeAddDTO> 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);
}

View File

@ -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<ErrorCodeMapper, ErrorCodeDO> implements ErrorCodeService {
@Autowired
private ErrorCodeMapper errorCodeMapper;
@Override
public ErrorCodeBO getErrorCode(Integer code) {
return ErrorCodeConvert.INSTANCE.convert(errorCodeMapper.selectByCode(code));
}
@Override
public List<ErrorCodeBO> getErrorCodeList(ErrorCodeGetListDTO errorCodeGetListDTO) {
return ErrorCodeConvert.INSTANCE.convertList(errorCodeMapper.selectListByIds(errorCodeGetListDTO.getIds()));
}
@Override
public List<ErrorCodeBO> getErrorCodeByGroup(Integer group) {
List<ErrorCodeDO> list = errorCodeMapper.selectByGroup(group);
// TODO FROM 芋艿 to 鱿鱼丝:这块微信交流一波哈。
return ErrorCodeConvert.INSTANCE.convertList(list);
}
@Override
public PageResult<ErrorCodeBO> getErrorCodePage(ErrorCodePageDTO pageDTO) {
IPage<ErrorCodeDO> 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<ErrorCodeAddDTO> list) {
List<ErrorCodeDO> 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<ErrorCodeDO> 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());
}
}
}

View File

@ -1,5 +0,0 @@
/**
* author: sin
* time: 2020/5/3 8:41
*/
package cn.iocoder.mall.user.rpc.request;