kongzy
2023-11-28 59d9ea33f503e363f2e2941c7c00cc9dd9d9d1c7
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package com.nanometer.smartlab.service;
 
import com.google.common.collect.ImmutableMap;
import com.nanometer.smartlab.dao.SysUserDao;
import com.nanometer.smartlab.entity.SysLaboratoryContainer;
import com.nanometer.smartlab.entity.SysReagent;
import com.nanometer.smartlab.entity.SysUser;
import com.nanometer.smartlab.entity.dto.HazardousWasteUser;
import com.nanometer.smartlab.entity.dto.LaboratoryVo;
import com.nanometer.smartlab.entity.dto.SysUserDto;
import com.nanometer.smartlab.entity.enumtype.ApproverFlag;
import com.nanometer.smartlab.entity.enumtype.SeeFlag;
import com.nanometer.smartlab.exception.AlarmCode;
import com.nanometer.smartlab.exception.AlarmException;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.model.CommonPage;
import com.nanometer.smartlab.util.ExcelUtils;
import com.nanometer.smartlab.util.FacesUtils;
import com.nanometer.smartlab.util.IDUtils;
import com.nanometer.smartlab.util.MessageUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * Created by johnny on 17/11/20.
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
 
    private static Logger logger = Logger.getLogger(SysUserService.class);
 
    @Resource(name = "sysUserDao")
    SysUserDao sysUserDao;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysUser> getSysUserList(String arp, String name,String departmentName,String project,String company, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(arp)) {
                params.put("arp", "%" + arp + "%");
            }
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            if (StringUtils.isNotBlank(departmentName)) {
                params.put("departmentNameLike", "%" + departmentName + "%");
            }
            params.put("project", project);
            params.put("company", company);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.sysUserDao.getSysUserList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public CommonPage<SysUser> getSysUserList(Integer pageNum, Integer pageSize, String name) {
        if(pageNum==null||pageNum<1){
            pageNum=1;
        }
        if(pageSize==null||pageSize<1){
            pageSize=10;
        }
        if(pageSize>50){
            pageSize=50;
        }
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("name","%" + name+ "%");
        }
        params.put("first", (pageNum-1)*pageSize);
        params.put("pageSize", pageSize);
        CommonPage commonPage=new CommonPage();
        commonPage.setPageNum(pageNum);
        commonPage.setPageSize(pageSize);
        Long total=new Long(sysUserDao.getSysUserTotalCount(params));
        commonPage.setTotal(total);
        commonPage.setTotalPage(CommonPage.getTotalPage(total,pageSize));
        List<SysUser> sysReagents=sysUserDao.getSysUserSimpleInfoList(params);
        commonPage.setList(sysReagents);
        return commonPage;
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public int getSysUserTotalCount(String arp, String name,String departmentName,String project,String company) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(arp)) {
                params.put("arp", "%" + arp + "%");
            }
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            if (StringUtils.isNotBlank(departmentName)) {
                params.put("departmentNameLike", "%" + departmentName + "%");
            }
            params.put("project", project);
            params.put("company", company);
            return this.sysUserDao.getSysUserTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public int getUserCountInProject(String arp, String name, String departmentName, String project, String company) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("arp", arp);
        params.put("name", name);
        params.put("departmentNameLike", departmentName);
        params.put("project", project);
        params.put("company", company);
        return sysUserDao.getUserCountInProject(params);
    }
 
    @Override
    public List<SysUser> getUserInProject(String arp, String name, String departmentName, String project, String company, Integer first, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("arp", arp);
        params.put("name", name);
        params.put("departmentNameLike", departmentName);
        params.put("project", project);
        params.put("company", company);
        return sysUserDao.getUserInProject(params);
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysUser getSysUser(String id) {
        try {
            return this.sysUserDao.getSysUser(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysUser getSysUserForSuppllier(String id) {
        try {
            return this.sysUserDao.getSysUserForSuppllier(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysUser getSysUserByAccount(String account) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("account", account);
            List<SysUser> list = this.sysUserDao.getSysUserList(params);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
 
            return null;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysUser getSysUserByIdCard(String idCard) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("idCard", idCard);
            List<SysUser> list = this.sysUserDao.getSysUserList(params);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
 
            return null;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean isSysUserExist(String arp, String account, String idCard, String editId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("arp", arp);
            params.put("account", account);
            params.put("idCard", idCard);
            params.put("editId", editId);
 
            int count = this.sysUserDao.getSysUserTotalCount(params);
            return count > 0;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysUser> getApproverUserList(String department) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("department", department);
            params.put("approverFlag", ApproverFlag.YES);
            return this.sysUserDao.getSysUserList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public List<SysUser> getHasProjectSysUserList(String department, String project) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("department", department);
            params.put("project", project);
            params.put("approverFlag", ApproverFlag.YES);
            return this.sysUserDao.getHasProjectSysUserList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysUser> getApplyUserList() {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            return this.sysUserDao.getSysUserList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysUser> getSeeUserList(String department) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("department", department);
            return this.sysUserDao.getSysUserList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysUser insertSysUser(SysUser sysUser) {
        try {
            if (sysUser.getId() == null) {
                sysUser.setId(IDUtils.uuid());
            }
            this.sysUserDao.insertSysUser(sysUser);
            return sysUser;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateSysUser(SysUser sysUser) {
        try {
            int row = this.sysUserDao.updateSysUser(sysUser);
 
            return row != 0;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteSysUser(List<SysUser> sysUserList) {
        try {
            if (sysUserList == null || sysUserList.size() == 0) {
                return false;
            }
 
            List<String> ids = new ArrayList<String>();
            for (SysUser sysUser : sysUserList) {
                ids.add(sysUser.getId());
            }
 
            int row = this.sysUserDao.deleteSysUsers(ids);
            return row != 0;
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Override
    @Transactional(readOnly = true)
    public List<SysUser> getSysUserInfoList(String startTime, String endTime){
        Map params = ImmutableMap.of("startTime",startTime,"endTime",endTime);
        return sysUserDao.getSysUserInfoList(params);
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserPointBySelective(Integer point,String id) {
        sysUserDao.updateUserPointBySelective(point,id);
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int insertSysUserList(List<SysUser> userList){
        try {
            int i = 0;
            if (userList.size()>0) {
                for (SysUser user:userList) {
                    if (this.isSysUserExist(user.getArp(), null, null, null)) {
                        throw new DuplicateKeyException("arp重复");
                    }else if(!com.alibaba.druid.util.StringUtils.isEmpty(user.getIdCard()) && this.isSysUserExist(null, null, user.getIdCard(), null)){
                        throw new DuplicateKeyException("idCard为空或重复");
                    }else {
                        SysUser sysUser=sysUserDao.selectByAccount(user);
                        if(sysUser==null){
                            if (user.getId() == null) {
                                user.setId(IDUtils.uuid());
                            }
                            sysUserDao.insertSysUser(user);
                        }else {
                            sysUserDao.updateSysUser(user);
                        }
                        i++;
                    }
 
                }
            }
            return i;
        }catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Override
    public SysUser getUser(String id) {
        try {
            return this.sysUserDao.getUser(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public void updateUserFavor(SysReagent reagent, SysUser user) throws Exception {
        //取消收藏
        Map params = new HashMap<>();
        if(reagent.getFavor() != null){
            params.put("id", reagent.getFavor());
            sysUserDao.deleteUserFavor(params);
        }else {
 
            params.put("id", IDUtils.uuid());
            params.put("user", user.getId());
            params.put("reagent", reagent.getId());
            int num = sysUserDao.selectUserFavor(params);
            if (num > 0) {
                throw new Exception("已收藏");
            }
            sysUserDao.insertUserFavor(params);
        }
 
    }
 
    @Override
    public List<SysUserDto> getUserInfo() {
        return sysUserDao.selectUserInfo();
    }
 
    @Override
    public HazardousWasteUser getUserByAccount(String account,String name) {
        Map<String, String> params = new HashMap<>();
        params.put("account", account);
        params.put("name", name);
        return sysUserDao.getUserByAccount(params);
    }
 
    @Override
    public List<LaboratoryVo.LaboratoryUser> getUserByProject(String project) {
        return sysUserDao.getUserByProject(project);
    }
 
    @Override
    public List<SysUser> getUserByArp(String arp) {
        return sysUserDao.getUserByArp(arp);
    }
 
    @Override
    public List<Map> getExportUserList(String arp, String name, String departmentName, String project, String company) {
        return sysUserDao.getExportUserList(arp, name, departmentName, project, company);
    }
 
    @Override
    public void exportUser2Excel(List<Map> list)throws Exception {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("companyName", "单位");
        map.put("departmentName", "部门");
        map.put("project", "课题组");
        map.put("arp", "ARP");
        map.put("name", "姓名");
        map.put("account", "账号");
        map.put("idCard", "ID卡号");
        map.put("phone", "电话");
        map.put("email", "邮箱");
        map.put("memo", "备注");
        map.put("roleName", "角色");
        map.put("approver", "审批者");
        map.put("seeName", "可见度");
        map.put("wasterName", "危废人员");
        map.put("updateTime", "更新时间");
        ExcelUtils.export2Excel(list,"人员信息",map);
    }
 
 
    //获取库管员邮箱
    @Override
    public List<String> getLibrarianEmail() {
        return sysUserDao.getLibrarianEmail();
    }
 
    @Override
    public SysUser getUserByName(String approveUserName) {
        List<SysUser> users = sysUserDao.getUserByName(approveUserName);
        if (users.size() != 1) return  null;
        return users.get(0);
    }
 
 
}