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; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.common.message.Message; 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.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 * @date 2024-11-04 9:30 * @LastEditTime 2024-11-04 9:30 * @Description 工单控制类 */ @Slf4j @RestController @RequestMapping(path = "workOrder") @RequiredArgsConstructor public class WorkOrderCtrl { private final WorkOrderSv workOrderSv; @Autowired private RocketMQTemplate rocketMQTemplate; @Value("${rocketmq.name-server}") protected String nameServer; @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 BaseResponse 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()); } Long workOrderId = workOrderSv.insertWorkOrder(po); if(workOrderId == null || workOrderId == 0) { return BaseResponseUtils.buildErrorMsg("创建工单失败"); } 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; } }