admin
2019-10-26 a56bd9db6b5c989fb2eaecd3a854ac6782b55f95
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -16,11 +16,18 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.entity.DateInfo;
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.user.UserSystemCouponMapper;
import com.yeshi.fanli.dto.msg.MsgOtherCouponActivateDTO;
import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO;
import com.yeshi.fanli.dto.msg.MsgOtherSystemGiveDTO;
import com.yeshi.fanli.dto.user.coupon.UserSystemCouponUseMQMsgDTO;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.msg.MsgOtherDetail.MsgTypeOtherTypeEnum;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.Order;
@@ -37,6 +44,8 @@
import com.yeshi.fanli.entity.system.SystemCoupon;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.money.UserMoneyDetailException;
import com.yeshi.fanli.exception.order.HongBaoException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.exception.user.UserSystemCouponException;
import com.yeshi.fanli.log.LogHelper;
@@ -66,8 +75,11 @@
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TokenUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.util.factory.msg.MsgOtherSystemGiveDTOFactory;
import com.yeshi.fanli.util.rocketmq.MQTagConstant;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import com.yeshi.fanli.vo.order.CommonOrderVO;
@@ -149,6 +161,9 @@
   @Resource
   private TokenRecordService tokenRecordService;
   @Resource(name = "orderTransactionProducer")
   private TransactionProducer orderTransactionProducer;
   @Override
   public int insertSelective(UserSystemCoupon record) {
@@ -764,55 +779,79 @@
   @Transactional
   @Override
   public void useOrderCoupon(Long uid, Long id, String orderNo, Integer sourceType)
   public void useOrderCoupon(Long uid, Long id, String orderNo, Integer sourceType, AcceptData acceptData)
         throws UserSystemCouponException, Exception {
      if (id == null) {
      if (id == null)
         throw new UserSystemCouponException(1, "券id不存在");
      }
      if (sourceType == null) {
      if (sourceType == null)
         sourceType = Constant.SOURCE_TYPE_TAOBAO;
      }
      // 用户券
      UserSystemCoupon userSystemCoupon = selectByPrimaryKey(id);
      if (userSystemCoupon == null) {
      if (userSystemCoupon == null)
         throw new UserSystemCouponException(1, "券信息不存在");
      }
      if (userSystemCoupon.getUid().longValue() != uid) {
      if (userSystemCoupon.getUid().longValue() != uid)
         throw new UserSystemCouponException(1, "不属于该用户的券");
      }
      Integer state = userSystemCoupon.getState();
      Integer stateActivated = userSystemCoupon.getStateActivated();
      if (UserSystemCoupon.STATE_CAN_USE != state || 1 != stateActivated) {
      if (UserSystemCoupon.STATE_CAN_USE != state || 1 != stateActivated)
         throw new UserSystemCouponException(1, "该券不能被使用");
      }
      // 系统券信息
      SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon();
      if (systemCoupon != null && systemCoupon.getId() != null) {
         systemCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
         if (systemCoupon == null) {
         if (systemCoupon == null)
            throw new UserSystemCouponException(1, "券已不存在");
         }
      }
      if (systemCoupon.getType() != CouponTypeEnum.rebatePercentCoupon) {
      if (systemCoupon.getType() != CouponTypeEnum.rebatePercentCoupon)
         throw new UserSystemCouponException(1, "券类型不匹配");
      }
      if (orderNo == null || orderNo.trim().length() == 0) {
      if (orderNo == null || orderNo.trim().length() == 0)
         throw new UserSystemCouponException(1, "订单号不存在");
      }
      // 订单
      CommonOrderVO orderVO = commonOrderService.getCommonOrderByOrderNo(uid, orderNo, CommonOrder.STATE_JS,
            sourceType);
      if (orderVO == null) {
      if (orderVO == null)
         throw new UserSystemCouponException(1, "订单信息获取失败");
      // 版本区分:2.0.2
      if (VersionUtil.greaterThan_2_0_2(acceptData.getPlatform(), acceptData.getVersion())) {
         Date thirdCreateTime = orderVO.getThirdCreateTime();
         if (thirdCreateTime != null) {
            long downOrderTime = thirdCreateTime.getTime();
            long limitDate = Long.parseLong("1573315200000"); // TODO
                                                   // 日期待确定2019-11-10
            if (downOrderTime > limitDate) {
               Date endDay = DateUtil.plusDayDate(4, thirdCreateTime);
               long currentTime = java.lang.System.currentTimeMillis();
               if (endDay.getTime() > currentTime) {
                  boolean valid = false;
                  DateInfo dateInfo = DateUtil.dateDiff3(currentTime, endDay.getTime());
                  if (dateInfo != null) {
                     if (dateInfo.getDay() > 0)
                        valid = true;
                     if (!valid && dateInfo.getHour() > 0)
                        valid = true;
                     if (!valid && dateInfo.getMinute() > 0)
                        valid = true;
                     if (!valid && dateInfo.getSecond() >= 0)
                        valid = true;
                  }
                  if (!valid)
                     throw new UserSystemCouponException(1, "该订单已不能使用使用奖励券,详情请咨询客服");
               }
            }
         }
      }
      BigDecimal hongBao = orderVO.getHongBao();
@@ -835,59 +874,81 @@
         // 提成金额
         BigDecimal money = MoneyBigDecimalUtil.mul(hongBao, percent);
         // 1、插入红包数据
         HongBaoV2 hongBaoV2 = new HongBaoV2();
         hongBaoV2.setMoney(money);
         hongBaoV2.setType(HongBaoV2.TYPE_COUPON);
         hongBaoV2.setState(HongBaoV2.STATE_YILINGQU);
         hongBaoV2.setVersion(2);
         hongBaoV2.setCreateTime(new Date());
         hongBaoV2.setUpdateTime(new Date());
         hongBaoV2.setUserInfo(new UserInfo(uid));
         hongBaoV2.setPreGetTime(new Date());
         hongBaoV2.setGetTime(new Date());
         UserSystemCouponUseMQMsgDTO mqMsg = new UserSystemCouponUseMQMsgDTO(userSystemCoupon.getId(),
               order.getOrderId(), sourceType);
         // 事务消息
         Message msg = new Message(MQTopicName.TOPIC_SYSTEM_COUPON_USE.name(),systemCoupon.getType().name(),
               new Gson().toJson(mqMsg).getBytes());
         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) {
                  return TransactionStatus.RollbackTransaction;
               }
            }
         }, null);
         hongBaoV2Service.insertSelective(hongBaoV2);
      }
   }
         // 2.插入关联
         orderHongBaoMapService.addOrderHongBaoMap(hongBaoV2, order);
   @Transactional
   private void useRewardCouponCore(UserSystemCoupon userSystemCoupon, BigDecimal money, BigDecimal hongBao, Long uid,
         Order order) throws UserMoneyDetailException {
      // 1、插入红包数据
      HongBaoV2 hongBaoV2 = new HongBaoV2();
      hongBaoV2.setMoney(money);
      hongBaoV2.setType(HongBaoV2.TYPE_COUPON);
      hongBaoV2.setState(HongBaoV2.STATE_YILINGQU);
      hongBaoV2.setVersion(2);
      hongBaoV2.setCreateTime(new Date());
      hongBaoV2.setUpdateTime(new Date());
      hongBaoV2.setUserInfo(new UserInfo(uid));
      hongBaoV2.setPreGetTime(new Date());
      hongBaoV2.setGetTime(new Date());
         // 3.插入资金明细,用户余额
         UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createOrderReward(orderNo, sourceType, money,
               new UserInfo(uid));
         userMoneyDetailService.addUserMoneyDetail(userMoneyDetail);
         userInfoService.addMoney(new UserInfo(uid), money);
      hongBaoV2Service.insertSelective(hongBaoV2);
         // 插入资金通知
         userMoneyMsgNotificationService.orderReward(uid, orderNo, sourceType, money,
               userInfoService.selectByPKey(uid).getMyHongBao());
      // 2.插入关联
      orderHongBaoMapService.addOrderHongBaoMap(hongBaoV2, order);
         /* 用户券信息状态及记录处理 */
      // 3.插入资金明细,用户余额
      UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createOrderReward(order.getOrderId(),
            order.getOrderType(), money, new UserInfo(uid));
      userMoneyDetailService.addUserMoneyDetail(userMoneyDetail);
      userInfoService.addMoney(new UserInfo(uid), money);
         Date date = new Date();
         // 更新券状态:已使用
         userSystemCoupon.setState(UserSystemCoupon.STATE_END_USE);
         userSystemCoupon.setUseTime(date);
         userSystemCoupon.setUpdateTime(date);
         userSystemCouponMapper.updateByPrimaryKeySelective(userSystemCoupon);
      // 插入资金通知
      userMoneyMsgNotificationService.orderReward(uid, order.getOrderId(), order.getOrderType(), money,
            userInfoService.selectByPKey(uid).getMyHongBao());
         // 插入使用券记录
         UserSystemCouponRecord couponRecord = new UserSystemCouponRecord();
         couponRecord.setState(UserSystemCouponRecord.STATE_SUCCESS);
         couponRecord.setGoodSource(sourceType);
         couponRecord.setOrderNo(orderNo);
         couponRecord.setUserSystemCoupon(userSystemCoupon);
         couponRecord.setCouponType(systemCoupon.getType().name());
         couponRecord.setCreateTime(date);
         couponRecord.setUpdateTime(date);
         userSystemCouponRecordService.insertSelective(couponRecord);
      /* 用户券信息状态及记录处理 */
         // 消息推送
         try {
            userOtherMsgNotificationService.rewardCouponUsed(userSystemCoupon, hongBao, money, orderNo);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
         }
      Date date = new Date();
      // 更新券状态:已使用
      userSystemCoupon.setState(UserSystemCoupon.STATE_END_USE);
      userSystemCoupon.setUseTime(date);
      userSystemCoupon.setUpdateTime(date);
      userSystemCouponMapper.updateByPrimaryKeySelective(userSystemCoupon);
      // 插入使用券记录
      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.setCreateTime(date);
      couponRecord.setUpdateTime(date);
      userSystemCouponRecordService.insertSelective(couponRecord);
      // 消息推送
      try {
         userOtherMsgNotificationService.rewardCouponUsed(userSystemCoupon, hongBao, money, order.getOrderId());
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
   }
@@ -1510,12 +1571,12 @@
   }
   @Override
   @Transactional(rollbackFor=Exception.class)
   @Transactional(rollbackFor = Exception.class)
   public void systemGiveRewardCoupon(Long uid, int num, String orderId, Integer orderType, String reason)
         throws Exception {
      // 添加奖励券
      insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(), "系统赠送",
            new BigDecimal(configService.get("exchange_rebate_percent")),false);
            new BigDecimal(configService.get("exchange_rebate_percent")), false);
      // 添加奖励券消息
      userOtherMsgNotificationService.systemGiveRewardCoupon(uid, null, MsgOtherSystemGiveDTOFactory
            .createRewardCouponGive(num, "请按照返利奖励券规则使用", "返利奖励券", orderId, orderType, reason));