yujian
2019-12-12 8c04d27e5cebb7e654c208533e9567c4df2c8acc
fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
@@ -7,9 +7,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.entity.wx.WXAPPInfo;
import org.yeshi.utils.NumberUtil;
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;
@@ -17,14 +16,19 @@
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.dto.msg.MsgRedPackUseContentDTO;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackDetail;
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsClass;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.entity.shop.ChargeTypeEnum;
import com.yeshi.fanli.exception.redpack.RedPackBalanceException;
import com.yeshi.fanli.exception.redpack.RedPackDetailException;
import com.yeshi.fanli.exception.shop.BanLiShopOrderException;
import com.yeshi.fanli.exception.shop.FuLuChargeException;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsClassService;
@@ -32,7 +36,11 @@
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.charge.FuLuChargeApiUtil;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil;
@@ -55,6 +63,9 @@
   @Resource
   private RedPackDetailService redPackDetailService;
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   @Resource(name = "producer")
   private Producer producer;
@@ -98,7 +109,10 @@
               || order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID)
               && (order.getMoneyPaymentState() == null
                     || order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID))// 其他待支付项已经支付
         {
            update.setState(BanLiShopOrder.STATE_PAID);
            update.setStateDesc("支付成功");
         }
      banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
      if (update.getState() != null && update.getState() == BanLiShopOrder.STATE_PAID) {
         paySuccess(order);
@@ -134,7 +148,10 @@
               || order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID)
               && (order.getHongBaoPaymentState() == null
                     || order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID))// 其他待支付项已经支付
         {
            update.setState(BanLiShopOrder.STATE_PAID);
            update.setStateDesc("支付成功");
         }
      banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
      if (update.getState() != null && update.getState() == BanLiShopOrder.STATE_PAID) {
         paySuccess(order);
@@ -145,18 +162,6 @@
    * 支付成功
    */
   private void paySuccess(BanLiShopOrder order) {
      // 将红包明细外显
      RedPackDetail detail = null;
      try {
         detail = RedPackDetailFactory.createUseByShopOrder(order.getId(), order.getUid(), "", "",
               order.getHongBaoPayment());
      } catch (RedPackDetailException e) {
         e.printStackTrace();
      }
      if (detail != null) {
         redPackDetailService.changeDisplayByIdentifyCode(detail.getIdentifyCode(), true);
      }
   }
   @Transactional
@@ -167,7 +172,8 @@
      BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKeyForUpdate(orderId);
      if (order == null)
         throw new BanLiShopOrderException(1, "订单不存在");
      if (order.getState() != BanLiShopOrder.STATE_REJECT)
      if (order.getState() == BanLiShopOrder.STATE_REJECT_REFUND_FAIL
            || order.getState() == BanLiShopOrder.STATE_REJECT_REFUND_SUCCESS)
         throw new BanLiShopOrderException(2, "订单未被拒绝/订单已退款");
      BanLiShopOrder update = new BanLiShopOrder();
@@ -205,9 +211,9 @@
               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小时通知检测微信退款状态
               Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER,
                     OrderTopicTagEnum.banLiShopOrderRefund, msg);
               message.setStartDeliverTime(System.currentTimeMillis() + 1000 * 60 * 5L);// 延时24小时通知检测微信退款状态
               SendResult result = producer.send(message);
               if (result == null) {
                  throw new BanLiShopOrderException(8, "消息发送失败");
@@ -218,10 +224,9 @@
            throw new BanLiShopOrderException(6, "微信支付退款失败");
         }
      }
      // 检查 是否退款成功
      if (update.getMoneyPaymentState() != null
            && update.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_REFUNDING) {
      if (order.getMoneyPaymentState() != null
            && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_REFUNDING) {
         try {
            boolean isS = BanLiShopWXPayUtil.isRefundSuccess(order.getOrderNo());
            if (isS) {
@@ -282,4 +287,78 @@
      }
   }
   @Transactional
   @Override
   public void charge(Long orderId) throws BanLiShopOrderException {
      BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKey(orderId);
      if (order == null)
         throw new BanLiShopOrderException(1, "订单不存在");
      if (order.getState() != BanLiShopOrder.STATE_PAID) {
         throw new BanLiShopOrderException(100, "订单不是处于付款状态");
      }
      if (order.getChargeAccountType() == ChargeTypeEnum.phone) {
         if (!StringUtil.isMobile(order.getChargeAccount())) {
            throw new BanLiShopOrderException(101, "电话号码格式不正确");
         }
      } else if (order.getChargeAccountType() == ChargeTypeEnum.qq) {
         if (!NumberUtil.isNumeric(order.getChargeAccount())) {
            throw new BanLiShopOrderException(101, "QQ号格式错误");
         }
      } else {
         throw new BanLiShopOrderException(102, "非充值商品");
      }
      // 获取福禄的套餐
      BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
      if (set == null) {
         throw new BanLiShopOrderException(104, "商品套餐已下线");
      }
      if (StringUtil.isNullOrEmpty(set.getChargeFuLuNum())) {
         throw new BanLiShopOrderException(105, "福禄充值ID为空");
      }
      // 改变状态为成功
      BanLiShopOrder update = new BanLiShopOrder(order.getId());
      update.setState(BanLiShopOrder.STATE_SUCCESS);
      update.setStateDesc("充值成功");
      update.setUpdateTime(new Date());
      update.setSuccessTime(new Date());
      banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
      if (order.getHongBaoPayment() != null && order.getHongBaoPayment().compareTo(new BigDecimal(0)) > 0) {
         // 将红包明细外显
         RedPackDetail detail = null;
         try {
            detail = RedPackDetailFactory.createUseByShopOrder(order.getId(), order.getUid(), "", "",
                  order.getHongBaoPayment());
         } catch (RedPackDetailException e) {
            e.printStackTrace();
         }
         if (detail != null) {
            redPackDetailService.changeDisplayByIdentifyCode(detail.getIdentifyCode(), true);
         }
      }
      // 消息发送
      MsgRedPackUseContentDTO dto = new MsgRedPackUseContentDTO();
      dto.setGoodsSetName(order.getOrderGoods().getSetName());
      dto.setHongBao(order.getHongBaoPayment());
      dto.setMoney(order.getMoneyPayment());
      dto.setTime(new Date());
      userMoneyMsgNotificationService.redPackMsg(order.getUid(), MsgTypeMoneyTypeEnum.redPackUseSuccess,
            new Gson().toJson(dto), null);
      try {
         if (Constant.IS_TEST) {
            FuLuChargeApiUtil.shaXiangCharge(set.getChargeFuLuNum(), "banlishop" + order.getOrderNo(),
                  order.getChargeAccount());
         } else
            FuLuChargeApiUtil.charge(set.getChargeFuLuNum(), "banlishop" + order.getOrderNo(),
                  order.getChargeAccount());
      } catch (FuLuChargeException e) {
         throw new BanLiShopOrderException(e.getCode(), e.getMsg());
      }
   }
}