longz
2026-06-10 1f4fd950f6aa0dd8f865f63daeb2303efb3f8d76
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.Convert;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.modular.system.dao.RoleMapper;
import com.stylefeng.guns.modular.system.dao.SysUserCompanyScopeMapper;
import com.stylefeng.guns.modular.system.model.Role;
import com.stylefeng.guns.modular.system.model.SysUserCompanyScope;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.ISysUserCompanyScopeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service
public class SysUserCompanyScopeServiceImpl extends ServiceImpl<SysUserCompanyScopeMapper, SysUserCompanyScope>
        implements ISysUserCompanyScopeService {
 
    @Autowired
    private RoleMapper roleMapper;
 
    @Override
    public void ensureDefaultScope(User user) {
        if (user == null || user.getId() == null || user.getRoleType() == null || user.getRoleType() != 2
                || user.getObjectId() == null) {
            return;
        }
        Integer count = this.baseMapper.countScopesByUserId(user.getId());
        if (count != null && count > 0) {
            return;
        }
        SysUserCompanyScope scope = buildScope(user.getId(), user.getObjectId(), user.getRoleid(), user.getDeptid(), 1);
        this.baseMapper.insert(scope);
    }
 
    @Override
    public List<Map<String, Object>> selectScopesByUserId(Integer userId) {
        return this.baseMapper.selectScopesByUserId(userId);
    }
 
    @Override
    public SysUserCompanyScope selectActiveScope(Integer userId, Integer companyId) {
        return this.baseMapper.selectActiveScope(userId, companyId);
    }
 
    @Override
    public void applyScopeToShiroUser(ShiroUser shiroUser, SysUserCompanyScope scope) {
        if (shiroUser == null || scope == null) {
            return;
        }
        shiroUser.setObjectId(scope.getCompanyId());
        shiroUser.setActiveCompanyId(scope.getCompanyId());
        shiroUser.setDeptId(scope.getDeptid());
        if (SinataUtil.isEmpty(scope.getRoleid())) {
            shiroUser.setRoleList(new ArrayList<Integer>());
            shiroUser.setRoleNames(new ArrayList<String>());
            shiroUser.setAdmin(false);
            return;
        }
        Integer[] roleArray = Convert.toIntArray(scope.getRoleid());
        List<Integer> roleList = new ArrayList<Integer>();
        List<String> roleNameList = new ArrayList<String>();
        for (int roleId : roleArray) {
            roleList.add(roleId);
            roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
        }
        shiroUser.setRoleList(roleList);
        shiroUser.setRoleNames(roleNameList);
        List<Role> roles = roleMapper.selectBatchIds(roleList);
        shiroUser.setAdmin(roles.stream().anyMatch(s -> "administrator".equals(s.getTips())));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveScopes(Integer userId, String scopesJson, Integer fallbackCompanyId, String fallbackRoleid, Integer fallbackDeptid) {
        this.baseMapper.deleteByUserId(userId);
        JSONArray array = parseScopes(scopesJson);
        if (array == null || array.isEmpty()) {
            if (fallbackCompanyId != null) {
                this.baseMapper.insert(buildScope(userId, fallbackCompanyId, fallbackRoleid, fallbackDeptid, 1));
            }
            return;
        }
        boolean hasDefault = false;
        for (int i = 0; i < array.size(); i++) {
            JSONObject item = array.getJSONObject(i);
            Integer companyId = item.getInteger("companyId");
            String roleid = item.getString("roleid");
            Integer deptid = item.getInteger("deptid");
            if (companyId == null || deptid == null) {
                continue;
            }
            Integer isDefault = item.getInteger("isDefault");
            if (isDefault != null && isDefault == 1 && !hasDefault) {
                hasDefault = true;
            } else {
                isDefault = 2;
            }
            this.baseMapper.insert(buildScope(userId, companyId, roleid, deptid, isDefault));
        }
        if (!hasDefault) {
            SysUserCompanyScope first = this.selectOne(new EntityWrapper<SysUserCompanyScope>()
                    .eq("userId", userId)
                    .eq("status", 1)
                    .orderBy("id", true));
            if (first != null) {
                first.setIsDefault(1);
                first.setUpdateTime(new Date());
                this.updateById(first);
            }
        }
    }
 
    @Override
    public void deleteByUserId(Integer userId) {
        this.baseMapper.deleteByUserId(userId);
    }
 
    private JSONArray parseScopes(String scopesJson) {
        if (SinataUtil.isEmpty(scopesJson)) {
            return null;
        }
        return JSON.parseArray(scopesJson);
    }
 
    private SysUserCompanyScope buildScope(Integer userId, Integer companyId, String roleid, Integer deptid, Integer isDefault) {
        SysUserCompanyScope scope = new SysUserCompanyScope();
        scope.setUserId(userId);
        scope.setCompanyId(companyId);
        scope.setRoleid(roleid);
        scope.setDeptid(deptid);
        scope.setIsDefault(isDefault);
        scope.setStatus(1);
        scope.setCreateTime(new Date());
        scope.setUpdateTime(new Date());
        return scope;
    }
}