admin
2019-11-05 e021e32a6ff0ec216f69c24f7ea929798bdce4c5
fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
@@ -7,7 +7,16 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.entity.wx.WXAPPInfo;
import org.yeshi.utils.exception.WXOrderException;
import org.yeshi.utils.wx.WXPayUtil;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.google.gson.Gson;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.BanLiShopOrderMQMsg;
import com.yeshi.fanli.entity.redpack.RedPackDetail;
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsClass;
@@ -24,6 +33,8 @@
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil;
@Service
public class BanLiShopOrderPayServiceImpl implements BanLiShopOrderPayService {
@@ -45,6 +56,9 @@
   @Resource
   private RedPackDetailService redPackDetailService;
   @Resource(name = "producer")
   private Producer producer;
   @Transactional
   @Override
   public void payOrderByHongBao(Long orderId) throws BanLiShopOrderException, RedPackBalanceException {
@@ -53,7 +67,7 @@
         throw new BanLiShopOrderException(1, "订单不存在");
      }
      if (order.getHongBaoPayment() == null)
         throw new BanLiShopOrderException(2, "不需要采用红包支付");
         return;
      if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
         throw new BanLiShopOrderException(3, "重复支付");
@@ -101,7 +115,7 @@
      if (order.getMoneyPayment() == null)
         throw new BanLiShopOrderException(2, "不需要采用现金支付");
      if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
      if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() != BanLiShopOrder.PAY_STATE_NOPAY) {
         throw new BanLiShopOrderException(3, "重复支付");
      }
@@ -141,15 +155,131 @@
      }
      if (detail != null) {
         RedPackDetail oldDetail = redPackDetailService.getByIdentifyCode(detail.getIdentifyCode());
         if (oldDetail != null) {// 外显红包详情
            RedPackDetail update = new RedPackDetail();
            update.setId(oldDetail.getId());
            update.setDisplay(true);
            redPackDetailService.updateByPrimaryKeySelective(update);
         redPackDetailService.changeDisplayByIdentifyCode(detail.getIdentifyCode(), true);
      }
   }
   @Transactional
   @Override
   public void refund(Long orderId) throws BanLiShopOrderException {
      // 订单退款
      // 查询订单是否已经被拒绝
      BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKeyForUpdate(orderId);
      if (order == null)
         throw new BanLiShopOrderException(1, "订单不存在");
      if (order.getState() != BanLiShopOrder.STATE_REJECT)
         throw new BanLiShopOrderException(2, "订单未被拒绝/订单已退款");
      BanLiShopOrder update = new BanLiShopOrder();
      update.setId(order.getId());
      if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
         BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
         BanLiShopGoodsClass goodsClass = banLiShopGoodsClassService
               .selectByPrimaryKey(goods.getGoodsClass().getId());
         BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
         // 红包退款
         RedPackDetail detail = null;
         try {
            detail = RedPackDetailFactory.createShopOrderDrawBack(orderId, order.getUid(), goodsClass.getName(),
                  set.getName(), order.getHongBaoPayment());
         } catch (RedPackDetailException e) {
            e.printStackTrace();
         }
         if (detail == null)
            throw new BanLiShopOrderException(4, "红包详情失败");
         try {
            redPackBalanceService.addRedPack(order.getUid(), order.getHongBaoPayment(), detail);
         } catch (RedPackBalanceException e) {
            throw new BanLiShopOrderException(5, "红包退款失败");
         }
         update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
         order.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
      }
      if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
         try {
            boolean success = BanLiShopWXPayUtil.refund(order.getOrderNo(), order.getMoneyPayment());
            if (success) {
               update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUNDING);
               order.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUNDING);
               // 发送退款消息
               BanLiShopOrderMQMsg msg = new BanLiShopOrderMQMsg(order.getId(), order.getUid());
               Message message = new Message(MQTopicName.TOPIC_ORDER.name(),
                     OrderTopicTagEnum.banLiShopOrderRefund.name(), new Gson().toJson(msg).getBytes());
               message.setStartDeliverTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24L);// 延时24小时通知检测微信退款状态
               SendResult result = producer.send(message);
               if (result == null) {
                  throw new BanLiShopOrderException(8, "消息发送失败");
               }
            } else
               throw new BanLiShopOrderException(6, "微信支付退款失败");
         } catch (WXOrderException e) {
            throw new BanLiShopOrderException(6, "微信支付退款失败");
         }
      }
      // 检查 是否退款成功
      if (update.getMoneyPaymentState() != null
            && update.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_REFUNDING) {
         try {
            boolean isS = BanLiShopWXPayUtil.isRefundSuccess(order.getOrderNo());
            if (isS) {
               update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
               order.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
            }
         } catch (WXOrderException e) {
            throw new BanLiShopOrderException(7, "微信支付退款查询失败");
         }
      }
      if (update.getHongBaoPaymentState() != null || update.getMoneyPaymentState() != null) {
         // 获取整个订单的退款状态
         int state = getRefundState(order);
         update.setState(state);
         update.setUpdateTime(new Date());
         banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
      }
   }
   /**
    * 获取整个退款状态
    *
    * @param order
    * @return
    */
   private int getRefundState(BanLiShopOrder order) {
      int refundCount = 0;
      int refundSuccessCount = 0;
      if (order.getHongBaoPayment() != null) {
         refundCount++;
         if (order.getHongBaoPaymentState() != null
               && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_REFUND)
            refundSuccessCount++;
      }
      if (order.getBalancePayment() != null) {
         refundCount++;
         if (order.getBalancePaymentState() != null
               && order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_REFUND)
            refundSuccessCount++;
      }
      if (order.getMoneyPayment() != null) {
         refundCount++;
         if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_REFUND)
            refundSuccessCount++;
      }
      if (refundCount == refundSuccessCount)
         return BanLiShopOrder.STATE_REJECT_REFUND_SUCCESS;// 退款成功
      else {
         if (refundSuccessCount == 0)
            return BanLiShopOrder.STATE_REJECT;// 退款中
         else
            return BanLiShopOrder.STATE_REJECT_REFUND_PART_SUCCESS;// 部分退款成功
      }
   }
}