|  |  | 
 |  |  | 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.CompareFieldUtils; | 
 |  |  | import com.moral.api.util.LogUtils; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.constant.ResponseCodeEnum; | 
 |  |  | import com.moral.pojo.CompareFieldResult; | 
 |  |  | import com.moral.util.*; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.boot.context.properties.ConfigurationProperties; | 
 |  |  | 
 |  |  |  * @since 2021-03-09 | 
 |  |  |  */ | 
 |  |  | @Service | 
 |  |  | @ConfigurationProperties(prefix = "log-aspect") | 
 |  |  | public class ManageAccountServiceImpl extends ServiceImpl<ManageAccountMapper, ManageAccount> implements ManageAccountService { | 
 |  |  |  | 
 |  |  |  | 
 |  |  | 
 |  |  |     ManageMenuService manageMenuService; | 
 |  |  |     @Autowired | 
 |  |  |     ManageAccountRoleMapper manageAccountRoleMapper; | 
 |  |  |     @Autowired | 
 |  |  |     LogUtils logUtils; | 
 |  |  |  | 
 |  |  |     Map<String, String> manageAccountFormMap; | 
 |  |  |  | 
 |  |  |     public void setManageAccountFormMap(Map<String, String> manageAccountFormMap) { | 
 |  |  |         this.manageAccountFormMap = manageAccountFormMap; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @Description: 登陆接口 | 
 |  |  | 
 |  |  |         //查询是否存在 | 
 |  |  |         QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.eq("account", account); | 
 |  |  |         wrapper.eq("is_delete",Constants.NOT_DELETE); | 
 |  |  |         wrapper.eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         ManageAccount manageAccount = manageAccountMapper.selectOne(wrapper); | 
 |  |  |         if (ObjectUtils.isEmpty(manageAccount)) { | 
 |  |  |             loginDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode()); | 
 |  |  | 
 |  |  |  | 
 |  |  |         //获取token 并且存入缓存 | 
 |  |  |         String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), accountInfoDTO); | 
 |  |  |  | 
 |  |  |         //封装返回结果 | 
 |  |  |         loginDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         loginDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  | 
 |  |  |         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
 |  |  |         StringBuilder content = new StringBuilder(); | 
 |  |  |         content.append(manageAccount.getUserName()).append("登陆了后台系统;"); | 
 |  |  |         logUtils.saveLoginForManage(request, content.toString(), manageAccount, manageAccount.getUserName()); | 
 |  |  |         LogUtils.saveLoginForManage(request, content.toString(), manageAccount, manageAccount.getUserName()); | 
 |  |  |  | 
 |  |  |         return loginDTO; | 
 |  |  |     } | 
 |  |  | 
 |  |  |                     } | 
 |  |  |             ); | 
 |  |  |         } | 
 |  |  |         //操作插入日志 | 
 |  |  |         insertLog(manageAccount); | 
 |  |  |         //封装返回结果 | 
 |  |  |         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("account:" + manageAccount.getAccount() + ";"); | 
 |  |  |         logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE); | 
 |  |  |  | 
 |  |  |         return accountDTO; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |             accountDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); | 
 |  |  |             return accountDTO; | 
 |  |  |         } | 
 |  |  |         //禁止删除自己的账号 | 
 |  |  |         AccountInfoDTO accountInfo = (AccountInfoDTO) TokenUtils.getUserInfo(); | 
 |  |  |         if(accountInfo.getAccount().getId().equals(accountId)){ | 
 |  |  |             accountDTO.setCode(ResponseCodeEnum.CANNOT_DELETE_ONESELF.getCode()); | 
 |  |  |             accountDTO.setMsg(ResponseCodeEnum.CANNOT_DELETE_ONESELF.getMsg()); | 
 |  |  |             return accountDTO; | 
 |  |  |         } | 
 |  |  |         //逻辑删除账号 | 
 |  |  |         UpdateWrapper<ManageAccount> deleteAccountWrapper = new UpdateWrapper<>(); | 
 |  |  |         deleteAccountWrapper.eq("id", accountId); | 
 |  |  | 
 |  |  |         accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |         //操作插入日志 | 
 |  |  |         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
 |  |  |         StringBuilder content = new StringBuilder(); | 
 |  |  |         content.append("删除了用户:").append(existManageAccount.getUserName() + ";") | 
 |  |  |                 .append("账号:" + existManageAccount.getAccount() + ";"); | 
 |  |  |         logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE); | 
 |  |  |  | 
 |  |  |         deleteLog(existManageAccount); | 
 |  |  |         return accountDTO; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |         //更新ManageAccount表 | 
 |  |  |         manageAccountMapper.updateById(manageAccount); | 
 |  |  |  | 
 |  |  |         //操作插入日志 | 
 |  |  |         insertUpdateLog(accountUpdateForm, oldManageAccount); | 
 |  |  |         updateLog(oldManageAccount,manageAccount); | 
 |  |  |         //销毁token | 
 |  |  |         TokenUtils.destoryToken(manageAccount.getId()); | 
 |  |  |         //封装返回结果 | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @Description: 将更新操作插入日志 | 
 |  |  |      * @Param: [form, newAccount, oldAccount] | 
 |  |  |      * @return: void | 
 |  |  |      * @Author: 陈凯裕 | 
 |  |  |      * @Date: 2021/4/8 | 
 |  |  |      */ | 
 |  |  |     private void insertUpdateLog(AccountUpdateForm updateForm, ManageAccount oldAccount) { | 
 |  |  |         //操作插入日志 | 
 |  |  |         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
 |  |  |     * @Description: 更新操作插入日志 | 
 |  |  |             * @Param: [oldAccount, newAccount] | 
 |  |  |             * @return: void | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/8/25 | 
 |  |  |             */ | 
 |  |  |     private void updateLog(ManageAccount oldAccount,ManageAccount newAccount){ | 
 |  |  |         List<CompareFieldResult> results = CompareFieldUtils.compare(ManageAccount.class, oldAccount, newAccount); | 
 |  |  |         StringBuilder content = new StringBuilder("修改了后台用户"+";"); | 
 |  |  |         content.append("账号:"+oldAccount.getAccount()+";"); | 
 |  |  |         //密码特殊处理 | 
 |  |  |         if(!ObjectUtils.isEmpty(newAccount.getPassword())) | 
 |  |  |             content.append("修改了密码"); | 
 |  |  |         String resultContent = CompareFieldUtils.resultsConvertContent(results,content.toString()); | 
 |  |  |         LogUtils.saveOperationForManage(resultContent, Constants.UPDATE_OPERATE_TYPE); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @Description: 插入操作插入日志 | 
 |  |  |             * @Param: [account] | 
 |  |  |             * @return: void | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/8/25 | 
 |  |  |             */ | 
 |  |  |     private void insertLog(ManageAccount manageAccount){ | 
 |  |  |         StringBuilder content = new StringBuilder(); | 
 |  |  |         content.append("修改了用户:").append(oldAccount.getUserName() + ";") | 
 |  |  |                 .append("账号:" + oldAccount.getAccount() + ";"); | 
 |  |  |         //对象转为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 (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(), Constants.UPDATE_OPERATE_TYPE); | 
 |  |  |         content.append("添加了后台用户:").append(manageAccount.getUserName() + ";") | 
 |  |  |                 .append("account:" + manageAccount.getAccount() + ";"); | 
 |  |  |         LogUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @Description: 删除操作插入日志 | 
 |  |  |             * @Param: [manageAccount] | 
 |  |  |             * @return: void | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/8/25 | 
 |  |  |             */ | 
 |  |  |     private void deleteLog(ManageAccount manageAccount){ | 
 |  |  |         StringBuilder content = new StringBuilder(); | 
 |  |  |         content.append("删除了用户:").append(manageAccount.getUserName() + ";") | 
 |  |  |                 .append("账号:" + manageAccount.getAccount() + ";"); | 
 |  |  |         LogUtils.saveOperationForManage( content.toString(), Constants.DELETE_OPERATE_TYPE); | 
 |  |  |     } | 
 |  |  | } | 
 |  |  |  |