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
package com.gkhy.labRiskManage.domain.basic.service.impl;
 
import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentStuffAppInsertBO;
import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentStuffAppQueryBO;
import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentStuffAppUpdateBO;
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.entity.BasicExperimentStuff;
import com.gkhy.labRiskManage.domain.basic.model.dto.*;
import com.gkhy.labRiskManage.domain.basic.repository.jpa.BasicExperimentStuffRepository;
import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentStuffService;
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 BasicExperimentStuffServiceImpl implements BasicExperimentStuffService {
 
    @Autowired
    private BasicExperimentStuffRepository stuffRepository;
    @Autowired
    private UserDomainService userDomainService;
 
    /**
     * 基础实验耗材管理 - 新增
     */
    @Override
    public StuffInsertDTO insertBasicExperimentStuff(Long currentUserId, BasicExperimentStuffAppInsertBO insertParam) {
 
        if (currentUserId < 0){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆");
        }
        //参数校验
        if (ObjectUtils.isEmpty(insertParam)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数不能为空");
        }
        if (ObjectUtils.isEmpty(insertParam.getStuffName())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料名称不能为空");
        }
        BasicExperimentStuff stuffByName = stuffRepository.getStuffByName(insertParam.getStuffName());
//        if (!ObjectUtils.isEmpty(stuffByName)){
//            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "该材料已存在,不需要重复添加");
//        }
        if (ObjectUtils.isEmpty(insertParam.getStuffCode())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料编号不能为空");
        }
        if (ObjectUtils.isEmpty(insertParam.getStuffType())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型不能为空");
        }
        if (!(insertParam.getStuffType() == 1 || insertParam.getStuffType() == 2)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型符合要求");
        }
        if (ObjectUtils.isEmpty(insertParam.getStuffUnit())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不能为空");
        }
        if (!(insertParam.getStuffUnit() == 1 || insertParam.getStuffUnit() == 2 || insertParam.getStuffUnit() == 3 || insertParam.getStuffUnit() == 4)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不符合要求");
        }
        if (ObjectUtils.isEmpty(insertParam.getStuffStorage())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "存储放方式不能为空");
        }
        UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId);
        BasicExperimentStuff stuff = BeanCopyUtils.copyBean(insertParam, BasicExperimentStuff.class);
        //设置必要参数
        LocalDateTime date  = LocalDateTime.now();
        stuff.setCreateTime(date);
        stuff.setCreateByUserId(userInfoById.getId());
        stuff.setUpdateTime(date);
        stuff.setUpdateByUserId(userInfoById.getId());
        stuff.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue());
 
        BasicExperimentStuff insertResult = stuffRepository.save(stuff);
 
        return BeanCopyUtils.copyBean(insertResult, StuffInsertDTO.class);
    }
 
    /**
     * 基础实验耗材管理 - 查询
     */
    @Override
    public SearchResult<StuffQueryDTO> selectBasicExperimentStuffPage(Long currentUserId, BasicExperimentStuffAppQueryBO 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.getStuffName() == ""){
            queryParam.setStuffName(null);
        }
        if (queryParam.getStuffCode()  == ""){
            queryParam.setStuffCode(null);
        }
 
        SearchResult searchResult = new SearchResult<>();
        searchResult.setPageIndex(queryParam.getPageIndex());
        searchResult.setPageSize(queryParam.getPageSize());
 
        UserInfoDomainDTO user = userDomainService.getUserById(currentUserId);
        int roleTag = GetRoleTagUtils.GetRoleTagUtils(user);
 
        //封装查询参数
        Specification<BasicExperimentStuff> specification = new Specification<BasicExperimentStuff>() {
            @Override
            public Predicate toPredicate(Root<BasicExperimentStuff> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
//                if (queryParam.getSiteName() != null && !queryParam.getSiteName().equals("")){
//                    predicateList.add(criteriaBuilder.equal(root.get("siteName"), queryParam.getSiteName()));
//                }
                if (queryParam.getStuffName() != null && !queryParam.getStuffName().equals("")){
                    predicateList.add(criteriaBuilder.like(root.get("stuffName"),"%"+queryParam.getStuffName()+"%"));
                }
                if (queryParam.getStuffCode() != null && !queryParam.getStuffCode().equals("")){
                    predicateList.add(criteriaBuilder.like(root.get("stuffCode"), "%"+queryParam.getStuffCode()+"%"));
                }
                if (queryParam.getStuffType() != null && !queryParam.getStuffType().equals("")){
                    predicateList.add(criteriaBuilder.equal(root.get("stuffType"), queryParam.getStuffType()));
                }
 
                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(queryParam.getPageIndex() - 1, queryParam.getPageSize(), Sort.Direction.DESC, "updateTime");
        Page<BasicExperimentStuff> pageResult = stuffRepository.findAll(specification, pageParam);
 
        List<StuffQueryDTO> stuffQueryDTOS = BeanCopyUtils.copyBeanList(pageResult.getContent(), StuffQueryDTO.class);
        List<UserInfoDomainDTO> userList = userDomainService.getUserList();
        for (StuffQueryDTO stuffQueryDTO : stuffQueryDTOS) {
            for (UserInfoDomainDTO userInfo : userList) {
                if (userInfo.getId() == stuffQueryDTO.getCreateByUserId()){
                    stuffQueryDTO.setCreateByUserName(userInfo.getRealName());
                }
                if (userInfo.getId() == stuffQueryDTO.getUpdateByUserId()){
                    stuffQueryDTO.setUpdateByUserName(userInfo.getRealName());
                }
            }
        }
 
        searchResult.setData(stuffQueryDTOS);
        searchResult.setTotal(pageResult.getTotalElements());
        return searchResult;
    }
 
    /**
     * 基础实验耗材管理 - 修改
     */
    @Override
    public StuffUpdateDTO updateBasicExperimentSite(Long currentUserId, BasicExperimentStuffAppUpdateBO 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.getStuffName())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料名称不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getStuffCode())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料编号不能为空");
        }
        if (ObjectUtils.isEmpty(updateParam.getStuffType())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型不能为空");
        }
        if (!(updateParam.getStuffType() == 1 || updateParam.getStuffType() == 2)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型不符合要求");
        }
        if (ObjectUtils.isEmpty(updateParam.getStuffUnit())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不能为空");
        }
        if (!(updateParam.getStuffUnit() == 1 || updateParam.getStuffUnit() == 2 || updateParam.getStuffUnit() == 3 || updateParam.getStuffUnit() == 4)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不符合要求");
        }
        if (ObjectUtils.isEmpty(updateParam.getStuffStorage())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "存储放方式不能为空");
        }
        BasicExperimentStuff stuffByName = stuffRepository.getStuffByName(updateParam.getStuffName());
//        if (!ObjectUtils.isEmpty(stuffByName) && !stuffByName.getId().equals(updateParam.getId())){
//            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "该材料已存在");
//        }
        UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId);
        BasicExperimentStuff stuffById = stuffRepository.getStuffById(updateParam.getId());
        BasicExperimentStuff stuff = BeanCopyUtils.copyBean(stuffById, BasicExperimentStuff.class);
        //设置必要参数
        LocalDateTime date = LocalDateTime.now();
 
        stuff.setUpdateTime(date);
        stuff.setUpdateByUserId(userInfoById.getId());
        stuff.setStuffCode(updateParam.getStuffCode());
        stuff.setStuffName(updateParam.getStuffName());
        stuff.setStuffType(updateParam.getStuffType());
        stuff.setStuffStorage(updateParam.getStuffStorage());
        stuff.setStuffUnit(updateParam.getStuffUnit());
 
        BasicExperimentStuff updateResult = stuffRepository.save(stuff);
 
        return BeanCopyUtils.copyBean(updateResult, StuffUpdateDTO.class);
    }
 
    /**
     * 基础实验耗材管理 - 删除
     */
    @Override
    public StuffDeleteDTO deleteBasicExperimentStuff(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(), "请选择正常的内容删除");
        }
        BasicExperimentStuff stuffById = stuffRepository.getStuffById(id);
        if (ObjectUtils.isEmpty(stuffById)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请选择正确的内容删除,或确认该耗材是否已经被删除");
        }
        UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId);
        BasicExperimentStuff stuff = BeanCopyUtils.copyBean(stuffById, BasicExperimentStuff.class);
        //设置必要参数
        LocalDateTime date = LocalDateTime.now();
 
        stuff.setUpdateTime(date);
        stuff.setUpdateByUserId(userInfoById.getId());
        stuff.setDeleteStatus(StatusEnum.DELETED.getCode().byteValue());
 
        BasicExperimentStuff deleteResult = stuffRepository.save(stuff);
 
        return BeanCopyUtils.copyBean(deleteResult, StuffDeleteDTO.class);
    }
 
    /**
     * 基础实验耗材管理 - 列表
     */
    @Override
    public List<StuffListDTO> listBasicExperimentStuff(Long currentUserId) {
 
 
        UserInfoDomainDTO user = userDomainService.getUserById(currentUserId);
        int roleTag = GetRoleTagUtils.GetRoleTagUtils(user);
 
 
        List<BasicExperimentStuff> listResult = new ArrayList<>();
        if (SearchAuthUtils.basicSearchAuth() == 0){
            listResult = stuffRepository.listStuff(currentUserId);
            return BeanCopyUtils.copyBeanList(listResult, StuffListDTO.class);
        }
 
 
        if (roleTag != UserTagEnum.USER_TAG_0.getCode()){
            listResult = stuffRepository.listStuff(currentUserId);
        }else {
            listResult = stuffRepository.listStuffByUserId(currentUserId);
        }
        return BeanCopyUtils.copyBeanList(listResult, StuffListDTO.class);
    }
 
    /**
     * 基础实验耗材管理 - 查询 by id
     */
    @Override
    public StuffQueryDTO getBasicExperimentStuffById(Long id) {
 
        if (ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数不能为空");
        }
 
        BasicExperimentStuff stuffById = stuffRepository.getStuffById(id);
 
        if (ObjectUtils.isEmpty(stuffById)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "查询的耗材不存在");
        }
        return BeanCopyUtils.copyBean(stuffById, StuffQueryDTO.class);
    }
 
    /**
     * 基础实验耗材管理  - 通过id列表查询
     */
    @Override
    public List<StuffQueryDTO> getBasicExperimentStuffByIdList(List<Long> ids) {
        if (ids.size() < 1){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "请求参数不能为空");
        }
 
        List<BasicExperimentStuff> listResult = stuffRepository.batchById(ids);
        if (listResult.size() < 1){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "查询结果为空");
        }
        return BeanCopyUtils.copyBeanList(listResult, StuffQueryDTO.class);
    }
}