zhubaomin
2024-11-06 549b9d60edcdb46366c60666ea4ee2c2e4533951
pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/workOrder/WorkOrderCtrl.java
@@ -1,6 +1,12 @@
package com.dy.pipIrrApp.workOrder;
import com.alibaba.fastjson2.JSON;
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pipIrrGlobal.pojoOp.OpeWorkOrder;
import com.dy.pipIrrGlobal.voOp.VoWorkOrder;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
@@ -11,12 +17,17 @@
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
 * @author ZhuBaoMin
@@ -35,26 +46,69 @@
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Value("${rocketmq.name-server}")
    protected String nameServer;
    @GetMapping("/sendWorkOrder")
    @Value("${rocketmq.producer.group}")
    protected String producerGroup;
    @Value("${rocketmq.topic}")
    protected String topic;
    /**
     * 创建工单
     * @param po
     * @param bindingResult
     * @return
     */
    @PostMapping(path = "addWorkOrder", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public void sendWorkOrder() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        ConsumerListener_push ConsumerListener_push = new ConsumerListener_push();
        ConsumerListener_push.receiveMessage();
    public BaseResponse<Boolean> addWorkOrder(@RequestBody @Valid OpeWorkOrder po, BindingResult bindingResult) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        String topic = "workOrder";
        String tag = "王五";
        String key = "11";
        String message = "Hello World";
        //SendResult sendResult = rocketMQTemplate.syncSend(topic + ":" + tag, message);
        //System.out.println("send result:" + sendResult.toString());
        Long workOrderId = workOrderSv.insertWorkOrder(po);
        if(workOrderId == null || workOrderId == 0) {
            return BaseResponseUtils.buildErrorMsg("创建工单失败");
        }
        DefaultMQProducer producer = new DefaultMQProducer("producer_group");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();
        Message msg = new Message(topic,tag,"11",message.getBytes(StandardCharsets.UTF_8));
        SendResult approveSendResult = producer.send(msg);
        System.out.println("send result:" + approveSendResult.toString());
        VoWorkOrder voWorkOrder = workOrderSv.getWorkOrderById(workOrderId);
        if(voWorkOrder == null) {
            return BaseResponseUtils.buildErrorMsg("获取工单失败");
        }
        if(!sendWorkOrder(voWorkOrder)) {
            return BaseResponseUtils.buildErrorMsg("工单推送失败");
        }
        return BaseResponseUtils.buildSuccess();
    }
    /**
     * 推送工单
     * @param voWorkOrder
     * @throws MQClientException
     * @throws MQBrokerException
     * @throws RemotingException
     * @throws InterruptedException
     */
    private Boolean sendWorkOrder(VoWorkOrder voWorkOrder) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        String tag = voWorkOrder.getInspector();
        String key = voWorkOrder.getInspectorId().toString();
        String message = JSON.toJSONString(voWorkOrder);
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(nameServer);
        producer.start();
        Message msg = new Message(topic, tag, key, message.getBytes(StandardCharsets.UTF_8));
        SendResult approveSendResult = producer.send(msg);
        if(!approveSendResult.getSendStatus().toString().equals("SEND_OK")) {
            return false;
        }
        return true;
    }
}