1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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.ManageAccount;
import com.moral.api.entity.ManageAccountRole;
import com.moral.api.entity.ManageRole;
import com.moral.api.mapper.ManageAccountMapper;
import com.moral.api.mapper.ManageAccountRoleMapper;
import com.moral.api.mapper.ManageRoleMapper;
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.api.service.ManageAccountService;
import com.moral.api.service.ManageRoleService;
import com.moral.api.util.LogUtils;
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;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
 
/**
 * <p>
 * 后台账户角色关系表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class ManageAccountRoleServiceImpl extends ServiceImpl<ManageAccountRoleMapper, ManageAccountRole> implements ManageAccountRoleService {
 
    @Autowired
    ManageAccountRoleMapper manageAccountRoleMapper;
    @Autowired
    ManageRoleMapper manageRoleMapper;
    @Autowired
    ManageRoleService manageRoleService;
    @Autowired
    ManageAccountMapper manageAccountMapper;
 
 
    @Override
    @Transactional
    public AccountRoleDTO updateAccountRole(AccountRoleUpdateForm form) {
        //创建返回对象
        AccountRoleDTO dto = new AccountRoleDTO();
        //取参
        Integer accountId = form.getAccountId();
        Integer roleId = form.getRoleId();
        //查询要更新的账号用于日志插入
        ManageAccount account = manageAccountMapper.selectById(accountId);
        //查询要更新的角色用于日志插入
        ManageRole updateRole = null;
        if (!ObjectUtils.isEmpty(roleId)) {
            QueryWrapper<ManageRole> queryWrapper = new QueryWrapper();
            queryWrapper.eq("is_delete", Constants.NOT_DELETE);
            queryWrapper.eq("id", roleId);
            updateRole = manageRoleMapper.selectOne(queryWrapper);
        }
        //查询原有角色用于插入日志
        ManageRole oldRole = manageRoleService.getRoleByAccountId(accountId);
        //先删除原有角色再进行分配
        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);
        }
        //插入日志
        insertUpdateLog(oldRole,updateRole,account);
        //封装返回参数
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
 
    private void insertUpdateLog(ManageRole oldRole, ManageRole updateRole, ManageAccount account) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了用户:").append(account.getUserName() + ";")
                .append("账号:" + account.getAccount() + ";")
                .append("角色:");
 
        if(ObjectUtils.isEmpty(oldRole))
            content.append("空->");
        else
            content.append(oldRole.getName()+"->");
 
        if(ObjectUtils.isEmpty(updateRole))
            content.append("空;");
        else
            content.append(updateRole.getName()+";");
 
        LogUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
    }
 
}