From c5edc037c088c812554b9aac16b9eadab2c4e1e6 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 29 十月 2019 10:54:33 +0800
Subject: [PATCH] 消息整形,维权订单修改

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java |  145 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 113 insertions(+), 32 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
index ab7f52a..546be60 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -12,15 +12,25 @@
 
 import org.springframework.context.annotation.Lazy;
 import org.springframework.core.task.TaskExecutor;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
 import org.yeshi.utils.DateUtil;
+import org.yeshi.utils.entity.DateInfo;
 
+import com.aliyun.openservices.ons.api.Message;
+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.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 +47,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 +78,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 +164,12 @@
 
 	@Resource
 	private TokenRecordService tokenRecordService;
+
+	@Resource(name = "transactionManager")
+	private DataSourceTransactionManager transactionManager;
+
+	@Resource(name = "orderTransactionProducer")
+	private TransactionProducer orderTransactionProducer;
 
 	@Override
 	public int insertSelective(UserSystemCoupon record) {
@@ -764,55 +785,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, "鍒竔d涓嶅瓨鍦�");
-		}
 
-		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,6 +880,36 @@
 			// 鎻愭垚閲戦
 			BigDecimal money = MoneyBigDecimalUtil.mul(hongBao, percent);
 
+			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());
+			try {
+				SendResult result = orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
+					@Override
+					public TransactionStatus execute(Message arg0, Object arg1) {
+						try {
+							useRewardCouponCore(userSystemCoupon, money, hongBao, uid, order);
+							return TransactionStatus.CommitTransaction;
+						} catch (Exception e) {
+							return TransactionStatus.RollbackTransaction;
+						}
+					}
+				}, null);
+				System.out.println(result);
+			} catch (Exception e) {
+				throw new Exception();
+			}
+		}
+	}
+
+	@Transactional
+	public void useRewardCouponCore(UserSystemCoupon userSystemCoupon, BigDecimal money, BigDecimal hongBao, Long uid,
+			Order order) throws Exception {
+		org.springframework.transaction.TransactionStatus transactionStatus = transactionManager
+				.getTransaction(new DefaultTransactionDefinition());
+		try {
 			// 1銆佹彃鍏ョ孩鍖呮暟鎹�
 			HongBaoV2 hongBaoV2 = new HongBaoV2();
 			hongBaoV2.setMoney(money);
@@ -853,14 +928,10 @@
 			orderHongBaoMapService.addOrderHongBaoMap(hongBaoV2, order);
 
 			// 3.鎻掑叆璧勯噾鏄庣粏,鐢ㄦ埛浣欓
-			UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createOrderReward(orderNo, sourceType, money,
-					new UserInfo(uid));
+			UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createOrderReward(order.getOrderId(),
+					order.getOrderType(), money, new UserInfo(uid));
 			userMoneyDetailService.addUserMoneyDetail(userMoneyDetail);
 			userInfoService.addMoney(new UserInfo(uid), money);
-
-			// 鎻掑叆璧勯噾閫氱煡
-			userMoneyMsgNotificationService.orderReward(uid, orderNo, sourceType, money,
-					userInfoService.selectByPKey(uid).getMyHongBao());
 
 			/* 鐢ㄦ埛鍒镐俊鎭姸鎬佸強璁板綍澶勭悊 */
 
@@ -871,23 +942,33 @@
 			userSystemCoupon.setUpdateTime(date);
 			userSystemCouponMapper.updateByPrimaryKeySelective(userSystemCoupon);
 
+			SystemCoupon coupon = systemCouponService.selectByPrimaryKey(userSystemCoupon.getSystemCoupon().getId());
+
 			// 鎻掑叆浣跨敤鍒歌褰�
 			UserSystemCouponRecord couponRecord = new UserSystemCouponRecord();
 			couponRecord.setState(UserSystemCouponRecord.STATE_SUCCESS);
-			couponRecord.setGoodSource(sourceType);
-			couponRecord.setOrderNo(orderNo);
+			couponRecord.setGoodSource(order.getOrderType());
+			couponRecord.setOrderNo(order.getOrderId());
 			couponRecord.setUserSystemCoupon(userSystemCoupon);
-			couponRecord.setCouponType(systemCoupon.getType().name());
+			couponRecord.setCouponType(coupon.getType().name());
 			couponRecord.setCreateTime(date);
 			couponRecord.setUpdateTime(date);
 			userSystemCouponRecordService.insertSelective(couponRecord);
 
+			// 鎻掑叆璧勯噾閫氱煡
+			userMoneyMsgNotificationService.orderReward(uid, order.getOrderId(), order.getOrderType(), money,
+					userInfoService.selectByPKey(uid).getMyHongBao());
 			// 娑堟伅鎺ㄩ��
 			try {
-				userOtherMsgNotificationService.rewardCouponUsed(userSystemCoupon, hongBao, money, orderNo);
+				userOtherMsgNotificationService.rewardCouponUsed(userSystemCoupon, hongBao, money, order.getOrderId());
 			} catch (Exception e) {
 				LogHelper.errorDetailInfo(e);
 			}
+			transactionManager.commit(transactionStatus);
+		} catch (Exception e) {
+			e.printStackTrace();
+			transactionManager.rollback(transactionStatus);
+			throw new Exception();
 		}
 
 	}
@@ -1510,12 +1591,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));

--
Gitblit v1.8.0