From f9155fec1956d54d5b8f40cab22571215975ef88 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 02 十一月 2019 15:41:59 +0800 Subject: [PATCH] 板栗商城订单系统修改 消息机制完善 --- fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java | 127 ++++++++++++++++++++++++++++++++++++++---- 1 files changed, 115 insertions(+), 12 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java index f64b837..729e8da 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java @@ -9,21 +9,32 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +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.dao.mybatis.shop.BanLiShopOrderMapper; +import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum; +import com.yeshi.fanli.dto.mq.order.body.BanLiShopOrderMQMsg; +import com.yeshi.fanli.entity.mq.MQUnSendInfo; 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.exception.mq.MQUnSendInfoException; 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.service.inter.mq.MQUnSendInfoService; 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.BanLiShopGoodsSetService; import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService; +import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.factory.RedPackDetailFactory; +import com.yeshi.fanli.util.rocketmq.MQTopicName; import com.yeshi.fanli.util.shop.BanLiShopUtil; @Service @@ -43,6 +54,12 @@ @Resource private RedPackBalanceService redPackBalanceService; + + @Resource(name = "producer") + private Producer producer; + + @Resource + private MQUnSendInfoService mqUnSendInfoService; @Transactional @Override @@ -95,6 +112,34 @@ update.setId(order.getId()); update.setOrderNo(orderNo); banLiShopOrderMapper.updateByPrimaryKeySelective(update); + + // 璁㈠崟娣诲姞鎴愬姛 ,寤舵椂閫氱煡鍚庣画 + sendPlaceOrderMsg(order.getId(), order.getUid()); + } + + private void sendPlaceOrderMsg(Long orderId, Long uid) { + + Message msg = new Message(MQTopicName.TOPIC_ORDER.name(), OrderTopicTagEnum.banLiShopOrderDelay.name(), + new Gson().toJson(new BanLiShopOrderMQMsg(orderId, uid)).getBytes()); + msg.setKey(orderId + ""); + long delayTime = System.currentTimeMillis() + (Constant.IS_TEST ? 1000 * 30L : 1000 * 60 * 10);// 10鍒嗛挓鍚庨�氱煡 + msg.setStartDeliverTime(delayTime); + SendResult sendResult = producer.send(msg); + if (sendResult == null) { + MQUnSendInfo info = new MQUnSendInfo(); + info.setBody(new String(msg.getBody())); + if (msg.getStartDeliverTime() > 0) + info.setDeliverTime(new Date(msg.getStartDeliverTime())); + info.setKey(msg.getKey()); + info.setTag(msg.getTag()); + info.setTopic(msg.getTopic()); + try { + mqUnSendInfoService.addMQUnSendInfo(info); + } catch (MQUnSendInfoException e) { + e.printStackTrace(); + } + } + } @Override @@ -150,13 +195,43 @@ update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_PAID); // 鍒ゆ柇鍏朵粬寰呮敮浠橀」鏄惁宸茬粡鏀粯 update.setUpdateTime(new Date()); - if (order.getState() == BanLiShopOrder.PAY_STATE_NOPAY) - if ((order.getBalancePaymentState() == null - || order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID) - && (order.getMoneyPaymentState() == null - || order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID))// 鍏朵粬寰呮敮浠橀」宸茬粡鏀粯 - update.setState(BanLiShopOrder.PAY_STATE_PAID); + order.setHongBaoPaymentState(update.getHongBaoPaymentState()); + update.setState(getOrderPayState(order)); banLiShopOrderMapper.updateByPrimaryKeySelective(update); + } + + private int getOrderPayState(BanLiShopOrder order) { + int needPayCount = 0; + int paidCount = 0; + // 鑾峰彇璁㈠崟浠樻鐘舵�� + if (order.getHongBaoPayment() != null) { + needPayCount++; + if (order.getHongBaoPaymentState() != null + && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) + paidCount++; + } + + if (order.getBalancePayment() != null) { + needPayCount++; + if (order.getBalancePaymentState() != null + && order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID) + paidCount++; + } + + if (order.getMoneyPayment() != null) { + needPayCount++; + if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) + paidCount++; + } + + if (needPayCount == paidCount) + return BanLiShopOrder.STATE_PAID; + else { + if (paidCount == 0) + return BanLiShopOrder.STATE_NO_PAY; + else + return BanLiShopOrder.STATE_PART_PAY; + } } @Override @@ -181,12 +256,8 @@ update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_PAID); // 鍒ゆ柇鍏朵粬寰呮敮浠橀」鏄惁宸茬粡鏀粯 update.setUpdateTime(new Date()); - if (order.getState() == BanLiShopOrder.PAY_STATE_NOPAY) - if ((order.getBalancePaymentState() == null - || order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID) - && (order.getHongBaoPaymentState() == null - || order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID))// 鍏朵粬寰呮敮浠橀」宸茬粡鏀粯 - update.setState(BanLiShopOrder.PAY_STATE_PAID); + order.setMoneyPaymentState(update.getMoneyPaymentState()); + update.setState(getOrderPayState(order)); banLiShopOrderMapper.updateByPrimaryKeySelective(update); } @@ -195,4 +266,36 @@ return banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id); } + @Override + public void invalidOrderByOrderId(Long id, String desc) { + // 鍒ゆ柇璁㈠崟鏄惁瀛樺湪 + BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id); + if (order == null) + return; + + // 鍒ゆ柇鏄惁澶勪簬鏈粯娆� + if (order.getState() == BanLiShopOrder.STATE_NO_PAY) { + BanLiShopOrder update = new BanLiShopOrder(); + update.setId(id); + update.setStateDesc(desc); + update.setState(BanLiShopOrder.STATE_INVALID); + update.setUpdateTime(new Date()); + banLiShopOrderMapper.updateByPrimaryKeySelective(update); + } + } + + @Override + public void rejectOrder(Long id, String msg) throws BanLiShopOrderException { + // 鍒ゆ柇璁㈠崟鏄惁澶勪簬浠樻鐘舵�� + BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id); + if (order == null) + throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�"); + + if (order.getState() != BanLiShopOrder.STATE_PAID) + throw new BanLiShopOrderException(1, "璁㈠崟鏈浜庡緟瀹℃牳鐘舵��"); + + // TODO 璁㈠崟閫�娆� + + } + } -- Gitblit v1.8.0