|
@@ -0,0 +1,145 @@
|
|
|
+package com.sd.business.listener;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
+import com.sd.business.entity.order.enums.OrderStatusEnum;
|
|
|
+import com.sd.business.entity.order.po.OrderInfo;
|
|
|
+import com.sd.business.entity.production.po.ProductionWorkOrder;
|
|
|
+import com.sd.business.service.inventory.InventoryFinishedService;
|
|
|
+import com.sd.business.service.order.OrderService;
|
|
|
+import com.sd.business.service.production.ProductionWorkOrderService;
|
|
|
+import com.sd.mq.config.WorkOrderConfig;
|
|
|
+import com.sd.mq.entity.EditWorkOrderMessage;
|
|
|
+import com.sd.mq.enums.WorkOrderStatusEnum;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.amqp.core.Message;
|
|
|
+import org.springframework.amqp.rabbit.annotation.Exchange;
|
|
|
+import org.springframework.amqp.rabbit.annotation.Queue;
|
|
|
+import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 图稿库监听
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class AddWorkOrderListener {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductionWorkOrderService productionWorkOrderService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private InventoryFinishedService inventoryFinishedService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderService orderService;
|
|
|
+
|
|
|
+ @RabbitListener(bindings = {
|
|
|
+ @QueueBinding(value = @Queue(WorkOrderConfig.ADD_WORK_ORDER_QUEUE_NAME), exchange = @Exchange(WorkOrderConfig.DIRECT_EXCHANGE_NAME))
|
|
|
+ })
|
|
|
+ public void msgSend(EditWorkOrderMessage workOrderMessage, Channel channel, Message message) throws IOException {
|
|
|
+
|
|
|
+ log.info("接收更新工单:{}", JSON.toJSONString(workOrderMessage));
|
|
|
+
|
|
|
+ long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Long workOrderId = workOrderMessage.getWorkOrderId();
|
|
|
+ ProductionWorkOrder productionWorkOrder = productionWorkOrderService.getById(workOrderId);
|
|
|
+ if (productionWorkOrder == null) {
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer status = workOrderMessage.getStatus();
|
|
|
+ WorkOrderStatusEnum workOrderStatusEnum = WorkOrderStatusEnum.getByKey(status);
|
|
|
+ switch (workOrderStatusEnum) {
|
|
|
+ case SCANNED:
|
|
|
+ if (Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.TO_BE_PRODUCED.getKey())) {
|
|
|
+ productionWorkOrder.setStatus(status);
|
|
|
+ productionWorkOrderService.updateById(productionWorkOrder);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case IN_PRODUCTION:
|
|
|
+ if (Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.TO_BE_PRODUCED.getKey())
|
|
|
+ || Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.SCANNED.getKey())) {
|
|
|
+ productionWorkOrder.setStatus(status);
|
|
|
+ productionWorkOrderService.updateById(productionWorkOrder);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case PRODUCTION_COMPLETION:
|
|
|
+ if (Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.TO_BE_PRODUCED.getKey())
|
|
|
+ || Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.SCANNED.getKey())
|
|
|
+ || Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.IN_PRODUCTION.getKey())) {
|
|
|
+
|
|
|
+ productionWorkOrder.setCompleteTime(workOrderMessage.getSendTime());
|
|
|
+ productionWorkOrder.setStatus(status);
|
|
|
+
|
|
|
+ productionWorkOrderService.updateById(productionWorkOrder);
|
|
|
+
|
|
|
+ // 查询订单所有工单是否完成,若完成,修改订单状态为已完成
|
|
|
+ completeOrder(productionWorkOrder.getOrderId());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case PRODUCTION_ANOMALY:
|
|
|
+ if (Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.TO_BE_PRODUCED.getKey())
|
|
|
+ || Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.SCANNED.getKey())
|
|
|
+ || Objects.equals(productionWorkOrder.getStatus(), WorkOrderStatusEnum.IN_PRODUCTION.getKey())) {
|
|
|
+ productionWorkOrder.setStatus(status);
|
|
|
+ productionWorkOrderService.updateById(productionWorkOrder);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("接收更新工单异常", e);
|
|
|
+ channel.basicReject(deliveryTag, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void completeOrder(Long orderId) {
|
|
|
+
|
|
|
+ // 存在待投产、已扫描、生产中、生产异常的工单,则订单位完成
|
|
|
+ long count = productionWorkOrderService.count(q -> q
|
|
|
+ .in(ProductionWorkOrder::getStatus,
|
|
|
+ WorkOrderStatusEnum.TO_BE_PRODUCED.getKey(),
|
|
|
+ WorkOrderStatusEnum.SCANNED.getKey(),
|
|
|
+ WorkOrderStatusEnum.IN_PRODUCTION.getKey(),
|
|
|
+ WorkOrderStatusEnum.PRODUCTION_ANOMALY.getKey())
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+
|
|
|
+ if (count != 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 订单已完成或已发货跳过
|
|
|
+ OrderInfo orderInfo = orderService.getById(orderId);
|
|
|
+ if (Objects.equals(orderInfo.getStatus(), OrderStatusEnum.COMPLETION_PRODUCTION.getKey())
|
|
|
+ || Objects.equals(orderInfo.getStatus(), OrderStatusEnum.HAVE_BEEN_SHIPPED.getKey())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生产入库
|
|
|
+ inventoryFinishedService.productionWarehousing(Collections.singletonList(orderId));
|
|
|
+
|
|
|
+ // 更新订单状态已完成
|
|
|
+ orderInfo.setStatus(OrderStatusEnum.COMPLETION_PRODUCTION.getKey());
|
|
|
+ orderService.updateById(orderInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|