kaiyu
2021-05-21 349c49d9c6407dbfc46a33f9795647a71406dd0d
screen-manage/src/main/java/com/moral/api/service/impl/ManageAccountServiceImpl.java
@@ -142,7 +142,7 @@
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        StringBuilder content = new StringBuilder();
        content.append(manageAccount.getUserName()).append("登陆了后台系统;");
        logUtils.saveLoginForManage(request, content.toString(), manageAccount.getId(), manageAccount.getUserName());
        logUtils.saveLoginForManage(request, content.toString(), manageAccount, manageAccount.getUserName());
        return loginDTO;
    }
@@ -175,7 +175,7 @@
        AccountDTO accountDTO = new AccountDTO();
        //取参
        ManageAccount manageAccount = accountInsertForm.formConvertEntity();
        List<String> roleIdsStr = accountInsertForm.getRoleIds();
        List<Integer> roleIds = accountInsertForm.getRoleIds();
        /*判断账号是否存在*/
        ManageAccount existAccount = new ManageAccount();
@@ -193,37 +193,28 @@
        //插入
        manageAccountMapper.insert(manageAccount);
        //封装account_role
        /*String to Integer*/
        List<ManageRole> roles = null;
        //如果新建账号没有分配角色则不进行操作
        if (!ObjectUtils.isEmpty(roleIdsStr)) {
            List<Integer> roleIds = new ArrayList<>();
            roleIdsStr.forEach(str -> roleIds.add(Integer.parseInt(str)));
        if (!ObjectUtils.isEmpty(roleIds)) {
            Integer accountId = manageAccount.getId();
            roleIdsStr.forEach(
            roleIds.forEach(
                    value -> {
                        ManageAccountRole manageAccountRole = new ManageAccountRole();
                        manageAccountRole.setAccountId(accountId);
                        manageAccountRole.setRoleId(Integer.parseInt(value));
                        manageAccountRole.setRoleId(value);
                        manageAccountRoleMapper.insert(manageAccountRole);
                    }
            );
            roles = manageRoleMapper.selectBatchIds(roleIds);
        }
        //封装返回结果
        accountDTO.setAccount(manageAccount);
        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());
        content.append("添加了用户:").append(manageAccount.getUserName() + ";")
                .append("account:" + manageAccount.getAccount() + ";");
        logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        return accountDTO;
    }
@@ -240,7 +231,7 @@
        AccountQueryDTO accountQueryDTO = new AccountQueryDTO();
        //取参
        Integer pageCount = accountQueryForm.getPage();
        Integer page = accountQueryForm.getPage();
        Integer size = accountQueryForm.getSize();
        String userName = accountQueryForm.getUserName();
        String email = accountQueryForm.getEmail();
@@ -250,10 +241,10 @@
        String order = accountQueryForm.getOrder();
        String orderType = accountQueryForm.getOrderType();
        Date createStartTime = accountQueryForm.getCreateStartTime();
        Date createEndTime = DateUtils.getDateOfDay(accountQueryForm.getCreateEndTime(), 1);
        Date createEndTime = accountQueryForm.getCreateEndTime();
        //组装查询条件
        Page<ManageAccount> page = new Page<>(pageCount, size);
        Page<ManageAccount> queryPage = new Page<>(page, size);
        NullFilterWrapper<ManageAccount> wrapper = new NullFilterWrapper<>();
        wrapper.like("user_name", userName);
@@ -275,20 +266,19 @@
            wrapper.eq("is_delete", Constants.NOT_DELETE);
        //查询结果
        Page resultPage = manageAccountMapper.selectPage(page, wrapper);
        Page resultPage = manageAccountMapper.selectPage(queryPage, wrapper);
        //查询用户对应的角色
        List<ManageAccount> accounts = resultPage.getRecords();
        List<AccountDTO> accountDTOS = new ArrayList<>();
        if (!ObjectUtils.isEmpty(accounts)) {
            for (ManageAccount manageAccount : accounts) {
                AccountDTO accountDTO = new AccountDTO();
                List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
                accountDTO.setRoles(roles);
                accountDTO.setAccount(manageAccount);
                accountDTOS.add(accountDTO);
            }
        for (ManageAccount manageAccount : accounts) {
            AccountDTO accountDTO = new AccountDTO();
            List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
            accountDTO.setRoles(roles);
            accountDTO.setAccount(manageAccount);
            accountDTOS.add(accountDTO);
        }
        //封装返回结果
        accountQueryDTO.setAccountDTOS(accountDTOS);
@@ -315,7 +305,7 @@
        AccountDTO accountDTO = new AccountDTO();
        //取参
        Integer accountId = accountDeleteForm.getAccountId();
        //查询是否存在
        //查询要删除的账号,用于插入日志
        ManageAccount manageAccount = new ManageAccount();
        manageAccount.setIsDelete(Constants.NOT_DELETE);
        manageAccount.setId(accountId);
@@ -339,14 +329,12 @@
        //封装返回结果
        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());
        content.append("删除了用户:").append(existManageAccount.getUserName() + ";")
                .append("账号:" + existManageAccount.getAccount() + ";");
        logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
        return accountDTO;
    }
@@ -366,26 +354,25 @@
        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)) {
        //查找要更新的用户用于插入日志
        QueryWrapper<ManageAccount> oldAccountWrapper = new QueryWrapper<>();
        ManageAccount oldManageAccount = new ManageAccount();
        oldManageAccount.setId(manageAccount.getId());
        oldManageAccount.setIsDelete(Constants.NOT_DELETE);
        oldAccountWrapper.setEntity(oldManageAccount);
        oldManageAccount = manageAccountMapper.selectOne(oldAccountWrapper);
        if (ObjectUtils.isEmpty(oldManageAccount)) {
            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表中没有相应字段更新则不更新
        Map manageAccountMap = JSONObject.parseObject(JSON.toJSONString(manageAccount), Map.class);//转换为Map判断属性是否有更新
        if (manageAccountMap.size() > 1) {//判断如果没有除了id以外的任何属性则不更新
            manageAccountMapper.updateById(manageAccount);
        }
        //获取更新后的对象
        manageAccount = manageAccountMapper.selectById(manageAccount.getId());
        //获取账号更新前的角色
        List<ManageRole> oldRoles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
@@ -395,8 +382,8 @@
         * 如果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);
@@ -407,21 +394,12 @@
                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);
        insertUpdateLog(accountUpdateForm, oldManageAccount, oldRoles);
        return accountDTO;
    }
@@ -436,8 +414,8 @@
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了账号:").append(oldAccount.getUserName() + ";")
                .append("id:" + oldAccount.getId() + ";");
        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);
@@ -453,7 +431,7 @@
                List<Integer> newRoleIds = updateForm.getRoleIds();
                if (newRoleIds != null && 0 == newRoleIds.size()) {//如果新的角色集合为空,则是删除了所有的角色
                    content.append("删除了所有角色;");
                    content.append("角色:删除了所有角色;");
                }
                if ((newRoleIds != null) && (newRoleIds.size() != 0)) {//如果新的角色集合不为空,且不为null,则角色进行了更新
                    StringBuilder oldRolesName = new StringBuilder("空");
@@ -465,7 +443,7 @@
                    }
                    if (!ObjectUtils.isEmpty(oldRoles)) {
                        oldRolesName.deleteCharAt(oldRolesName.length()-1);//删除 "空"
                        oldRolesName.deleteCharAt(oldRolesName.length() - 1);//删除 "空"
                        oldRolesName.append("[");
                        for (ManageRole role : oldRoles) {
                            oldRolesName.append(role.getName() + ",");
@@ -475,7 +453,7 @@
                    }
                    if (!ObjectUtils.isEmpty(newRoles)) {
                        newRolesName.deleteCharAt(newRolesName.length()-1);//删除 "空"
                        newRolesName.deleteCharAt(newRolesName.length() - 1);//删除 "空"
                        newRolesName.append("[");
                        for (ManageRole role : newRoles) {
                            newRolesName.append(role.getName() + ",");
@@ -484,7 +462,7 @@
                        newRolesName.append("]");
                    }
                    //拼接完整content
                    content.append(value+":"+oldRolesName + "->" + newRolesName + ";");
                    content.append(value + ":" + oldRolesName + "->" + newRolesName + ";");
                }
            } else {//其他属性处理
                if (newParameters.get(key) != null) {
@@ -500,7 +478,7 @@
                }
            }
        }
        logUtils.saveOperationForManage(request, content.toString());
        logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
    }