From 54d7eb96e071c62fa96f95b56ff8be66f7c3718e Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Fri, 09 Apr 2021 14:32:25 +0800
Subject: [PATCH] screen-manage     修改日志插入工具类     规范日志插入格式

---
 screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java |  265 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 195 insertions(+), 70 deletions(-)

diff --git a/screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java b/screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java
index 9d89da3..5b50ee5 100644
--- a/screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java
+++ b/screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java
@@ -1,5 +1,7 @@
 package com.moral.api.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -24,14 +26,19 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.moral.api.config.mybatis.wrapper.NullFilterWrapper;
 import com.moral.api.service.ManageMenuService;
+import com.moral.api.util.LogUtils;
 import com.moral.constant.Constants;
 import com.moral.constant.ResponseCodeEnum;
 import com.moral.util.*;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.ObjectUtils;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.*;
 
 /**
@@ -43,6 +50,7 @@
  * @since 2021-03-09
  */
 @Service
+@ConfigurationProperties(prefix = "log-aspect")
 public class ManageAccountServiceImpl extends ServiceImpl<ManageAccountMapper, ManageAccount> implements ManageAccountService {
 
 
@@ -54,15 +62,22 @@
     ManageMenuService manageMenuService;
     @Autowired
     ManageAccountRoleMapper manageAccountRoleMapper;
+    @Autowired
+    LogUtils logUtils;
 
+    Map<String, String> manageAccountFormMap;
+
+    public void setManageAccountFormMap(Map<String, String> manageAccountFormMap) {
+        this.manageAccountFormMap = manageAccountFormMap;
+    }
 
     /**
-    * @Description: ������������
-            * @Param: [loginForm]
-            * @return: com.moral.api.pojo.dto.login.LoginDTO
-            * @Author: ���������
-            * @Date: 2021/3/30
-            */
+     * @Description: ������������
+     * @Param: [loginForm]
+     * @return: com.moral.api.pojo.dto.login.LoginDTO
+     * @Author: ���������
+     * @Date: 2021/3/30
+     */
     @Override
     public LoginDTO login(LoginForm loginForm) {
         LoginDTO loginDTO = new LoginDTO();
@@ -122,6 +137,12 @@
         loginDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
         loginDTO.setAccountInfoDTO(accountInfoDTO);
         loginDTO.setToken(token);
+
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append(manageAccount.getUserName()).append("���������������������;");
+        logUtils.saveLoginForManage(request, content.toString(), manageAccount.getId(), manageAccount.getUserName());
 
         return loginDTO;
     }
@@ -196,6 +217,14 @@
         accountDTO.setRoles(roles);
         accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
         accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
+
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append("������������������").append(manageAccount.getUserName() + ";")
+                .append("id���" + manageAccount.getId() + ";");
+        logUtils.saveOperationForManage(request, content.toString());
+
         return accountDTO;
     }
 
@@ -274,70 +303,6 @@
     }
 
     /**
-     * @Description: ������������������
-     * @Param: [accountUpdateRequest]
-     * @return: com.moral.api.pojo.dto.account.AccountUpdateDTO
-     * @Author: ���������
-     * @Date: 2021/3/16
-     */
-    @Override
-    @Transactional
-    public AccountDTO updateAccount(AccountUpdateForm accountUpdateForm) {
-        AccountDTO accountDTO = new AccountDTO();
-        //������
-        ManageAccount manageAccount = accountUpdateForm.formConvertEntity();
-        List<Integer> roleIds = accountUpdateForm.getRoleIds();
-
-        //������������������������������������
-        QueryWrapper<ManageAccount> exitWrapper = new QueryWrapper<>();
-        ManageAccount exitManageAccount = new ManageAccount();
-        exitManageAccount.setId(manageAccount.getId());
-        exitManageAccount.setIsDelete(Constants.NOT_DELETE);
-        exitWrapper.setEntity(exitManageAccount);
-        List<ManageAccount> manageAccounts = manageAccountMapper.selectList(exitWrapper);
-        if (ObjectUtils.isEmpty(manageAccounts)) {
-            accountDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode());
-            accountDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
-            return accountDTO;
-        }
-
-        //������ManageAccount���
-        manageAccountMapper.updateById(manageAccount);
-
-        //���������������������������������ManageAccountRole���
-        /*
-         * ������roleIds���null������������������������������������������
-         * ������roleIds���������������������������������������������������
-         * ������roleIds���������������������������������������������
-         * */
-        List<ManageRole> roles = null;
-        if (roleIds != null) {
-            UpdateWrapper<ManageAccountRole> deleteWrapper = new UpdateWrapper<>();
-            deleteWrapper.eq("account_id", manageAccount.getId()).set("is_delete", Constants.DELETE);
-            manageAccountRoleMapper.update(null, deleteWrapper);
-            /*������������������*/
-            for (Integer roleId : roleIds) {
-                ManageAccountRole manageAccountRole = new ManageAccountRole();
-                manageAccountRole.setAccountId(manageAccount.getId());
-                manageAccountRole.setRoleId(roleId);
-                manageAccountRoleMapper.insert(manageAccountRole);
-            }
-            //������������������������
-            if (roleIds.size() != 0)
-                roles = manageRoleMapper.selectBatchIds(roleIds);
-        } else {
-            roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
-        }
-        accountDTO.setRoles(roles);
-        //������������������
-        accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
-        accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
-        accountDTO.setAccount(manageAccountMapper.selectById(manageAccount.getId()));
-        return accountDTO;
-    }
-
-
-    /**
      * @Description: ������������������
      * @Param: [accountDeleteRequest]
      * @return: com.moral.api.pojo.dto.account.AccountDeleteDTO
@@ -375,8 +340,168 @@
         accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
         accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
         accountDTO.setAccount(existManageAccount);
+
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append("������������������").append(existManageAccount.getUserName() + ";")
+                .append("id���" + existManageAccount.getId() + ";");
+        logUtils.saveOperationForManage(request, content.toString());
+
         return accountDTO;
     }
 
+    /**
+     * @Description: ������������������
+     * @Param: [accountUpdateRequest]
+     * @return: com.moral.api.pojo.dto.account.AccountUpdateDTO
+     * @Author: ���������
+     * @Date: 2021/3/16
+     */
+    @Override
+    @Transactional
+    public AccountDTO updateAccount(AccountUpdateForm accountUpdateForm) {
+        AccountDTO accountDTO = new AccountDTO();
+        //������
+        ManageAccount manageAccount = accountUpdateForm.formConvertEntity();
+        List<Integer> roleIds = accountUpdateForm.getRoleIds();
+
+        //������������������������������������
+        QueryWrapper<ManageAccount> exitWrapper = new QueryWrapper<>();
+        ManageAccount exitManageAccount = new ManageAccount();
+        exitManageAccount.setId(manageAccount.getId());
+        exitManageAccount.setIsDelete(Constants.NOT_DELETE);
+        exitWrapper.setEntity(exitManageAccount);
+        exitManageAccount = manageAccountMapper.selectOne(exitWrapper);
+        if (ObjectUtils.isEmpty(exitManageAccount)) {
+            accountDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode());
+            accountDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
+            return accountDTO;
+        }
+
+        //������ManageAccount���
+        Map manageAccountMap = JSONObject.parseObject(JSON.toJSONString(manageAccount),Map.class);//���������Map���������������������������
+        if (manageAccountMap.size()>2) {//������������account������������������������������������������
+            manageAccountMapper.updateById(manageAccount);
+        }
+        //������������������������
+        manageAccount = manageAccountMapper.selectById(manageAccount.getId());
+        //������������������������������
+        List<ManageRole> oldRoles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
+
+        //���������������������������������ManageAccountRole���
+        /*
+         * ������roleIds���null������������������������������������������
+         * ������roleIds���������������������������������������������������
+         * ������roleIds���������������������������������������������
+         * */
+        List<ManageRole> roles = null;
+        if (roleIds != null) {
+            UpdateWrapper<ManageAccountRole> deleteWrapper = new UpdateWrapper<>();
+            deleteWrapper.eq("account_id", manageAccount.getId()).set("is_delete", Constants.DELETE);
+            manageAccountRoleMapper.update(null, deleteWrapper);
+            /*������������������*/
+            for (Integer roleId : roleIds) {
+                ManageAccountRole manageAccountRole = new ManageAccountRole();
+                manageAccountRole.setAccountId(manageAccount.getId());
+                manageAccountRole.setRoleId(roleId);
+                manageAccountRoleMapper.insert(manageAccountRole);
+            }
+            //������������������������
+            if (roleIds.size() != 0)
+                roles = manageRoleMapper.selectBatchIds(roleIds);
+        } else {
+            roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
+        }
+        accountDTO.setRoles(roles);
+        //������������������
+        accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
+        accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
+        accountDTO.setAccount(manageAccount);
+
+        //������������������
+        insertUpdateLog(accountUpdateForm, exitManageAccount, oldRoles);
+
+        return accountDTO;
+    }
+
+    /**
+     * @Description: ���������������������������
+     * @Param: [form, newAccount, oldAccount]
+     * @return: void
+     * @Author: ���������
+     * @Date: 2021/4/8
+     */
+    private void insertUpdateLog(AccountUpdateForm updateForm, ManageAccount oldAccount, List<ManageRole> oldRoles) {
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append("������������������").append(oldAccount.getUserName() + ";")
+                .append("id���" + oldAccount.getId() + ";");
+        //������������Map,���������������������������������
+        Map<String, Object> newParameters = JSONObject.parseObject(JSON.toJSONString(updateForm), Map.class);
+        Map<String, Object> oldParameters = JSONObject.parseObject(JSON.toJSONString(oldAccount), Map.class);
+        //������������������������Map���������������������������
+        Set<String> keys = manageAccountFormMap.keySet();
+        for (String key : keys) {
+            String value = manageAccountFormMap.get(key);//���������������������
+            if ("password".equals(key)) {//������������������������������������������
+                if (!ObjectUtils.isEmpty(updateForm.getPassword())) {//���������������������������������
+                    content.append("���������������;");
+                }
+            } else if ("roleIds".equals(key)) {//������������������������Id���������������������
+                List<Integer> newRoleIds = updateForm.getRoleIds();
+
+                if (newRoleIds != null && 0 == newRoleIds.size()) {//���������������������������������������������������������������
+                    content.append("���������������������;");
+                }
+                if ((newRoleIds != null) && (newRoleIds.size() != 0)) {//���������������������������������������������null���������������������������
+                    StringBuilder oldRolesName = new StringBuilder("���");
+                    StringBuilder newRolesName = new StringBuilder("���");
+
+                    List<ManageRole> newRoles = null;
+                    if (!ObjectUtils.isEmpty(updateForm.getRoleIds())) {
+                        newRoles = manageRoleMapper.selectBatchIds(updateForm.getRoleIds());
+                    }
+
+                    if (!ObjectUtils.isEmpty(oldRoles)) {
+                        oldRolesName.deleteCharAt(oldRolesName.length()-1);//������ "���"
+                        oldRolesName.append("[");
+                        for (ManageRole role : oldRoles) {
+                            oldRolesName.append(role.getName() + ",");
+                        }
+                        oldRolesName.deleteCharAt(oldRolesName.length() - 1);//������������������������
+                        oldRolesName.append("]");
+                    }
+
+                    if (!ObjectUtils.isEmpty(newRoles)) {
+                        newRolesName.deleteCharAt(newRolesName.length()-1);//������ "���"
+                        newRolesName.append("[");
+                        for (ManageRole role : newRoles) {
+                            newRolesName.append(role.getName() + ",");
+                        }
+                        newRolesName.deleteCharAt(newRolesName.length() - 1);//������������������������
+                        newRolesName.append("]");
+                    }
+                    //������������content
+                    content.append(value+":"+oldRolesName + "->" + newRolesName + ";");
+                }
+            } else {//������������������
+                if (newParameters.get(key) != null) {
+                    String newValue = "���";
+                    String oldValue = "���";
+                    if (newParameters.get(key) != null && !newParameters.get(key).equals(" ")) {
+                        newValue = String.valueOf(newParameters.get(key));
+                    }
+                    if (oldParameters.get(key) != null && !oldParameters.get(key).equals(" ")) {
+                        oldValue = String.valueOf(oldParameters.get(key));
+                    }
+                    content.append(value + ":" + oldValue + "->" + newValue + ";");
+                }
+            }
+        }
+        logUtils.saveOperationForManage(request, content.toString());
+    }
+
 
 }

--
Gitblit v1.8.0