错误码第一版续,分组暂时未加,第二版会添加分组和mq订阅

This commit is contained in:
benpaodeyouyusi 2020-05-10 23:38:20 +08:00
parent 413f75f402
commit 96858fae21
11 changed files with 163 additions and 33 deletions

View File

@ -2,9 +2,8 @@ package cn.iocoder.mall.system.biz.config;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; 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.enums.SystemErrorCodeEnum;
import cn.iocoder.mall.system.biz.service.errorCode.ErrorCodeService; import cn.iocoder.mall.system.biz.service.errorcode.ErrorCodeService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -15,18 +14,18 @@ import java.util.List;
@Configuration @Configuration
public class ServiceExceptionConfiguration { public class ServiceExceptionConfiguration {
// @Autowired @Autowired
// private ErrorCodeService errorCodeService; private ErrorCodeService errorCodeService;
@EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html @EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html
public void initMessages() { public void initMessages() {
// List<ErrorCodeBO> list = errorCodeService.getErrorCodeList(); List<ErrorCodeBO> list = errorCodeService.getErrorCodeList();
for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) { for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) {
ServiceExceptionUtil.put(item.getCode(), item.getMessage()); ServiceExceptionUtil.put(item.getCode(), item.getMessage());
} }
// for (ErrorCodeBO bo : list) { for (ErrorCodeBO bo : list) {
// ServiceExceptionUtil.put(bo.getCode(),bo.getMessage()); ServiceExceptionUtil.put(bo.getCode(),bo.getMessage());
// } }
} }
} }

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.biz.service.errorCode; package cn.iocoder.mall.system.biz.service.errorcode;
import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO;
@ -20,8 +20,23 @@ public interface ErrorCodeService {
*/ */
ErrorCodeBO getErrorCode(Integer code); ErrorCodeBO getErrorCode(Integer code);
/**
* 从db取出错误码列表数据
* @return db错误码列表
*/
List<ErrorCodeBO> getErrorCodeList(); List<ErrorCodeBO> getErrorCodeList();
/**
* 取出所有错误码列表数据
* @return 所有错误码列表
*/
List<ErrorCodeBO> getErrorCodeListAll();
/**
* 分页取出所有错误码列表数据
* @param pageDTO 分页数据
* @return 错误码列表
*/
PageResult<ErrorCodeBO> getErrorCodePage(ErrorCodePageDTO pageDTO); PageResult<ErrorCodeBO> getErrorCodePage(ErrorCodePageDTO pageDTO);
/** /**

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.biz.service.errorCode; package cn.iocoder.mall.system.biz.service.errorcode;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.common.framework.vo.PageResult;
@ -41,12 +41,25 @@ public class ErrorCodeServiceImpl implements ErrorCodeService {
return ErrorCodeConvert.INSTANCE.convertList(list); return ErrorCodeConvert.INSTANCE.convertList(list);
} }
@Override
public List<ErrorCodeBO> getErrorCodeListAll() {
List<ErrorCodeDO> list = errorCodeMapper.selectList(new QueryWrapperX<ErrorCodeDO>());
for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) {
list.add(new ErrorCodeDO().setId(0).setCode(item.getCode()).
setMessage(item.getMessage()).setType(ErrorCodeTypeEnum.SYSTEM.getType()));
}
return ErrorCodeConvert.INSTANCE.convertList(list);
}
@Override @Override
public PageResult<ErrorCodeBO> getErrorCodePage(ErrorCodePageDTO pageDTO) { public PageResult<ErrorCodeBO> getErrorCodePage(ErrorCodePageDTO pageDTO) {
// List<ErrorCodeDO> list = errorCodeMapper.selectList(new QueryWrapperX<ErrorCodeDO>()); List<ErrorCodeDO> list = errorCodeMapper.selectList(new QueryWrapperX<ErrorCodeDO>());
// List<> for (SystemErrorCodeEnum item : SystemErrorCodeEnum.values()) {
return null; list.add(new ErrorCodeDO().setId(0).setCode(item.getCode()).
setMessage(item.getMessage()).setType(ErrorCodeTypeEnum.SYSTEM.getType()));
}
return listToPageList(pageDTO.getPageNo(),pageDTO.getPageSize(),list);
} }
@Override @Override
@ -85,14 +98,15 @@ public class ErrorCodeServiceImpl implements ErrorCodeService {
@Override @Override
public void deleteErrorCode(ErrorCodeDeleteDTO errorCodeDeleteDTO) { public void deleteErrorCode(ErrorCodeDeleteDTO errorCodeDeleteDTO) {
// 校验角色是否存在 // 校验错误码是否存在
ErrorCodeDO errorCodeDO = errorCodeMapper.selectById(errorCodeDeleteDTO.getId()); ErrorCodeDO errorCodeDO = errorCodeMapper.selectById(errorCodeDeleteDTO.getId());
if (errorCodeDO == null) { if (errorCodeDO == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_NOT_EXISTS); throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_NOT_EXISTS);
} }
// 更新到数据库标记删除 // 更新到数据库标记删除
errorCodeMapper.deleteById(errorCodeDO.getId()); errorCodeMapper.deleteById(errorCodeDO.getId());
// TODO: 2020-05-08 刷新对外提供的错误码列表 // TODO: 2020-05-10 刷新对外提供的错误码列表
// TODO: 2020-05-10 ServiceExceptionUtil中未提供去除错误码操作后续新增此接口是否影响
} }
/** /**
@ -109,4 +123,15 @@ public class ErrorCodeServiceImpl implements ErrorCodeService {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_DUPLICATE, errorCodeDO.getCode()); throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ERROR_CODE_DUPLICATE, errorCodeDO.getCode());
} }
} }
private PageResult listToPageList(int currentPage, int rows, List list){
currentPage = currentPage * rows;
Integer sum = list.size();
if (currentPage + rows > sum){
list = list.subList(currentPage, sum);
}else {
list = list.subList(currentPage, currentPage + rows);
}
return new PageResult().setList(list).setTotal(sum);
}
} }

View File

@ -3,16 +3,17 @@ package cn.iocoder.mall.system.rest.controller.errorCode;
import cn.iocoder.common.framework.constant.MallConstants; import cn.iocoder.common.framework.constant.MallConstants;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.security.core.annotation.RequiresPermissions;
import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO; 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.ErrorCodeAddDTO;
import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeDeleteDTO; 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.ErrorCodePageDTO;
import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO;
import cn.iocoder.mall.system.biz.service.errorCode.ErrorCodeService; import cn.iocoder.mall.system.biz.service.errorcode.ErrorCodeService;
import cn.iocoder.mall.system.rest.convert.errorcode.ErrorCodeConvert; 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.ErrorCodeAddRequest;
import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageRequest; import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageRequest;
import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageResponse; import cn.iocoder.mall.system.rest.response.errorcode.ErrorCodePageResponse;
import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeUpdateRequest; import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeUpdateRequest;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
@ -26,7 +27,7 @@ import org.springframework.web.bind.annotation.*;
* @author youyusi * @author youyusi
*/ */
@RestController @RestController
@RequestMapping(MallConstants.ROOT_PATH_ADMIN + "/errorCode") @RequestMapping(MallConstants.ROOT_PATH_ADMIN + "/errorcode")
@Api("错误码") @Api("错误码")
public class SystemErrorCodeController { public class SystemErrorCodeController {
@Autowired @Autowired
@ -34,7 +35,7 @@ public class SystemErrorCodeController {
@GetMapping("/page") @GetMapping("/page")
@ApiOperation(value = "错误码分页") @ApiOperation(value = "错误码分页")
// @RequiresPermissions("system:errorCode:page") @RequiresPermissions("system:errorcode:page")
public CommonResult<PageResult<ErrorCodePageResponse>> page(ErrorCodePageRequest request) { public CommonResult<PageResult<ErrorCodePageResponse>> page(ErrorCodePageRequest request) {
ErrorCodePageDTO pageDTO = ErrorCodeConvert.INSTANCE.convert(request); ErrorCodePageDTO pageDTO = ErrorCodeConvert.INSTANCE.convert(request);
PageResult<ErrorCodeBO> pageResult = errorCodeService.getErrorCodePage(pageDTO); PageResult<ErrorCodeBO> pageResult = errorCodeService.getErrorCodePage(pageDTO);
@ -43,7 +44,7 @@ public class SystemErrorCodeController {
@PostMapping("/add") @PostMapping("/add")
@ApiOperation(value = "创建错误码") @ApiOperation(value = "创建错误码")
// @RequiresPermissions("system:errorCode:add") @RequiresPermissions("system:errorcode:add")
public CommonResult<Integer> add(ErrorCodeAddRequest request) { public CommonResult<Integer> add(ErrorCodeAddRequest request) {
ErrorCodeAddDTO addDTO = ErrorCodeConvert.INSTANCE.convert(request); ErrorCodeAddDTO addDTO = ErrorCodeConvert.INSTANCE.convert(request);
return CommonResult.success(errorCodeService.addErrorCode(addDTO)); return CommonResult.success(errorCodeService.addErrorCode(addDTO));
@ -51,7 +52,7 @@ public class SystemErrorCodeController {
@PostMapping("/update") @PostMapping("/update")
@ApiOperation(value = "更新错误码") @ApiOperation(value = "更新错误码")
// @RequiresPermissions("system:errorCode:update") @RequiresPermissions("system:errorcode:update")
public CommonResult<Boolean> update(ErrorCodeUpdateRequest request) { public CommonResult<Boolean> update(ErrorCodeUpdateRequest request) {
ErrorCodeUpdateDTO updateDTO = ErrorCodeConvert.INSTANCE.convert(request); ErrorCodeUpdateDTO updateDTO = ErrorCodeConvert.INSTANCE.convert(request);
errorCodeService.updateErrorCode(updateDTO); errorCodeService.updateErrorCode(updateDTO);
@ -60,7 +61,7 @@ public class SystemErrorCodeController {
@PostMapping("/delete") @PostMapping("/delete")
@ApiOperation(value = "删除错误码") @ApiOperation(value = "删除错误码")
// @RequiresPermissions("system:errorCode:delete") @RequiresPermissions("system:errorcode:delete")
@ApiImplicitParam(name = "id", value = "错误码编号", required = true, example = "1") @ApiImplicitParam(name = "id", value = "错误码编号", required = true, example = "1")
public CommonResult<Boolean> delete(@RequestParam("id") Integer id) { public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
ErrorCodeDeleteDTO deleteDTO = new ErrorCodeDeleteDTO().setId(id); ErrorCodeDeleteDTO deleteDTO = new ErrorCodeDeleteDTO().setId(id);

View File

@ -1,23 +1,14 @@
package cn.iocoder.mall.system.rest.convert.errorcode; package cn.iocoder.mall.system.rest.convert.errorcode;
import cn.iocoder.common.framework.vo.PageResult; 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.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.ErrorCodeAddDTO;
import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO; import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodePageDTO;
import cn.iocoder.mall.system.biz.dto.errorcode.ErrorCodeUpdateDTO; 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.ErrorCodeAddRequest;
import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageRequest; import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageRequest;
import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodePageResponse; import cn.iocoder.mall.system.rest.response.errorcode.ErrorCodePageResponse;
import cn.iocoder.mall.system.rest.request.errorcode.ErrorCodeUpdateRequest; 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.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.rest.request.errorcode; package cn.iocoder.mall.system.rest.response.errorcode;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import lombok.Data; import lombok.Data;

View File

@ -0,0 +1,16 @@
package cn.iocoder.mall.system.rpc.api.errorcode;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.system.rpc.response.errorcode.ErrorCodeResponse;
import java.util.List;
/**
* ErrorCode RPC 接口
* @author ding
*/
public interface ErrorCodeRPC {
CommonResult<List<ErrorCodeResponse>> getErrorCode();
}

View File

@ -0,0 +1,33 @@
package cn.iocoder.mall.system.rpc.response.errorcode;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* 错误码模块
* @author ding
*/
@ApiModel("管理员 - 错误码模块 - 查询错误码")
@Data
@Accessors(chain = true)
public class ErrorCodeResponse {
/**
* 错误码编号
*/
private Integer id;
/**
* 错误码编码
*/
private Integer code;
/**
* 错误码错误信息
*/
private String message;
/**
* 添加时间
*/
private Date createTime;
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.mall.system.rpc.convert.errorcode;
import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO;
import cn.iocoder.mall.system.rpc.response.errorcode.ErrorCodeResponse;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ErrorCodeConvert {
ErrorCodeConvert INSTANCE = Mappers.getMapper(ErrorCodeConvert.class);
List<ErrorCodeResponse> convert(List<ErrorCodeBO> bean);
}

View File

@ -0,0 +1,30 @@
package cn.iocoder.mall.system.rpc.rpc.errorcode;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.system.biz.bo.errorcode.ErrorCodeBO;
import cn.iocoder.mall.system.biz.service.errorcode.ErrorCodeService;
import cn.iocoder.mall.system.rpc.api.errorcode.ErrorCodeRPC;
import cn.iocoder.mall.system.rpc.convert.errorcode.ErrorCodeConvert;
import cn.iocoder.mall.system.rpc.convert.user.UserConvert;
import cn.iocoder.mall.system.rpc.response.errorcode.ErrorCodeResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
*
* @author ding
*/
@Service(version = "${dubbo.provider.ErrorCodeRPC.version}", validation = "true")
public class ErrorCodeRPCImpl implements ErrorCodeRPC {
@Autowired
private ErrorCodeService errorCodeService;
@Override
public CommonResult<List<ErrorCodeResponse>> getErrorCode() {
List<ErrorCodeBO> list = errorCodeService.getErrorCodeListAll();
return CommonResult.success(ErrorCodeConvert.INSTANCE.convert(list));
}
}

View File

@ -25,6 +25,8 @@ dubbo:
version: 1.0.0 version: 1.0.0
UserAddressRPC: UserAddressRPC:
version: 1.0.0 version: 1.0.0
ErrorCodeRPC:
version: 1.0.0
# Dubbo 服务消费者的配置 # Dubbo 服务消费者的配置
consumer: consumer:
@ -38,3 +40,5 @@ dubbo:
version: 1.0.0 version: 1.0.0
UserRPC: UserRPC:
version: 1.0.0 version: 1.0.0
ErrorCodeRPC:
version: 1.0.0