From 16f3c0a66181095ff4d353ebe78fbaa30a36a3d3 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 22 十月 2019 14:32:42 +0800 Subject: [PATCH] rocketmq修改 --- fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java | 87 +++++++++++++++++++++++++------------------ 1 files changed, 50 insertions(+), 37 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java index c2b3dc3..8ffe63e 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java @@ -18,6 +18,10 @@ import org.springframework.transaction.annotation.Transactional; import org.yeshi.utils.NumberUtil; +import com.aliyun.openservices.ons.api.Message; +import com.aliyun.openservices.ons.api.transaction.LocalTransactionExecuter; +import com.aliyun.openservices.ons.api.transaction.TransactionProducer; +import com.aliyun.openservices.ons.api.transaction.TransactionStatus; import com.google.gson.Gson; import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper; import com.yeshi.fanli.dao.mybatis.UserInfoMapper; @@ -27,6 +31,7 @@ import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper; import com.yeshi.fanli.dto.HongBao; import com.yeshi.fanli.dto.order.CommonOrderAddResultDTO; +import com.yeshi.fanli.dto.order.OrderMQMsgDTO; import com.yeshi.fanli.entity.bus.user.HongBaoV2; import com.yeshi.fanli.entity.bus.user.Order; import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo; @@ -139,6 +144,9 @@ @Resource private PDDOrderService pddOrderService; + + @Resource(name = "orderTransactionProducer") + private TransactionProducer orderTransactionProducer; /** * 鏄惁鏄垎浜鍗� @@ -986,8 +994,7 @@ List<CommonOrderAddResultDTO> commonOrderList = null; try { commonOrderList = commonOrderService.addTaoBaoOrder(orderList, uid); - hongBaoV2Service.addHongBao(convertCommonOrder(commonOrderList), HongBaoV2.TYPE_ZIGOU); - + addHongBaoWithMQ(commonOrderList, orderId, uid, Constant.SOURCE_TYPE_TAOBAO, HongBaoV2.TYPE_ZIGOU); if (isCommonOrderAllAdd(commonOrderList)) { Order order = new Order(); order.setOrderId(orderId); @@ -1046,7 +1053,7 @@ // 鐢ㄧ涓�涓瓙璁㈠崟閿佸畾鐢ㄦ埛 try { List<CommonOrderAddResultDTO> commonOrders = commonOrderService.addTaoBaoOrder(orderList, uid); - hongBaoV2Service.addHongBao(convertCommonOrder(commonOrders), HongBaoV2.TYPE_SHARE_GOODS); + addHongBaoWithMQ(commonOrders, orderId, uid, Constant.SOURCE_TYPE_TAOBAO, HongBaoV2.TYPE_SHARE_GOODS); if (isCommonOrderAllAdd(commonOrders)) { Order order = new Order(); order.setOrderId(orderId); @@ -1059,8 +1066,6 @@ } } catch (CommonOrderException e) { e.printStackTrace(); - } catch (HongBaoException e1) { - e1.printStackTrace(); } } } @@ -1165,11 +1170,13 @@ try { List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addJDOrder(jdOrder, uid); - hongBaoV2Service.addHongBao(convertCommonOrder(commonOrderList), HongBaoV2.TYPE_ZIGOU); + addHongBaoWithMQ(commonOrderList, jdOrder.getOrderId() + "", uid, Constant.SOURCE_TYPE_JD, + HongBaoV2.TYPE_ZIGOU); + if (isCommonOrderAllAdd(commonOrderList)) { Order order = new Order(); order.setOrderId(jdOrder.getOrderId() + ""); - order.setOrderType(Constant.SOURCE_TYPE_TAOBAO); + order.setOrderType(Constant.SOURCE_TYPE_JD); order.setUserInfo(new UserInfo(uid)); try { PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order); @@ -1177,12 +1184,6 @@ } } } catch (CommonOrderException e) { - try { - LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + jdOrder.getOrderId()); - } catch (Exception e1) { - e1.printStackTrace(); - } - } catch (HongBaoException e) { try { LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + jdOrder.getOrderId()); } catch (Exception e1) { @@ -1201,11 +1202,12 @@ private void processShareJDOrder(JDOrder jdOrder, Long uid) { try { List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addJDOrder(jdOrder, uid); - hongBaoV2Service.addHongBao(convertCommonOrder(commonOrderList), HongBaoV2.TYPE_SHARE_GOODS); + addHongBaoWithMQ(commonOrderList, jdOrder.getOrderId() + "", uid, Constant.SOURCE_TYPE_JD, + HongBaoV2.TYPE_SHARE_GOODS); if (isCommonOrderAllAdd(commonOrderList)) { Order order = new Order(); order.setOrderId(jdOrder.getOrderId() + ""); - order.setOrderType(Constant.SOURCE_TYPE_TAOBAO); + order.setOrderType(Constant.SOURCE_TYPE_JD); order.setUserInfo(new UserInfo(uid)); try { PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order); @@ -1213,12 +1215,6 @@ } } } catch (CommonOrderException e) { - try { - LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + jdOrder.getOrderId()); - } catch (Exception e1) { - e1.printStackTrace(); - } - } catch (HongBaoException e) { try { LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + jdOrder.getOrderId()); } catch (Exception e1) { @@ -1315,11 +1311,12 @@ List<PDDOrder> pddOrderList = new ArrayList<>(); pddOrderList.add(pddOrder); List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addPDDOrder(pddOrderList, uid); - hongBaoV2Service.addHongBao(convertCommonOrder(commonOrderList), HongBaoV2.TYPE_ZIGOU); + addHongBaoWithMQ(commonOrderList, pddOrder.getOrderSn(), uid, Constant.SOURCE_TYPE_PDD, + HongBaoV2.TYPE_ZIGOU); if (isCommonOrderAllAdd(commonOrderList)) { Order order = new Order(); order.setOrderId(pddOrder.getOrderSn()); - order.setOrderType(Constant.SOURCE_TYPE_TAOBAO); + order.setOrderType(Constant.SOURCE_TYPE_PDD); order.setUserInfo(new UserInfo(uid)); try { PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order); @@ -1327,12 +1324,6 @@ } } } catch (CommonOrderException e) { - try { - LogHelper.errorDetailInfo(e, "addPDDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + pddOrder.getOrderSn()); - } catch (Exception e1) { - e1.printStackTrace(); - } - } catch (HongBaoException e) { try { LogHelper.errorDetailInfo(e, "addPDDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + pddOrder.getOrderSn()); } catch (Exception e1) { @@ -1353,11 +1344,12 @@ List<PDDOrder> pddOrderList = new ArrayList<>(); pddOrderList.add(pddOrder); List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addPDDOrder(pddOrderList, uid); - hongBaoV2Service.addHongBao(convertCommonOrder(commonOrderList), HongBaoV2.TYPE_SHARE_GOODS); + addHongBaoWithMQ(commonOrderList, pddOrder.getOrderSn(), uid, Constant.SOURCE_TYPE_PDD, + HongBaoV2.TYPE_SHARE_GOODS); if (isCommonOrderAllAdd(commonOrderList)) { Order order = new Order(); order.setOrderId(pddOrder.getOrderSn()); - order.setOrderType(Constant.SOURCE_TYPE_TAOBAO); + order.setOrderType(Constant.SOURCE_TYPE_PDD); order.setUserInfo(new UserInfo(uid)); try { PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order); @@ -1365,12 +1357,6 @@ } } } catch (CommonOrderException e) { - try { - LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + pddOrder.getOrderId()); - } catch (Exception e1) { - e1.printStackTrace(); - } - } catch (HongBaoException e) { try { LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + pddOrder.getOrderId()); } catch (Exception e1) { @@ -1560,4 +1546,31 @@ // 杩斿埄鍒拌处 } + private void addHongBaoWithMQ(List<CommonOrderAddResultDTO> commonOrderList, String orderId, Long uid, + int sourceType, int hongBaoType) { + final List<CommonOrder> coList = convertCommonOrder(commonOrderList); + OrderMQMsgDTO mqMsg = new OrderMQMsgDTO(orderId, sourceType, uid); + Message msg = new Message("TOPIC_ORDER", String.format("orderhongbao-%s-%s", sourceType, hongBaoType), + new Gson().toJson(mqMsg).getBytes()); + orderTransactionProducer.send(msg, new LocalTransactionExecuter() { + @Override + public TransactionStatus execute(Message arg0, Object arg1) { + int resultCode = 0; + try { + resultCode = hongBaoV2Service.addHongBao(coList, hongBaoType); + } catch (HongBaoException e) { + try { + LogHelper.errorDetailInfo(e, "addJDOrder鎴朼ddHongBao鍑洪敊", "璁㈠崟鍙�:" + orderId); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + if (resultCode != 0) + return TransactionStatus.CommitTransaction; + else + return TransactionStatus.RollbackTransaction; + } + }, null); + } + } -- Gitblit v1.8.0