物资LOG日志

This commit is contained in:
syruan 2024-04-18 16:34:01 +08:00
parent fc5aad9205
commit 4ba7fa1e04
8 changed files with 144 additions and 13 deletions

View File

@ -34,6 +34,10 @@ public class BmNumLogs extends BaseEntity {
@Excel(name = "接口地址/请求方法")
private String method;
/** 请求方式 */
@Excel(name = "请求方式")
private String requestMethod;
/**
* 实例
*/
@ -46,6 +50,12 @@ public class BmNumLogs extends BaseEntity {
@Excel(name = "规格id")
private Integer typeId;
/**
* 数量
*/
@Excel(name = "数量")
private String num;
/**
* 描述/请求参数/实体
*/
@ -80,6 +90,6 @@ public class BmNumLogs extends BaseEntity {
* 状态: 默认0, 其他值则为异常
*/
@Excel(name = "状态")
private Byte status;
private Integer status;
}

View File

@ -2,12 +2,15 @@ package com.bonus.sgzb.common.log.aspect;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.log.filter.PropertyPreExcludeFilter;
import com.bonus.sgzb.common.log.service.AsyncLogService;
import com.bonus.sgzb.system.api.domain.BmNumLogs;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
@ -67,8 +70,12 @@ public class LogAspect
@AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
{
if (controllerLog.businessType() == BusinessType.MATERIAL) {
handleMaterialLog(joinPoint, controllerLog, null, jsonResult);
} else {
handleLog(joinPoint, controllerLog, null, jsonResult);
}
}
/**
* 拦截异常操作
@ -129,6 +136,48 @@ public class LogAspect
}
}
protected void handleMaterialLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
try
{
// *========数据库物资日志=========*//
BmNumLogs bmNumLogs = new BmNumLogs();
bmNumLogs.setStatus(BusinessStatus.SUCCESS.ordinal());
// 请求的地址
bmNumLogs.setMethod(StringUtils.substring(Objects.requireNonNull(ServletUtils.getRequest()).getRequestURI(), 0, 255));
String username = SecurityUtils.getUsername();
if (StringUtils.isNotBlank(username))
{
bmNumLogs.setCreator(username);
}
if (e != null) {
bmNumLogs.setStatus(BusinessStatus.FAIL.ordinal());
bmNumLogs.setJsonResult(StringUtils.substring(e.getMessage(), 0, 2000));
}
// 设置方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
bmNumLogs.setModelTitle(className + "." + methodName + "()");
// 设置请求方式
bmNumLogs.setRequestMethod(ServletUtils.getRequest().getMethod());
// 处理设置注解上的参数
getControllerMethodDescriptionByMaterial(joinPoint, controllerLog, bmNumLogs, jsonResult);
// 保存数据库
asyncLogService.saveNumLog(bmNumLogs);
}
catch (Exception exp)
{
// 记录本地异常日志
log.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
}
finally
{
TIME_THREADLOCAL.remove();
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
@ -157,6 +206,25 @@ public class LogAspect
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
* @param log 日志
* @param materialLog 物资日志
* @throws Exception
*/
public void getControllerMethodDescriptionByMaterial(JoinPoint joinPoint, Log log, BmNumLogs materialLog, Object jsonResult) throws Exception
{
// 设置标题
materialLog.setModelTitle(log.title());
// 获取参数的信息传入到数据库中
setRequestValue(joinPoint, materialLog, log.excludeParamNames());
// 保存response参数和值
if (StringUtils.isNotNull(jsonResult)) {
materialLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000));
}
}
/**
* 获取请求的参数放到log中
*
@ -176,6 +244,42 @@ public class LogAspect
}
}
/**
* 获取请求的参数放到log中
*
* @param numLog 物资日志
* @throws Exception 异常
*/
private void setRequestValue(JoinPoint joinPoint, BmNumLogs numLog, String[] excludeParamNames) throws Exception {
String requestMethod = numLog.getRequestMethod();
Map<?, ?> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
if (StringUtils.isEmpty(paramsMap)) {
if ((HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))){
String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
numLog.setDescription(StringUtils.substring(params, 0, 2000));
}
} else {
if (paramsMap.containsKey("num")) {
if (paramsMap.get("num") instanceof Integer) {
numLog.setNum(String.valueOf(paramsMap.get("num")));
} else if (paramsMap.get("num") instanceof String) {
numLog.setNum((String) paramsMap.get("num"));
}
}
if (paramsMap.containsKey("taskId")) {
numLog.setTask(String.valueOf(paramsMap.get("taskId")));
}
if (paramsMap.containsKey("typeId")) {
if (paramsMap.get("typeId") instanceof Integer) {
numLog.setTypeId((Integer) paramsMap.get("typeId"));
} else if (paramsMap.get("typeId") instanceof String) {
numLog.setTypeId(Integer.valueOf((String) paramsMap.get("typeId")));
}
}
numLog.setDescription(StringUtils.substring(JSON.toJSONString(paramsMap, excludePropertyPreFilter(excludeParamNames)), 0, 2000));
}
}
/**
* 参数拼装
*/

View File

@ -1,5 +1,6 @@
package com.bonus.sgzb.common.log.service;
import com.bonus.sgzb.system.api.domain.BmNumLogs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@ -26,4 +27,12 @@ public class AsyncLogService
{
remoteLogService.saveLog(sysOperLog, SecurityConstants.INNER);
}
/**
* 保存物资日志记录
*/
@Async
public void saveNumLog(BmNumLogs bmNumLogsLog) throws Exception {
remoteLogService.saveNumberLog(bmNumLogsLog, SecurityConstants.INNER);
}
}

View File

@ -54,6 +54,7 @@ public class SecondaryWarehouseController extends BaseController {
*/
@ApiOperation(value = "出库/退库操作")
@PostMapping("/operate")
@Log(title = "二级库管理--出库退库操作", businessType = BusinessType.MATERIAL)
public AjaxResult operate(TeamLeaseInfo bean){
return toAjax(service.addOperate(bean));
}

View File

@ -1,10 +1,10 @@
package com.bonus.sgzb.material.controller;
package com.bonus.sgzb.system.controller;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.security.annotation.InnerAuth;
import com.bonus.sgzb.system.api.domain.BmNumLogs;
import com.bonus.sgzb.material.service.impl.BmNumLogsService;
import com.bonus.sgzb.system.service.impl.BmNumLogsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,4 +1,4 @@
package com.bonus.sgzb.material.mapper;
package com.bonus.sgzb.system.mapper;
import com.bonus.sgzb.system.api.domain.BmNumLogs;
import org.apache.ibatis.annotations.Mapper;

View File

@ -1,11 +1,11 @@
package com.bonus.sgzb.material.service.impl;
package com.bonus.sgzb.system.service.impl;
import com.bonus.sgzb.system.mapper.BmNumLogsMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.bonus.sgzb.system.api.domain.BmNumLogs;
import com.bonus.sgzb.material.mapper.BmNumLogsMapper;
import java.util.List;

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.sgzb.material.mapper.BmNumLogsMapper">
<mapper namespace="com.bonus.sgzb.system.mapper.BmNumLogsMapper">
<resultMap id="BaseResultMap" type="com.bonus.sgzb.system.api.domain.BmNumLogs">
<!--@mbg.generated-->
<!--@Table bm_num_logs-->
@ -9,6 +9,7 @@
<result column="method" jdbcType="VARCHAR" property="method" />
<result column="task" jdbcType="VARCHAR" property="task" />
<result column="type_id" jdbcType="INTEGER" property="typeId" />
<result column="num" jdbcType="VARCHAR" property="num" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="json_result" jdbcType="VARCHAR" property="jsonResult" />
<result column="time" jdbcType="TIMESTAMP" property="time" />
@ -19,8 +20,8 @@
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, model_title, `method`, task, type_id, description, json_result, `time`, creator,
remark, status
id, model_title, `method`, task, type_id, num, `description`, json_result, `time`, creator,
remark, `status`
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
@ -47,11 +48,11 @@
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.bonus.sgzb.system.api.domain.BmNumLogs" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into bm_num_logs (model_title, `method`, task,
type_id, description, json_result,
type_id, num, description, json_result,
`time`, creator, remark
)
values (#{modelTitle,jdbcType=VARCHAR}, #{method,jdbcType=VARCHAR}, #{task,jdbcType=VARCHAR},
#{typeId,jdbcType=INTEGER}, #{description,jdbcType=VARCHAR}, #{jsonResult,jdbcType=VARCHAR},
#{typeId,jdbcType=INTEGER}, #{num,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{jsonResult,jdbcType=VARCHAR},
#{time,jdbcType=TIMESTAMP}, #{creator,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}
)
</insert>
@ -72,8 +73,11 @@
<if test="typeId != null">
type_id,
</if>
<if test="num != null and num != ''">
num,
</if>
<if test="description != null and description != ''">
description,
`description`,
</if>
<if test="jsonResult != null and jsonResult != ''">
json_result,
@ -101,6 +105,9 @@
<if test="typeId != null">
#{typeId,jdbcType=INTEGER},
</if>
<if test="num != null and num != ''">
#{num,jdbcType=VARCHAR},
</if>
<if test="description != null and description != ''">
#{description,jdbcType=VARCHAR},
</if>