|
|
@@ -0,0 +1,156 @@
|
|
|
+package com.jzg.quotation.summary.state.listener;
|
|
|
+
|
|
|
+import com.jzg.commons.core.base.StateResult;
|
|
|
+import com.jzg.commons.entity.po.PtlAgreementCostExternal;
|
|
|
+import com.jzg.quotation.summary.state.OrderState;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import org.springframework.messaging.Message;
|
|
|
+import org.springframework.messaging.support.MessageBuilder;
|
|
|
+import org.springframework.statemachine.StateMachine;
|
|
|
+import org.springframework.statemachine.StateMachineEventResult;
|
|
|
+import org.springframework.statemachine.support.DefaultStateMachineContext;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import reactor.core.publisher.Flux;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author quchen
|
|
|
+ * @date 2025/2/26 10:57
|
|
|
+ */
|
|
|
+
|
|
|
+@Component
|
|
|
+public class OrderStateListener {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private StateMachine<OrderState.OrderStateEnum, OrderState.OrderStateEvent> costStateMachine;
|
|
|
+
|
|
|
+ private synchronized boolean sendEvent(Mono<Message<OrderState.OrderStateEvent>> message) {
|
|
|
+ AtomicBoolean result = new AtomicBoolean(false);
|
|
|
+ try {
|
|
|
+ costStateMachine.stop(); // 停止状态机
|
|
|
+ Message<OrderState.OrderStateEvent> block = message.block();
|
|
|
+ String orderStatus = (String) block.getHeaders().get("entity");
|
|
|
+ costStateMachine.getStateMachineAccessor().doWithAllRegions(accessor -> {
|
|
|
+ accessor.resetStateMachineReactively(new DefaultStateMachineContext<>(
|
|
|
+ // 目标状态
|
|
|
+ OrderState.OrderStateEnum.fromValue(Integer.parseInt(orderStatus)),
|
|
|
+ null, // 当前事件(可以为 null)
|
|
|
+ null, // 扩展上下文(可以为 null)
|
|
|
+ null // 转换历史(通常为 null)
|
|
|
+ )).block();
|
|
|
+ });
|
|
|
+ Mono<Void> startMono = costStateMachine.startReactively(); //启用状态机
|
|
|
+ startMono.block();
|
|
|
+
|
|
|
+ Flux<StateMachineEventResult<OrderState.OrderStateEnum, OrderState.OrderStateEvent>> stateMachineEventResultFlux = costStateMachine.sendEvent(message);
|
|
|
+ StateMachineEventResult<OrderState.OrderStateEnum, OrderState.OrderStateEvent> eventResult = stateMachineEventResultFlux.blockFirst();
|
|
|
+
|
|
|
+ if (eventResult != null && eventResult.getResultType() == StateMachineEventResult.ResultType.ACCEPTED) {
|
|
|
+ result.set(true);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ costStateMachine.stopReactively();
|
|
|
+ return result.get();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized boolean sendCostExternalEvent(Mono<Message<OrderState.OrderStateEvent>> message) {
|
|
|
+ AtomicBoolean result = new AtomicBoolean(false);
|
|
|
+ try {
|
|
|
+ costStateMachine.stop(); // 停止状态机
|
|
|
+ Message<OrderState.OrderStateEvent> block = message.block();
|
|
|
+ PtlAgreementCostExternal ptlAgreementCostExternal = (PtlAgreementCostExternal) block.getHeaders().get("entity");
|
|
|
+ costStateMachine.getStateMachineAccessor().doWithAllRegions(accessor -> {
|
|
|
+ accessor.resetStateMachineReactively(new DefaultStateMachineContext<>(
|
|
|
+ OrderState.OrderStateEnum.fromValue(ptlAgreementCostExternal.getAuditStatus()), // 目标状态
|
|
|
+ null, // 当前事件(可以为 null)
|
|
|
+ null, // 扩展上下文(可以为 null)
|
|
|
+ null // 转换历史(通常为 null)
|
|
|
+ )).block();
|
|
|
+ });
|
|
|
+ Mono<Void> startMono = costStateMachine.startReactively(); //启用状态机
|
|
|
+ startMono.block();
|
|
|
+
|
|
|
+ Flux<StateMachineEventResult<OrderState.OrderStateEnum, OrderState.OrderStateEvent>> stateMachineEventResultFlux = costStateMachine.sendEvent(message);
|
|
|
+ StateMachineEventResult<OrderState.OrderStateEnum, OrderState.OrderStateEvent> eventResult = stateMachineEventResultFlux.blockFirst();
|
|
|
+
|
|
|
+ if (eventResult != null && eventResult.getResultType() == StateMachineEventResult.ResultType.ACCEPTED) {
|
|
|
+ result.set(true);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ costStateMachine.stopReactively();
|
|
|
+ return result.get();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private StateResult callEvent(String orderStatus, OrderState.OrderStateEvent event) {
|
|
|
+ StateResult stateResult = new StateResult();
|
|
|
+ Mono<Message<OrderState.OrderStateEvent>> message = Mono.just(MessageBuilder.withPayload(event).setHeader("entity",orderStatus).build());
|
|
|
+ stateResult.setResult(sendEvent(message));
|
|
|
+ stateResult.setCurrentState(costStateMachine.getState().getId().getValue());
|
|
|
+ return stateResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报价失败
|
|
|
+ * @param cureentStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public StateResult quoteFail(String cureentStatus) {
|
|
|
+ return callEvent(cureentStatus, OrderState.OrderStateEvent.QUOTE_FAIL_EVENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报价成功
|
|
|
+ * @param currentStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public StateResult quoteSuccess(String currentStatus) {
|
|
|
+ return callEvent(currentStatus, OrderState.OrderStateEvent.AUDIT_ING_EVENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 人工处理
|
|
|
+ * @param currentStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public StateResult staffProcess(String currentStatus) {
|
|
|
+ return callEvent(currentStatus, OrderState.OrderStateEvent.STAFF_PROCESS_EVENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 核保成功
|
|
|
+ * @param currentStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public StateResult auditSuccess(String currentStatus){
|
|
|
+ return callEvent(currentStatus, OrderState.OrderStateEvent.PAY_PENDING_EVENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 核保失败
|
|
|
+ * @param currentStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public StateResult auditFail(String currentStatus){
|
|
|
+ return callEvent(currentStatus, OrderState.OrderStateEvent.AUDIT_FAILED_EVENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 已承保
|
|
|
+ * @param currentStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public StateResult underwrited(String currentStatus){
|
|
|
+ return callEvent(currentStatus, OrderState.OrderStateEvent.UNDERWRITED_EVENT);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|