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 { @Autowired private InventoryReserveService inventoryReserveService; @Autowired private InventoryReserveLogService inventoryReserveLogService; @Autowired private OrderPakoutService orderPakoutService; @Transactional(rollbackFor = Exception.class) public ReturnT start() { Date now = new Date(); // 查询所有预留库存记录 List 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().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; } }