16639036659
2024-05-14 964dd88319269c16d4ebb99007a954b51e625ef2
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
package com.gkhy.labRiskManage.domain.basic.service.impl;
 
import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentPersonAppInsertBO;
import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentPersonAppQueryBO;
import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentPersonAppUpdateBO;
import com.gkhy.labRiskManage.commons.domain.SearchResult;
import com.gkhy.labRiskManage.commons.enums.ResultCode;
import com.gkhy.labRiskManage.commons.enums.StatusEnum;
import com.gkhy.labRiskManage.commons.enums.UserTagEnum;
import com.gkhy.labRiskManage.commons.exception.BusinessException;
import com.gkhy.labRiskManage.commons.utils.BeanCopyUtils;
import com.gkhy.labRiskManage.domain.account.model.dto.UserInfoDomainDTO;
import com.gkhy.labRiskManage.domain.account.service.UserDomainService;
import com.gkhy.labRiskManage.domain.basic.converter.ToInsertPersonBOConverter;
import com.gkhy.labRiskManage.domain.basic.converter.ToQueryPersonBOConverter;
import com.gkhy.labRiskManage.domain.basic.entity.BasicExperimentPerson;
import com.gkhy.labRiskManage.domain.basic.model.bo.PersonInsertBO;
import com.gkhy.labRiskManage.domain.basic.model.bo.PersonQueryBO;
import com.gkhy.labRiskManage.domain.basic.model.dto.*;
import com.gkhy.labRiskManage.domain.basic.repository.jpa.BasicExperimentPersonRepository;
import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentPersonService;
import com.gkhy.labRiskManage.domain.riskReport.utils.GetRoleTagUtils;
import com.gkhy.labRiskManage.domain.riskReport.utils.SearchAuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * 基础实验人员管理
 */
@Service
public class BasicExperimentPersonServiceImpl implements BasicExperimentPersonService {
 
    @Autowired
    private BasicExperimentPersonRepository personRepository;
    @Autowired
    private UserDomainService userDomainService;
 
    /**
     * 基础实验人员 - 插入
     * */
    @Override
    public PersonInsertDTO insertBasicExperimentPerson(Long currentUserId, BasicExperimentPersonAppInsertBO personAppInsertDO) {
 
        if (currentUserId < 0){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆");
        }
 
        //接收,转换参数  todo ,目前所有信息,缺乏唯一性标识。需要如身份证,手机号,人员身份码之类的信息
        ToInsertPersonBOConverter converter = new ToInsertPersonBOConverter();
        PersonInsertBO personInsertBO = converter.toInsertPersonDTO(personAppInsertDO);
 
        //参数校验
        if (ObjectUtils.isEmpty(personInsertBO)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "人员信息不能为空");
        }
        if (ObjectUtils.isEmpty(personInsertBO.getPersonName())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "姓名不能为空");
        }
        if (ObjectUtils.isEmpty(personInsertBO.getPersonAge())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "年龄不能为空");
        }
        if (ObjectUtils.isEmpty(personInsertBO.getPersonGender())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "性别不能为空");
        }
        if (ObjectUtils.isEmpty(personInsertBO.getPersonMajor())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "专业不能为空");
        }
//        if (ObjectUtils.isEmpty(insertPersonDO.getDepName())){
//            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "所属部门不能为空");
//        }  todo 暂时没有部门
        if (ObjectUtils.isEmpty(personInsertBO.getAptitude())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "相关资质不能为空");
        }
        if (ObjectUtils.isEmpty(personInsertBO.getTraining())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "培训情况不能为空");
        }
        if (ObjectUtils.isEmpty(personInsertBO.getPhone())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "手机号不能为空");
        }
        BasicExperimentPerson personByPhone = personRepository.getPersonByPhone(personInsertBO.getPhone());
        if (!ObjectUtils.isEmpty(personByPhone)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "手机号已被使用");
        }
 
        BasicExperimentPerson person = BeanCopyUtils.copyBean(personInsertBO, BasicExperimentPerson.class);
        UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId);
        //获取参数
        LocalDateTime date = LocalDateTime.now();
        //封装参数
        person.setCreateByUserId(userInfoById.getId());
        person.setCreateTime(date);
        person.setUpdateByUserId(userInfoById.getId());
        person.setUpdateTime(date);
        person.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue());
 
 
        BasicExperimentPerson saveResult = personRepository.save(person);
        return BeanCopyUtils.copyBean(saveResult, PersonInsertDTO.class);
    }
 
    /**
     * 基础实验人员 - 分页查询
     * */
    @Override
    public SearchResult<PersonQueryDTO> getBasicExperimentPersonPage(Long currentUserId, BasicExperimentPersonAppQueryBO queryParam) {
 
        //校验参数
        if (ObjectUtils.isEmpty(queryParam.getPageSize())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"分页信息不能为空");
        }
        if (ObjectUtils.isEmpty(queryParam.getPageIndex())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"分页信息不能为空");
        }
        if (queryParam.getPersonName() == ""){
            queryParam.setPersonName(null);
        }
        if (queryParam.getDepName()  == ""){
            queryParam.setDepName(null);
        }
 
        //接收,转换参数
        ToQueryPersonBOConverter converter = new ToQueryPersonBOConverter();
        PersonQueryBO queryPageParam = converter.toQueryPersonDOConverter(queryParam);
 
        SearchResult searchResult = new SearchResult<>();
 
        searchResult.setPageIndex(queryPageParam.getPageIndex());
        searchResult.setPageSize(queryPageParam.getPageSize());
 
        UserInfoDomainDTO user = userDomainService.getUserById(currentUserId);
        int roleTag = GetRoleTagUtils.GetRoleTagUtils(user);
 
        //封装查询参数
        Specification<BasicExperimentPerson> specification = new Specification<BasicExperimentPerson>() {
            @Override
            public Predicate toPredicate(Root<BasicExperimentPerson> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (queryPageParam.getPersonName() != null && !queryPageParam.getPersonName().equals("")){
                    predicateList.add(criteriaBuilder.equal(root.get("personName"), queryPageParam.getPersonName()));
                }
                if (queryPageParam.getDepName() != null && !queryPageParam.getDepName().equals("")){
                    predicateList.add(criteriaBuilder.equal(root.get("depName"), queryPageParam.getDepName()));
                }
                if (queryPageParam.getTraining() != null && !queryPageParam.getTraining().equals("")){
                    predicateList.add(criteriaBuilder.equal(root.get("training"), queryPageParam.getTraining()));
                }
                if (SearchAuthUtils.basicSearchAuth() == 1){
                    if (roleTag == UserTagEnum.USER_TAG_0.getCode()){
                        predicateList.add(criteriaBuilder.equal(root.get("createByUserId"), currentUserId));
                    }
                }
                predicateList.add(criteriaBuilder.equal(root.get("deleteStatus"),StatusEnum.DELETE_NOT.getCode()));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
            }
        };
        PageRequest pageParam = PageRequest.of(queryPageParam.getPageIndex() - 1, queryPageParam.getPageSize(), Sort.Direction.DESC, "updateTime");
 
        Page<BasicExperimentPerson> pageResult = personRepository.findAll(specification, pageParam);
        List<PersonQueryDTO> personQueryDTOS = BeanCopyUtils.copyBeanList(pageResult.getContent(), PersonQueryDTO.class);
//        BasicExperimentPerson person = new BasicExperimentPerson();
//        person.set
//        personQueryDTOS.add()
 
        List<UserInfoDomainDTO> userList = userDomainService.getUserList();
        for (PersonQueryDTO personQueryDTO : personQueryDTOS) {
            for (UserInfoDomainDTO userInfo : userList) {
                if (userInfo.getId() == personQueryDTO.getCreateByUserId()){
                    personQueryDTO.setCreateByUserName(userInfo.getRealName());
                }
                if (userInfo.getId() == personQueryDTO.getUpdateByUserId()){
                    personQueryDTO.setUpdateByUserName(userInfo.getRealName());
                }
            }
        }
 
        searchResult.setData(personQueryDTOS);
        searchResult.setTotal(pageResult.getTotalElements());
 
        return searchResult;
    }
 
    /**
     * 基础实验人员 - 修改
     * */
    @Override
    public PersonUpdateDTO updateBasicExperimentPerson(Long currentUserId, BasicExperimentPersonAppUpdateBO updateParam) {
 
        if (currentUserId < 0){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆");
        }
 
        //参数校验
        if (ObjectUtils.isEmpty(updateParam)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "人员信息不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getPersonName())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "姓名不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getPersonAge())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "年龄不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getPersonGender())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "性别不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getPersonMajor())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "专业不能为空");
        }
//        if (ObjectUtils.isEmpty(insertPersonDO.getDepName())){
//            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "所属部门不能为空");
//        }  todo 暂时没有部门
        if (ObjectUtils.isEmpty(updateParam.getAptitude())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "相关资质不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getTraining())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "培训情况不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getPhone())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "手机号不能为空");
        }
        BasicExperimentPerson personByPhone = personRepository.getPersonByPhone(updateParam.getPhone());
//        if (!ObjectUtils.isEmpty(personByPhone) && updateParam.getId() != personByPhone.getId()){
//            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "手机号已被使用");
//        }
 
        BasicExperimentPerson personById = personRepository.getPersonById(updateParam.getId());
        BasicExperimentPerson person = BeanCopyUtils.copyBean(personById, BasicExperimentPerson.class);
        UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId);
 
        //获取参数
        LocalDateTime date = LocalDateTime.now();
        //封装参数
        person.setUpdateByUserId(userInfoById.getId());
        person.setUpdateTime(date);
        person.setPersonName(updateParam.getPersonName());
        person.setPersonAge(updateParam.getPersonAge());
        person.setPersonGender(updateParam.getPersonGender());
        person.setPersonMajor(updateParam.getPersonMajor());
        person.setDepName(updateParam.getDepName());
        person.setAptitude(updateParam.getAptitude());
        person.setTraining(updateParam.getTraining());
        person.setPhone(updateParam.getPhone());
        person.setImage(updateParam.getImage());
 
        //更新数据
        BasicExperimentPerson saveResult = personRepository.save(person);
 
        return BeanCopyUtils.copyBean(saveResult, PersonUpdateDTO.class);
    }
 
    /**
     * 基础实验人员 - 删除
     * */
    @Override
    public PersonDeleteDTO deleteBasicExperimentPerson(Long currentUserId, Long id) {
 
        if (currentUserId < 0){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆");
        }
 
        //校验参数
        if (ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数错误");
        }
        BasicExperimentPerson personById = personRepository.getPersonById(id);
        if (ObjectUtils.isEmpty(personById)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "该人员不存在,或已被删除");
        }
        UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId);
        LocalDateTime date = LocalDateTime.now();
        //设置数据为删除
        personById.setDeleteStatus(StatusEnum.DELETED.getCode().byteValue());
        personById.setUpdateByUserId(userInfoById.getId());
        personById.setUpdateTime(date);
        //执行数据更新
        BasicExperimentPerson deleteResult = personRepository.save(personById);
 
        return BeanCopyUtils.copyBean(deleteResult, PersonDeleteDTO.class);
    }
 
    /**
     * 基础实验人员 - 列表
     * */
    @Override
    public List<PersonListDTO> listBasicExperimentPerson(Long currentUserId) {
 
        UserInfoDomainDTO user = userDomainService.getUserById(currentUserId);
        int roleTag = GetRoleTagUtils.GetRoleTagUtils(user);
 
        List<BasicExperimentPerson> peopleList = new ArrayList<>();
        if (SearchAuthUtils.basicSearchAuth() ==  0){
            peopleList = personRepository.listPerson(currentUserId);
            return BeanCopyUtils.copyBeanList(peopleList, PersonListDTO.class);
        }
 
 
        if (roleTag != UserTagEnum.USER_TAG_0.getCode()){
            peopleList = personRepository.listPerson(currentUserId);
        }else {
            peopleList = personRepository.listPersonByUserId(currentUserId);
        }
        return BeanCopyUtils.copyBeanList(peopleList, PersonListDTO.class);
    }
 
    /**
     * 基础实验人员 - 查询 by id
     * */
    @Override
    public PersonQueryDTO getBasicExperimentPersonById(Long id) {
        if (ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数不能为空");
        }
 
        BasicExperimentPerson personById = personRepository.getPersonById(id);
 
        if (ObjectUtils.isEmpty(personById)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "请检查是否输入有误或人员已被删除");
        }
        return BeanCopyUtils.copyBean(personById, PersonQueryDTO.class);
    }
 
    /**
     * 基础实验人员 - 查询 by id --评估计划使用
     * */
    @Override
    public PersonQueryDTO getPersonByIdAndSellInfo(Long id, String Info) {
        if (ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数不能为空");
        }
 
        BasicExperimentPerson personById = personRepository.getPersonById(id);
 
        if (ObjectUtils.isEmpty(personById)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), Info + "不存在,请检查是否输入有误或人员已被删除");
        }
        return BeanCopyUtils.copyBean(personById, PersonQueryDTO.class);
    }
 
    /**
     * 基础实验人员 - 通过id列表查询
     * */
    @Override
    public List<PersonQueryDTO> getBasicExperimentPersonByIdList(List<Long> ids) {
        if (ids.size() < 1){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "请求参数不能为空");
        }
 
        List<BasicExperimentPerson> listResult  = personRepository.batchById(ids);
        if (listResult.size() < 1){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "查询结果为空");
        }
 
        return BeanCopyUtils.copyBeanList(listResult, PersonQueryDTO.class);
    }
}