刘光辉
昨天 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
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
package jnpf.limsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.audit.sdk.annotation.AuditLog;
import jnpf.base.ActionResult;
import jnpf.base.BillRuleApi;
import jnpf.base.UserInfo;
import jnpf.exception.DataException;
import jnpf.limsEntity.*;
import jnpf.limsMapper.*;
import jnpf.limsService.LimsWendingxingKaocaJihuaZhouqiService;
import jnpf.limsService.LimsQingyandanService;
import jnpf.limsService.ZhiliangBiaozhunGuanliService;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
@Service
public class LimsWendingxingKaocaJihuaZhouqiServiceImpl
        implements LimsWendingxingKaocaJihuaZhouqiService {
 
    private static final String JIANYAN_LIUSHUIHAO_RULE = "jianyan_liushuihao";
 
    @Autowired
    private LimsWendingxingKaocaJihuaBiangengMapper biangengMapper;
 
    @Autowired
    private LimsWendingxingKaocaJihuaBiangengZhouqiMapper biangengZhouqiMapper;
 
    @Autowired
    private LimsWendingxingKaocaJihuaZhouqiMapper jihuaZhouqiMapper;
 
    @Autowired
    private LimsQingyandanService qingyandanService;
 
    @Autowired
    private ZhiliangBiaozhunGuanliService zhiliangBiaozhunGuanliService;
 
    @Autowired
    private LimsWendingxingKaocaJihuaMapper jihuaMapper;
 
    @Autowired
    private YangpinGuanliMapper yangpinGuanliMapper;
 
    @Autowired
    private BillRuleApi billRuleApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "EXECUTE", label = "稳定性考察计划检测执行", bizType = "WENDINGXING",
            bizCodeExpr = "#jihuaZhouqiId", bizCodeRequired = false,
            targetTable = "lims_wendingxing_kaoca_jihua_zhouqi", targetIdExpr = "#jihuaZhouqiId",
            diff = true, entityClass = LimsWendingxingKaocaJihuaZhouqiEntity.class)
    public String executeJiance(String jihuaZhouqiId, String zhixingBizSign) {
        UserInfo userInfo = UserProvider.getUser();
        if (userInfo == null || !StringUtils.hasText(userInfo.getUserId())) {
            throw new DataException("当前未登录");
        }
 
        Date now = new Date();
        LambdaUpdateWrapper<LimsWendingxingKaocaJihuaZhouqiEntity> update = new LambdaUpdateWrapper<>();
        update.eq(LimsWendingxingKaocaJihuaZhouqiEntity::getId, jihuaZhouqiId)
                .and(wrapper -> wrapper.isNull(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark)
                        .or().eq(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, 0))
                .and(wrapper -> wrapper.isNull(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouYizhixing)
                        .or().ne(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouYizhixing, "yes"))
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouYizhixing, "yes")
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhixingBizSign, zhixingBizSign)
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhixingShijian, now);
        int updated = jihuaZhouqiMapper.update(null, update);
        if (updated == 0) {
            LambdaQueryWrapper<LimsWendingxingKaocaJihuaZhouqiEntity> existing =
                    new LambdaQueryWrapper<>();
            existing.eq(LimsWendingxingKaocaJihuaZhouqiEntity::getId, jihuaZhouqiId)
                    .and(wrapper -> wrapper
                            .isNull(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark)
                            .or().eq(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, 0));
            if (jihuaZhouqiMapper.selectCount(existing) > 0) {
                throw new DataException("稳定性考察计划周期已执行,请勿重复操作");
            }
            throw new DataException("稳定性考察计划周期不存在");
        }
 
        LimsWendingxingJianceZhixingVo jihuaZhouqi =
                jihuaZhouqiMapper.selectJianceZhixing(jihuaZhouqiId);
        if (jihuaZhouqi == null) {
            throw new DataException("稳定性样品还未取样");
        }
 
 
        QueryWrapper<LimsWendingxingKaocaJihuaEntity> query = new QueryWrapper<>();
        query.eq("f_id", jihuaZhouqi.getKaocaJihuaId());
        query.and(w -> w.isNull("f_delete_mark").or().ne("f_delete_mark", 1));
        LimsWendingxingKaocaJihuaEntity jihuaEntity = jihuaMapper.selectOne(query);
        if (jihuaEntity == null) {
            throw new DataException("稳定性计划不存在");
        }
 
        ZhiliangBiaozhunGuanliEntity zhiliangBiaozhun =
                zhiliangBiaozhunGuanliService.getById(jihuaZhouqi.getZhiliangBiaozhunId());
        if (zhiliangBiaozhun == null) {
            throw new DataException("稳定性样品关联的质量标准不存在");
        }
 
        QueryWrapper<YangpinGuanliEntity> yangpinQuery = new QueryWrapper<>();
        yangpinQuery.eq("f_id", zhiliangBiaozhun.getYangpinId());
        yangpinQuery.and(w -> w.isNull("f_delete_mark").or().ne("f_delete_mark", 1));
        YangpinGuanliEntity yangpinEntity = yangpinGuanliMapper.selectOne(yangpinQuery);
        if (yangpinEntity == null) {
            throw new DataException("样品不存在");
        }
 
        LimsQingyandanEntity qingyandan = buildQingyandan(jihuaZhouqi, userInfo, zhiliangBiaozhun, jihuaEntity, yangpinEntity);
        if (!qingyandanService.save(qingyandan)) {
            throw new DataException("稳定性请验单生成失败");
        }
        qingyandanService.wendingxingSubmit(qingyandan);
 
        return qingyandan.getId();
    }
 
    private LimsQingyandanEntity buildQingyandan(LimsWendingxingJianceZhixingVo jihuaZhouqi,
                                                  UserInfo userInfo,
                                                 ZhiliangBiaozhunGuanliEntity zhiliangBiaozhun,
                                                 LimsWendingxingKaocaJihuaEntity jihuaEntity,
                                                 YangpinGuanliEntity yangpinEntity) {
        LimsQingyandanEntity qingyandan = new LimsQingyandanEntity();
        qingyandan.setId(RandomUtil.uuId());
        qingyandan.setJianyanLiushuihao(nextJianyanLiushuihao());
        qingyandan.setBizType(jihuaZhouqi.getLeixin());
        qingyandan.setChanpinMingcheng(jihuaZhouqi.getYangpinMingcheng());
        qingyandan.setChanpinId(jihuaZhouqi.getYangpinId());
        qingyandan.setChanpinBianma(jihuaZhouqi.getYangpinBianhao());
        qingyandan.setGuige(jihuaZhouqi.getGuige());
        qingyandan.setChucunTiaojian(yangpinEntity.getCucunTiaojian());
        qingyandan.setPihao(jihuaZhouqi.getPihao());
        qingyandan.setJianceXiangmu(jihuaZhouqi.getJianchaXiangmuIds());
        qingyandan.setQuyangren(jihuaZhouqi.getQuyangren());
        qingyandan.setQuyangRiqi(jihuaZhouqi.getQuyangRiqi());
        qingyandan.setQingyanRen(userInfo.getUserId());
        qingyandan.setQingyanRiqi(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()));
        qingyandan.setJianyanLiang(jihuaZhouqi.getShiquShuliang());
        qingyandan.setJianyanLiangShiji(jihuaZhouqi.getShiquShuliang());
        qingyandan.setJianceLiuyangLiang("0");
        qingyandan.setJianceLiuyangLiangShiji("0");
        qingyandan.setLiuyangLiang("0");
        qingyandan.setLiuyangLiangShiji("0");
        qingyandan.setQuyangDanwei(jihuaZhouqi.getYangpinDanwei());
        qingyandan.setLiuyangDanwei(jihuaZhouqi.getYangpinDanwei());
        qingyandan.setZhouqi(jihuaZhouqi.getZhouqi().toPlainString());
        qingyandan.setZhouqiDanwei(jihuaZhouqi.getZhouqiDanwei());
        qingyandan.setQingyanFenlei(QingyanFenlei.WENDINGXING.getCode());
        qingyandan.setWendingxingJiluId(jihuaZhouqi.getKaocaJihuaId());
        qingyandan.setQuyangStatus(BizStatus.FETCHED.getCode());
        qingyandan.setBizStatus(BizStatus.CONFIRMED.getCode());
        qingyandan.setJieyangStatus(BizStatus.PENDING_RECEIVE.getCode());
        qingyandan.setJianceFangshi(JianceFangshi.QUANJIAN.getCode());
        qingyandan.setJianyanBiaozhun(jihuaZhouqi.getZhiliangBiaozhunId());
        qingyandan.setShifouShixiaoxingYaoqiu(zhiliangBiaozhun.getShifouShixiaoxingYaoqiu());
        qingyandan.setCreatorUserId(userInfo.getUserId());
        qingyandan.setCreatorTime(new Date());
        qingyandan.setWendingxingZhouqiId(jihuaZhouqi.getId());
        qingyandan.setWendingxingBianhao(jihuaEntity.getBianhao());
        qingyandan.setChangqiFangzhiTiaojian(jihuaZhouqi.getChangqiFangzhiTiaojian());
 
        return qingyandan;
    }
 
    private String nextJianyanLiushuihao() {
        ActionResult<String> result = billRuleApi.getBillNumber(JIANYAN_LIUSHUIHAO_RULE);
        String liushuihao = result == null ? null : result.getData();
        if (StringUtil.isEmpty(liushuihao)) {
            throw new DataException("检验流水号取号失败:单据规则[" + JIANYAN_LIUSHUIHAO_RULE
                    + "]不可用,请确认规则已配置且系统服务在线");
        }
        return liushuihao;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "PLAN_CHANGE_SYNC", label = "同步稳定性考察计划变更周期", bizType = "WENDINGXING",
            bizCodeExpr = "#result > 0 ? #flowId : ''",
            targetTable = "lims_wendingxing_kaoca_jihua_zhouqi",
            targetIdExpr = "T(jnpf.limsService.support.LimsAuditTargetIds).firstWendingxingChangedPeriod(#flowId, #flowTaskId)",
            diff = true, entityClass = LimsWendingxingKaocaJihuaZhouqiEntity.class)
    public int syncByFlow(String flowId, String flowTaskId) {
        LambdaQueryWrapper<LimsWendingxingKaocaJihuaBiangengEntity> biangengQuery = new LambdaQueryWrapper<>();
        biangengQuery.eq(LimsWendingxingKaocaJihuaBiangengEntity::getFlowId, flowId)
                .eq(LimsWendingxingKaocaJihuaBiangengEntity::getFlowTaskId, flowTaskId);
        LimsWendingxingKaocaJihuaBiangengEntity biangeng = biangengMapper.selectOne(biangengQuery);
        if (biangeng == null) {
            throw new DataException("未找到对应的稳定性考察计划变更数据");
        }
 
        String biangengId = biangeng.getId();
        LambdaQueryWrapper<LimsWendingxingKaocaJihuaBiangengZhouqiEntity> query = new LambdaQueryWrapper<>();
        query.eq(LimsWendingxingKaocaJihuaBiangengZhouqiEntity::getBiangengId, biangengId)
                .isNull(LimsWendingxingKaocaJihuaBiangengZhouqiEntity::getDeleteMark);
        List<LimsWendingxingKaocaJihuaBiangengZhouqiEntity> biangengZhouqiList =
                biangengZhouqiMapper.selectList(query);
        if (biangengZhouqiList.isEmpty()) {
            throw new DataException("未找到对应的稳定性考察计划变更周期数据");
        }
 
        UserInfo userInfo = UserProvider.getUser();
        if (userInfo == null || !StringUtils.hasText(userInfo.getUserId())) {
            throw new DataException("当前未登录");
        }
 
        Date now = new Date();
        Set<String> kaocaJihuaIds = new LinkedHashSet<>();
        for (LimsWendingxingKaocaJihuaBiangengZhouqiEntity row : biangengZhouqiList) {
            if (!StringUtils.hasText(row.getKaocaJihuaId())) {
                throw new DataException("变更周期未关联稳定性考察计划:" + row.getId());
            }
            kaocaJihuaIds.add(row.getKaocaJihuaId());
        }
 
        softDeleteJihuaZhouqi(kaocaJihuaIds, userInfo.getUserId(), now);
        List<LimsWendingxingKaocaJihuaBiangengZhouqiEntity> insertList = new ArrayList<>();
        for (LimsWendingxingKaocaJihuaBiangengZhouqiEntity row : biangengZhouqiList) {
            if (StringUtils.hasText(row.getYuanjihuaZhouqiId())) {
                restoreAndUpdateJihuaZhouqi(row);
            } else {
                insertList.add(row);
            }
        }
        insertJihuaZhouqi(insertList);
        return biangengZhouqiList.size();
    }
 
    private void softDeleteJihuaZhouqi(Set<String> kaocaJihuaIds, String userId, Date now) {
        LambdaUpdateWrapper<LimsWendingxingKaocaJihuaZhouqiEntity> update = new LambdaUpdateWrapper<>();
        update.in(LimsWendingxingKaocaJihuaZhouqiEntity::getKaocaJihuaId, kaocaJihuaIds)
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, 1)
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteTime, now)
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteUserId, userId);
        jihuaZhouqiMapper.update(null, update);
    }
 
    private void restoreAndUpdateJihuaZhouqi(LimsWendingxingKaocaJihuaBiangengZhouqiEntity source) {
        LambdaUpdateWrapper<LimsWendingxingKaocaJihuaZhouqiEntity> update = new LambdaUpdateWrapper<>();
        update.eq(LimsWendingxingKaocaJihuaZhouqiEntity::getId, source.getYuanjihuaZhouqiId())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhouqi, source.getZhouqi())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhouqiDanwei, source.getZhouqiDanwei())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getPihao, source.getPihao())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJiahuaAnpaiRiqi, source.getJiahuaAnpaiRiqi())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJianyanLeixingId, source.getJianyanLeixingId())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJianchaXiangmuIds, source.getJianchaXiangmuIds())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJianchaXiangmuMingcheng,
                        source.getJianchaXiangmuMingcheng())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getYangpinDanwei, source.getYangpinDanwei())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getShiquShuliang, source.getShiquShuliang())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getKaocaJihuaId, source.getKaocaJihuaId())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouJixuZhixing, source.getShifouJixuZhixing())
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, null)
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteTime, null)
                .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteUserId, null);
        if (jihuaZhouqiMapper.update(null, update) != 1) {
            throw new DataException("原计划周期数据不存在:" + source.getYuanjihuaZhouqiId());
        }
    }
 
    private void insertJihuaZhouqi(List<LimsWendingxingKaocaJihuaBiangengZhouqiEntity> sourceList) {
        if (sourceList.isEmpty()) {
            return;
        }
        List<LimsWendingxingKaocaJihuaZhouqiEntity> targetList = new ArrayList<>(sourceList.size());
        for (LimsWendingxingKaocaJihuaBiangengZhouqiEntity source : sourceList) {
            LimsWendingxingKaocaJihuaZhouqiEntity target = new LimsWendingxingKaocaJihuaZhouqiEntity();
            target.setId(RandomUtil.uuId());
            target.setKaocaJihuaId(source.getKaocaJihuaId());
            target.setZhouqi(source.getZhouqi());
            target.setZhouqiDanwei(source.getZhouqiDanwei());
            target.setPihao(source.getPihao());
            target.setJiahuaAnpaiRiqi(source.getJiahuaAnpaiRiqi());
            target.setJianyanLeixingId(source.getJianyanLeixingId());
            target.setJianchaXiangmuIds(source.getJianchaXiangmuIds());
            target.setJianchaXiangmuMingcheng(source.getJianchaXiangmuMingcheng());
            target.setYangpinDanwei(source.getYangpinDanwei());
            target.setShiquShuliang(source.getShiquShuliang());
            target.setShifouJixuZhixing(source.getShifouJixuZhixing());
            targetList.add(target);
        }
        jihuaZhouqiMapper.insert(targetList);
    }
}