|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @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: 登陆接口 | 
|---|
|  |  |  | * @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(); | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @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 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|