刘光辉
昨天 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
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
package jnpf.limsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.audit.sdk.annotation.AuditLog;
import jnpf.base.service.SuperServiceImpl;
import jnpf.exception.DataException;
import jnpf.limsEntity.LimsShebeiYiqiJiliangEntity;
import jnpf.limsEntity.LimsShebeiYiqiXinxiEntity;
import jnpf.limsEntity.LimsShebeiYiqiYanzhengEntity;
import jnpf.limsMapper.LimsShebeiYiqiJiliangMapper;
import jnpf.limsMapper.LimsShebeiYiqiXinxiMapper;
import jnpf.limsMapper.LimsShebeiYiqiYanzhengMapper;
import jnpf.limsService.LimsShebeiYiqiXinxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
@Service
public class LimsShebeiYiqiXinxiServiceImpl
        extends SuperServiceImpl<LimsShebeiYiqiXinxiMapper, LimsShebeiYiqiXinxiEntity>
        implements LimsShebeiYiqiXinxiService {
 
    private static final String BIZ_STATUS_YIBAOFEI = "yibaofei";
    private static final String XINGNENG_ZHUANGTAI_ZHUNYONG = "zhunyong";
    @Autowired
    private LimsShebeiYiqiYanzhengMapper limsShebeiYiqiYanzhengMapper;
 
    @Autowired
    private LimsShebeiYiqiJiliangMapper limsShebeiYiqiJiliangMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "SCRAP", label = "仪器报废", bizType = "SHEBEI_YIQI",
            bizCodeExpr = "#yiqiId", bizCodeRequired = false, targetTable = "lims_shebei_yiqi_xinxi",
            targetIdExpr = "#yiqiId", diff = true, entityClass = LimsShebeiYiqiXinxiEntity.class)
    public void baofei(String yiqiId) throws DataException {
        String id = trimToEmpty(yiqiId);
        if (id.isEmpty()) {
            throw new DataException("仪器ID(yiqi_id)不能为空");
        }
 
        LimsShebeiYiqiXinxiEntity yiqi = this.getById(id);
        if (yiqi == null) {
            throw new DataException("仪器信息不存在:" + id);
        }
 
        LimsShebeiYiqiXinxiEntity update = new LimsShebeiYiqiXinxiEntity();
        update.setId(id);
        update.setBizStatus(BIZ_STATUS_YIBAOFEI);
        if (!this.updateById(update)) {
            throw new DataException("仪器报废失败,请刷新后重试");
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "ENABLE", label = "仪器批量启用", bizType = "SHEBEI_YIQI",
            bizCodeExpr = "", bizCodeRequired = false, targetTable = "lims_shebei_yiqi_xinxi",
            targetIdExpr = "#yiqiIds[0]", diff = true,
            entityClass = LimsShebeiYiqiXinxiEntity.class)
    public int qiyong(List<String> yiqiIds) throws DataException {
        List<String> ids = normalizeIds(yiqiIds);
        if (ids.isEmpty()) {
            throw new DataException("仪器ID列表(yiqi_ids)不能为空");
        }
 
        LimsShebeiYiqiXinxiEntity update = new LimsShebeiYiqiXinxiEntity();
        update.setXingnengZhuangtai(XINGNENG_ZHUANGTAI_ZHUNYONG);
        LambdaUpdateWrapper<LimsShebeiYiqiXinxiEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(LimsShebeiYiqiXinxiEntity::getId, ids);
        int updated = this.baseMapper.update(update, wrapper);
        if (updated <= 0) {
            throw new DataException("未找到可启用的仪器信息");
        }
        return updated;
    }
 
    @Override
    public void checkTodayJiaoyan(String yiqiId, LocalDateTime jiaoyanShijian) throws DataException {
        String id = trimToEmpty(yiqiId);
        if (id.isEmpty()) {
            throw new DataException("仪器ID(yiqi_id)不能为空");
        }
        if (jiaoyanShijian == null) {
            throw new DataException("校准时间(jiaoyan_shijian)不能为空");
        }
 
        LocalDateTime startTime = jiaoyanShijian.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        if (this.baseMapper.existsTodayJiaoyan(id, startTime, endTime)) {
            throw new DataException("该仪器设备今日已校准过,不需要重复校准!");
        }
    }
 
    @Override
    public void checkShiyong(String yiqiId) throws DataException {
        String id = trimToEmpty(yiqiId);
        if (id.isEmpty()) {
            throw new DataException("仪器ID(yiqi_id)不能为空");
        }
 
        LocalDateTime startTime = LocalDate.now().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        if (this.baseMapper.requiresTodayJiaoyan(id, startTime, endTime)) {
            throw new DataException("该仪器设备今日需要校准,请进行日常校准!");
        }
        if (this.baseMapper.existsUsingYiqi(id)) {
            throw new DataException("该仪器设备正在使用,请稍后!");
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "VERIFY_CONFIRM", label = "确认仪器验证", bizType = "SHEBEI_YIQI",
            bizCodeExpr = "#yanzhengId", bizCodeRequired = false, targetTable = "lims_shebei_yiqi_xinxi",
            targetIdExpr = "T(jnpf.limsService.support.LimsAuditTargetIds).yiqiByVerification(#yanzhengId)",
            diff = true, entityClass = LimsShebeiYiqiXinxiEntity.class)
    public void confirmYanzheng(String yanzhengId) throws DataException {
        String id = trimToEmpty(yanzhengId);
        if (id.isEmpty()) {
            throw new DataException("仪器验证ID(yanzheng_id)不能为空");
        }
 
        LimsShebeiYiqiYanzhengEntity yanzheng = limsShebeiYiqiYanzhengMapper.selectById(id);
        if (yanzheng == null) {
            throw new DataException("仪器验证记录不存在:" + id);
        }
 
        LimsShebeiYiqiXinxiEntity update = new LimsShebeiYiqiXinxiEntity();
        update.setShangciYanzhengShijian(yanzheng.getShangciYanzhengShijian());
        update.setXiaciYanzhengShijian(yanzheng.getXiaciYanzhengShijian());
        update.setYanzhengZhouqi(yanzheng.getYanzhengZhouqi());
        update.setYanzhengShijianDanwei(yanzheng.getYanzhengShijianDanwei());
        update.setTiqianTixingShijian(yanzheng.getTiqianTixingShijian());
        update.setTixingShijianDanwei(yanzheng.getTixingShijianDanwei());
        update.setYanzhengTixingRiqi(yanzheng.getYanzhengTixingRiqi());
        updateYiqiXinxi(yanzheng.getYiqiId(), update, id, "仪器验证记录");
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "MEASURE_CONFIRM", label = "确认仪器计量", bizType = "SHEBEI_YIQI",
            bizCodeExpr = "#jiliangId", bizCodeRequired = false, targetTable = "lims_shebei_yiqi_xinxi",
            targetIdExpr = "T(jnpf.limsService.support.LimsAuditTargetIds).yiqiByMeasurement(#jiliangId)",
            diff = true, entityClass = LimsShebeiYiqiXinxiEntity.class)
    public void confirmJiliang(String jiliangId) throws DataException {
        String id = trimToEmpty(jiliangId);
        if (id.isEmpty()) {
            throw new DataException("仪器计量ID(jiliang_id)不能为空");
        }
 
        LimsShebeiYiqiJiliangEntity jiliang = limsShebeiYiqiJiliangMapper.selectById(id);
        if (jiliang == null) {
            throw new DataException("仪器计量记录不存在:" + id);
        }
 
        LimsShebeiYiqiXinxiEntity update = new LimsShebeiYiqiXinxiEntity();
        update.setShangciJiliangShijian(jiliang.getShangciJiliangShijian());
        update.setXiaciJiliangShijian(jiliang.getXiaciJiliangShijian());
        update.setJiliangZhouqi(jiliang.getJiliangZhouqi());
        update.setJiliangShijianDanwei(jiliang.getJiliangShijianDanwei());
        update.setTiqianTixingShijian(jiliang.getTiqianTixingShijian());
        update.setTixingShijianDanwei(jiliang.getTixingShijianDanwei());
        update.setJiliangTixingRiqi(jiliang.getJiliangTixingRiqi());
        updateYiqiXinxi(jiliang.getYiqiId(), update, id, "仪器计量记录");
    }
 
    private void updateYiqiXinxi(String yiqiId, LimsShebeiYiqiXinxiEntity update,
                                 String recordId, String recordDescription) throws DataException {
        yiqiId = trimToEmpty(yiqiId);
        if (yiqiId.isEmpty()) {
            throw new DataException(recordDescription + "未关联仪器(yiqi_id):" + recordId);
        }
        if (this.getById(yiqiId) == null) {
            throw new DataException("关联仪器信息不存在:" + yiqiId);
        }
 
        update.setId(yiqiId);
        if (!this.updateById(update)) {
            throw new DataException(recordDescription + "同步仪器信息失败,请刷新后重试");
        }
    }
 
    private List<String> normalizeIds(List<String> yiqiIds) {
        Set<String> ids = new LinkedHashSet<>();
        if (yiqiIds == null) {
            return new ArrayList<>(ids);
        }
        for (String yiqiId : yiqiIds) {
            String id = trimToEmpty(yiqiId);
            if (!id.isEmpty()) {
                ids.add(id);
            }
        }
        return new ArrayList<>(ids);
    }
 
    private String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
 
}