From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 25 二月 2025 16:41:22 +0800
Subject: [PATCH] 淘宝转链接口更新

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopOrderServiceImpl.java |  919 +++++++++++++++++++++++++++++++--------------------------
 1 files changed, 496 insertions(+), 423 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 aa04c6a..6d70f5f 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
@@ -1,423 +1,496 @@
-package com.yeshi.fanli.service.impl.shop;
-
-import java.math.BigDecimal;
-import java.util.Date;
-import java.util.List;
-
-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;
-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.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;
-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.BanLiShopGoodsSetPayService;
-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.BanLiShopOrderUtil;
-
-@Service
-public class BanLiShopOrderServiceImpl implements BanLiShopOrderService {
-
-	@Resource
-	private BanLiShopOrderMapper banLiShopOrderMapper;
-
-	@Resource
-	private BanLiShopGoodsSetService banLiShopGoodsSetService;
-
-	@Resource
-	private BanLiShopGoodsSetPayService banLiShopGoodsSetPayService;
-
-	@Resource
-	private BanLiShopGoodsService banLiShopGoodsService;
-
-	@Resource
-	private BanLiShopGoodsClassService banLiShopGoodsClassService;
-
-	@Resource
-	private RedPackBalanceService redPackBalanceService;
-
-	@Resource(name = "producer")
-	private Producer producer;
-
-	@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 {
-		// 鏌ヨ蹇呰鐨勫弬鏁版槸鍚︽坊鍔�
-		if (order.getGoodsSet() == null || order.getGoodsSet().getId() == null || order.getGoods() == null
-				|| order.getGoods().getId() == null)
-			throw new BanLiShopOrderException(1, "鏈�夋嫨鍟嗗搧");
-
-		if (order.getUid() == null)
-			throw new BanLiShopOrderException(1, "缂哄皯鐢ㄦ埛ID");
-
-		if (order.getHongBaoPayment() == null && order.getBalancePayment() == null && order.getMoneyPayment() == null) {
-			throw new BanLiShopOrderException(1, "鏈�夋嫨鏀粯鏂瑰紡");
-		}
-
-		if (order.getHongBaoPayment() != null) {
-			order.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
-		}
-
-		if (order.getBalancePayment() != null) {
-			order.setBalancePaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
-		}
-
-		if (order.getMoneyPayment() != null) {
-			order.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
-		}
-
-		order.setState(BanLiShopOrder.STATE_NO_PAY);
-		// 鍒ゆ柇鍟嗗搧濂楅鏄惁瀛樺湪
-		BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
-		if (set == null) {
-			throw new BanLiShopOrderException(1, "鍟嗗搧濂楅涓嶅瓨鍦�");
-		}
-
-		BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
-		if (goods == null) {
-			throw new BanLiShopOrderException(1, "鍟嗗搧宸蹭笅绾�");
-		}
-
-		if (goods.getCreateTime() == null)
-			goods.setCreateTime(new Date());
-
-		// 鎻掑叆鍟嗗搧
-		BanLiShopOrderGoods orderGoods = getOrderGoods(goods, set);
-		order.setOrderGoods(orderGoods);
-		banLiShopOrderMapper.insertSelective(order);
-
-		// 娣诲姞璁㈠崟鍙�
-		String orderNo = BanLiShopOrderUtil.createOrderNo(order.getId());
-		// 鏇存柊璁㈠崟鍙�
-		BanLiShopOrder update = new BanLiShopOrder();
-		update.setId(order.getId());
-		update.setOrderNo(orderNo);
-		banLiShopOrderMapper.updateByPrimaryKeySelective(update);
-
-		// 璁㈠崟娣诲姞鎴愬姛 ,寤舵椂閫氱煡鍚庣画
-		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) {
-
-		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
-	public List<BanLiShopOrder> listByUid(Long uid, int page, int pageSize) {
-
-		return banLiShopOrderMapper.listByUid(null, uid, (page - 1) * pageSize, pageSize);
-	}
-
-	@Override
-	public long countByUid(Long uid) {
-		return banLiShopOrderMapper.countByUid(null, uid);
-	}
-
-	@Override
-	public BanLiShopOrder selectByPrimaryKey(Long id) {
-		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
-	@Override
-	public void payOrderByHongBao(Long orderId) throws BanLiShopOrderException, RedPackBalanceException {
-		BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(orderId);
-		if (order == null) {
-			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
-		}
-		if (order.getHongBaoPayment() == null)
-			throw new BanLiShopOrderException(2, "涓嶉渶瑕侀噰鐢ㄧ孩鍖呮敮浠�");
-
-		if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
-			throw new BanLiShopOrderException(3, "閲嶅鏀粯");
-		}
-
-		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.createUseByShopOrder(orderId, order.getUid(), goodsClass.getName(),
-					set.getName(), order.getHongBaoPayment());
-		} catch (RedPackDetailException e) {
-			e.printStackTrace();
-		}
-
-		if (detail == null)
-			throw new RedPackBalanceException(4, "绾㈠寘璇︽儏澶辫触");
-		redPackBalanceService.subRedPack(order.getUid(), order.getHongBaoPayment(), detail);
-
-		BanLiShopOrder update = new BanLiShopOrder();
-		update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_PAID);
-		// 鍒ゆ柇鍏朵粬寰呮敮浠橀」鏄惁宸茬粡鏀粯
-		update.setUpdateTime(new Date());
-		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
-	public void payOrderByMoney(Long orderId, BigDecimal money) throws BanLiShopOrderException {
-		BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(orderId);
-		if (order == null) {
-			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
-		}
-		if (order.getMoneyPayment() == null)
-			throw new BanLiShopOrderException(2, "涓嶉渶瑕侀噰鐢ㄧ幇閲戞敮浠�");
-
-		if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
-			throw new BanLiShopOrderException(3, "閲嶅鏀粯");
-		}
-
-		if (order.getMoneyPayment().compareTo(money) > 0) {
-			throw new BanLiShopOrderException(4, "鏀粯閲戦涓嶅");
-		}
-
-		// 鏀粯鎴愬姛
-		BanLiShopOrder update = new BanLiShopOrder();
-		update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_PAID);
-		// 鍒ゆ柇鍏朵粬寰呮敮浠橀」鏄惁宸茬粡鏀粯
-		update.setUpdateTime(new Date());
-		order.setMoneyPaymentState(update.getMoneyPaymentState());
-		update.setState(getOrderPayState(order));
-		banLiShopOrderMapper.updateByPrimaryKeySelective(update);
-	}
-
-	@Override
-	public BanLiShopOrder selectByPrimaryKeyForUpdate(Long id) {
-		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.selectByPrimaryKey(id);
-		if (order == null)
-			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
-
-		if (order.getState() != BanLiShopOrder.STATE_PAID)
-			throw new BanLiShopOrderException(1, "璁㈠崟鏈浜庡緟瀹℃牳鐘舵��");
-
-		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;
-	}
-
-}
+package com.yeshi.fanli.service.impl.shop;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+
+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 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.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;
+import com.yeshi.fanli.dto.msg.MsgRedPackUseContentDTO;
+import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
+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.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;
+import com.yeshi.fanli.exception.shop.BanLiShopOrderException;
+import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
+import com.yeshi.fanli.service.inter.mq.MQUnSendInfoService;
+import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
+import com.yeshi.fanli.service.inter.redpack.RedPackForbidService;
+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.service.manger.msg.RocketMQManager;
+import com.yeshi.fanli.util.Constant;
+import com.yeshi.fanli.util.factory.RedPackDetailFactory;
+import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
+import com.yeshi.fanli.util.rocketmq.MQTopicName;
+import com.yeshi.fanli.util.shop.BanLiShopOrderUtil;
+import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil;
+
+@Service
+public class BanLiShopOrderServiceImpl implements BanLiShopOrderService {
+
+	@Resource
+	private BanLiShopOrderMapper banLiShopOrderMapper;
+
+	@Resource
+	private BanLiShopGoodsSetService banLiShopGoodsSetService;
+
+	@Resource
+	private BanLiShopGoodsSetPayService banLiShopGoodsSetPayService;
+
+	@Resource
+	private BanLiShopGoodsService banLiShopGoodsService;
+
+	@Resource
+	private BanLiShopGoodsClassService banLiShopGoodsClassService;
+
+	@Resource
+	private RedPackBalanceService redPackBalanceService;
+
+	@Resource
+	private RocketMQManager rocketMQManager;
+
+	@Resource
+	private MQUnSendInfoService mqUnSendInfoService;
+
+	@Resource
+	private BanLiShopOrderGoodsMapper banLiShopOrderGoodsMapper;
+
+	@Resource(name = "orderTransactionProducer")
+	private TransactionProducer orderTransactionProducer;
+
+	@Resource(name = "transactionManager")
+	private DataSourceTransactionManager transactionManager;
+
+	@Resource
+	private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
+
+	@Resource
+	private RedPackForbidService redPackForbidService;
+
+	@Transactional(rollbackFor = Exception.class)
+	@Override
+	public void addOrder(BanLiShopOrder order) throws BanLiShopOrderException {
+		// 鏌ヨ蹇呰鐨勫弬鏁版槸鍚︽坊鍔�
+		if (order.getGoodsSet() == null || order.getGoodsSet().getId() == null || order.getGoods() == null
+				|| order.getGoods().getId() == null)
+			throw new BanLiShopOrderException(1, "鏈�夋嫨鍟嗗搧");
+
+		if (order.getUid() == null)
+			throw new BanLiShopOrderException(1, "缂哄皯鐢ㄦ埛ID");
+
+		if (order.getHongBaoPayment() == null && order.getBalancePayment() == null && order.getMoneyPayment() == null) {
+			throw new BanLiShopOrderException(1, "鏈�夋嫨鏀粯鏂瑰紡");
+		}
+
+		if (order.getHongBaoPayment() != null) {
+			order.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
+		}
+
+		if (order.getBalancePayment() != null) {
+			order.setBalancePaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
+		}
+
+		if (order.getMoneyPayment() != null) {
+			order.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
+		}
+
+		order.setState(BanLiShopOrder.STATE_NO_PAY);
+		// 鍒ゆ柇鍟嗗搧濂楅鏄惁瀛樺湪
+		BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
+		if (set == null) {
+			throw new BanLiShopOrderException(1, "鍟嗗搧濂楅涓嶅瓨鍦�");
+		}
+
+		BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
+		if (goods == null) {
+			throw new BanLiShopOrderException(1, "鍟嗗搧宸蹭笅绾�");
+		}
+
+		if (goods.getCreateTime() == null)
+			goods.setCreateTime(new Date());
+
+		// 鎻掑叆鍟嗗搧
+		BanLiShopOrderGoods orderGoods = getOrderGoods(goods, set);
+		order.setOrderGoods(orderGoods);
+		banLiShopOrderMapper.insertSelective(order);
+
+		// 娣诲姞璁㈠崟鍙�
+		String orderNo = BanLiShopOrderUtil.createOrderNo(order.getId());
+		// 鏇存柊璁㈠崟鍙�
+		BanLiShopOrder update = new BanLiShopOrder();
+		update.setId(order.getId());
+		update.setOrderNo(orderNo);
+		order.setOrderNo(orderNo);
+		banLiShopOrderMapper.updateByPrimaryKeySelective(update);
+
+		// 澧炲姞閿�閲�
+		banLiShopGoodsSetService.addSalesCount(order.getGoodsSet().getId(), 1);
+		banLiShopGoodsService.addSalesCount(order.getGoods().getId(), 1);
+
+		// 璁㈠崟娣诲姞鎴愬姛 ,寤舵椂閫氱煡鍚庣画
+		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());
+		orderGoods.setPicture(goods.getSquarePicture());
+		banLiShopOrderGoodsMapper.insertSelective(orderGoods);
+		return orderGoods;
+	}
+
+	private void sendPlaceOrderMsg(Long orderId, Long uid) {
+
+		Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.banLiShopOrderDelay,
+				new BanLiShopOrderMQMsg(orderId, uid));
+		msg.setKey(orderId + "");
+		long delayTime = System.currentTimeMillis() + (Constant.IS_TEST ? 1000 * 30L : 1000 * 60 * 10);// 10鍒嗛挓鍚庨�氱煡
+		msg.setStartDeliverTime(delayTime);
+		
+		SendResult sendResult =	rocketMQManager.sendNormalMsg(msg,  Constant.IS_TEST ? 1000 * 30L : 1000 * 60 * 10L, orderId + "");
+		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
+	public List<BanLiShopOrder> listByUid(Long uid, int page, int pageSize) {
+
+		return banLiShopOrderMapper.listByUidAndState(null, uid, (page - 1) * pageSize, pageSize);
+	}
+
+	@Override
+	public long countByUid(Long uid) {
+		return banLiShopOrderMapper.countByUidAndState(null, uid);
+	}
+
+	@Override
+	public BanLiShopOrder selectByPrimaryKey(Long id) {
+		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(rollbackFor = Exception.class)
+	@Override
+	public void payOrderByHongBao(Long orderId) throws BanLiShopOrderException, RedPackBalanceException {
+		BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(orderId);
+		if (order == null) {
+			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
+		}
+		if (order.getHongBaoPayment() == null)
+			throw new BanLiShopOrderException(2, "涓嶉渶瑕侀噰鐢ㄧ孩鍖呮敮浠�");
+
+		if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
+			throw new BanLiShopOrderException(3, "閲嶅鏀粯");
+		}
+
+		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.createUseByShopOrder(orderId, order.getUid(), goodsClass.getName(),
+					set.getName(), order.getHongBaoPayment());
+		} catch (RedPackDetailException e) {
+			e.printStackTrace();
+		}
+
+		if (detail == null)
+			throw new RedPackBalanceException(4, "绾㈠寘璇︽儏澶辫触");
+		redPackBalanceService.subRedPack(order.getUid(), order.getHongBaoPayment(), detail);
+
+		BanLiShopOrder update = new BanLiShopOrder();
+		update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_PAID);
+		// 鍒ゆ柇鍏朵粬寰呮敮浠橀」鏄惁宸茬粡鏀粯
+		update.setUpdateTime(new Date());
+		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
+	public void payOrderByMoney(Long orderId, BigDecimal money) throws BanLiShopOrderException {
+		BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(orderId);
+		if (order == null) {
+			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
+		}
+		if (order.getMoneyPayment() == null)
+			throw new BanLiShopOrderException(2, "涓嶉渶瑕侀噰鐢ㄧ幇閲戞敮浠�");
+
+		if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
+			throw new BanLiShopOrderException(3, "閲嶅鏀粯");
+		}
+
+		if (order.getMoneyPayment().compareTo(money) > 0) {
+			throw new BanLiShopOrderException(4, "鏀粯閲戦涓嶅");
+		}
+
+		// 鏀粯鎴愬姛
+		BanLiShopOrder update = new BanLiShopOrder();
+		update.setId(orderId);
+		update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_PAID);
+		// 鍒ゆ柇鍏朵粬寰呮敮浠橀」鏄惁宸茬粡鏀粯
+		update.setUpdateTime(new Date());
+		order.setMoneyPaymentState(update.getMoneyPaymentState());
+		update.setState(getOrderPayState(order));
+		banLiShopOrderMapper.updateByPrimaryKeySelective(update);
+	}
+
+	@Override
+	public BanLiShopOrder selectByPrimaryKeyForUpdate(Long id) {
+		return banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
+	}
+
+	@Transactional(rollbackFor = Exception.class)
+	@Override
+	public void invalidOrderByOrderId(Long id, String desc) {
+		// 鍒ゆ柇璁㈠崟鏄惁瀛樺湪
+		BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
+		if (order == null)
+			return;
+
+		// 鍒ゆ柇鏄惁澶勪簬鏈粯娆�
+		if (order.getState() == BanLiShopOrder.STATE_NO_PAY) {
+			/**
+			 * 鏌ヨ鏄惁宸茬粡寰俊鏀粯
+			 */
+			if (order.getMoneyPayment() != null) {
+				try {
+					boolean isS = BanLiShopWXPayUtil.isPaySuccess(order.getOrderNo());
+					if (isS)// 鏀粯鎴愬姛锛岄噸鏂板彂閫佹敮浠樻垚鍔熸秷鎭�
+					{
+						BanLiShopOrderMQMsg msg = new BanLiShopOrderMQMsg(order.getId(), order.getUid());
+						Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER,
+								OrderTopicTagEnum.banLiShopOrderPaid, msg);
+						rocketMQManager.sendNormalMsg(message, null);
+						return;
+					}
+
+				} catch (WXOrderException e) {
+					e.printStackTrace();
+				} catch (Exception e) {
+
+				}
+			}
+
+			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.selectByPrimaryKey(id);
+		if (order == null)
+			throw new BanLiShopOrderException(1, "璁㈠崟涓嶅瓨鍦�");
+
+		if (order.getState() != BanLiShopOrder.STATE_PAID)
+			throw new BanLiShopOrderException(1, "璁㈠崟鏈浜庡緟瀹℃牳鐘舵��");
+
+		Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.banLiShopOrderRefund,
+				new BanLiShopOrderMQMsg(order.getId(), order.getUid()));
+		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());
+			update.setRejectTime(new Date());
+			banLiShopOrderMapper.updateByPrimaryKeySelective(update);
+			// 娑堟伅鍙戦��
+			MsgRedPackUseContentDTO dto = new MsgRedPackUseContentDTO();
+			dto.setGoodsSetName(order.getOrderGoods().getSetName());
+			dto.setHongBao(order.getHongBaoPayment());
+			dto.setMoney(order.getMoneyPayment());
+			dto.setReason(msg);
+			dto.setTime(new Date());
+			userMoneyMsgNotificationService.redPackMsg(order.getUid(), MsgTypeMoneyTypeEnum.redPackUseFail,
+					new Gson().toJson(dto), null);
+			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 (redPackForbidService.verifyForbid(uid))
+			throw new BanLiShopOrderException(25, "绾㈠寘鍔熻兘宸茶灏佺");
+
+		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;
+	}
+
+	@Override
+	public BanLiShopOrder selectByOrderNo(String orderNo) {
+		return banLiShopOrderMapper.selectByOrderNo(orderNo);
+	}
+
+	@Override
+	public List<BanLiShopOrder> listByUidAndState(Long uid, List<Integer> stateList, int page, int pageSize) {
+		return banLiShopOrderMapper.listByUidAndState(stateList, uid, (page - 1) * pageSize, pageSize);
+	}
+
+	@Override
+	public long countByUidAndState(Long uid, List<Integer> stateList) {
+		return banLiShopOrderMapper.countByUidAndState(stateList, uid);
+	}
+
+}

--
Gitblit v1.8.0