CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.ld.igds.sys.service;
 
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.model.DefaultDept;
import com.bstek.bdf2.core.model.UserDept;
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import java.util.*;
 
@Repository
public class SysDeptServiceImpl extends HibernateDao implements SysDeptService {
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private JdbcDeptServiceImpl jdbcDeptService;
 
    @Override
    public List<DefaultDept> loadDeptByParentId(String parentId,
            String companyId) {
 
        String hql = " from " + DefaultDept.class.getName()
                + " where companyId=:companyId ";
        Map<String, Object> args = new HashMap<>();
        args.put("companyId", companyId);
 
        List<DefaultDept> list;
        if (parentId == null) {
            hql += " AND parentId IS NULL ORDER BY id ";
 
            list = this.query(hql, args);
        } else {
            hql += " AND parentId=:parentId  ORDER BY id";
            args.put("parentId", parentId);
            list = this.query(hql, args);
        }
 
        return list;
    }
 
    private List<DefaultDept> getAll(String companyId) {
        String hql = " from " + DefaultDept.class.getName()
                + " where companyId=:companyId ";
        Map<String, Object> args = new HashMap<>();
        args.put("companyId", companyId);
        return this.query(hql, args);
    }
 
    @Override
    public void saveDept(Collection<DefaultDept> list, String companyId) {
        EntityState state = null;
        DefaultDept dept = null;
        String id;
        for (DefaultDept defaultDept : list) {
            if (StringUtils.isEmpty(defaultDept.getVal())) {
                defaultDept.setVal(Constant.YN_Y);
            }
 
            state = EntityUtils.getState(defaultDept);
            if (state.equals(EntityState.NEW)) {
                dept = this
                        .findLastDeptId(defaultDept.getParentId(), companyId);
                if (dept != null) {
                    id = ContextUtil.getNextId(companyId,
                            defaultDept.getParentId(), dept.getId(), 1000);
                } else {
                    id = ContextUtil.getNextId(companyId,
                            defaultDept.getParentId(), null, 1000);
                }
                defaultDept.setId(id);
                defaultDept.setCompanyId(companyId);
                jdbcDeptService.saveDefaultDept(defaultDept);
                // this.saveData(defaultDept);
            }
            if (state.equals(EntityState.MODIFIED)
                    || state.equals(EntityState.MOVED)) {
                this.updateData(defaultDept);
            }
 
            if (defaultDept.getChildren() != null) {
                DefaultDept[] defaultDepts = new DefaultDept[defaultDept
                        .getChildren().size()];
                saveDept(
                        Arrays.asList(defaultDept.getChildren().toArray(
                                defaultDepts)), defaultDept.getCompanyId());
            }
 
            if (state.equals(EntityState.DELETED)) {
                if (countChildren(defaultDept.getId()) == 0) {
                    this.delData(defaultDept);
                } else {
                    throw new RuntimeException("请先删除子部门");
                }
            }
        }
    }
 
    private void delData(DefaultDept defaultDept) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(defaultDept);
        } finally {
            session.flush();
            session.close();
        }
    }
 
    private void updateData(DefaultDept defaultDept) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.update(defaultDept);
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void saveData(DefaultDept data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.update(data);
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public int countChildren(String parentId) {
        String hql = " from " + DefaultDept.class.getName()
                + " where parentId=:parentId";
        Map<String, Object> args = new HashMap<>();
        args.put("parentId", parentId);
        List<DefaultDept> list = this.query(hql, args);
        if (null == list)
            return 0;
        return list.size();
    }
 
    @Override
    public List<DefaultDept> flushDeptCache(String companyId) {
        List<DefaultDept> list = this.getAll(companyId);
 
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST);
 
        redisUtil.set(key, list);
 
        return list;
    }
 
    @Override
    public DefaultDept getCacheDept(String companyId, String deptId) {
        List<DefaultDept> list = getCacheDept(companyId);
 
        if (null == list || list.isEmpty())
            return null;
 
        for (DefaultDept dept : list) {
            if (dept.getId().equals(deptId))
                return dept;
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<DefaultDept> getCacheDept(String companyId) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST);
 
        return (List<DefaultDept>) redisUtil.get(key);
    }
 
    @Override
    public DefaultDept getDeptById(String id) {
        String hql = " from " + DefaultDept.class.getName() + " where id =:id ";
        Map<String, Object> args = new HashMap<>();
        args.put("id", id);
        List<DefaultDept> list = this.query(hql, args);
        if (list == null || list.isEmpty()) {
            return null;
        }
 
        return list.get(0);
    }
 
    private DefaultDept findLastDeptId(String parentId, String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getDefaultCompanyId();
        }
        List<DefaultDept> list = this.loadDeptByParentId(parentId, companyId);
 
        if (list != null && list.size() > 0) {
            return list.get(list.size() - 1);
        } else {
            return null;
        }
    }
 
    @Override
    public List<DefaultDept> loadUserDepts(String username) {
 
        String hql = " from " + DefaultDept.class.getName() + " where id in("
                + " select deptId from " + UserDept.class.getName()
                + " where username=:username)";
 
        Map<String, Object> args = new HashMap<>();
        args.put("username", username);
 
        return this.query(hql, args);
    }
 
    @Override
    public DefaultDept loadUserDept(String username) {
 
        List<DefaultDept> list = this.loadUserDepts(username);
 
        if (null == list || list.isEmpty())
            return null;
 
        return list.get(0);
    }
 
    @Override
    public void updateUserDept(String username, String deptId) {
        Session session = null;
        try {
            UserDept userDept = new UserDept();
            userDept.setDeptId(deptId);
            userDept.setId(ContextUtil.getUUID());
            userDept.setUsername(username);
 
            session = this.getSessionFactory().openSession();
 
            String hql = " delete from " + UserDept.class.getName()
                    + " where username=:username";
 
            Query query = session.createQuery(hql);
            query.setString("username", username);
            query.executeUpdate();
 
            session.save(userDept);
        } finally {
            session.flush();
            session.close();
        }
 
        ContextUtil.updateSubDept(username, deptId);
    }
 
    public List<UserDept> loadUserDept() {
        String hql = " from " + UserDept.class.getName();
        return this.query(hql);
    }
 
    @Override
    public void initUserDeptMap() {
        List<UserDept> list = loadUserDept();
        if (null == list || list.isEmpty())
            return;
        for (UserDept userDept : list) {
            ContextUtil.updateSubDept(userDept.getUsername(),
                    userDept.getDeptId());
        }
    }
 
    @Override
    public DefaultDept getSubDept(IUser user, String deptId) {
 
        if (null == user) {
            user = ContextUtil.getLoginUser();
        }
        if (null == deptId) {
            deptId = ContextUtil.subDeptId(user);
        }
 
        DefaultDept defaultDept = getCacheDept(user.getCompanyId(), deptId);
        if (Constant.DEPT_TYPE_20.equals(defaultDept.getType()))
            return defaultDept;
 
        List<DefaultDept> list = getCacheDept(user.getCompanyId());
 
        // 所在位置为公司,获取当前公司下第一个库点
        if (Constant.DEPT_TYPE_10.equals(defaultDept.getType())) {
            for (DefaultDept depot : list) {
                if (depot.getParentId().equals(defaultDept.getId()))
                    return depot;
            }
        }
 
        if (Constant.DEPT_TYPE_30.equals(defaultDept.getType())) {
            for (DefaultDept depot : list) {
                if (defaultDept.getParentId().equals(depot.getId()))
                    return depot;
            }
        }
 
        return null;
    }
}