yujian
2019-11-05 09c716a16d02ddb06df2bc2af5e588128bc7fdb4
fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java
@@ -6,13 +6,19 @@
import javax.annotation.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
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;
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.shop.BanLiShopOrderGoodsMapper;
import com.yeshi.fanli.dao.mybatis.shop.BanLiShopOrderMapper;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.BanLiShopOrderMQMsg;
@@ -21,7 +27,9 @@
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.BanLiShopGoodsSetsPay;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.entity.shop.BanLiShopOrderGoods;
import com.yeshi.fanli.exception.mq.MQUnSendInfoException;
import com.yeshi.fanli.exception.redpack.RedPackBalanceException;
import com.yeshi.fanli.exception.redpack.RedPackDetailException;
@@ -30,6 +38,7 @@
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsClassService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.Constant;
@@ -47,6 +56,9 @@
   private BanLiShopGoodsSetService banLiShopGoodsSetService;
   @Resource
   private BanLiShopGoodsSetPayService banLiShopGoodsSetPayService;
   @Resource
   private BanLiShopGoodsService banLiShopGoodsService;
   @Resource
@@ -61,6 +73,15 @@
   @Resource
   private MQUnSendInfoService mqUnSendInfoService;
   @Resource
   private BanLiShopOrderGoodsMapper banLiShopOrderGoodsMapper;
   @Resource(name = "orderTransactionProducer")
   private TransactionProducer orderTransactionProducer;
   @Resource(name = "transactionManager")
   private DataSourceTransactionManager transactionManager;
   @Transactional
   @Override
   public void addOrder(BanLiShopOrder order) throws BanLiShopOrderException {
@@ -72,7 +93,7 @@
      if (order.getUid() == null)
         throw new BanLiShopOrderException(1, "缺少用户ID");
      if (order.getHongBaoPayment() == null || order.getBalancePayment() == null || order.getMoneyPayment() == null) {
      if (order.getHongBaoPayment() == null && order.getBalancePayment() == null && order.getMoneyPayment() == null) {
         throw new BanLiShopOrderException(1, "未选择支付方式");
      }
@@ -103,6 +124,9 @@
      if (goods.getCreateTime() == null)
         goods.setCreateTime(new Date());
      // 插入商品
      BanLiShopOrderGoods orderGoods = getOrderGoods(goods, set);
      order.setOrderGoods(orderGoods);
      banLiShopOrderMapper.insertSelective(order);
      // 添加订单号
@@ -115,6 +139,19 @@
      // 订单添加成功 ,延时通知后续
      sendPlaceOrderMsg(order.getId(), order.getUid());
   }
   private BanLiShopOrderGoods getOrderGoods(BanLiShopGoods goods, BanLiShopGoodsSets set) {
      BanLiShopOrderGoods orderGoods = new BanLiShopOrderGoods();
      orderGoods.setCreateTime(new Date());
      orderGoods.setGoodsId(goods.getId());
      orderGoods.setGoodsName(goods.getTitle());
      orderGoods.setGoodsSetId(set.getId());
      orderGoods.setSetName(set.getName());
      orderGoods.setState(set.getState());
      orderGoods.setZkPrice(set.getZkPrice());
      banLiShopOrderGoodsMapper.insertSelective(orderGoods);
      return orderGoods;
   }
   private void sendPlaceOrderMsg(Long orderId, Long uid) {
@@ -144,22 +181,27 @@
   @Override
   public List<BanLiShopOrder> listByUid(Long uid, int page, int pageSize) {
      return null;
      return banLiShopOrderMapper.listByUid(null, uid, (page - 1) * pageSize, pageSize);
   }
   @Override
   public long countByUid(Long uid) {
      return 0;
      return banLiShopOrderMapper.countByUid(null, uid);
   }
   @Override
   public BanLiShopOrder selectByPrimaryKey(Long id) {
      return null;
      return banLiShopOrderMapper.selectByPrimaryKey(id);
   }
   @Override
   public void udpateSelectiveByPrimaryKey(BanLiShopOrder order) {
      if (order == null || order.getId() == null)
         return;
      if (order.getUpdateTime() == null)
         order.setUpdateTime(new Date());
      banLiShopOrderMapper.updateByPrimaryKeySelective(order);
   }
   @Transactional
@@ -284,23 +326,98 @@
      }
   }
   @Transactional
   @Override
   public void rejectOrder(Long id, String msg) throws BanLiShopOrderException {
      // 判断订单是否处于付款状态
      BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
      BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKey(id);
      if (order == null)
         throw new BanLiShopOrderException(1, "订单不存在");
      if (order.getState() != BanLiShopOrder.STATE_PAID)
         throw new BanLiShopOrderException(1, "订单未处于待审核状态");
      // TODO 订单退款
      BanLiShopOrder update = new BanLiShopOrder();
      update.setState(BanLiShopOrder.STATE_REJECT);
      update.setStateDesc(msg);
      update.setUpdateTime(new Date());
      banLiShopOrderMapper.updateByPrimaryKeySelective(update);
      Message message = new Message(MQTopicName.TOPIC_ORDER.name(), OrderTopicTagEnum.banLiShopOrderRefund.name(),
            new Gson().toJson(new BanLiShopOrderMQMsg(order.getId(), order.getUid())).getBytes());
      try {
         orderTransactionProducer.send(message, new LocalTransactionExecuter() {
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
               rejectOrderLocal(id, msg);
               return TransactionStatus.CommitTransaction;
            }
         }, null);
      } catch (Exception e) {// 消息发送失败
         throw new BanLiShopOrderException(2, "MQ发送失败");
      }
   }
   private void rejectOrderLocal(Long id, String msg) {
      // 手动开启事务
      org.springframework.transaction.TransactionStatus transactionStatus = transactionManager
            .getTransaction(new DefaultTransactionDefinition());
      try {
         BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
         BanLiShopOrder update = new BanLiShopOrder();
         update.setId(order.getId());
         update.setState(BanLiShopOrder.STATE_REJECT);
         update.setStateDesc(msg);
         update.setUpdateTime(new Date());
         banLiShopOrderMapper.updateByPrimaryKeySelective(update);
         transactionManager.commit(transactionStatus);
      } catch (Exception e) {
         transactionManager.rollback(transactionStatus);
      }
   }
   @Override
   public BanLiShopOrder createOrder(Long goodsSetPayId, String chargeAccount, Long uid)
         throws BanLiShopOrderException {
      BanLiShopGoodsSetsPay pay = banLiShopGoodsSetPayService.selectByPrimaryKey(goodsSetPayId);
      if (pay == null || pay.getGoodsSet() == null) {
         throw new BanLiShopOrderException(21, "支付方式已下线");
      }
      BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(pay.getGoodsSet().getId());
      if (set == null || set.getState() == BanLiShopGoodsSets.STATE_OFFLINE) {
         throw new BanLiShopOrderException(21, "商品已下线");
      }
      BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(set.getGoods().getId());
      if (goods == null || goods.getState() == BanLiShopGoods.STATE_OFFLINE) {
         throw new BanLiShopOrderException(22, "商品已下线");
      }
      if (pay.getHongBaoPrice() != null && pay.getHongBaoPrice().compareTo(new BigDecimal(0)) > 0) {
         BigDecimal money = redPackBalanceService.getBalance(uid);
         if (money.compareTo(pay.getHongBaoPrice()) < 0) {
            throw new BanLiShopOrderException(24, "红包余额不足");
         }
      }
      if (set.getStock() <= 0)
         throw new BanLiShopOrderException(23, "商品库存不足");
      BanLiShopOrder order = new BanLiShopOrder();
      order.setChargeAccount(chargeAccount);
      order.setChargeAccountType(goods.getChargeType());
      order.setCreateTime(new Date());
      order.setGoods(goods);
      order.setGoodsSet(set);
      if (pay.getHongBaoPrice() != null && pay.getHongBaoPrice().compareTo(new BigDecimal(0)) > 0) {
         order.setHongBaoPayment(pay.getHongBaoPrice());
         order.setHongBaoPaymentState(BanLiShopOrder.STATE_NO_PAY);
      }
      if (pay.getMoneyPrice() != null && pay.getMoneyPrice().compareTo(new BigDecimal(0)) > 0) {
         order.setMoneyPayment(pay.getMoneyPrice());
         order.setMoneyPaymentState(BanLiShopOrder.STATE_NO_PAY);
      }
      order.setState(BanLiShopOrder.STATE_NO_PAY);
      order.setStateDesc("未付款");
      order.setUid(uid);
      return order;
   }
}