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.POExceStatus;
|
import com.vincent.rsf.server.manager.service.*;
|
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;
|
|
/**
|
* @param
|
* @return
|
* @author Ryan
|
* @description 删除已完成订单加入Log表
|
* @time 2025/3/19 19:09
|
*/
|
@Scheduled(cron = "0/35 * * * * ? ")
|
@Transactional(rollbackFor = Exception.class)
|
public void moveOrderToLog() {
|
List<WkOrder> wkOrders = asnOrderService.list(new LambdaQueryWrapper<WkOrder>()
|
.eq(WkOrder::getType, OrderType.ORDER_IN.type)
|
.eq(WkOrder::getExceStatus, AsnExceStatus.ASN_EXCE_STATUS_TASK_DONE.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<WkOrder> wkOrders = asnOrderService.list(new LambdaQueryWrapper<WkOrder>()
|
.eq(WkOrder::getType, OrderType.ORDER_OUT.type)
|
.eq(WkOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_DONE.val)
|
.apply("anfme = work_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<WkOrder> wkOrders, String type) throws Exception{
|
Set<Long> longSet = wkOrders.stream().map(WkOrder::getId).collect(Collectors.toSet());
|
List<WkOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>()
|
.in(WkOrderItem::getOrderId, longSet));
|
if (orderItems.isEmpty()) {
|
throw new CoolException("收货明细为空!!");
|
}
|
|
for (WkOrder order : wkOrders) {
|
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<AsnOrderItemLog> logs = new ArrayList<>();
|
List<WkOrderItem> items = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>()
|
.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 (!Objects.isNull(order.getPoId())) {
|
if (!purchaseService.update(new LambdaUpdateWrapper<Purchase>()
|
.set(Purchase::getExceStatus, POExceStatus.PO_EXCE_STATUS_ALL_DONE.val)
|
.eq(Purchase::getId, order.getPoId()))) {
|
throw new CoolException("PO单更新状态更新失败!!");
|
}
|
}
|
} else {
|
if (!Objects.isNull(order.getPoId())) {
|
if (!deliveryService.update(new LambdaUpdateWrapper<Delivery>()
|
.eq(Delivery::getId, order.getPoId())
|
.set(Delivery::getExceStatus, POExceStatus.PO_EXCE_STATUS_ALL_DONE.val))) {
|
throw new CoolException("DO单更新状态更新失败");
|
}
|
}
|
}
|
}
|
|
// if (!asnOrderItemService.remove(new LambdaQueryWrapper<WkOrderItem>()
|
// .in(WkOrderItem::getOrderId, longSet))) {
|
// throw new CoolException("原单据明细删除失败!!");
|
// }
|
// if (!this.asnOrderService.removeByIds(longSet)) {
|
// throw new CoolException("原单据删除失败!!");
|
// }
|
}
|
|
|
}
|