jinpengyong
2021-08-26 3be7bd55ff0a4ab2ed25b46cdfd1dede92300ea3
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
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.Group;
import com.moral.api.entity.GroupMenu;
import com.moral.api.mapper.GroupMapper;
import com.moral.api.mapper.GroupMenuMapper;
import com.moral.api.pojo.dto.group.GroupDTO;
import com.moral.api.pojo.form.group.AdminGroupUpdateMenuForm;
import com.moral.api.service.GroupService;
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;
 
import java.util.List;
 
/**
 * <p>
 * 用户自定义角色表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {
 
    @Autowired
    GroupMapper groupMapper;
    @Autowired
    GroupMenuMapper groupMenuMapper;
 
    @Override
    public Group queryAdminGroupByOrganizationId(Integer OrganizationId) {
        QueryWrapper<Group> queryGroupWrapper = new QueryWrapper<>();
        Group groupConditon = new Group();
        groupConditon.setOrganizationId(OrganizationId);
        groupConditon.setGroupName("admin");
        groupConditon.setIsDelete(Constants.NOT_DELETE);
        queryGroupWrapper.setEntity(groupConditon);
        return groupMapper.selectOne(queryGroupWrapper);
    }
 
    @Override
    @Transactional
    public GroupDTO updateAdminGroupMenu(AdminGroupUpdateMenuForm form) {
        //创建返回对象
        GroupDTO dto = new GroupDTO();
        //取参
        Integer orgId = form.getOrganizationId();
        String channelKey = form.getChannelKey();
        List<Integer> insertMenuIds = form.getMenuIds();
        //根据orgId查询组织admin角色id
        Group group = queryAdminGroupByOrganizationId(orgId);
        if (ObjectUtils.isEmpty(group)) {//如果没有角色则证明组织还没有账号
            dto.setCode(ResponseCodeEnum.ORGANIZATION_USER_NOT_EXIST.getCode());
            dto.setMsg(ResponseCodeEnum.ORGANIZATION_USER_NOT_EXIST.getMsg());
            return dto;
        }
        //删除角色当前所有的菜单
        UpdateWrapper deleteWrapper = new UpdateWrapper();
        deleteWrapper.set("is_delete", Constants.DELETE);
        deleteWrapper.eq("group_id", group.getId());
        groupMenuMapper.update(null, deleteWrapper);
        //重新添加
        for (Integer menuId : insertMenuIds) {
            GroupMenu groupMenu = new GroupMenu();
            groupMenu.setChannelKey(channelKey);
            groupMenu.setGroupId(group.getId());
            groupMenu.setMenuId(menuId);
            groupMenu.setOrganizationId(orgId);
            groupMenuMapper.insert(groupMenu);
        }
        //封装返回对象
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
}