浏览代码

日志功能

chenw 7 年之前
父节点
当前提交
840494727d

+ 91 - 0
bi-server/src/main/java/com/usoftchina/bi/server/aspect/LogAspect.java

@@ -0,0 +1,91 @@
+package com.usoftchina.bi.server.aspect;
+
+import com.usoftchina.bi.core.utils.GetTokenDataUtil;
+import com.usoftchina.bi.server.model.pojo.annotation.Log;
+import com.usoftchina.bi.server.service.common.MessageLogService;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.After;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
+import org.springframework.expression.EvaluationContext;
+import org.springframework.expression.Expression;
+import org.springframework.expression.ExpressionParser;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.expression.spel.support.StandardEvaluationContext;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+
+/**
+ * Log注解处理类,用于记录操作日志(保存/更新/删除)
+ * @Author chenwei
+ * @Date 2019-04-26
+ */
+@Aspect
+@Component
+public class LogAspect {
+
+    private ExpressionParser parser = new SpelExpressionParser();
+
+    private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
+
+    @Autowired
+    private MessageLogService messageLogService;
+
+    @Pointcut("@annotation(com.usoftchina.bi.server.model.pojo.annotation.Log)")
+    public void Log(){
+
+    }
+
+    @After(value = "Log()")
+    public void writeLog(JoinPoint joinPoint){
+        Object[] arguments = joinPoint.getArgs();
+        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
+        Method method = signature.getMethod();
+        Log log = method.getAnnotation(Log.class);
+        String content = log.value(), token = log.token(), module = log.module(), name = log.name();
+        String contentValue = parseSpEL(method, arguments, content, String.class, null);
+        String nameValue = parseSpEL(method, arguments, name, String.class, null);
+        String tokenValue = parseSpEL(method, arguments, token, String.class, null);
+        String methodName = method.getName();
+        String username = GetTokenDataUtil.getTokenData(tokenValue).get("name");
+        if (methodName.startsWith("save")) {
+            // 记录新增日志
+            messageLogService.save(module, nameValue, username, contentValue);
+        }else if (methodName.startsWith("update")) {
+            // 记录更改日志
+            messageLogService.update(module, nameValue, username, contentValue);
+        }else if (methodName.startsWith("delete")) {
+            // 记录删除日志
+            messageLogService.delete(module, nameValue, username, contentValue);
+        }
+    }
+
+    /**
+     * 解析SpringEL表达式的值
+     * @param method
+     * @param arguments
+     * @param spel
+     * @param Clazz
+     * @param defaultResult
+     * @param <T>
+     * @return
+     */
+    private <T> T parseSpEL(Method method, Object[] arguments, String spel, Class<T> Clazz, T defaultResult){
+        String[] params = discoverer.getParameterNames(method);
+        EvaluationContext context = new StandardEvaluationContext();
+        for (int i = 0, len = params.length; i < len; i++) {
+            context.setVariable(params[i], arguments[i]);
+        }
+        try {
+            Expression expression = parser.parseExpression(spel);
+            return expression.getValue(context, Clazz);
+        }catch (Exception e) {
+            return defaultResult;
+        }
+    }
+
+}

+ 39 - 0
bi-server/src/main/java/com/usoftchina/bi/server/controller/common/MessageLogController.java

@@ -0,0 +1,39 @@
+package com.usoftchina.bi.server.controller.common;
+
+import com.github.pagehelper.PageInfo;
+import com.usoftchina.bi.core.base.RepCode;
+import com.usoftchina.bi.core.base.RepEntity;
+import com.usoftchina.bi.core.base.TestPage;
+import com.usoftchina.bi.server.model.po.MessageLog;
+import com.usoftchina.bi.server.model.pojo.annotation.CheckToken;
+import com.usoftchina.bi.server.service.common.MessageLogService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * @Author chenwei
+ * @Date 2019-04-26
+ */
+@RestController
+@RequestMapping("/messageLog")
+@Api(description = "操作日志接口")
+public class MessageLogController {
+
+    @Autowired
+    private MessageLogService messageLogService;
+
+    @GetMapping("/list")
+    @CheckToken
+    @ApiOperation(value = "操作日志列表", notes = "操作日志列表(分页)", response = RepEntity.class)
+    public RepEntity<PageInfo<MessageLog>> list(@RequestHeader String token, TestPage testPage){
+        return new RepEntity<>(RepCode.success, messageLogService.list(testPage));
+    }
+
+}

+ 34 - 0
bi-server/src/main/java/com/usoftchina/bi/server/dao/common/MessageLogMapper.java

@@ -0,0 +1,34 @@
+package com.usoftchina.bi.server.dao.common;
+
+import com.usoftchina.bi.core.base.TestPage;
+import com.usoftchina.bi.server.model.po.MessageLog;
+import org.apache.ibatis.annotations.*;
+import org.apache.ibatis.type.JdbcType;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Mapper
+@Repository
+public interface MessageLogMapper {
+
+    @Insert("INSERT INTO BI_MESSAGELOG(BML_MODULE,BML_ORDER,BML_ID,BML_DATE,BML_MAN,BML_CONTENT,BML_RESULT,BML_SEARCH) "
+            + "VALUES(#{module,jdbcType=VARCHAR}, #{order,jdbcType=VARCHAR}, #{id,jdbcType=INTEGER}, SYSDATE, #{userName,jdbcType=VARCHAR},"
+            + "#{content,jdbcType=VARCHAR}, #{result,jdbcType=VARCHAR}, #{search,jdbcType=VARCHAR})")
+    @SelectKey(before=true,keyProperty="id",resultType=Integer.class,statement="SELECT BI_MESSAGELOG_SEQ.NEXTVAL FROM DUAL",keyColumn = "id")
+    void insert(MessageLog messageLog);
+
+
+    @Select("SELECT * FROM BI_MESSAGELOG ORDER BY BML_ID DESC")
+    @Results(id = "BaseResultMap", value = {
+            @Result(id = true, column = "BML_ID", property = "id", jdbcType = JdbcType.INTEGER),
+            @Result(column = "BML_DATE", property = "date", jdbcType = JdbcType.TIMESTAMP),
+            @Result(column = "BML_MAN", property = "userName", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "BML_CONTENT", property = "content", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "BML_RESULT", property = "result", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "BML_SEARCH", property = "search", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "BML_MODULE", property = "module", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "BML_ORDER", property = "order", jdbcType = JdbcType.VARCHAR),
+    })
+    List<MessageLog> list(TestPage testPage);
+}

+ 132 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/po/MessageLog.java

@@ -0,0 +1,132 @@
+package com.usoftchina.bi.server.model.po;
+
+import java.util.Date;
+
+/**
+ * @Author chenwei
+ * @Date 2019-04-26
+ */
+public class MessageLog {
+
+    /**
+     * ID
+     */
+    private Integer id;
+    /**
+     * 操作时间
+     */
+    private Date date;
+    /**
+     * 操作人
+     */
+    private String userName;
+    /**
+     * 操作内容
+     */
+    private String content;
+    /**
+     * 操作结果
+     */
+    private String result;
+    /**
+     * 搜索字段
+     */
+    private String search;
+    /**
+     * 模块名
+     */
+    private String module;
+    /**
+     * 单据名称
+     */
+    private String order;
+
+    @Override
+    public String toString() {
+        return "MessageLog{" +
+                "id=" + id +
+                ", date=" + date +
+                ", userName='" + userName + '\'' +
+                ", content='" + content + '\'' +
+                ", result='" + result + '\'' +
+                ", search='" + search + '\'' +
+                '}';
+    }
+
+    public MessageLog() {
+    }
+
+    public MessageLog(String userName, String content, String result, String search, String module, String order) {
+        this.userName = userName;
+        this.content = content;
+        this.result = result;
+        this.search = search;
+        this.module = module;
+        this.order = order;
+    }
+
+    public Integer getId() {
+
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Date getDate() {
+        return date;
+    }
+
+    public void setDate(Date date) {
+        this.date = date;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public String getResult() {
+        return result;
+    }
+
+    public void setResult(String result) {
+        this.result = result;
+    }
+
+    public String getSearch() {
+        return search;
+    }
+
+    public void setSearch(String search) {
+        this.search = search;
+    }
+
+    public String getModule() {
+        return module;
+    }
+
+    public void setModule(String module) {
+        this.module = module;
+    }
+
+    public String getOrder() {
+        return order;
+    }
+
+    public void setOrder(String order) {
+        this.order = order;
+    }
+}

+ 37 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/pojo/annotation/Log.java

@@ -0,0 +1,37 @@
+package com.usoftchina.bi.server.model.pojo.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * 日志注解
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Log {
+
+    /**
+     * 模块名称:  看板/图表 等等
+     * @return
+     */
+    String module() default "";
+
+    /**
+     * 单据名称   具体的单据名称,如: 图表中的 '销售情况分析折线图'
+     * @return
+     */
+    String name() default "";
+
+    /**
+     * 身份token
+     * @return
+     */
+    String token() default "";
+
+    /**
+     * 前端传入的自定义centent
+     * @return
+     */
+    String value() default "";
+
+}

+ 56 - 0
bi-server/src/main/java/com/usoftchina/bi/server/service/common/MessageLogService.java

@@ -0,0 +1,56 @@
+package com.usoftchina.bi.server.service.common;
+
+import com.github.pagehelper.PageInfo;
+import com.usoftchina.bi.core.base.TestPage;
+import com.usoftchina.bi.server.dao.common.MessageLogMapper;
+import com.usoftchina.bi.server.model.po.MessageLog;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+
+import java.util.List;
+
+/**
+ * @Author chenwei
+ * @Date 2019-04-26
+ */
+@Service
+public class MessageLogService {
+
+    @Autowired
+    private MessageLogMapper messageLogMapper;
+
+    public PageInfo<MessageLog> list(TestPage testPage) {
+        List<MessageLog> messageLogList = messageLogMapper.list(testPage.enablePaging());
+        return new PageInfo<MessageLog>(messageLogList);
+    }
+
+    public void save(String module, String name, String username, String content){
+        if (StringUtils.isEmpty(content)) {
+            content = "保存操作";
+        }
+        MessageLog messageLog = new MessageLog(username, content, "保存成功", null, module, name);
+        customizeLog(messageLog);
+    }
+
+    public void update(String module, String name, String username, String content){
+        if (StringUtils.isEmpty(content)) {
+            content = "更新操作";
+        }
+        MessageLog messageLog = new MessageLog(username, content, "更新成功", null, module, name);
+        customizeLog(messageLog);
+    }
+
+    public void delete(String module, String name, String username, String content){
+        if (StringUtils.isEmpty(content)) {
+            content = "删除操作";
+        }
+        MessageLog messageLog = new MessageLog(username, content, "删除成功", null, module, name);
+        customizeLog(messageLog);
+    }
+
+    public void customizeLog(MessageLog messageLog){
+        messageLogMapper.insert(messageLog);
+    }
+
+}