From b24e45947bfaf32c3190f3c309370f96d6f53273 Mon Sep 17 00:00:00 2001 From: benpaodeyouyusi Date: Sat, 9 May 2020 21:19:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=A0=81=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/biz/enums/SystemErrorCodeEnum.java | 5 + .../enums/errorcode/ErrorCodeTypeEnum.java | 26 ++++ .../system/biz/bo/errorcode/ErrorCodeBO.java | 31 +++++ .../config/ServiceExceptionConfiguration.java | 13 ++ .../convert/errorcode/ErrorCodeConvert.java | 34 ++++++ .../biz/dao/errorcode/ErrorCodeMapper.java | 31 +++++ .../biz/dataobject/errorcode/ErrorCodeDO.java | 33 ++++++ .../biz/dto/errorcode/ErrorCodeAddDTO.java | 21 ++++ .../biz/dto/errorcode/ErrorCodeDTO.java | 28 +++++ .../biz/dto/errorcode/ErrorCodeDeleteDTO.java | 17 +++ .../dto/errorcode/ErrorCodeGetListDTO.java | 21 ++++ .../biz/dto/errorcode/ErrorCodePageDTO.java | 10 ++ .../biz/dto/errorcode/ErrorCodeUpdateDTO.java | 31 +++++ .../service/errorCode/ErrorCodeService.java | 45 +++++++ .../errorCode/ErrorCodeServiceImpl.java | 112 ++++++++++++++++++ .../errorCode/SystemErrorCodeController.java | 70 +++++++++++ .../convert/errorcode/ErrorCodeConvert.java | 38 ++++++ .../errorcode/ErrorCodeAddRequest.java | 23 ++++ .../errorcode/ErrorCodePageRequest.java | 15 +++ .../errorcode/ErrorCodePageResponse.java | 33 ++++++ .../errorcode/ErrorCodeUpdateRequest.java | 30 +++++ 21 files changed, 667 insertions(+) create mode 100644 system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/errorcode/ErrorCodeTypeEnum.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/bo/errorcode/ErrorCodeBO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/convert/errorcode/ErrorCodeConvert.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dao/errorcode/ErrorCodeMapper.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dataobject/errorcode/ErrorCodeDO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeAddDTO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDTO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDeleteDTO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeGetListDTO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodePageDTO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeUpdateDTO.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorCode/ErrorCodeService.java create mode 100644 system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorCode/ErrorCodeServiceImpl.java create mode 100644 system/system-rest/src/main/java/cn/iocoder/mall/system/rest/controller/errorCode/SystemErrorCodeController.java create mode 100644 system/system-rest/src/main/java/cn/iocoder/mall/system/rest/convert/errorcode/ErrorCodeConvert.java create mode 100644 system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeAddRequest.java create mode 100644 system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageRequest.java create mode 100644 system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageResponse.java create mode 100644 system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeUpdateRequest.java diff --git a/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/SystemErrorCodeEnum.java b/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/SystemErrorCodeEnum.java index 9fac37acd..7dbe84910 100644 --- a/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/SystemErrorCodeEnum.java +++ b/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/SystemErrorCodeEnum.java @@ -87,6 +87,11 @@ public enum SystemErrorCodeEnum implements ServiceExceptionUtil.Enumerable { USER_ADDRESS_NOT_EXISTENT(1001004000, "用户地址不存在!"), USER_ADDRESS_IS_DELETED(1001004001, "用户地址已被删除!"), USER_GET_ADDRESS_NOT_EXISTS(1001004002, "获取的地址不存在!"), + + // ========== 错误码模块 1002009000 ========== + ERROR_CODE_NOT_EXISTS(1002009000, "错误码不存在"), + ERROR_CODE_DUPLICATE(1002009001, "已经存在编码为【{}}】的错误码"), + ERROR_CAN_NOT_UPDATE_SYSTEM_TYPE_ERROR(1002004003, "不能修改类型为系统内置的错误码"), ; diff --git a/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/errorcode/ErrorCodeTypeEnum.java b/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/errorcode/ErrorCodeTypeEnum.java new file mode 100644 index 000000000..34a2a681e --- /dev/null +++ b/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/enums/errorcode/ErrorCodeTypeEnum.java @@ -0,0 +1,26 @@ +package cn.iocoder.mall.system.biz.enums.errorcode; + +/** + * 错误码枚举,内置错误码是在 枚举中 + * @author ding + */ +public enum ErrorCodeTypeEnum { + /** + * 内置错误码 + */ + SYSTEM(1), + /** + * 自定义错误码 + */ + CUSTOM(2); + + private final Integer type; + + ErrorCodeTypeEnum(Integer type) { + this.type = type; + } + + public Integer getType() { + return type; + } +} 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 new file mode 100644 index 000000000..0672e6142 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/bo/errorcode/ErrorCodeBO.java @@ -0,0 +1,31 @@ +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/ServiceExceptionConfiguration.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/config/ServiceExceptionConfiguration.java index 81ea30d07..db8792b92 100644 --- 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 @@ -1,19 +1,32 @@ package cn.iocoder.mall.system.biz.config; import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; +import cn.iocoder.mall.system.biz.dataobject.errorcode.ErrorCodeDO; 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; +import java.util.List; + @Configuration public class ServiceExceptionConfiguration { +// @Autowired +// private ErrorCodeService errorCodeService; + @EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html public void initMessages() { +// List list = errorCodeService.getErrorCodeList(); for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) { ServiceExceptionUtil.put(item.getCode(), item.getMessage()); } +// for (ErrorCodeBO bo : list) { +// 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 new file mode 100644 index 000000000..10eed4aab --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/convert/errorcode/ErrorCodeConvert.java @@ -0,0 +1,34 @@ +package cn.iocoder.mall.system.biz.convert.errorcode; + +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 org.mapstruct.Mapper; +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 new file mode 100644 index 000000000..5d95a15e4 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dao/errorcode/ErrorCodeMapper.java @@ -0,0 +1,31 @@ +package cn.iocoder.mall.system.biz.dao.errorcode; + +import cn.iocoder.mall.mybatis.query.QueryWrapperX; +import cn.iocoder.mall.system.biz.dataobject.errorcode.ErrorCodeDO; +import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum; +import cn.iocoder.mall.system.biz.enums.errorcode.ErrorCodeTypeEnum; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.springframework.stereotype.Repository; + +/** + * @author ding + */ +@Repository +public interface ErrorCodeMapper extends BaseMapper { + + default ErrorCodeDO selectByCode(Integer code){ + //从db查询 + ErrorCodeDO errorCodeDO = selectOne(new QueryWrapperX().eqIfPresent("code", code)); + if (null == errorCodeDO){ + //从enum查询 + for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) { + if(code.equals(item.getCode())){ + return new ErrorCodeDO().setCode(item.getCode()). + setId(0).setType(ErrorCodeTypeEnum.SYSTEM.getType()); + } + } + } + return null; + + } +} diff --git a/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dataobject/errorcode/ErrorCodeDO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dataobject/errorcode/ErrorCodeDO.java new file mode 100644 index 000000000..055996696 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dataobject/errorcode/ErrorCodeDO.java @@ -0,0 +1,33 @@ +package cn.iocoder.mall.system.biz.dataobject.errorcode; + +import cn.iocoder.mall.mybatis.dataobject.DeletableDO; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** + * 错误码实体 + */ +@TableName(value = "error_code") +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +public class ErrorCodeDO extends DeletableDO { + /** + * 错误码编号 + */ + 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/ErrorCodeAddDTO.java b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeAddDTO.java new file mode 100644 index 000000000..1e55ec16e --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeAddDTO.java @@ -0,0 +1,21 @@ +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; +} 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 new file mode 100644 index 000000000..980d538ee --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDTO.java @@ -0,0 +1,28 @@ +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 new file mode 100644 index 000000000..3db1f447c --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeDeleteDTO.java @@ -0,0 +1,17 @@ +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 new file mode 100644 index 000000000..02af2e5bb --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeGetListDTO.java @@ -0,0 +1,21 @@ +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 codes; +} 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 new file mode 100644 index 000000000..9db3edd29 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodePageDTO.java @@ -0,0 +1,10 @@ +package cn.iocoder.mall.system.biz.dto.errorcode; + +import cn.iocoder.common.framework.vo.PageParam; + +/** + * @author ding + */ +public class ErrorCodePageDTO extends PageParam { + +} 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 new file mode 100644 index 000000000..9cca65e27 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/dto/errorcode/ErrorCodeUpdateDTO.java @@ -0,0 +1,31 @@ +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 { + + /** + * 错误码编号,内置错误码的id是没有的 + */ + private Integer id; + + @NotNull(message = "错误码编码不能为空") + private Integer code; + /** + * 错误码错误信息 + */ + private String message; + /** + * 错误码类型 {@link ErrorCodeTypeEnum} + */ + private Integer type; +} 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 new file mode 100644 index 000000000..bafebd248 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorCode/ErrorCodeService.java @@ -0,0 +1,45 @@ +package cn.iocoder.mall.system.biz.service.errorCode; + +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeAddDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeDeleteDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; + +import java.util.List; + +/** + * @author ding + */ +public interface ErrorCodeService { + /** + * 根据code查询错误码 + * @param code code + * @return 错误码信息 + */ + ErrorCodeBO getErrorCode(Integer code); + + List getErrorCodeList(); + + PageResult getErrorCodePage(ErrorCodePageDTO pageDTO); + + /** + * 新增 + * @param errorCodeAddDTO 错误码信息,默认类型为自定义错误码 + * @return + */ + Integer addErrorCode(ErrorCodeAddDTO errorCodeAddDTO); + + /** + * 更新错误码,系统内置错误码是不允许更新 + * @param errorCodeUpdateDTO 错误码信息 + */ + void updateErrorCode(ErrorCodeUpdateDTO errorCodeUpdateDTO); + + /** + * 删除错误码 + * @param errorCodeDeleteDTO 只允许删除自定义错误码 + */ + void deleteErrorCode(ErrorCodeDeleteDTO errorCodeDeleteDTO); +} 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 new file mode 100644 index 000000000..04d963574 --- /dev/null +++ b/system/system-biz/src/main/java/cn/iocoder/mall/system/biz/service/errorCode/ErrorCodeServiceImpl.java @@ -0,0 +1,112 @@ +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.enums.DeletedStatusEnum; +import cn.iocoder.mall.mybatis.query.QueryWrapperX; +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.ErrorCodeAddDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeDeleteDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; +import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum; +import cn.iocoder.mall.system.biz.enums.errorcode.ErrorCodeTypeEnum; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +/** + * @author ding + */ +@Service +public class ErrorCodeServiceImpl implements ErrorCodeService { + + @Autowired + private ErrorCodeMapper errorCodeMapper; + + + @Override + public ErrorCodeBO getErrorCode(Integer code) { + return ErrorCodeConvert.INSTANCE.convert(errorCodeMapper.selectByCode(code)); + } + + @Override + public List getErrorCodeList() { + List list = errorCodeMapper.selectList(new QueryWrapperX()); + return ErrorCodeConvert.INSTANCE.convertList(list); + } + + + @Override + public PageResult getErrorCodePage(ErrorCodePageDTO pageDTO) { +// List list = errorCodeMapper.selectList(new QueryWrapperX()); +// List<> + return null; + } + + @Override + public Integer addErrorCode(ErrorCodeAddDTO errorCodeAddDTO) { + // 校验错误码 + checkDuplicateErrorCode(errorCodeAddDTO.getCode(), null); + // 保存到数据库 + ErrorCodeDO errorCode = ErrorCodeConvert.INSTANCE.convert(errorCodeAddDTO); + errorCode.setType(ErrorCodeTypeEnum.CUSTOM.getType()); + errorCode.setCreateTime(new Date()); + errorCode.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); + errorCodeMapper.insert(errorCode); + // TODO 插入操作日志 + // 返回成功 + return errorCode.getId(); + } + + @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); + } + // 更新到数据库,标记删除 + errorCodeMapper.deleteById(errorCodeDO.getId()); + // TODO: 2020-05-08 刷新对外提供的错误码列表 + } + + /** + * 校验错误码的唯一字段是否重复 + * + * 是否存在相同编码的错误码 + * + * @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/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/controller/errorCode/SystemErrorCodeController.java b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/controller/errorCode/SystemErrorCodeController.java new file mode 100644 index 000000000..8c85e2807 --- /dev/null +++ b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/controller/errorCode/SystemErrorCodeController.java @@ -0,0 +1,70 @@ +package cn.iocoder.mall.system.rest.controller.errorCode; + +import cn.iocoder.common.framework.constant.MallConstants; +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeAddDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeDeleteDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; +import cn.iocoder.mall.system.biz.service.errorCode.ErrorCodeService; +import cn.iocoder.mall.system.rest.convert.errorcode.ErrorCodeConvert; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeAddRequest; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageRequest; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageResponse; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeUpdateRequest; +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.web.bind.annotation.*; + +/** + * 错误码 + * + * @author youyusi + */ +@RestController +@RequestMapping(MallConstants.ROOT_PATH_ADMIN + "/errorCode") +@Api("错误码") +public class SystemErrorCodeController { + @Autowired + private ErrorCodeService errorCodeService; + + @GetMapping("/page") + @ApiOperation(value = "错误码分页") +// @RequiresPermissions("system:errorCode:page") + public CommonResult> page(ErrorCodePageRequest request) { + ErrorCodePageDTO pageDTO = ErrorCodeConvert.INSTANCE.convert(request); + PageResult pageResult = errorCodeService.getErrorCodePage(pageDTO); + return CommonResult.success(ErrorCodeConvert.INSTANCE.convertPage(pageResult)); + } + + @PostMapping("/add") + @ApiOperation(value = "创建错误码") +// @RequiresPermissions("system:errorCode:add") + public CommonResult add(ErrorCodeAddRequest request) { + ErrorCodeAddDTO addDTO = ErrorCodeConvert.INSTANCE.convert(request); + return CommonResult.success(errorCodeService.addErrorCode(addDTO)); + } + + @PostMapping("/update") + @ApiOperation(value = "更新错误码") +// @RequiresPermissions("system:errorCode:update") + public CommonResult update(ErrorCodeUpdateRequest request) { + ErrorCodeUpdateDTO updateDTO = ErrorCodeConvert.INSTANCE.convert(request); + errorCodeService.updateErrorCode(updateDTO); + return CommonResult.success(true); + } + + @PostMapping("/delete") + @ApiOperation(value = "删除错误码") +// @RequiresPermissions("system:errorCode:delete") + @ApiImplicitParam(name = "id", value = "错误码编号", required = true, example = "1") + public CommonResult delete(@RequestParam("id") Integer id) { + ErrorCodeDeleteDTO deleteDTO = new ErrorCodeDeleteDTO().setId(id); + errorCodeService.deleteErrorCode(deleteDTO); + return CommonResult.success(true); + } +} diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/convert/errorcode/ErrorCodeConvert.java b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/convert/errorcode/ErrorCodeConvert.java new file mode 100644 index 000000000..f869a5d7b --- /dev/null +++ b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/convert/errorcode/ErrorCodeConvert.java @@ -0,0 +1,38 @@ +package cn.iocoder.mall.system.rest.convert.errorcode; + +import cn.iocoder.common.framework.vo.PageResult; +import cn.iocoder.mall.system.biz.bo.authorization.RoleBO; +import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; +import cn.iocoder.mall.system.biz.dto.authorization.RoleAddDTO; +import cn.iocoder.mall.system.biz.dto.authorization.RolePageDTO; +import cn.iocoder.mall.system.biz.dto.authorization.RoleUpdateDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeAddDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO; +import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; +import cn.iocoder.mall.system.rest.convert.authorization.AdminsRoleConvert; +import cn.iocoder.mall.system.rest.request.authorization.AdminsRoleAddRequest; +import cn.iocoder.mall.system.rest.request.authorization.AdminsRolePageRequest; +import cn.iocoder.mall.system.rest.request.authorization.AdminsRoleUpdateRequest; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeAddRequest; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageRequest; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageResponse; +import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeUpdateRequest; +import cn.iocoder.mall.system.rest.response.authorization.AdminsRolePageResponse; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * @author ding + */ +@Mapper +public interface ErrorCodeConvert { + ErrorCodeConvert INSTANCE = Mappers.getMapper(ErrorCodeConvert.class); + + ErrorCodeAddDTO convert(ErrorCodeAddRequest bean); + + ErrorCodeUpdateDTO convert(ErrorCodeUpdateRequest bean); + + ErrorCodePageDTO convert(ErrorCodePageRequest bean); + + PageResult convertPage(PageResult bean); +} diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeAddRequest.java b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeAddRequest.java new file mode 100644 index 000000000..82861e9b8 --- /dev/null +++ b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeAddRequest.java @@ -0,0 +1,23 @@ +package cn.iocoder.mall.system.rest.request.errorcode; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotEmpty; + +@ApiModel("管理员 - 错误码模块 - 添加错误码 Request") +@Data +@Accessors(chain = true) +public class ErrorCodeAddRequest { + + @ApiModelProperty(value = "错误码信息", required = true, example = "系统管理员") + @NotEmpty(message = "错误码信息不能为空") + private String message; + + @ApiModelProperty(value = "错误码编码", example = "SUPER_ADMIN") + @NotEmpty(message = "错误码编码不能为空") + private Integer code; + +} diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageRequest.java b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageRequest.java new file mode 100644 index 000000000..17ef20468 --- /dev/null +++ b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageRequest.java @@ -0,0 +1,15 @@ +package cn.iocoder.mall.system.rest.request.errorcode; + +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.experimental.Accessors; + + +/** + * @author ding + */ +@ApiModel("管理员 - 错误码模块 - 错误码分页 Request") +@Data +@Accessors(chain = true) +public class ErrorCodePageRequest { +} diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageResponse.java b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageResponse.java new file mode 100644 index 000000000..b9a2c17e9 --- /dev/null +++ b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodePageResponse.java @@ -0,0 +1,33 @@ +package cn.iocoder.mall.system.rest.request.errorcode; + +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.experimental.Accessors; + +import java.util.Date; + +/** + * 类 描 述:错误码模块 + * @author ding + */ +@ApiModel("管理员 - 错误码模块 - 查询错误码 Request") +@Data +@Accessors(chain = true) +public class ErrorCodePageResponse { + /** + * 错误码编号 + */ + private Integer id; + /** + * 错误码编码 + */ + private Integer code; + /** + * 错误码错误信息 + */ + private String message; + /** + * 添加时间 + */ + private Date createTime; +} diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeUpdateRequest.java b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeUpdateRequest.java new file mode 100644 index 000000000..53e2d5e0d --- /dev/null +++ b/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/errorcode/ErrorCodeUpdateRequest.java @@ -0,0 +1,30 @@ +package cn.iocoder.mall.system.rest.request.errorcode; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** + * 类 描 述:错误码模块 + * @author ding + */ +@ApiModel("管理员 - 错误码模块 - 修改错误码 Request") +@Data +@Accessors(chain = true) +public class ErrorCodeUpdateRequest { + @ApiModelProperty(value = "错误码编号", required = true, example = "1") + @NotNull(message = "错误码不能为空") + private Integer id; + + @ApiModelProperty(value = "错误码信息", required = true, example = "系统管理员") + @NotEmpty(message = "错误码信息不能为空") + private String message; + + @ApiModelProperty(value = "错误码编码", example = "SUPER_ADMIN") + @NotEmpty(message = "错误码编码不能为空") + private Integer code; +}