Commit 17666132 by wuwenlong

操作日志;

parent 8fef1e5d
package com.baosight.hpjx.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baosight.hpjx.aspect.annotation.OperationLogAnnotation;
import com.baosight.hpjx.core.security.UserSessionUtils;
import com.baosight.hpjx.util.DateUtils;
import com.baosight.iplat4j.core.data.ibatis.dao.Dao;
import com.baosight.iplat4j.core.data.ibatis.dao.SqlMapDaoLogProxy;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.ioc.spring.PlatApplicationContext;
import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
/**
* 切面处理类,操作日志异常日志记录处理
*/
@Aspect //记录日志实现性能跟踪以及用户行为分析
@Component
public class OperationLogAspect {
private static Dao dao = (Dao) PlatApplicationContext.getBean("dao");
/**
* 设置操作日志切入点 记录操作日志 在注解的位置切入代码
*/
@Pointcut("@annotation(com.baosight.hpjx.aspect.annotation.OperationLogAnnotation)")
public void operLogPointCut() {
}
/**
* 设置操作异常切入点记录异常日志 扫描所有dc包下操作
*/
@Pointcut("execution(* com.baosight.hpjx.hp.*.service..*.*(..))")
public void operExceptionLogPointCut() {
}
/**
* 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
*
* @param joinPoint 切入点
* @param keys 返回结果
*/
@AfterReturning(value = "operLogPointCut()", returning = "keys")
public void saveOperLog(JoinPoint joinPoint, Object keys) {
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取切入点所在的方法
Method method = signature.getMethod();
OperationLogAnnotation opLog = method.getAnnotation(OperationLogAnnotation.class);
if (opLog != null) {
try {
String companyCode;
try {
companyCode = UserSessionUtils.getCompanyCode();
} catch (Exception e) {
companyCode = "";
}
String userId = UserSession.getLoginName();
String loginName = UserSession.getLoginCName();
String operModul = opLog.operModul();
String operType = opLog.operType();
String operDesc = opLog.operDesc();
boolean saveContet = opLog.saveContent();
String sql = "INSERT INTO hpjx.SYS_OPERATION_LOG (OPER_MODUL,OPER_TYPE,OPER_DESC," +
"OPER_CONTENT,COMPANY_CODE,CREATED_BY,CREATED_NAME,CREATED_TIME)" +
" VALUES (?,?,?,?,?,?,?,?)";
PreparedStatement ps = ((SqlMapDaoLogProxy) dao).getSqlMapClient().getDataSource().getConnection().prepareStatement(sql);
ps.setString(1,operModul);
ps.setString(2,operType);
ps.setString(3,operDesc);
if(saveContet && joinPoint.getArgs() != null) {
String strBlock = EiConstant.resultBlock;
EiInfo result = ((EiInfo) joinPoint.getArgs()[0]);
ps.setString(4, JSONObject.toJSONString(((EiInfo) joinPoint.getArgs()[0]).getBlock(strBlock).getRows().get(0)));
}else {
ps.setString(4, "");
}
ps.setString(5,companyCode);
ps.setString(6,userId);
ps.setString(7,loginName);
ps.setString(8,DateUtils.shortDateTime());
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 异常返回通知,用于拦截异常日志信息 连接点抛出异常后执行
*
* @param joinPoint 切入点
* @param e 异常信息
*/
@AfterThrowing(pointcut = "operExceptionLogPointCut()", throwing = "e")
public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
// 获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 从获取RequestAttributes中获取HttpServletRequest的信息
HttpServletRequest request = (HttpServletRequest) requestAttributes
.resolveReference(RequestAttributes.REFERENCE_REQUEST);
try {
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取切入点所在的方法
Method method = signature.getMethod();
// 获取请求的类名
String className = joinPoint.getTarget().getClass().getName();
// 获取请求的方法名
String methodName = method.getName();
methodName = className + "." + methodName;
// 请求的参数
Map<String, String> rtnMap = converMap(request.getParameterMap());
// 将参数所在的数组转换成json
String params = JSON.toJSONString(rtnMap);
// 异常信息
String errorMsg = stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace());
//TODO 异常信息持久化
System.out.println(params);
System.out.println(errorMsg);
} catch (Exception e2) {
e2.printStackTrace();
}
}
/**
* 转换request 请求参数
*
* @param paramMap request获取的参数数组
*/
public Map<String, String> converMap(Map<String, String[]> paramMap) {
Map<String, String> rtnMap = new HashMap<String, String>();
for (String key : paramMap.keySet()) {
rtnMap.put(key, paramMap.get(key)[0]);
}
return rtnMap;
}
/**
* 转换异常信息为字符串
*
* @param exceptionName 异常名称
* @param exceptionMessage 异常信息
* @param elements 堆栈信息
*/
public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
StringBuffer strbuff = new StringBuffer();
for (StackTraceElement stet : elements) {
strbuff.append(stet + "\n");
}
String message = exceptionName + ":" + exceptionMessage + "\n\t" + strbuff.toString();
return message;
}
}
\ No newline at end of file
package com.baosight.hpjx.aspect.annotation;
import java.lang.annotation.*;
/**
* @description: 自定义操作日志注解
*/
@Target(ElementType.METHOD) //方法
@Retention(RetentionPolicy.RUNTIME) //生命周期
@Documented
public @interface OperationLogAnnotation {
String operModul() default ""; // 操作模块
String operType() default ""; // 操作类型
String operDesc() default ""; // 操作说明
boolean saveContent() default true;
}
package com.baosight.hpjx.hp.sc.service;
import com.baosight.hpjx.aspect.annotation.OperationLogAnnotation;
import com.baosight.hpjx.common.DdynamicEnum;
import com.baosight.hpjx.core.constant.CommonConstant;
import com.baosight.hpjx.core.dao.DaoUtils;
......@@ -30,6 +31,7 @@ public class ServiceHPSC001 extends ServiceBase {
/**
* 画面初始化.
*/
@OperationLogAnnotation(operModul = "销售管理",operType = "查询",operDesc = "初始化")
public EiInfo initLoad(EiInfo inInfo) {
try {
inInfo = super.query(inInfo, "HPSC001.query", new HPSC001());
......@@ -48,6 +50,7 @@ public class ServiceHPSC001 extends ServiceBase {
* 查询操作.
*/
@Override
@OperationLogAnnotation(operModul = "销售管理",operType = "查询")
public EiInfo query(EiInfo inInfo) {
/* 调用EI查询方法.*/
EiInfo outInfo = super.query(inInfo, "HPSC001.query", new HPSC001());
......@@ -59,6 +62,7 @@ public class ServiceHPSC001 extends ServiceBase {
* 新增操作.
*/
@Override
@OperationLogAnnotation(operModul = "销售管理",operType = "新增")
public EiInfo insert(EiInfo inInfo) {
try {
HPSC001 hpsc001 = new HPSC001();
......@@ -87,6 +91,7 @@ public class ServiceHPSC001 extends ServiceBase {
/**
* 修改操作.
*/
@OperationLogAnnotation(operModul = "销售管理",operType = "修改")
public EiInfo update(EiInfo inInfo) {
try {
HPSC001 hpsc001 = new HPSC001();
......@@ -111,6 +116,7 @@ public class ServiceHPSC001 extends ServiceBase {
/**
* 删除操作.
*/
@OperationLogAnnotation(operModul = "销售管理",operType = "删除")
public EiInfo delete(EiInfo eiInfo) {
HPSC001 hpsc001 = new HPSC001();
EiBlock eiBlock = eiInfo.getBlock(EiConstant.resultBlock);
......@@ -138,6 +144,7 @@ public class ServiceHPSC001 extends ServiceBase {
/**
* 提交 撤回.
*/
@OperationLogAnnotation(operModul = "销售管理",operType = "提交")
public EiInfo check(EiInfo eiInfo) {
HPSC001 hpsc001 = new HPSC001();
EiBlock eiBlock = eiInfo.getBlock(EiConstant.resultBlock);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment