刘光辉
昨天 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
package jnpf.limsService.impl;
 
import jnpf.audit.sdk.annotation.AuditLog;
 
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.exception.DataException;
import jnpf.limsEntity.LimsJieyangTuihuiEntity;
import jnpf.limsMapper.LimsJieyangTuihuiMapper;
import jnpf.limsService.LimsJieyangTuihuiService;
import jnpf.util.UserProvider;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.Date;
 
@Service
public class LimsJieyangTuihuiServiceImpl
        extends SuperServiceImpl<LimsJieyangTuihuiMapper, LimsJieyangTuihuiEntity>
        implements LimsJieyangTuihuiService {
 
    @Override
    @AuditLog(action = "CREATE", label = "样品退回记录新增", bizType = "JIEYANG",
            bizCodeExpr = "#entity.jianyanLiushuihao", targetTable = "lims_jieyang_tuihui",
            targetIdExpr = "#result?.id", diff = true, entityClass = LimsJieyangTuihuiEntity.class)
    public LimsJieyangTuihuiEntity createTuihui(LimsJieyangTuihuiEntity entity) {
        if (entity.getJianyanLiushuihao() == null || entity.getJianyanLiushuihao().isEmpty()) {
            throw new DataException("检验流水号不能为空");
        }
        if (entity.getJieyangBanzuId() == null || entity.getJieyangBanzuId().isEmpty()) {
            throw new DataException("接样班组 id 不能为空");
        }
        BigDecimal tuihui = parsePositive(entity.getTuihuiShuliang(), "退回数量");
 
        BigDecimal ketuihui = parseOptional(entity.getKetuihuiShuliang());
        if (ketuihui != null && tuihui.compareTo(ketuihui) > 0) {
            throw new DataException("退回数量不能大于可退回数量");
        }
 
        // 结余数量:前端若未传,则按 (ketuihui - tuihui) 兜底
        if ((entity.getJieyuShuliang() == null || entity.getJieyuShuliang().isEmpty())
                && ketuihui != null) {
            entity.setJieyuShuliang(ketuihui.subtract(tuihui).toPlainString());
        }
 
        if (entity.getCunruRen() == null || entity.getCunruRen().isEmpty()) {
            UserInfo userInfo = UserProvider.getUser();
            entity.setCunruRen(userInfo.getUserId());
        }
        if (entity.getCunruShijian() == null) {
            entity.setCunruShijian(new Date());
        }
 
        entity.setDeleteMark(null);
        entity.setDeleteTime(null);
        entity.setDeleteUserId(null);
 
        if (!this.save(entity)) {
            throw new DataException("退回记录保存失败");
        }
        return entity;
    }
 
    private BigDecimal parsePositive(String value, String fieldName) {
        if (value == null || value.isEmpty()) {
            throw new DataException(fieldName + "不能为空");
        }
        try {
            BigDecimal v = new BigDecimal(value.trim());
            if (v.signum() <= 0) {
                throw new DataException(fieldName + "必须大于 0");
            }
            return v;
        } catch (NumberFormatException e) {
            throw new DataException(fieldName + "必须是数字");
        }
    }
 
    private BigDecimal parseOptional(String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        try {
            return new BigDecimal(value.trim());
        } catch (NumberFormatException e) {
            return null;
        }
    }
}