czt
9 天以前 db67639449287bcec461916a7dca6003ee5dd03c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.fzzy.igds.service;
 
import com.fzzy.igds.constant.Constant;
import com.fzzy.igds.constant.RedisConst;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Description
 * @Author CZT
 * @Date 2024/11/22 18:03
 */
@Slf4j
@Service
public class SysDeptService {
 
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private ISysDeptService iSysDeptService;
    @Resource
    private RedisCache redisCache;
 
    public void initUserDeptMap(String companyId) {
        if(StringUtils.isEmpty(companyId)){
            companyId = ContextUtil.getCompanyId();
        }
 
        SysUser sysUser = new SysUser();
        sysUser.setCompanyId(companyId);
        List<SysUser> list = userMapper.selectUserList(sysUser);
        if (null == list || list.isEmpty()){
            return;
        }
        for (SysUser userDept : list) {
            ContextUtil.updateSubDept(userDept.getLoginName(), userDept.getDeptId().toString());
        }
    }
 
    /**
     * 刷新部门架构信息到缓存
     * @param companyId
     */
    public List<SysDept> flushDeptCache(String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        SysDept sysDept = new SysDept();
        sysDept.setCompanyId(companyId);
        List<SysDept> listSysDept = iSysDeptService.selectDeptList(sysDept);
 
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST);
 
        redisCache.setCacheObject(key, listSysDept);
        return listSysDept;
    }
 
    /**
     * 根据组织编码获取所有部门信息
     * @param companyId
     * @return
     */
    public List<SysDept> getCacheDept(String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST);
 
        List<SysDept> listSysDept = redisCache.getCacheObject(key);
        if(null == listSysDept){
            listSysDept = flushDeptCache(companyId);
        }
        return listSysDept;
    }
 
    /**
     * 缓存获取组织下所有库区列表
     * @param companyId
     */
    public List<SysDept> getAllDeptByCompanyId(String companyId) {
        List<SysDept> list = getCacheDept(companyId);
        return list.stream()
                .filter(item -> item.getType().equals(Constant.DEPT_TYPE_20))
                .collect(Collectors.toList());
    }
 
    /**
     * 根据id获取部门信息
     * @param companyId
     * @param deptId
     * @return
     */
    public SysDept getCacheDept(String companyId, String deptId) {
        if (StringUtils.isEmpty(deptId)) {
            return null;
        }
        List<SysDept> list = getCacheDept(companyId);
 
        if (null == list || list.isEmpty()){
            return null;
        }
 
        for (SysDept dept : list) {
            if (deptId.equals(dept.getDeptId() + ""))
                return dept;
        }
        return null;
    }
 
    /**
     * 根据类型获取部门信息
     * type: 10-公司,20-库区,30-部门
     * @param type
     * @return
     */
    public List<SysDept> getDeptByType(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        List<SysDept> list = getCacheDept(null);
        if (null == list || list.isEmpty()) {
            return null;
        }
        //根据类型查询
        return list.stream()
                .filter(item -> type.equals(item.getType()))
                .collect(Collectors.toList());
    }
 
    /**
     * 根据类型获取部门信息
     * type: 00-公司,10-公司,20-库区,多个用英文逗号隔开
     *
     * @param
     * @return
     */
    public List<SysDept> getDeptById(SysDept dept) {
 
        if (null == dept) {
            return null;
        }
        List<SysDept> list = getCacheDept(null);
        if ("20".equals(dept.getType())) {
            //根据类型查询
            return list.stream()
                    .filter(item -> dept.getDeptId().longValue() == item.getDeptId())
                    .collect(Collectors.toList());
        } else if ("10".equals(dept.getType())) {
            //根据类型查询
            return list.stream()
                    .filter(item -> dept.getDeptId().longValue() == item.getParentId())
                    .collect(Collectors.toList());
        } else {
            return list.stream()
                    .filter(item -> dept.getDeptId().toString().equals(item.getCompanyId()) && "20".equals(item.getType()))
                    .collect(Collectors.toList());
        }
 
    }
}