yujian
2019-10-29 50361d9b29962f66b05df0732820840c6531f84f
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -12,13 +12,16 @@
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.entity.DateInfo;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.transaction.LocalTransactionExecuter;
import com.aliyun.openservices.ons.api.transaction.TransactionProducer;
import com.aliyun.openservices.ons.api.transaction.TransactionStatus;
@@ -160,6 +163,9 @@
   @Resource
   private TokenRecordService tokenRecordService;
   @Resource(name = "transactionManager")
   private DataSourceTransactionManager transactionManager;
   @Resource(name = "orderTransactionProducer")
   private TransactionProducer orderTransactionProducer;
@@ -882,24 +888,31 @@
         // 事务消息
         Message msg = new Message(MQTopicName.TOPIC_SYSTEM_COUPON_USE.name(),systemCoupon.getType().name(),
               new Gson().toJson(mqMsg).getBytes());
         orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
         try {
            SendResult result = orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
               try {
                  useRewardCouponCore(userSystemCoupon, money, hongBao, uid, order);
                  return TransactionStatus.CommitTransaction;
               } catch (UserMoneyDetailException e) {
                  } catch (Exception e) {
                  return TransactionStatus.RollbackTransaction;
               }
            }
         }, null);
            System.out.println(result);
         } catch (Exception e) {
            throw new Exception();
         }
      }
   }
   @Transactional
   private void useRewardCouponCore(UserSystemCoupon userSystemCoupon, BigDecimal money, BigDecimal hongBao, Long uid,
         Order order) throws UserMoneyDetailException {
   public void useRewardCouponCore(UserSystemCoupon userSystemCoupon, BigDecimal money, BigDecimal hongBao, Long uid,
         Order order) throws Exception {
      org.springframework.transaction.TransactionStatus transactionStatus = transactionManager
            .getTransaction(new DefaultTransactionDefinition());
      try {
      // 1、插入红包数据
      HongBaoV2 hongBaoV2 = new HongBaoV2();
      hongBaoV2.setMoney(money);
@@ -923,10 +936,6 @@
      userMoneyDetailService.addUserMoneyDetail(userMoneyDetail);
      userInfoService.addMoney(new UserInfo(uid), money);
      // 插入资金通知
      userMoneyMsgNotificationService.orderReward(uid, order.getOrderId(), order.getOrderType(), money,
            userInfoService.selectByPKey(uid).getMyHongBao());
      /* 用户券信息状态及记录处理 */
      Date date = new Date();
@@ -936,23 +945,34 @@
      userSystemCoupon.setUpdateTime(date);
      userSystemCouponMapper.updateByPrimaryKeySelective(userSystemCoupon);
         SystemCoupon coupon = systemCouponService.selectByPrimaryKey(userSystemCoupon.getSystemCoupon().getId());
      // 插入使用券记录
      UserSystemCouponRecord couponRecord = new UserSystemCouponRecord();
      couponRecord.setState(UserSystemCouponRecord.STATE_SUCCESS);
      couponRecord.setGoodSource(order.getOrderType());
      couponRecord.setOrderNo(order.getOrderId());
      couponRecord.setUserSystemCoupon(userSystemCoupon);
      couponRecord.setCouponType(userSystemCoupon.getSystemCoupon().getType().name());
         couponRecord.setCouponType(coupon.getType().name());
      couponRecord.setCreateTime(date);
      couponRecord.setUpdateTime(date);
      userSystemCouponRecordService.insertSelective(couponRecord);
         // 插入资金通知
         userMoneyMsgNotificationService.orderReward(uid, order.getOrderId(), order.getOrderType(), money,
               userInfoService.selectByPKey(uid).getMyHongBao());
      // 消息推送
      try {
         userOtherMsgNotificationService.rewardCouponUsed(userSystemCoupon, hongBao, money, order.getOrderId());
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
         transactionManager.commit(transactionStatus);
      } catch (Exception e) {
         e.printStackTrace();
         transactionManager.rollback(transactionStatus);
         throw new Exception();
      }
   }