screen-manage/src/main/java/com/moral/api/controller/AccountController.java
@@ -1,11 +1,11 @@ package com.moral.api.controller; import com.moral.api.pojo.dto.account.*; import com.moral.api.pojo.form.account.AccountDeleteForm; import com.moral.api.pojo.form.account.AccountInsertForm; import com.moral.api.pojo.form.account.AccountQueryForm; import com.moral.api.pojo.form.account.AccountUpdateForm; import com.moral.api.pojo.dto.accountRole.AccountRoleDTO; import com.moral.api.pojo.form.account.*; import com.moral.api.pojo.form.accountRole.AccountRoleUpdateForm; import com.moral.api.pojo.vo.account.AccountQueryVO; import com.moral.api.service.ManageAccountRoleService; import com.moral.api.service.ManageAccountService; import com.moral.constant.ResponseCodeEnum; import com.moral.constant.ResultMessage; @@ -23,6 +23,8 @@ public class AccountController { @Autowired ManageAccountService accountService; @Autowired ManageAccountRoleService manageAccountRoleService; @PostMapping("insert") public ResultMessage insert(@RequestBody AccountInsertForm form) { @@ -92,4 +94,18 @@ return new ResultMessage(accountQueryDTO.getCode(), accountQueryDTO.getMsg(), accountQueryVO); } @PostMapping("updateRole") public ResultMessage updateRole(@RequestBody AccountRoleUpdateForm form){ //判断是否缺少参数 if (!form.valid()) return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); //处理更新业务 AccountRoleDTO dto = manageAccountRoleService.updateAccountRole(form); return new ResultMessage(dto.getCode(), dto.getMsg(), null); } } screen-manage/src/main/java/com/moral/api/pojo/dto/accountRole/AccountRoleDTO.java
New file @@ -0,0 +1,19 @@ package com.moral.api.pojo.dto.accountRole; import lombok.Data; /** * @ClassName AccountRoleDTO * @Description TODO * @Author 陈凯裕 * @Date 2021/6/9 8:56 * @Version TODO **/ @Data public class AccountRoleDTO { private Integer code; private String msg; } screen-manage/src/main/java/com/moral/api/pojo/form/account/AccountUpdateForm.java
@@ -33,7 +33,6 @@ private String wechat; private List<Integer> roleIds; public boolean valid() { if (ObjectUtils.isEmpty(accountId)) @@ -43,8 +42,7 @@ ObjectUtils.isEmpty(password) && ObjectUtils.isEmpty(email) && ObjectUtils.isEmpty(mobile) && ObjectUtils.isEmpty(wechat) && null == roleIds ObjectUtils.isEmpty(wechat) ) return false; return true; screen-manage/src/main/java/com/moral/api/pojo/form/accountRole/AccountRoleUpdateForm.java
New file @@ -0,0 +1,24 @@ package com.moral.api.pojo.form.accountRole; import lombok.Data; /** * @ClassName AccountUpdateRoleForm * @Description TODO * @Author 陈凯裕 * @Date 2021/6/9 8:51 * @Version TODO **/ @Data public class AccountRoleUpdateForm { private Integer accountId; private Integer roleId; public boolean valid(){ if(accountId==null) return false; return true; } } screen-manage/src/main/java/com/moral/api/service/ManageAccountRoleService.java
@@ -2,6 +2,8 @@ import com.moral.api.entity.ManageAccountRole; import com.baomidou.mybatisplus.extension.service.IService; import com.moral.api.pojo.dto.accountRole.AccountRoleDTO; import com.moral.api.pojo.form.accountRole.AccountRoleUpdateForm; /** * <p> @@ -13,4 +15,13 @@ */ public interface ManageAccountRoleService extends IService<ManageAccountRole> { /** * @Description: 更新账号角色 * @Param: [form] * @return: com.moral.api.pojo.dto.accountRole.AccountRoleDTO * @Author: 陈凯裕 * @Date: 2021/6/9 */ AccountRoleDTO updateAccountRole(AccountRoleUpdateForm form); } screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountRoleServiceImpl.java
@@ -1,10 +1,19 @@ package com.moral.api.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.moral.api.entity.ManageAccountRole; import com.moral.api.mapper.ManageAccountRoleMapper; import com.moral.api.pojo.dto.accountRole.AccountRoleDTO; import com.moral.api.pojo.form.accountRole.AccountRoleUpdateForm; import com.moral.api.service.ManageAccountRoleService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.constant.Constants; import com.moral.constant.ResponseCodeEnum; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; /** * <p> @@ -17,4 +26,32 @@ @Service public class ManageAccountRoleServiceImpl extends ServiceImpl<ManageAccountRoleMapper, ManageAccountRole> implements ManageAccountRoleService { @Autowired ManageAccountRoleMapper manageAccountRoleMapper; @Override @Transactional public AccountRoleDTO updateAccountRole(AccountRoleUpdateForm form) { //创建返回对象 AccountRoleDTO dto = new AccountRoleDTO(); //取参 Integer accountId = form.getAccountId(); Integer roleId = form.getRoleId(); //先删除原有角色再进行分配 UpdateWrapper updateWrapper = new UpdateWrapper(); updateWrapper.set("is_delete", Constants.DELETE); updateWrapper.eq("account_id", accountId); manageAccountRoleMapper.update(null, updateWrapper); if(!ObjectUtils.isEmpty(roleId)){ ManageAccountRole role = new ManageAccountRole(); role.setAccountId(accountId); role.setRoleId(roleId); manageAccountRoleMapper.insert(role); } //封装返回参数 dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); return dto; } } screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java
@@ -372,7 +372,6 @@ AccountDTO accountDTO = new AccountDTO(); //取参 ManageAccount manageAccount = accountUpdateForm.formConvertEntity(); List<Integer> roleIds = accountUpdateForm.getRoleIds(); //查找要更新的用户用于插入日志 QueryWrapper<ManageAccount> oldAccountWrapper = new QueryWrapper<>(); @@ -388,35 +387,10 @@ } //更新ManageAccount表 Map manageAccountMap = JSONObject.parseObject(JSON.toJSONString(manageAccount), Map.class);//转换为Map判断属性是否有更新 if (manageAccountMap.size() > 1) {//判断如果没有除了id以外的任何属性则不更新 manageAccountMapper.updateById(manageAccount); } 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); } } //操作插入日志 insertUpdateLog(accountUpdateForm, oldManageAccount, oldRoles); insertUpdateLog(accountUpdateForm, oldManageAccount); //销毁token TokenUtils.destoryToken(manageAccount.getId()); //封装返回结果 @@ -432,7 +406,7 @@ * @Author: 陈凯裕 * @Date: 2021/4/8 */ private void insertUpdateLog(AccountUpdateForm updateForm, ManageAccount oldAccount, List<ManageRole> oldRoles) { private void insertUpdateLog(AccountUpdateForm updateForm, ManageAccount oldAccount) { //操作插入日志 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); StringBuilder content = new StringBuilder(); @@ -448,43 +422,6 @@ 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) {