tuqin
2026-11-03 9494075e6d9d2a4b2b884b4ed740dee0c434734c
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
package cn.iocoder.yudao.module.trade.service.foreign;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.trade.controller.admin.foreign.vo.TradePortBatchPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.foreign.vo.TradePortBatchSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.foreign.TradePortBatchDO;
import cn.iocoder.yudao.module.trade.dal.mysql.foreign.TradePortBatchMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.math.BigDecimal;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
 
/**
 * 港口入库批次 Service 实现类
 *
 * @author mom
 */
@Service
@Validated
public class TradePortBatchServiceImpl implements TradePortBatchService {
 
    @Resource
    private TradePortBatchMapper batchMapper;
 
    @Override
    public Long createBatch(TradePortBatchSaveReqVO reqVO) {
        if (batchMapper.selectByCode(reqVO.getCode()) != null) {
            throw exception(PORT_BATCH_CODE_EXISTS);
        }
        TradePortBatchDO batch = BeanUtils.toBean(reqVO, TradePortBatchDO.class);
        batch.setMatchedCount(BigDecimal.ZERO);
        batchMapper.insert(batch);
        return batch.getId();
    }
 
    @Override
    public void updateBatch(TradePortBatchSaveReqVO reqVO) {
        TradePortBatchDO old = batchMapper.selectById(reqVO.getId());
        if (old == null) {
            throw exception(PORT_BATCH_NOT_EXISTS);
        }
        TradePortBatchDO sameCode = batchMapper.selectByCode(reqVO.getCode());
        if (sameCode != null && !sameCode.getId().equals(reqVO.getId())) {
            throw exception(PORT_BATCH_CODE_EXISTS);
        }
        TradePortBatchDO update = BeanUtils.toBean(reqVO, TradePortBatchDO.class);
        update.setMatchedCount(old.getMatchedCount()); // 占用数不因编辑而改变
        batchMapper.updateById(update);
    }
 
    @Override
    public void deleteBatch(Long id) {
        if (batchMapper.selectById(id) == null) {
            throw exception(PORT_BATCH_NOT_EXISTS);
        }
        batchMapper.deleteById(id);
    }
 
    @Override
    public TradePortBatchDO getBatch(Long id) {
        return batchMapper.selectById(id);
    }
 
    @Override
    public PageResult<TradePortBatchDO> getBatchPage(TradePortBatchPageReqVO reqVO) {
        return batchMapper.selectPage(reqVO);
    }
 
    @Override
    public List<TradePortBatchDO> getBatchList() {
        return batchMapper.selectList();
    }
 
    @Override
    public void occupy(String code, BigDecimal count) {
        if (code == null || code.isEmpty() || count == null) {
            return;
        }
        TradePortBatchDO batch = batchMapper.selectByCode(code);
        if (batch == null) {
            throw exception(PORT_BATCH_NOT_EXISTS);
        }
        BigDecimal matched = batch.getMatchedCount() == null ? BigDecimal.ZERO : batch.getMatchedCount();
        BigDecimal total = batch.getTotalCount() == null ? BigDecimal.ZERO : batch.getTotalCount();
        if (matched.add(count).compareTo(total) > 0) {
            throw exception(PORT_BATCH_EXCEED, code);
        }
        batchMapper.updateById(new TradePortBatchDO().setId(batch.getId()).setMatchedCount(matched.add(count)));
    }
 
    @Override
    public void release(String code, BigDecimal count) {
        if (code == null || code.isEmpty() || count == null) {
            return;
        }
        TradePortBatchDO batch = batchMapper.selectByCode(code);
        if (batch == null) {
            return;
        }
        BigDecimal matched = batch.getMatchedCount() == null ? BigDecimal.ZERO : batch.getMatchedCount();
        BigDecimal newMatched = matched.subtract(count);
        if (newMatched.compareTo(BigDecimal.ZERO) < 0) {
            newMatched = BigDecimal.ZERO;
        }
        batchMapper.updateById(new TradePortBatchDO().setId(batch.getId()).setMatchedCount(newMatched));
    }
 
}