|
@@ -0,0 +1,132 @@
|
|
|
+package com.sd.business.listener;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
+import com.sd.business.entity.bom.po.Bom;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
+import com.sd.business.service.bom.BomService;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
+import com.sd.mq.config.BomSpecConfig;
|
|
|
+import com.sd.mq.entity.BomMessage;
|
|
|
+import com.sd.mq.entity.BomSpecDetail;
|
|
|
+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.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * bom规格监听
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class BomSpecListener {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BomService bomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+
|
|
|
+ @RabbitListener(bindings = {
|
|
|
+ @QueueBinding(value = @Queue(BomSpecConfig.ADD_BOM_NAME), exchange = @Exchange(BomSpecConfig.DIRECT_EXCHANGE_NAME))
|
|
|
+ })
|
|
|
+ public void addMsgReceive(BomMessage bomMessage, Channel channel, Message message) throws IOException {
|
|
|
+
|
|
|
+ log.info("接收新增bom:{}", JSON.toJSONString(bomMessage));
|
|
|
+
|
|
|
+ long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Bom bom = BeanUtil.copyProperties(bomMessage, Bom.class);
|
|
|
+ bom.setMesId(bom.getId());
|
|
|
+ bom.setId(IdWorker.getId());
|
|
|
+ List<BomSpecDetail> bomSpecDetailList = bomMessage.getBomSpecDetailList();
|
|
|
+ List<BomSpec> bomSpecList = BeanUtil.copyToList(bomSpecDetailList, BomSpec.class);
|
|
|
+ bomSpecList.forEach(item -> {
|
|
|
+ item.setMesId(item.getId());
|
|
|
+ item.setId(null);
|
|
|
+ item.setBomId(bom.getId());
|
|
|
+ });
|
|
|
+ bomService.save(bom);
|
|
|
+ bomSpecService.saveBatch(bomSpecList);
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("接收新增bom异常", e);
|
|
|
+ channel.basicReject(deliveryTag, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @RabbitListener(bindings = {
|
|
|
+ @QueueBinding(value = @Queue(BomSpecConfig.EDIT_BOM_NAME), exchange = @Exchange(BomSpecConfig.DIRECT_EXCHANGE_NAME))
|
|
|
+ })
|
|
|
+ public void editMsgReceive(BomMessage bomMessage, Channel channel, Message message) throws IOException {
|
|
|
+
|
|
|
+ log.info("接收更新bom:{}", JSON.toJSONString(bomMessage));
|
|
|
+
|
|
|
+ long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Bom bom = bomService.getOne(q -> q.eq(Bom::getMesId, bomMessage.getId()));
|
|
|
+ if (bom == null) {
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Bom editBom = BeanUtil.copyProperties(bomMessage, Bom.class);
|
|
|
+ editBom.setId(bom.getId());
|
|
|
+
|
|
|
+ Map<Long, Long> bomSpecMap = bomSpecService.mapKV(BomSpec::getMesId, BomSpec::getId, q -> q.eq(BomSpec::getBomId, bom.getId()));
|
|
|
+
|
|
|
+ List<BomSpecDetail> bomSpecDetailList = bomMessage.getBomSpecDetailList();
|
|
|
+ List<BomSpec> bomSpecList = BeanUtil.copyToList(bomSpecDetailList, BomSpec.class);
|
|
|
+ bomSpecList.forEach(item -> {
|
|
|
+ item.setMesId(item.getId());
|
|
|
+ item.setId(bomSpecMap.get(item.getMesId()));
|
|
|
+ item.setBomId(bom.getId());
|
|
|
+ });
|
|
|
+
|
|
|
+ // 更新bom明细
|
|
|
+ bomService.updateById(editBom);
|
|
|
+ bomSpecService.editLinked(bomSpecList, BomSpec::getBomId, bom.getId());
|
|
|
+
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("接收更新bom异常", e);
|
|
|
+ channel.basicReject(deliveryTag, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RabbitListener(bindings = {
|
|
|
+ @QueueBinding(value = @Queue(BomSpecConfig.DELETE_BOM_NAME), exchange = @Exchange(BomSpecConfig.DIRECT_EXCHANGE_NAME))
|
|
|
+ })
|
|
|
+ public void deleteMsgReceive(BomMessage bomMessage, Channel channel, Message message) throws IOException {
|
|
|
+
|
|
|
+ log.info("接收删除bom:{}", JSON.toJSONString(bomMessage));
|
|
|
+
|
|
|
+ long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Bom bom = bomService.getOne(q -> q.eq(Bom::getMesId, bomMessage.getId()));
|
|
|
+ if (bom == null) {
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ bomService.removeById(bom.getId());
|
|
|
+ bomSpecService.remove(q -> q.eq(BomSpec::getBomId, bom.getId()));
|
|
|
+ channel.basicAck(deliveryTag, true);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("接收删除bom异常", e);
|
|
|
+ channel.basicReject(deliveryTag, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|