huanglc 1 개월 전
부모
커밋
901e954125

+ 117 - 10
bpm-core/src/main/java/com/srm/bpm/logic/service/impl/BillLogicImpl.java

@@ -114,6 +114,8 @@ import org.apache.commons.codec.binary.StringUtils;
114 114
 import org.apache.commons.lang3.tuple.Pair;
115 115
 import org.apache.commons.lang3.tuple.Triple;
116 116
 import org.springframework.stereotype.Service;
117
+import org.springframework.transaction.PlatformTransactionManager;
118
+import org.springframework.transaction.TransactionDefinition;
117 119
 import org.springframework.transaction.annotation.Transactional;
118 120
 
119 121
 import java.math.BigDecimal;
@@ -134,15 +136,11 @@ import cn.hutool.core.util.RandomUtil;
134 136
 import cn.hutool.core.util.StrUtil;
135 137
 import lombok.RequiredArgsConstructor;
136 138
 import lombok.extern.slf4j.Slf4j;
139
+import org.springframework.transaction.support.TransactionSynchronizationAdapter;
140
+import org.springframework.transaction.support.TransactionSynchronizationManager;
141
+import org.springframework.transaction.support.TransactionTemplate;
137 142
 
138
-import static com.srm.bpm.logic.error.BillCode.APPROVAL_STATUS_ERROR;
139
-import static com.srm.bpm.logic.error.BillCode.BILL_CANCEL_ONAGREE;
140
-import static com.srm.bpm.logic.error.BillCode.BILL_HAS_COMPLETE;
141
-import static com.srm.bpm.logic.error.BillCode.BILL_NOT_IN_DRAFTS;
142
-import static com.srm.bpm.logic.error.BillCode.Bill_APPROVAL_IN;
143
-import static com.srm.bpm.logic.error.BillCode.ENDORSE_CANNOT_IN_NODE;
144
-import static com.srm.bpm.logic.error.BillCode.TARGET_TASK_NULL;
145
-import static com.srm.bpm.logic.error.BillCode.TURN_CANNOT_IN_NODE;
143
+import static com.srm.bpm.logic.error.BillCode.*;
146 144
 import static org.apache.commons.lang3.StringUtils.EMPTY;
147 145
 
148 146
 /**
@@ -185,6 +183,7 @@ public class BillLogicImpl implements BillLogic {
185 183
     private final BillBasicConvert billBasicConvert;
186 184
     private final ProcessTypeService processTypeService;
187 185
     private final ProcessBasicConvert processBasicConvert;
186
+    private PlatformTransactionManager transactionManager;
188 187
 
189 188
     /**
190 189
      * 创建审批单
@@ -898,13 +897,15 @@ public class BillLogicImpl implements BillLogic {
898 897
      *
899 898
      * @param bill 审批单ID
900 899
      */
901
-    @Override
902
-    //    @Transactional(rollbackFor = Exception.class)
900
+    /*@Override
901
+    @Transactional(rollbackFor = Exception.class)
903 902
     public void complete(ToaBillEntity bill, String action) {
904 903
         final int dateline = DateTimeUtil.unixTime();
905 904
         bill.setCompletionTime(dateline);
906 905
         bill.setStatus(BillStatus.COMPLETE.getStatus());
907 906
         bill.setArchivedTime(dateline);
907
+
908
+
908 909
         TimerTask businessTask = new TimerTask() {
909 910
             @Override
910 911
             public void run() {
@@ -926,7 +927,113 @@ public class BillLogicImpl implements BillLogic {
926 927
                 }
927 928
             }
928 929
         };
930
+
929 931
         CallBackThreadPoolManager.me().executeLog(businessTask);
932
+
933
+
934
+        *//*try {
935
+            log.info("查询到的审批单数据:{}", bill);
936
+            billService.upldate(bill);
937
+            
938
+            if (action.equals(BillAction.refuse.name())) {
939
+                callBackLogic.callBack(bill.getProcessId(), bill.getId(), BillTaskStatus.REFUSE.getStatus());
940
+            } else {
941
+                BillTaskEntity a = new BillTaskEntity();
942
+                a.setBillId(bill.getId());
943
+                a.setUserCode(bill.getSender());
944
+                a.setNodeStatus(BillTaskStatus.COMPLATE.getStatus());
945
+                a.setNodeName(bill.getTitle());
946
+                a.setOpinion("通过");
947
+                flowMsgLogic.sendMsg(Lists.newArrayList(a));
948
+                callBackLogic.callBack(bill.getProcessId(), bill.getId(), BillTaskStatus.COMPLATE.getStatus());
949
+            }
950
+        } catch (Exception e) {
951
+            log.error("流程完成失败: billId={}, action={}, error={}", 
952
+                      bill.getId(), action, e.getMessage(), e);
953
+            throw new RbException("流程完成失败:" + e.getMessage());
954
+        }*//*
955
+
956
+    }*/
957
+    @Override
958
+    @Transactional(rollbackFor = Exception.class)
959
+    public void complete(ToaBillEntity bill, String action) {
960
+        final int dateline = DateTimeUtil.unixTime();
961
+        bill.setCompletionTime(dateline);
962
+        bill.setStatus(BillStatus.COMPLETE.getStatus());
963
+        bill.setArchivedTime(dateline);
964
+
965
+        // 准备数据(避免闭包问题)
966
+        final Long processId = bill.getProcessId();
967
+        final Long billId = bill.getId();
968
+        final String sender = bill.getSender();
969
+        final String title = bill.getTitle();
970
+
971
+        // 使用事务同步器:在事务提交后执行异步任务
972
+        TransactionSynchronizationManager.registerSynchronization(
973
+                new TransactionSynchronizationAdapter() {
974
+                    @Override
975
+                    public void afterCommit() {
976
+                        // 事务提交成功后,在异步线程中开启新事务
977
+                        TimerTask businessTask = new TimerTask() {
978
+                            @Override
979
+                            public void run() {
980
+                                // 在异步线程中使用编程式事务
981
+                                TransactionTemplate transactionTemplate =
982
+                                        new TransactionTemplate(transactionManager);
983
+                                transactionTemplate.setPropagationBehavior(
984
+                                        TransactionDefinition.PROPAGATION_REQUIRES_NEW);
985
+                                transactionTemplate.setIsolationLevel(
986
+                                        TransactionDefinition.ISOLATION_READ_COMMITTED);
987
+                                transactionTemplate.setTimeout(30); // 30秒超时
988
+
989
+                                transactionTemplate.execute(status -> {
990
+                                    try {
991
+                                        // 重新查询最新的bill数据(避免脏数据)
992
+                                        ToaBillEntity latestBill = billService.getById(billId);
993
+                                        if (latestBill == null) {
994
+                                            log.error("审批单不存在: billId={}", billId);
995
+                                            return null;
996
+                                        }
997
+
998
+                                        // 在事务中更新数据库
999
+                                        latestBill.setCompletionTime(dateline);
1000
+                                        latestBill.setStatus(BillStatus.COMPLETE.getStatus());
1001
+                                        latestBill.setArchivedTime(dateline);
1002
+                                        billService.upldate(latestBill);
1003
+
1004
+                                        log.info("查询到的审批单数据:{}", latestBill);
1005
+
1006
+                                        // 在事务中执行回调
1007
+                                        if (action.equals(BillAction.refuse.name())) {
1008
+                                            callBackLogic.callBack(processId, billId,
1009
+                                                    BillTaskStatus.REFUSE.getStatus());
1010
+                                        } else {
1011
+                                            BillTaskEntity a = new BillTaskEntity();
1012
+                                            a.setBillId(billId);
1013
+                                            a.setUserCode(sender);
1014
+                                            a.setNodeStatus(BillTaskStatus.COMPLATE.getStatus());
1015
+                                            a.setNodeName(title);
1016
+                                            a.setOpinion("通过");
1017
+                                            flowMsgLogic.sendMsg(Lists.newArrayList(a));
1018
+                                            callBackLogic.callBack(processId, billId,
1019
+                                                    BillStatus.COMPLETE.getStatus());
1020
+                                        }
1021
+
1022
+                                        return null;
1023
+                                    } catch (Exception e) {
1024
+                                        log.error("异步任务执行失败: billId={}, error={}",
1025
+                                                billId, e.getMessage(), e);
1026
+                                        // 抛出异常触发事务回滚
1027
+                                        throw new RuntimeException("异步任务执行失败", e);
1028
+                                    }
1029
+                                });
1030
+                            }
1031
+                        };
1032
+
1033
+                        CallBackThreadPoolManager.me().executeLog(businessTask);
1034
+                    }
1035
+                }
1036
+        );
930 1037
     }
931 1038
 
932 1039
     /**

+ 8 - 0
bpm-server/src/main/java/com/srm/bpmserver/logic/impl/CallBackLogicImpl.java

@@ -8,6 +8,7 @@ import com.srm.bpm.logic.service.CallBackLogic;
8 8
 import com.srm.config.BpmConfig;
9 9
 import com.srm.common.data.rest.R;
10 10
 
11
+import org.springframework.http.HttpStatus;
11 12
 import org.springframework.http.ResponseEntity;
12 13
 import org.springframework.stereotype.Service;
13 14
 
@@ -46,5 +47,12 @@ public class CallBackLogicImpl implements CallBackLogic {
46 47
 
47 48
         final ResponseEntity<R> post = restTemplateUtil.post(bpmConfig.getCallbackUrl(), data, "1");
48 49
         log.info("回调返回的数据:{}", post);
50
+
51
+        HttpStatus statusCode = post.getStatusCode();
52
+        //如果是500报错
53
+        if(statusCode !=null && statusCode.compareTo(HttpStatus.INTERNAL_SERVER_ERROR)==0){
54
+            throw new RuntimeException("回调返回数据异常");
55
+        }
56
+
49 57
     }
50 58
 }