From c23bccf7f39ebae903cc20d255b014d3e80c0221 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期三, 06 十一月 2019 18:50:04 +0800
Subject: [PATCH] 红包H5接口修改

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java |  145 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 137 insertions(+), 8 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
index 5e42f2c..4a5e0be 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderPayServiceImpl.java
@@ -7,7 +7,14 @@
 
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.yeshi.utils.exception.WXOrderException;
 
+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.dto.mq.order.OrderTopicTagEnum;
+import com.yeshi.fanli.dto.mq.order.body.BanLiShopOrderMQMsg;
 import com.yeshi.fanli.entity.redpack.RedPackDetail;
 import com.yeshi.fanli.entity.shop.BanLiShopGoods;
 import com.yeshi.fanli.entity.shop.BanLiShopGoodsClass;
@@ -24,6 +31,8 @@
 import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService;
 import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
 import com.yeshi.fanli.util.factory.RedPackDetailFactory;
+import com.yeshi.fanli.util.rocketmq.MQTopicName;
+import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil;
 
 @Service
 public class BanLiShopOrderPayServiceImpl implements BanLiShopOrderPayService {
@@ -45,6 +54,9 @@
 	@Resource
 	private RedPackDetailService redPackDetailService;
 
+	@Resource(name = "producer")
+	private Producer producer;
+
 	@Transactional
 	@Override
 	public void payOrderByHongBao(Long orderId) throws BanLiShopOrderException, RedPackBalanceException {
@@ -53,7 +65,7 @@
 			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
 		}
 		if (order.getHongBaoPayment() == null)
-			throw new BanLiShopOrderException(2, "涓嶉渶瑕侀噰鐢ㄧ孩鍖呮敮浠�");
+			return;
 
 		if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
 			throw new BanLiShopOrderException(3, "閲嶅鏀粯");
@@ -101,7 +113,7 @@
 		if (order.getMoneyPayment() == null)
 			throw new BanLiShopOrderException(2, "涓嶉渶瑕侀噰鐢ㄧ幇閲戞敮浠�");
 
-		if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
+		if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() != BanLiShopOrder.PAY_STATE_NOPAY) {
 			throw new BanLiShopOrderException(3, "閲嶅鏀粯");
 		}
 
@@ -141,15 +153,132 @@
 		}
 
 		if (detail != null) {
-			RedPackDetail oldDetail = redPackDetailService.getByIdentifyCode(detail.getIdentifyCode());
-			if (oldDetail != null) {// 澶栨樉绾㈠寘璇︽儏
-				RedPackDetail update = new RedPackDetail();
-				update.setId(oldDetail.getId());
-				update.setDisplay(true);
-				redPackDetailService.updateByPrimaryKeySelective(update);
+			redPackDetailService.changeDisplayByIdentifyCode(detail.getIdentifyCode(), true);
+		}
+	}
+
+	@Transactional
+	@Override
+	public void refund(Long orderId) throws BanLiShopOrderException {
+		// 璁㈠崟閫�娆�
+		// 鏌ヨ璁㈠崟鏄惁宸茬粡琚嫆缁�
+		BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKeyForUpdate(orderId);
+		if (order == null)
+			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
+		if (order.getState() == BanLiShopOrder.STATE_REJECT_REFUND_FAIL
+				|| order.getState() == BanLiShopOrder.STATE_REJECT_REFUND_SUCCESS)
+			throw new BanLiShopOrderException(2, "璁㈠崟鏈鎷掔粷/璁㈠崟宸查��娆�");
+
+		BanLiShopOrder update = new BanLiShopOrder();
+		update.setId(order.getId());
+		if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
+			BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
+			BanLiShopGoodsClass goodsClass = banLiShopGoodsClassService
+					.selectByPrimaryKey(goods.getGoodsClass().getId());
+			BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
+			// 绾㈠寘閫�娆�
+			RedPackDetail detail = null;
+			try {
+				detail = RedPackDetailFactory.createShopOrderDrawBack(orderId, order.getUid(), goodsClass.getName(),
+						set.getName(), order.getHongBaoPayment());
+			} catch (RedPackDetailException e) {
+				e.printStackTrace();
+			}
+
+			if (detail == null)
+				throw new BanLiShopOrderException(4, "绾㈠寘璇︽儏澶辫触");
+			try {
+				redPackBalanceService.addRedPack(order.getUid(), order.getHongBaoPayment(), detail);
+			} catch (RedPackBalanceException e) {
+				throw new BanLiShopOrderException(5, "绾㈠寘閫�娆惧け璐�");
+			}
+			update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
+			order.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
+		}
+
+		if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
+			try {
+				boolean success = BanLiShopWXPayUtil.refund(order.getOrderNo(), order.getMoneyPayment());
+				if (success) {
+					update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUNDING);
+					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 *5L);// 寤舵椂24灏忔椂閫氱煡妫�娴嬪井淇¢��娆剧姸鎬�
+					SendResult result = producer.send(message);
+					if (result == null) {
+						throw new BanLiShopOrderException(8, "娑堟伅鍙戦�佸け璐�");
+					}
+				} else
+					throw new BanLiShopOrderException(6, "寰俊鏀粯閫�娆惧け璐�");
+			} catch (WXOrderException e) {
+				throw new BanLiShopOrderException(6, "寰俊鏀粯閫�娆惧け璐�");
 			}
 		}
 
+		// 妫�鏌� 鏄惁閫�娆炬垚鍔�
+		if (update.getMoneyPaymentState() != null
+				&& update.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_REFUNDING) {
+			try {
+				boolean isS = BanLiShopWXPayUtil.isRefundSuccess(order.getOrderNo());
+				if (isS) {
+					update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
+					order.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
+				}
+			} catch (WXOrderException e) {
+				throw new BanLiShopOrderException(7, "寰俊鏀粯閫�娆炬煡璇㈠け璐�");
+			}
+		}
+
+		if (update.getHongBaoPaymentState() != null || update.getMoneyPaymentState() != null) {
+			// 鑾峰彇鏁翠釜璁㈠崟鐨勯��娆剧姸鎬�
+			int state = getRefundState(order);
+			update.setState(state);
+			update.setUpdateTime(new Date());
+			banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
+		}
+	}
+
+	/**
+	 * 鑾峰彇鏁翠釜閫�娆剧姸鎬�
+	 * 
+	 * @param order
+	 * @return
+	 */
+	private int getRefundState(BanLiShopOrder order) {
+		int refundCount = 0;
+		int refundSuccessCount = 0;
+
+		if (order.getHongBaoPayment() != null) {
+			refundCount++;
+			if (order.getHongBaoPaymentState() != null
+					&& order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_REFUND)
+				refundSuccessCount++;
+		}
+
+		if (order.getBalancePayment() != null) {
+			refundCount++;
+			if (order.getBalancePaymentState() != null
+					&& order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_REFUND)
+				refundSuccessCount++;
+		}
+
+		if (order.getMoneyPayment() != null) {
+			refundCount++;
+			if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_REFUND)
+				refundSuccessCount++;
+		}
+
+		if (refundCount == refundSuccessCount)
+			return BanLiShopOrder.STATE_REJECT_REFUND_SUCCESS;// 閫�娆炬垚鍔�
+		else {
+			if (refundSuccessCount == 0)
+				return BanLiShopOrder.STATE_REJECT;// 閫�娆句腑
+			else
+				return BanLiShopOrder.STATE_REJECT_REFUND_PART_SUCCESS;// 閮ㄥ垎閫�娆炬垚鍔�
+		}
 	}
 
 }

--
Gitblit v1.8.0