自动化立体仓库 - WMS系统
zhou zhou
2025-12-19 b0d80cc56a883a6fac242623e778a3ae20c71b79
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
package com.zy.asrs.task.handler;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.InventoryReserve;
import com.zy.asrs.entity.InventoryReserveLog;
import com.zy.asrs.entity.OrderPakout;
import com.zy.asrs.service.InventoryReserveLogService;
import com.zy.asrs.service.InventoryReserveService;
import com.zy.asrs.service.OrderPakoutService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.List;
 
/**
 * 预留库存过期处理器
 * 将过期的预留库存转移到日志表
 * 转历史条件:
 * 1. 过期时间不为空且已过期
 * 2. 订单号不为空且订单状态为作业中(1)或已完成(2)
 */
@Slf4j
@Service
public class InventoryReserveExpireHandler extends AbstractHandler<String> {
 
    @Autowired
    private InventoryReserveService inventoryReserveService;
 
    @Autowired
    private InventoryReserveLogService inventoryReserveLogService;
 
    @Autowired
    private OrderPakoutService orderPakoutService;
 
    @Transactional(rollbackFor = Exception.class)
    public ReturnT<String> start() {
        Date now = new Date();
 
        // 查询所有预留库存记录
        List<InventoryReserve> allReserves = inventoryReserveService.selectList(new EntityWrapper<>());
 
        if (allReserves.isEmpty()) {
            return SUCCESS;
        }
 
        int processedCount = 0;
        for (InventoryReserve reserve : allReserves) {
            try {
                boolean shouldArchive = false;
                String archiveReason = null;
 
                // 条件1:过期时间不为空且已过期
                if (reserve.getExpireTime() != null && reserve.getExpireTime().before(now)) {
                    shouldArchive = true;
                    archiveReason = "已过期";
                }
 
                // 条件2:订单号不为空且订单状态为作业中或已完成
                if (!shouldArchive && !Cools.isEmpty(reserve.getOrderNo())) {
                    OrderPakout order = orderPakoutService.selectOne(
                            new EntityWrapper<OrderPakout>().eq("order_no", reserve.getOrderNo()));
                    if (null != order) {
                        if (order.getSettle() != 1) {
                            shouldArchive = true;
                            archiveReason =  "订单不为待处理状态";
                        }
                    }
                }
 
                if (shouldArchive) {
                    // 转移到日志表
                    InventoryReserveLog logRecord = new InventoryReserveLog();
                    BeanUtils.copyProperties(reserve, logRecord);
                    logRecord.setMemo(archiveReason);
                    inventoryReserveLogService.insert(logRecord);
 
                    // 删除原记录
                    inventoryReserveService.deleteById(reserve.getId());
 
                    log.info("预留库存转历史完成: id={}, matnr={}, batch={}, 原因={}",
                            reserve.getId(), reserve.getMatnr(), reserve.getBatch(), archiveReason);
                    processedCount++;
                }
 
            } catch (Exception e) {
                log.error("预留库存转历史失败: id={}, error={}", reserve.getId(), e.getMessage());
            }
        }
 
        if (processedCount > 0) {
            log.info("本次共处理 {} 条预留库存转历史记录", processedCount);
        }
 
        return SUCCESS;
    }
 
}