16639036659
2023-08-28 7a9a4f4764b0a402e3d0b4a1dafc7ecf96cb1583
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
package com.gkhy.labRiskManage.domain.experiment.service.impl;
 
import com.gkhy.labRiskManage.application.experiment.dto.bo.ExperimentAndStuffAppInsertBO;
import com.gkhy.labRiskManage.commons.enums.ResultCode;
import com.gkhy.labRiskManage.commons.enums.StatusEnum;
import com.gkhy.labRiskManage.commons.exception.BusinessException;
import com.gkhy.labRiskManage.domain.basic.model.dto.StuffQueryDTO;
import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentStuffService;
import com.gkhy.labRiskManage.domain.experiment.entity.ExperimentAndStuff;
import com.gkhy.labRiskManage.domain.experiment.repository.jpa.ExperimentAndStuffRepository;
import com.gkhy.labRiskManage.domain.experiment.service.ExperimentAndStuffService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
 
/**
 * 实验与耗材
 */
@Service
public class ExperimentAndStuffServiceImpl implements ExperimentAndStuffService {
    @Autowired
    private ExperimentAndStuffRepository repository;
    @Autowired
    private BasicExperimentStuffService basicExperimentStuffService;
 
    @Override
    public boolean saveBatch(Long currentUserId, List<ExperimentAndStuffAppInsertBO> stuffAppInsertBOList) {
        Boolean flag = false;
        if(CollectionUtils.isEmpty(stuffAppInsertBOList)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择试验实验材料!");
        }
        //获取材料
        List<Long> stuffIdList = stuffAppInsertBOList.stream().map(ExperimentAndStuffAppInsertBO::getStuffId).collect(Collectors.toList());
        List<StuffQueryDTO> basicExperimentStuffByIdList = basicExperimentStuffService.getBasicExperimentStuffByIdList(stuffIdList);
        for(ExperimentAndStuffAppInsertBO experimentAndStuffAppInsertBO:stuffAppInsertBOList){
            if(ObjectUtils.isEmpty(experimentAndStuffAppInsertBO.getStuffId())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"材料主键不可为空!");
            }
            if(ObjectUtils.isEmpty(experimentAndStuffAppInsertBO.getStuffUseCount())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"材料数量不可为空!");
            }
            if(ObjectUtils.isEmpty(experimentAndStuffAppInsertBO.getExperimentId())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验主键不可为空!");
            }
            List<StuffQueryDTO> selectList = basicExperimentStuffByIdList.stream().filter(item -> item.getId().equals(experimentAndStuffAppInsertBO.getStuffId())).collect(Collectors.toList());
            if(selectList.size() == 0){
                throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"未查询到实验材料!");
            }
        }
        List<ExperimentAndStuff> experimentAndStuffInsertList = new ArrayList<>();
        for (ExperimentAndStuffAppInsertBO stuffInsertBO:stuffAppInsertBOList){
            ExperimentAndStuff stuff = new ExperimentAndStuff();
            BeanUtils.copyProperties(stuffInsertBO,stuff);
            stuff.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue());
            stuff.setCreateByUserId(currentUserId);
            stuff.setUpdateByUserId(currentUserId);
            experimentAndStuffInsertList.add(stuff);
        }
        List<ExperimentAndStuff> experimentAndStuffList = repository.saveAll(experimentAndStuffInsertList);
        if(!CollectionUtils.isEmpty(experimentAndStuffList)){
            flag = true;
        }
        return flag;
    }
 
    @Override
    public void deleteByExperimentId(Long id, Long currentUserId) {
        if(ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验主键不可为空!");
        }
        repository.deleteByExperimentId(id);
 
    }
}