| | |
| | | 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; |
| | |
| | | /** |
| | | * 预留库存过期处理器 |
| | | * 将过期的预留库存转移到日志表 |
| | | * 转历史条件: |
| | | * 1. 过期时间不为空且已过期 |
| | | * 2. 订单号不为空且订单状态为作业中(1)或已完成(2) |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | |
| | | @Autowired |
| | | private InventoryReserveLogService inventoryReserveLogService; |
| | | |
| | | @Autowired |
| | | private OrderPakoutService orderPakoutService; |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public ReturnT<String> start() { |
| | | // 查询过期的预留库存(过期时间不为空且小于当前时间) |
| | | Date now = new Date(); |
| | | List<InventoryReserve> expiredList = inventoryReserveService.selectList( |
| | | new EntityWrapper<InventoryReserve>() |
| | | .isNotNull("expire_time") |
| | | .lt("expire_time", now)); |
| | | |
| | | if (expiredList.isEmpty()) { |
| | | // 查询所有预留库存记录 |
| | | List<InventoryReserve> allReserves = inventoryReserveService.selectList(new EntityWrapper<>()); |
| | | |
| | | if (allReserves.isEmpty()) { |
| | | return SUCCESS; |
| | | } |
| | | |
| | | log.info("发现 {} 条过期的预留库存记录", expiredList.size()); |
| | | |
| | | for (InventoryReserve reserve : expiredList) { |
| | | int processedCount = 0; |
| | | for (InventoryReserve reserve : allReserves) { |
| | | try { |
| | | // 转移到日志表 |
| | | InventoryReserveLog logRecord = getInventoryReserveLog(reserve); |
| | | boolean shouldArchive = false; |
| | | String archiveReason = null; |
| | | |
| | | inventoryReserveLogService.insert(logRecord); |
| | | // 条件1:过期时间不为空且已过期 |
| | | if (reserve.getExpireTime() != null && reserve.getExpireTime().before(now)) { |
| | | shouldArchive = true; |
| | | archiveReason = "已过期"; |
| | | } |
| | | |
| | | // 删除原记录 |
| | | inventoryReserveService.deleteById(reserve.getId()); |
| | | // 条件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 = "订单不为待处理状态"; |
| | | } |
| | | } |
| | | } |
| | | |
| | | log.info("预留库存过期处理完成: id={}, matnr={}, batch={}", |
| | | reserve.getId(), reserve.getMatnr(), reserve.getBatch()); |
| | | 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()); |
| | | log.error("预留库存转历史失败: id={}, error={}", reserve.getId(), e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | return SUCCESS; |
| | | } |
| | | if (processedCount > 0) { |
| | | log.info("本次共处理 {} 条预留库存转历史记录", processedCount); |
| | | } |
| | | |
| | | private static InventoryReserveLog getInventoryReserveLog(InventoryReserve reserve) { |
| | | InventoryReserveLog logRecord = new InventoryReserveLog(); |
| | | logRecord.setMatnr(reserve.getMatnr()); |
| | | logRecord.setMaktx(reserve.getMaktx()); |
| | | logRecord.setOrderNo(reserve.getOrderNo()); |
| | | logRecord.setBatch(reserve.getBatch()); |
| | | logRecord.setQuantity(reserve.getQuantity()); |
| | | logRecord.setExpireTime(reserve.getExpireTime()); |
| | | logRecord.setDeptId(reserve.getDeptId()); |
| | | logRecord.setCreateTime(reserve.getCreateTime()); |
| | | logRecord.setCreateBy(reserve.getCreateBy()); |
| | | logRecord.setUpdateTime(new Date()); |
| | | return logRecord; |
| | | return SUCCESS; |
| | | } |
| | | |
| | | } |