package com.vincent.rsf.server.manager.schedules; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.vincent.rsf.framework.exception.CoolException; import com.vincent.rsf.server.manager.entity.*; import com.vincent.rsf.server.manager.enums.AsnExceStatus; import com.vincent.rsf.server.manager.enums.OrderType; import com.vincent.rsf.server.manager.enums.OrderWorkType; import com.vincent.rsf.server.manager.enums.POExceStatus; import com.vincent.rsf.server.manager.service.*; import com.vincent.rsf.server.manager.service.impl.TransferOrderServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; /** * @author Ryan * @version 1.0 * @title AsnOrderLogSchedule * @description * @create 2025/3/19 19:07 */ @Component public class AsnOrderLogSchedule { @Autowired private PurchaseService purchaseService; @Autowired private DeliveryService deliveryService; @Autowired private AsnOrderService asnOrderService; @Autowired private AsnOrderItemService asnOrderItemService; @Autowired private AsnOrderLogService asnOrderLogService; @Autowired private AsnOrderItemLogService asnOrderItemLogService; @Autowired private TransferOrderService transferOrderService; @Autowired private TransferService transferService; /** * @param * @return * @author Ryan * @description 删除已完成订单加入Log表 * @time 2025/3/19 19:09 */ @Scheduled(cron = "0/35 * * * * ? ") @Transactional(rollbackFor = Exception.class) public void InStockToLog() { List wkOrders = asnOrderService.list(new LambdaQueryWrapper() .eq(WkOrder::getType, OrderType.ORDER_IN.type) .eq(WkOrder::getExceStatus, AsnExceStatus.ASN_EXCE_STATUS_TASK_EXCE.val)); if (wkOrders.isEmpty()) { return; } try { moveOrderToLog(wkOrders, OrderType.ORDER_IN.type); } catch (Exception e) { throw new CoolException(e.getMessage()); } } /** * @param * @return * @author Ryan * @description 出库单完成后,状态修改 * @time 2025/6/16 08:35 */ @Scheduled(cron = "0/30 * * * * ? ") @Transactional(rollbackFor = Exception.class) public void outStockComplete() { List wkOrders = asnOrderService.list(new LambdaQueryWrapper() .eq(WkOrder::getType, OrderType.ORDER_OUT.type) .eq(WkOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val) .apply("anfme = qty") ); if (wkOrders.isEmpty()) { return; } try { moveOrderToLog(wkOrders, OrderType.ORDER_OUT.type); } catch (Exception e) { throw new CoolException(e.getMessage()); } } /** * @param * @param type * @return * @author Ryan * @description 添加历史单据 * @time 2025/6/16 08:56 */ @Transactional(rollbackFor = Exception.class) public void moveOrderToLog(List wkOrders, String type) { Set longSet = wkOrders.stream().map(WkOrder::getId).collect(Collectors.toSet()); List orderItems = asnOrderItemService.list(new LambdaQueryWrapper() .in(WkOrderItem::getOrderId, longSet)); if (orderItems.isEmpty()) { throw new CoolException("收货明细为空!!"); } for (WkOrder order : wkOrders) { AsnOrderLog one = asnOrderLogService.getOne(new LambdaQueryWrapper().eq(AsnOrderLog::getCode, order.getCode()), false); if (Objects.isNull(one)) { AsnOrderLog orderLog = new AsnOrderLog(); if (type.equals(OrderType.ORDER_OUT.type)) { order.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_DONE.val); order.setQty(order.getWorkQty()); } BeanUtils.copyProperties(order, orderLog); orderLog.setId(null); orderLog.setAsnId(order.getId()); if (!asnOrderLogService.save(orderLog)) { throw new CoolException("主单历史档添加失败!!"); } List logs = new ArrayList<>(); List items = asnOrderItemService.list(new LambdaQueryWrapper() .eq(WkOrderItem::getOrderId, order.getId())); items.forEach(item -> { AsnOrderItemLog itemLog = new AsnOrderItemLog(); BeanUtils.copyProperties(item, itemLog); itemLog.setAsnItemId(itemLog.getId()) .setId(null) .setMatnrId(item.getMatnrId()) .setLogId(orderLog.getId()) .setAsnId(item.getOrderId()); logs.add(itemLog); }); if (!asnOrderItemLogService.saveBatch(logs)) { throw new CoolException("单据明细历史档保存失败!!"); } //更新PO/DO单执行状态 if (type.equals(OrderType.ORDER_IN.type)) { if (order.getWkType().equals(OrderWorkType.ORDER_WORK_TYPE_OTHER_TERANSFER.type)) { order.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_DONE.val); if (!asnOrderService.updateById(order)) { throw new CoolException("单据状态更新失败!!"); } // List orders = transferOrderService.list(new LambdaQueryWrapper().eq(TransferOrder::getTransferId, order.getPoId())); // if (orders.isEmpty()) { // throw new CoolException("数据错误:关联表信息不存在!!"); // } // Set orderIds = orders.stream().map(TransferOrder::getOrderId).collect(Collectors.toSet()); // if (!asnOrderItemService.remove(new LambdaQueryWrapper() // .in(WkOrderItem::getOrderId, orderIds))) { // throw new CoolException("原单据明细删除失败!!"); // } // if (!this.asnOrderService.removeByIds(orderIds)) { // throw new CoolException("原单据删除失败!!"); // } if (!transferService.update(new LambdaUpdateWrapper() .eq(Transfer::getId, order.getPoId()) .set(Transfer::getExceStatus, AsnExceStatus.ASN_EXCE_STATUS_TASK_DONE.val))) { throw new CoolException("调拔单状态修改失败!!"); } return; } else { if (!Objects.isNull(order.getPoId())) { purchaseService.update(new LambdaUpdateWrapper() .set(Purchase::getExceStatus, POExceStatus.PO_EXCE_STATUS_ALL_DONE.val) .eq(Purchase::getId, order.getPoId())); } } } else { if (order.getWkType().equals(OrderWorkType.ORDER_WORK_TYPE_STOCK_TERANSFER.type)) { order.setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_DONE.val); if (!asnOrderService.updateById(order)) { throw new CoolException("单据状态更新失败!!"); } //如果为调拔单据保留 return; } else { if (!Objects.isNull(order.getPoId())) { deliveryService.update(new LambdaUpdateWrapper() .eq(Delivery::getId, order.getPoId()) .set(Delivery::getExceStatus, POExceStatus.PO_EXCE_STATUS_ALL_DONE.val)); } } } } if (!asnOrderItemService.remove(new LambdaQueryWrapper() .eq(WkOrderItem::getOrderId, order.getId()))) { throw new CoolException("原单据明细删除失败!!"); } if (!this.asnOrderService.removeById(order.getId())) { throw new CoolException("原单据删除失败!!"); } } } }