系统异常日志模块的迁移

This commit is contained in:
YunaiV
2020-07-16 09:04:18 +08:00
parent 42e30bf380
commit 02dda60e60
35 changed files with 814 additions and 428 deletions

View File

@@ -1,8 +1,14 @@
package cn.iocoder.mall.systemservice.convert.systemlog;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.systemlog.SystemExceptionLogDO;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogCreateDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogPageDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.vo.SystemExceptionLogVO;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogBO;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogCreateBO;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -15,4 +21,14 @@ public interface SystemExceptionLogConvert {
SystemExceptionLogCreateBO convert(SystemExceptionLogCreateDTO bean);
PageResult<SystemExceptionLogBO> convertPage(IPage<SystemExceptionLogDO> page);
SystemExceptionLogBO convert(SystemExceptionLogDO bean);
SystemExceptionLogVO convert(SystemExceptionLogBO bean);
SystemExceptionLogPageBO convert(SystemExceptionLogPageDTO bean);
PageResult<SystemExceptionLogVO> convertPage(PageResult<SystemExceptionLogBO> page);
}

View File

@@ -2,6 +2,8 @@ package cn.iocoder.mall.systemservice.dal.mysql.dataobject.systemlog;
import cn.iocoder.common.framework.enums.UserTypeEnum;
import cn.iocoder.mall.mybatis.dataobject.BaseDO;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO;
import cn.iocoder.mall.systemservice.enums.systemlog.SystemExceptionLogProcessStatusEnum;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -117,4 +119,21 @@ public class SystemExceptionLogDO extends BaseDO {
*/
private Integer exceptionLineNumber;
/**
* 处理状态
*
* 外键 {@link SystemExceptionLogProcessStatusEnum}
*/
private Integer processStatus;
/**
* 处理时间
*/
private Date processTime;
/**
* 处理管理员编号
*
* 外键 {@link AdminDO#getId()}
*/
private Integer processAdminId;
}

View File

@@ -1,10 +1,24 @@
package cn.iocoder.mall.systemservice.dal.mysql.mapper.systemlog;
import cn.iocoder.mall.mybatis.query.QueryWrapperX;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.systemlog.SystemExceptionLogDO;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogPageBO;
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;
@Repository
public interface SystemExceptionLogMapper extends BaseMapper<SystemExceptionLogDO> {
default IPage<SystemExceptionLogDO> selectPage(SystemExceptionLogPageBO pageBO) {
return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
new QueryWrapperX<SystemExceptionLogDO>()
.eqIfPresent("user_id", pageBO.getUserId())
.eqIfPresent("user_type", pageBO.getUserType())
.eqIfPresent("application_name", pageBO.getApplicationName())
.eqIfPresent("processStatus", pageBO.getProcessStatus())
.orderByDesc("start_time"));
}
}

View File

@@ -18,7 +18,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.Set;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.AUTHORIZATION_PERMISSION_DENY;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.PERMISSION_DENY;
/**
* 权限 Manager
@@ -98,7 +98,7 @@ public class PermissionManager {
// 查询管理员拥有的角色关联数据
Set<Integer> roleIds = permissionService.listAdminRoleIds(checkDTO.getAdminId());
if (CollectionUtil.isEmpty(roleIds)) { // 如果没有角色,默认无法访问
throw ServiceExceptionUtil.exception(AUTHORIZATION_PERMISSION_DENY);
throw ServiceExceptionUtil.exception(PERMISSION_DENY);
}
// 判断是否为超管。若是超管,默认有所有权限
if (roleService.hasSuperAdmin(roleIds)) {

View File

@@ -1,8 +1,13 @@
package cn.iocoder.mall.systemservice.manager.systemlog;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.convert.systemlog.SystemExceptionLogConvert;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogCreateDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogPageDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogProcessDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.vo.SystemExceptionLogVO;
import cn.iocoder.mall.systemservice.service.systemlog.SystemExceptionLogService;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -13,10 +18,47 @@ import org.springframework.stereotype.Service;
public class SystemExceptionLogManager {
@Autowired
private SystemExceptionLogService systemLogService;
private SystemExceptionLogService systemExceptionLogService;
/**
* 创建系统异常日志
*
* @param createDTO 创建系统异常日志 DTO
*/
public void createSystemExceptionLog(SystemExceptionLogCreateDTO createDTO) {
systemLogService.createSystemExceptionLog(SystemExceptionLogConvert.INSTANCE.convert(createDTO));
systemExceptionLogService.createSystemExceptionLog(SystemExceptionLogConvert.INSTANCE.convert(createDTO));
}
/**
* 获得系统异常日志
*
* @param systemExceptionLogId 系统异常日志编号
* @return 系统异常日志
*/
public SystemExceptionLogVO getSystemExceptionLog(Integer systemExceptionLogId) {
SystemExceptionLogBO systemExceptionLogBO = systemExceptionLogService.getSystemExceptionLog(systemExceptionLogId);
return SystemExceptionLogConvert.INSTANCE.convert(systemExceptionLogBO);
}
/**
* 获得系统异常日志分页
*
* @param pageDTO 系统异常日志分页查询
* @return 系统异常日志分页结果
*/
public PageResult<SystemExceptionLogVO> pageSystemExceptionLog(SystemExceptionLogPageDTO pageDTO) {
PageResult<SystemExceptionLogBO> pageResultBO = systemExceptionLogService.pageSystemExceptionLog(SystemExceptionLogConvert.INSTANCE.convert(pageDTO));
return SystemExceptionLogConvert.INSTANCE.convertPage(pageResultBO);
}
/**
* 处理系统异常日志,完成或者忽略
*
* @param processDTO 处理 DTO
*/
public void processSystemExceptionLog(SystemExceptionLogProcessDTO processDTO) {
systemExceptionLogService.processSystemExceptionLog(processDTO.getLogId(), processDTO.getProcessAdminId(),
processDTO.getProcessStatus());
}
}

View File

@@ -1,11 +1,17 @@
package cn.iocoder.mall.systemservice.rpc.systemlog;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.manager.systemlog.SystemExceptionLogManager;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogCreateDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogPageDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogProcessDTO;
import cn.iocoder.mall.systemservice.rpc.systemlog.vo.SystemExceptionLogVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@Service(version = "${dubbo.provider.SystemExceptionLogRpc.version}", validation = "false")
public class SystemExceptionLogRpcImpl implements SystemExceptionLogRpc {
@@ -15,6 +21,22 @@ public class SystemExceptionLogRpcImpl implements SystemExceptionLogRpc {
@Override
public CommonResult<Boolean> createSystemExceptionLog(SystemExceptionLogCreateDTO createDTO) {
systemExceptionLogManager.createSystemExceptionLog(createDTO);
return success(true);
}
@Override
public CommonResult<SystemExceptionLogVO> getSystemExceptionLog(Integer systemExceptionLogId) {
return success(systemExceptionLogManager.getSystemExceptionLog(systemExceptionLogId));
}
@Override
public CommonResult<PageResult<SystemExceptionLogVO>> pageSystemExceptionLog(SystemExceptionLogPageDTO pageDTO) {
return success(systemExceptionLogManager.pageSystemExceptionLog(pageDTO));
}
@Override
public CommonResult<Boolean> processSystemExceptionLog(SystemExceptionLogProcessDTO processDTO) {
systemExceptionLogManager.processSystemExceptionLog(processDTO);
return CommonResult.success(true);
}

View File

@@ -68,7 +68,7 @@ public class PermissionService {
if (!CollectionUtils.isEmpty(resourceIds)) {
int dbResourceSize = resourceMapper.selectCountByIdsAndType(resourceIds, null);
if (resourceIds.size() != dbResourceSize) {
throw ServiceExceptionUtil.exception(AUTHORIZATION_ROLE_ASSIGN_RESOURCE_NOT_EXISTS);
throw ServiceExceptionUtil.exception(PERMISSION_ROLE_ASSIGN_RESOURCE_NOT_EXISTS);
}
}
// TODO 芋艿,这里先简单实现。即方式是,删除老的分配的资源关系,然后添加新的分配的资源关系
@@ -150,13 +150,13 @@ public class PermissionService {
// 权限验证
List<RoleResourceDO> roleResourceDOs = roleResourceMapper.selectListByResourceIds(permissionIds);
if (CollectionUtil.isEmpty(roleResourceDOs)) { // 资源未授予任何角色,必然权限验证不通过
throw ServiceExceptionUtil.exception(AUTHORIZATION_PERMISSION_DENY);
throw ServiceExceptionUtil.exception(PERMISSION_DENY);
}
Map<Integer, List<Integer>> resourceRoleMap = CollectionUtils.convertMultiMap(roleResourceDOs,
RoleResourceDO::getResourceId, RoleResourceDO::getRoleId);
for (Map.Entry<Integer, List<Integer>> entry : resourceRoleMap.entrySet()) {
if (!CollectionUtil.containsAny(roleIds, entry.getValue())) { // 所以有任一不满足,就验证失败,抛出异常
throw ServiceExceptionUtil.exception(AUTHORIZATION_PERMISSION_DENY);
throw ServiceExceptionUtil.exception(PERMISSION_DENY);
}
}
}

View File

@@ -1,12 +1,21 @@
package cn.iocoder.mall.systemservice.service.systemlog;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.convert.systemlog.SystemExceptionLogConvert;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.systemlog.SystemExceptionLogDO;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.systemlog.SystemExceptionLogMapper;
import cn.iocoder.mall.systemservice.enums.systemlog.SystemExceptionLogProcessStatusEnum;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogBO;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogCreateBO;
import cn.iocoder.mall.systemservice.service.systemlog.bo.SystemExceptionLogPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.SYSTEM_EXCEPTION_LOG_NOT_FOUND;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.SYSTEM_EXCEPTION_LOG_PROCESSED;
/**
* 系统异常日志 Service
*/
@@ -16,9 +25,51 @@ public class SystemExceptionLogService {
@Autowired
private SystemExceptionLogMapper systemExceptionLogMapper;
/**
* 创建系统异常日志
*
* @param createBO 创建 BO
*/
public void createSystemExceptionLog(SystemExceptionLogCreateBO createBO) {
SystemExceptionLogDO logDO = SystemExceptionLogConvert.INSTANCE.convert(createBO);
logDO.setProcessStatus(SystemExceptionLogProcessStatusEnum.INIT.getStatus());
systemExceptionLogMapper.insert(logDO);
}
/**
* 处理系统异常日志
*
* @param logId 日志编号
* @param processAdminId 处理管理员编号
* @param processStatus 处理状态
*/
public void processSystemExceptionLog(Integer logId, Integer processAdminId, Integer processStatus) {
SystemExceptionLogDO logDO = systemExceptionLogMapper.selectById(logId);
if (logDO == null) {
throw ServiceExceptionUtil.exception(SYSTEM_EXCEPTION_LOG_NOT_FOUND);
}
if (!SystemExceptionLogProcessStatusEnum.INIT.getStatus().equals(logDO.getProcessStatus())) {
throw ServiceExceptionUtil.exception(SYSTEM_EXCEPTION_LOG_PROCESSED);
}
// 标记处理
SystemExceptionLogDO updateObj = new SystemExceptionLogDO().setId(logId)
.setProcessAdminId(processAdminId).setProcessStatus(processStatus);
systemExceptionLogMapper.updateById(updateObj);
}
/**
* 获得系统异常日志分页
*
* @param pageBO 系统异常日志分页查询
* @return 系统异常日志分页结果
*/
public PageResult<SystemExceptionLogBO> pageSystemExceptionLog(SystemExceptionLogPageBO pageBO) {
IPage<SystemExceptionLogDO> systemExceptionLogDOPage = systemExceptionLogMapper.selectPage(pageBO);
return SystemExceptionLogConvert.INSTANCE.convertPage(systemExceptionLogDOPage);
}
public SystemExceptionLogBO getSystemExceptionLog(Integer logId) {
SystemExceptionLogDO logDO = systemExceptionLogMapper.selectById(logId);
return SystemExceptionLogConvert.INSTANCE.convert(logDO);
}
}

View File

@@ -0,0 +1,128 @@
package cn.iocoder.mall.systemservice.service.systemlog.bo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* 系统异常日志 BO
*/
@Data
@Accessors(chain = true)
public class SystemExceptionLogBO {
/**
* 编号
*/
private Integer id;
/**
* 用户编号
*/
private Integer userId;
/**
* 用户类型
*/
private Integer userType;
/**
* 链路追踪编号
*
* 一般来说通过链路追踪编号可以将访问日志错误日志链路追踪日志logger 打印日志等,结合在一起,从而进行排错。
*/
private String traceId;
/**
* 应用名
*
* 目前读取 spring.application.name
*/
private String applicationName;
/**
* 访问地址
*/
private String uri;
/**
* 参数
*/
private String queryString;
/**
* http 方法
*/
private String method;
/**
* userAgent
*/
private String userAgent;
/**
* ip
*/
private String ip;
/**
* 异常发生时间
*/
private Date exceptionTime;
/**
* 异常名
*
* {@link Throwable#getClass()} 的类全名
*/
private String exceptionName;
/**
* 异常导致的消息
*
* {@link cn.iocoder.common.framework.util.ExceptionUtil#getMessage(Throwable)}
*/
private String exceptionMessage;
/**
* 异常导致的根消息
*
* {@link cn.iocoder.common.framework.util.ExceptionUtil#getRootCauseMessage(Throwable)}
*/
private String exceptionRootCauseMessage;
/**
* 异常的栈轨迹
*
* {@link cn.iocoder.common.framework.util.ExceptionUtil#getServiceException(Exception)}
*/
private String exceptionStackTrace;
/**
* 异常发生的类全名
*
* {@link StackTraceElement#getClassName()}
*/
private String exceptionClassName;
/**
* 异常发生的类文件
*
* {@link StackTraceElement#getFileName()}
*/
private String exceptionFileName;
/**
* 异常发生的方法名
*
* {@link StackTraceElement#getMethodName()}
*/
private String exceptionMethodName;
/**
* 异常发生的方法所在行
*
* {@link StackTraceElement#getLineNumber()}
*/
private Integer exceptionLineNumber;
/**
* 处理状态
*/
private Integer processStatus;
/**
* 处理时间
*/
private Date processTime;
/**
* 处理管理员编号
*/
private Integer processAdminId;
/**
* 创建时间
*/
private Date createTime;
}

View File

@@ -0,0 +1,35 @@
package cn.iocoder.mall.systemservice.service.systemlog.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 SystemExceptionLogPageBO extends PageParam {
/**
* 用户编号
*/
private Integer userId;
/**
* 用户类型
*/
private Integer userType;
/**
* 应用名
*
* 目前读取 spring.application.name
*/
private String applicationName;
/**
* 处理状态
*/
private Integer processStatus;
}