admin
2019-10-22 04f5c3bd20524c97dce07cc03bb24e190303083a
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,10 +31,13 @@
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;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.elme.ElmeHongBaoOrderMap;
import com.yeshi.fanli.entity.elme.ElmeOrder;
import com.yeshi.fanli.entity.jd.JDOrder;
import com.yeshi.fanli.entity.jd.JDOrderItem;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
@@ -48,6 +55,8 @@
import com.yeshi.fanli.exception.order.OrderItemException;
import com.yeshi.fanli.exception.order.TaoBaoWeiQuanException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.elme.ElmeHongBaoOrderMapService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderService;
import com.yeshi.fanli.service.inter.hongbao.AccountDetailsHongBaoMapService;
import com.yeshi.fanli.service.inter.money.UserMoneyService;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
@@ -135,6 +144,9 @@
   @Resource
   private PDDOrderService pddOrderService;
   @Resource(name = "orderTransactionProducer")
   private TransactionProducer orderTransactionProducer;
   /**
    * 是否是分享订单
@@ -982,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);
@@ -1042,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);
@@ -1055,8 +1066,6 @@
            }
         } catch (CommonOrderException e) {
            e.printStackTrace();
         } catch (HongBaoException e1) {
            e1.printStackTrace();
         }
      }
   }
@@ -1161,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);
@@ -1173,12 +1184,6 @@
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + jdOrder.getOrderId());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      } catch (HongBaoException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + jdOrder.getOrderId());
         } catch (Exception e1) {
@@ -1197,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);
@@ -1209,12 +1215,6 @@
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + jdOrder.getOrderId());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      } catch (HongBaoException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + jdOrder.getOrderId());
         } catch (Exception e1) {
@@ -1311,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);
@@ -1323,12 +1324,6 @@
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addPDDOrder或addHongBao出错", "订单号:" + pddOrder.getOrderSn());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      } catch (HongBaoException e) {
         try {
            LogHelper.errorDetailInfo(e, "addPDDOrder或addHongBao出错", "订单号:" + pddOrder.getOrderSn());
         } catch (Exception e1) {
@@ -1349,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);
@@ -1361,12 +1357,6 @@
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + pddOrder.getOrderId());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      } catch (HongBaoException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + pddOrder.getOrderId());
         } catch (Exception e1) {
@@ -1490,7 +1480,97 @@
            }
      }
   }
   public void elmeFanli() {
   }
   // 饿了么订单
   @Resource
   private ElmeHongBaoOrderMapService elmeHongBaoOrderMapService;
   @Resource
   private ElmeOrderService elmeOrderService;
   /**
    * 获取饿了么可以返利的红包
    *
    * @return
    */
   @Override
   public List<HongBaoV2> getCanBalanceElmeFanliHongBao() {
      List<Integer> list = new ArrayList<>();
      list.add(HongBaoV2.TYPE_ELME);
      return hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(list, null, 0, 1000);
   }
   @Override
   @Transactional
   public void elmeFanli(Long hongBaoId) {
      // 查询详情
      HongBaoV2 hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBaoId);
      if (hongBao.getType() == HongBaoV2.TYPE_ELME && hongBao.getState() == HongBaoV2.STATE_KELINGQU
            && hongBao.getPreGetTime() != null && System.currentTimeMillis() > hongBao.getPreGetTime().getTime()) {// 可以到账了
         // 判断用户状态
         UserInfo user = userInfoMapper.selectByPrimaryKey(hongBao.getUserInfo().getId());
         if (user == null || user.getState() != UserInfo.STATE_NORMAL)
            return;
         ElmeHongBaoOrderMap map = elmeHongBaoOrderMapService.selectByHongBaoId(hongBaoId);
         if (map == null)
            return;
         ElmeOrder order = elmeOrderService.selectByPrimaryKey(map.getElmeOrder().getId());
         if (order == null)
            return;
         // 到账,加数据
         HongBaoV2 update = new HongBaoV2(hongBao.getId());
         update.setUpdateTime(new Date());
         update.setGetTime(new Date());
         update.setState(HongBaoV2.STATE_YILINGQU);
         hongBaoV2Mapper.updateByPrimaryKeySelective(update);
         // 增加资金
         UserMoneyDetail detail = null;
         try {
            detail = UserMoneyDetailFactory.createElmeFanLi(hongBao.getUserInfo().getId(), order.getOrderId(),
                  hongBao.getId(), hongBao.getMoney());
         } catch (UserMoneyDetailException e) {
            e.printStackTrace();
         }
         userMoneyService.addUserMoney(hongBao.getUserInfo().getId(), hongBao.getMoney(), detail);
         BigDecimal balance = userInfoMapper.selectByPrimaryKey(hongBao.getUserInfo().getId()).getMyHongBao();
         // 添加消息
         userMoneyMsgNotificationService.elmeFanli(hongBao.getUserInfo().getId(), order.getOrderId(),
               hongBao.getMoney(), balance);
      }
      // 返利到账
   }
   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或addHongBao出错", "订单号:" + orderId);
               } catch (Exception e1) {
                  e1.printStackTrace();
               }
            }
            if (resultCode != 0)
               return TransactionStatus.CommitTransaction;
            else
               return TransactionStatus.RollbackTransaction;
         }
      }, null);
   }
}