刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
package jnpf.limsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.base.service.SuperServiceImpl;
import jnpf.enums.LimsSampleEnum;
import jnpf.limsEntity.*;
import jnpf.limsMapper.LimsApplyTestMapper;
import jnpf.limsService.*;
import jnpf.util.DateUtil;
import jnpf.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
 
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class LimsApplyTestServiceImpl extends SuperServiceImpl<LimsApplyTestMapper, LimsApplyTestEntity> implements LimsApplyTestService {
 
    @Autowired
    private LimsApplyTestItemService limsApplyTestItemService;
    @Autowired
    private LimsSampleInfoService limsSampleInfoService;
    @Autowired
    private LimsSamplingRecordService limsSamplingRecordService;
    @Autowired
    private LimsTestTaskService limsTestTaskService;
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    /**
     * 根据检验信息生成样品信息
     * @param limsApplyTestEntity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createSampleInfo(LimsApplyTestEntity limsApplyTestEntity) {
 
        LimsSampleInfoEntity limsSampleInfoEntity = new LimsSampleInfoEntity();
        LimsSamplingRecordEntity limsSamplingRecordEntity = new LimsSamplingRecordEntity();
 
        // 获取样品编号
        String sampleCode = creatSampleCode();
        limsApplyTestEntity.setSampleCode(sampleCode);
 
        // 查询sampleId 是否有值 ,避免重复生成
        String sampleId = limsApplyTestEntity.getSampleId();
        if (StringUtil.isNotEmpty(sampleId)) {
            limsSampleInfoEntity = limsSampleInfoService.getById(sampleId);
        }
 
        // 复制信息
        BeanUtils.copyProperties(limsApplyTestEntity, limsSampleInfoEntity,"fId");
        BeanUtils.copyProperties(limsApplyTestEntity, limsSamplingRecordEntity,"fId");
 
        // 回写请检ID
        limsSampleInfoEntity.setApplyId(limsApplyTestEntity.getId());
 
        //获取对应流程信息
        LimsSampleEnum sampleRecord = LimsSampleEnum.SAMPLE_RECORD;
        // 请检已完成 状态为待取样
        limsSampleInfoEntity.setSampleStatus(String.valueOf(sampleRecord.getCode()));
        //limsSamplingRecordEntity.setApplyId(limsApplyTestEntity.getFId());
 
        // 保存对应信息
        limsSampleInfoService.save(limsSampleInfoEntity);
 
        // 回写SampleId
        limsSamplingRecordEntity.setSampleId(limsSampleInfoEntity.getId());
        limsSamplingRecordService.save(limsSamplingRecordEntity);
 
        // 请检已完成 回写SampleId
        limsApplyTestEntity.setSampleId(limsSampleInfoEntity.getId());
        limsApplyTestEntity.setApplyStatus("2");
        this.updateById(limsApplyTestEntity);
 
        // 生成对应的检验任务
        LambdaQueryWrapper<LimsApplyTestItemEntity> itemQueryWrapper = new LambdaQueryWrapper<>();
        itemQueryWrapper.eq(LimsApplyTestItemEntity::getFForeignId, limsApplyTestEntity.getId());
        // 获取检验项目信息
        List<LimsApplyTestItemEntity> itemList = limsApplyTestItemService.list(itemQueryWrapper);
        // 赋值
        List<LimsTestTaskEntity> taskEntityList = itemList.stream().map(item -> {
            LimsTestTaskEntity limsTestTaskEntity = new LimsTestTaskEntity();
            BeanUtils.copyProperties(item, limsTestTaskEntity, "id");
            BeanUtils.copyProperties(limsApplyTestEntity, limsTestTaskEntity, "id");
            limsTestTaskEntity.setCheckStatus(String.valueOf(sampleRecord.getItemCode()));
            return limsTestTaskEntity;
        }).collect(Collectors.toList());
 
        // 将 null 值排在最后面
        taskEntityList = taskEntityList.stream()
                .sorted(Comparator.nullsLast(
                        Comparator.comparing(LimsTestTaskEntity::getItemSort)))
                .collect(Collectors.toList());
 
        // 保存信息
        limsTestTaskService.saveBatch(taskEntityList);
    }
 
    public String creatSampleCode() {
        String sql = "SELECT MAX(SAMPLE_CODE) FROM lims_sample_info";
 
        String maxCode;
        try {
            maxCode = jdbcTemplate.queryForObject(sql, String.class);
        } catch (EmptyResultDataAccessException e) {
            maxCode = null;
        }
 
        // 获取年份
        String year = DateUtil.getNow().split("-")[0];
 
        if (StringUtil.isEmpty(maxCode)) {
            maxCode = "SAMPLE-" + year + "-001";
        } else {
            // 判断是否为新的一年
            if (year.equals(maxCode.split("-")[1])){
                maxCode = "SAMPLE-" + year + "-" + String.format("%03d", Integer.parseInt(maxCode.split("-")[2] + 1));
            } else {
                maxCode = "SAMPLE-" + year + "-001";
            }
        }
        return maxCode;
    }
}