#
vincentlu
2025-12-26 a4a7479ffbee57c642387baeca5783b943bb1af3
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
package com.zy.acs.manager.manager.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.manager.entity.Sta;
import com.zy.acs.manager.manager.entity.StaReserve;
import com.zy.acs.manager.manager.entity.Task;
import com.zy.acs.manager.manager.enums.StaReserveStateType;
import com.zy.acs.manager.manager.enums.StaReserveType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.mapper.StaMapper;
import com.zy.acs.manager.manager.mapper.StaReserveMapper;
import com.zy.acs.manager.manager.service.StaReserveService;
import com.zy.acs.manager.manager.service.StaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.Optional;
 
@Service("staReserveService")
public class StaReserveServiceImpl extends ServiceImpl<StaReserveMapper, StaReserve> implements StaReserveService {
 
    public static final Long RESERVE_EXPIRE_TIME = 30 * 60 * 1000L;
 
    @Autowired
    private StaService staService;
    @Autowired
    private StaMapper staMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StaReserve reserveStaIn(Sta sta, Task task, Integer qty) {
        qty = Optional.ofNullable(qty).orElse(1);
 
        // reserve sta in
        if (staMapper.tryReserveIn(sta.getId(), qty) < qty) {
            throw new BusinessException("Sta[" + sta.getStaNo() + "] can't reserve IN");
        }
        Date now = new Date();
 
        // save staReserve
        StaReserve reserve = new StaReserve();
        reserve.setStaId(sta.getId());
        reserve.setTaskId(task.getId());
        reserve.setType(StaReserveType.IN.toString());
        reserve.setQty(qty);
        reserve.setState(StaReserveStateType.RESERVED.toString());
 
        reserve.setUniqKey(this.generateReserveUniqKey(sta.getId(), task.getId(), StaReserveType.IN));
        reserve.setExpireTime(new Date(now.getTime() + RESERVE_EXPIRE_TIME));
 
        reserve.setStatus(StatusType.ENABLE.val);
        reserve.setCreateTime(now);
        reserve.setUpdateTime(now);
 
        if (!this.save(reserve)) {
            staMapper.releaseReserveIn(sta.getId(), qty);
            throw new BusinessException("reserve record insert failed");
        }
 
        return reserve;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StaReserve reserveStaOut(Sta sta, Task task, Integer qty) {
        qty = Optional.ofNullable(qty).orElse(1);
 
        // reserve sta out
        if (staMapper.tryReserveOut(sta.getId(), qty) < qty) {
            throw new BusinessException("Sta[" + sta.getStaNo() + "] can't reserve OUT");
        }
 
        Date now = new Date();
 
        // save staReserve
        StaReserve reserve = new StaReserve();
        reserve.setStaId(sta.getId());
        reserve.setTaskId(task.getId());
        reserve.setType(StaReserveType.OUT.toString());
        reserve.setQty(qty);
        reserve.setState(StaReserveStateType.RESERVED.toString());
 
        reserve.setUniqKey(this.generateReserveUniqKey(sta.getId(), task.getId(), StaReserveType.OUT));
        reserve.setExpireTime(new Date(now.getTime() + RESERVE_EXPIRE_TIME));
 
        reserve.setStatus(StatusType.ENABLE.val);
        reserve.setCreateTime(now);
        reserve.setUpdateTime(now);
 
        if (!this.save(reserve)) {
            staMapper.releaseReserveOut(sta.getId(), qty);
            throw new BusinessException("reserve record insert failed");
        }
 
        return reserve;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void confirmStaReserve(Sta sta, Task task, Integer qty, StaReserveType type) {
        qty = Optional.ofNullable(qty).orElse(1);
 
        // update reserve state to be confirmed
        if (0 == this.baseMapper.updateState(task.getId(), sta.getId(), type.toString(), StaReserveStateType.CONFIRMED.toString())) {
            throw new BusinessException("failed to confirm sta reserve");
        }
 
        int cntOfDealWithReserve = 0;
        switch (type) {
            case IN:
                cntOfDealWithReserve = staMapper.confirmReserveIn(sta.getId(), qty);
                break;
            case OUT:
                cntOfDealWithReserve = staMapper.confirmReserveOut(sta.getId(), qty);
                break;
            default:
                break;
        }
        if (cntOfDealWithReserve == 0) {
            throw new BusinessException("Sta[" + sta.getStaNo() + "] apply confirmed failed, type=" + type.toString());
        }
    }
 
 
    private String generateReserveUniqKey(Long staId, Long taskId, StaReserveType type) {
        return "STA:" + staId + "-TASK:" + taskId + "-T:" + type.toString();
    }
 
}