package com.zy.asrs.task.kingdee.handler; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.zy.asrs.entity.*; import com.zy.asrs.service.*; import com.zy.asrs.service.impl.ErpSecretServiceImpl; import com.zy.asrs.service.impl.OrderDetlPakinServiceImpl; import com.zy.asrs.service.impl.OrderDetlServiceImpl; import com.zy.asrs.service.impl.OrderPakinServiceImpl; 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.List; @Slf4j @Service public class AutoTransferHandler extends AbstractHandler { @Autowired private OrderService orderService; @Autowired private ApiLogService apiLogService; @Autowired private DocTypeService docTypeService; @Autowired private LoginAuthenticationHandler loginAuthenticationHandler; @Autowired private ErpSecretServiceImpl erpSecretService; @Autowired private LocDetlService locDetlService; @Autowired private MatService matService; @Autowired private OrderDetlPakinServiceImpl orderDetlPakinService; @Autowired private OrderDetlServiceImpl orderDetlService; @Autowired private OrderPakinServiceImpl orderPakinServiceImpl; @Transactional(rollbackFor = Exception.class) public ReturnT start(OrderPakin order) { JSONObject logData = new JSONObject(); String responseMsg = ""; boolean success = false; try { logData.put("orderNo", order.getOrderNo()); logData.put("docType", order.getDocType()); logData.put("settle", order.getSettle()); logData.put("startTime", System.currentTimeMillis()); // 获取该订单下的所有明细 List orderDetlPakins = orderDetlPakinService.selectList( new EntityWrapper().eq("order_no", order.getOrderNo())); if (orderDetlPakins.isEmpty()) { throw new IllegalArgumentException("订单 " + order.getOrderNo() + " 无任何明细,无法转换"); } // 遍历每个订单明细进行库存转换(原逻辑保持不变) for (OrderDetlPakin orderDetlPakin : orderDetlPakins) { double orderQuantity = orderDetlPakin.getAnfme(); double remainingQuantity = orderQuantity; double convertedQuantity = 0.0; log.info("开始处理订单明细: 订单号={}, 物料号={}, 需要转换数量={}", order.getOrderNo(), orderDetlPakin.getMatnr(), orderQuantity); List locDetls = locDetlService.selectList( new EntityWrapper() .eq("matnr", orderDetlPakin.getMatnr()) .like("box_type3", "HDU%") .orderBy("anfme") ); if (locDetls.isEmpty()) { throw new IllegalArgumentException("无符合条件的库存: 订单 " + order.getOrderNo() + " 物料 " + orderDetlPakin.getMatnr()); } for (LocDetl locDetl : locDetls) { if (remainingQuantity <= 0) break; double stockQty = locDetl.getAnfme(); if (stockQty <= 0) continue; double consumeQty = Math.min(stockQty, remainingQuantity); // 创建分配记录... LocDetl allocated = new LocDetl(); BeanUtils.copyProperties(locDetl, allocated); allocated.setAnfme(consumeQty); allocated.setStandby1(orderDetlPakin.getStandby1()); allocated.setStandby2(orderDetlPakin.getStandby2()); allocated.setStandby3(orderDetlPakin.getStandby3()); allocated.setBoxType1(orderDetlPakin.getBoxType1()); allocated.setBoxType2(orderDetlPakin.getBoxType2()); allocated.setBoxType3(orderDetlPakin.getBoxType3()); allocated.setOrderNo(orderDetlPakin.getOrderNo()); locDetlService.insert(allocated); // 扣减原库存... double remainingStock = stockQty - consumeQty; if (remainingStock <= 0) { locDetlService.delete(new EntityWrapper() .eq("loc_no", locDetl.getLocNo()) .eq("matnr", locDetl.getMatnr()) .eq("box_type3", locDetl.getBoxType3())); } else { locDetl.setAnfme(remainingStock); locDetlService.update(locDetl, new EntityWrapper() .eq("loc_no", locDetl.getLocNo()) .eq("matnr", locDetl.getMatnr()) .eq("box_type3", locDetl.getBoxType3())); } convertedQuantity += consumeQty; remainingQuantity -= consumeQty; } if (remainingQuantity > 0) { throw new IllegalArgumentException("库存不足: 订单 " + order.getOrderNo() + " 物料 " + orderDetlPakin.getMatnr() + " 缺少 " + remainingQuantity); } orderDetlPakin.setQty(convertedQuantity); orderDetlPakinService.updateById(orderDetlPakin); } // 全部成功 order.setSettle(67L); orderPakinServiceImpl.updateById(order); success = true; responseMsg = "库存转换成功,订单号: " + order.getOrderNo() + ",已设置 settle=67"; log.info(responseMsg); } catch (Exception e) { success = false; responseMsg = "库存转换失败: " + e.getMessage(); log.error("自动转换失败,订单号: {}, 原因: {}", order.getOrderNo(), e.getMessage(), e); throw e; // 让 @Transactional 回滚 } finally { // 无论成功失败,都记录日志 logData.put("endTime", System.currentTimeMillis()); logData.put("durationMs", logData.getLongValue("endTime") - logData.getLongValue("startTime")); logData.put("success", success); logData.put("message", responseMsg); saveApiLog(logData, responseMsg, success); } return success ? SUCCESS : FAIL; } private void saveApiLog(JSONObject add, String response, boolean success) { try { apiLogService.save( "货主转换", "AutoTransferHandler", null, "127.0.0.1", add.toJSONString(), response, success ); } catch (Exception e) { log.error("接口日志保存失败", e); } } }