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;
   /**
    * 是否是分享订单
@@ -488,16 +500,7 @@
               if (olist != null && olist.size() > 0) {
                  long time = TimeUtil.convertToTimeTemp(olist.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss");
                  olist = TaoKeOrderApiUtil.getTaoBaoAllOrder(TimeUtil.getGernalTime(time, "yyyy-MM-dd HH:mm:ss"),
                        TaoBaoConstant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET,
                        TaoKeOrderApiUtil.TK_STATUS_ALL);
                  List<TaoBaoOrder> thirdOList = TaoKeOrderApiUtil.getTaoBaoThirdAllOrder(
                        TimeUtil.getGernalTime(time, "yyyy-MM-dd HH:mm:ss"), 30, 1,
                        TaoKeOrderApiUtil.TK_STATUS_ALL, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                        TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
                  if (thirdOList != null && thirdOList.size() > 0)
                     olist.addAll(thirdOList);
                  olist = TaoKeOrderApiUtil.getTaoBaoCommonOrderList(time, time + 1000 * 2L, 1);
                  if (olist != null && olist.size() > 0) {
                     for (int i = 0; i < olist.size(); i++) {
                        if (!olist.get(i).getOrderId()
@@ -991,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);
@@ -1051,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);
@@ -1064,8 +1066,6 @@
            }
         } catch (CommonOrderException e) {
            e.printStackTrace();
         } catch (HongBaoException e1) {
            e1.printStackTrace();
         }
      }
   }
@@ -1170,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);
@@ -1182,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) {
@@ -1206,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);
@@ -1218,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) {
@@ -1320,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);
@@ -1332,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) {
@@ -1358,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);
@@ -1370,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) {
@@ -1452,4 +1433,144 @@
         return false;
   }
   @Transactional(rollbackFor = Exception.class)
   public void invalidHongBaoV2AndGiveGodenCorn(Long hongBaoId, Long uid, String orderId, int sourceType,
         String beiZhu) throws Exception {
      HongBaoV2 v2 = new HongBaoV2(hongBaoId);
      v2.setState(HongBaoV2.STATE_SHIXIAO);
      v2.setBeizhu(beiZhu);
      v2.setUpdateTime(new Date());
      hongBaoV2Service.updateByPrimaryKeySelective(v2);
      List<HongBaoV2> children = hongBaoV2Service.listChildrenById(hongBaoId);
      if (children != null)
         for (HongBaoV2 child : children) {
            HongBaoV2 update = new HongBaoV2(child.getId());
            update.setState(HongBaoV2.STATE_SHIXIAO);
            update.setBeizhu(beiZhu);
            update.setUpdateTime(new Date());
            hongBaoV2Service.updateByPrimaryKeySelective(update);
         }
      userSystemCouponService.systemGiveRewardCoupon(uid, 1, orderId, sourceType, "因商家违约未能结算返利或商家已经关店");
   }
   @Transactional
   @Override
   public void doTaoBaoSellerNotPaid(TaoBaoOrder order) {
      // 根据交易ID查询
      if (!StringUtil.isNullOrEmpty(order.getTradeId())) {
         List<CommonOrder> commonOrderList = commonOrderService
               .listBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO, order.getTradeId());
         if (commonOrderList != null)
            // 查询主订单
            for (CommonOrder commonOrder : commonOrderList) {
            // 查询主红包
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null) {
            if (hongBaoOrder.getHongBaoV2().getState() == HongBaoV2.STATE_BUKELINGQU) {
            // 订单失效,赠送金币
            try {
            invalidHongBaoV2AndGiveGodenCorn(hongBaoOrder.getHongBaoV2().getId(), hongBaoOrder.getHongBaoV2().getUserInfo().getId(), commonOrder.getOrderNo(), commonOrder.getSourceType(), "订单成功,商家未打款");
            } catch (Exception e) {
            e.printStackTrace();
            }
            }
            }
            }
      }
   }
   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);
   }
}