|  |  | 
 |  |  | import com.moral.api.entity.ManageRole; | 
 |  |  | import com.moral.api.mapper.ManageAccountMapper; | 
 |  |  | import com.moral.api.mapper.ManageAccountRoleMapper; | 
 |  |  | import com.moral.api.mapper.ManageMenuMapper; | 
 |  |  | import com.moral.api.mapper.ManageRoleMapper; | 
 |  |  | import com.moral.api.pojo.dto.account.*; | 
 |  |  | import com.moral.api.pojo.dto.login.AccountInfoDTO; | 
 |  |  | import com.moral.api.pojo.redisBean.AccountInfoDTO; | 
 |  |  | import com.moral.api.pojo.dto.login.LoginDTO; | 
 |  |  | import com.moral.api.pojo.form.account.AccountDeleteForm; | 
 |  |  | import com.moral.api.pojo.form.account.AccountInsertForm; | 
 |  |  | 
 |  |  | 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; | 
 |  |  | 
 |  |  |  | 
 |  |  | import javax.servlet.http.HttpServletRequest; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.function.Predicate; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * <p> | 
 |  |  | 
 |  |  |  * @since 2021-03-09 | 
 |  |  |  */ | 
 |  |  | @Service | 
 |  |  | @ConfigurationProperties(prefix = "log-aspect") | 
 |  |  | public class ManageAccountServiceImpl extends ServiceImpl<ManageAccountMapper, ManageAccount> implements ManageAccountService { | 
 |  |  |  | 
 |  |  |  | 
 |  |  | 
 |  |  |     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); | 
 |  |  |         List<ManageAccount> manageAccounts = manageAccountMapper.selectList(wrapper); | 
 |  |  |         if (ObjectUtils.isEmpty(manageAccounts)) { | 
 |  |  |             loginDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode()); | 
 |  |  |             loginDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); | 
 |  |  |             return loginDTO; | 
 |  |  |         } | 
 |  |  |         //查询是否逻辑删除 | 
 |  |  |         ManageAccount manageAccount = null; | 
 |  |  |         for (ManageAccount value : manageAccounts) { | 
 |  |  |             if (Constants.NOT_DELETE.equals(value.getIsDelete())) | 
 |  |  |                 manageAccount = value; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         wrapper.eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         ManageAccount manageAccount = manageAccountMapper.selectOne(wrapper); | 
 |  |  |         if (ObjectUtils.isEmpty(manageAccount)) { | 
 |  |  |             loginDTO.setCode(ResponseCodeEnum.ACCOUNT_IS_DELETE.getCode()); | 
 |  |  |             loginDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode()); | 
 |  |  |             loginDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); | 
 |  |  |             return loginDTO; | 
 |  |  |         } | 
 |  |  | 
 |  |  |         List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId()); | 
 |  |  |  | 
 |  |  |         //查询菜单 | 
 |  |  |         List<ManageMenu> menus = null; | 
 |  |  |         List<ManageMenu> menus = new ArrayList<>(); | 
 |  |  |         if (!ObjectUtils.isEmpty(roles)) { | 
 |  |  |             menus = manageMenuService.getParentChildrenMenusByRoles(roles); | 
 |  |  |         } | 
 |  |  | 
 |  |  |  | 
 |  |  |         //获取token 并且存入缓存 | 
 |  |  |         String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), accountInfoDTO); | 
 |  |  |  | 
 |  |  |         //封装返回结果 | 
 |  |  |         loginDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         loginDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |         loginDTO.setAccountInfoDTO(accountInfoDTO); | 
 |  |  |         loginDTO.setToken(token); | 
 |  |  |  | 
 |  |  |         //登陆插入日志 | 
 |  |  | 
 |  |  |                     } | 
 |  |  |             ); | 
 |  |  |         } | 
 |  |  |         //操作插入日志 | 
 |  |  |         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; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |         //查询结果 | 
 |  |  |         Page resultPage = manageAccountMapper.selectPage(queryPage, wrapper); | 
 |  |  |  | 
 |  |  |         //查询用户对应的角色 | 
 |  |  |         List<ManageAccount> accounts = resultPage.getRecords(); | 
 |  |  |         //过滤系统最高权限账号admin,除admin角色外无法查看 | 
 |  |  |         AccountInfoDTO accountInfoDTO = (AccountInfoDTO) TokenUtils.getUserInfo(); | 
 |  |  |         List<ManageRole> ownRoles = accountInfoDTO.getRoles(); | 
 |  |  |         boolean ownAdminRole = false; | 
 |  |  |         for (ManageRole ownRole : ownRoles) { | 
 |  |  |             String roleName = ownRole.getName(); | 
 |  |  |             if ("admin".equals(roleName)) | 
 |  |  |                 ownAdminRole = true; | 
 |  |  |         } | 
 |  |  |         if (!ownAdminRole) { | 
 |  |  |             accounts.removeIf(new Predicate<ManageAccount>() { | 
 |  |  |                 @Override | 
 |  |  |                 public boolean test(ManageAccount manageAccount) { | 
 |  |  |                     if (manageAccount.getAccount().equals("admin")) | 
 |  |  |                         return true; | 
 |  |  |                     return false; | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  |         //查询用户对应的角色 | 
 |  |  |         List<AccountDTO> accountDTOS = new ArrayList<>(); | 
 |  |  |         for (ManageAccount manageAccount : accounts) { | 
 |  |  |             AccountDTO accountDTO = new AccountDTO(); | 
 |  |  | 
 |  |  |         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; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |         AccountDTO accountDTO = new AccountDTO(); | 
 |  |  |         //取参 | 
 |  |  |         ManageAccount manageAccount = accountUpdateForm.formConvertEntity(); | 
 |  |  |         List<Integer> roleIds = accountUpdateForm.getRoleIds(); | 
 |  |  |  | 
 |  |  |         //查找要更新的用户用于插入日志 | 
 |  |  |         QueryWrapper<ManageAccount> oldAccountWrapper = new QueryWrapper<>(); | 
 |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //更新ManageAccount表 | 
 |  |  |         Map manageAccountMap = JSONObject.parseObject(JSON.toJSONString(manageAccount), Map.class);//转换为Map判断属性是否有更新 | 
 |  |  |         if (manageAccountMap.size() > 1) {//判断如果没有除了id以外的任何属性则不更新 | 
 |  |  |             manageAccountMapper.updateById(manageAccount); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //获取账号更新前的角色 | 
 |  |  |         List<ManageRole> oldRoles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId()); | 
 |  |  |  | 
 |  |  |         //如果角色有变动,则更新ManageAccountRole表 | 
 |  |  |         /* | 
 |  |  |          * 如果roleIds为null,则是该账号角色没有发生改变 | 
 |  |  |          * 如果roleIds为空,则是该账号所有的角色都被移除 | 
 |  |  |          * 如果roleIds有元素,则是该账号的角色有更新 | 
 |  |  |          * */ | 
 |  |  |         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); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         manageAccountMapper.updateById(manageAccount); | 
 |  |  |         //操作插入日志 | 
 |  |  |         updateLog(oldManageAccount,manageAccount); | 
 |  |  |         //销毁token | 
 |  |  |         TokenUtils.destoryToken(manageAccount.getId()); | 
 |  |  |         //封装返回结果 | 
 |  |  |         accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |         //操作插入日志 | 
 |  |  |         insertUpdateLog(accountUpdateForm, oldManageAccount, 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(); | 
 |  |  |     * @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 ("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(), 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); | 
 |  |  |     } | 
 |  |  | } | 
 |  |  |  |