From fb5a8918a7983bbdf83586f773884d3961bb3c6d Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期五, 16 八月 2019 17:50:56 +0800
Subject: [PATCH] 口令领取+消息

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java |  201 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 170 insertions(+), 31 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java
index cbdd881..bad3925 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java
@@ -1,25 +1,34 @@
 package com.yeshi.fanli.service.impl.user;
 
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
 import javax.annotation.Resource;
 
+import org.springframework.core.task.TaskExecutor;
 import org.springframework.stereotype.Service;
 
 import com.yeshi.fanli.dao.mybatis.user.TokenRecordMapper;
+import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO;
 import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinGiveRecord;
 import com.yeshi.fanli.entity.bus.user.TokenRecord;
 import com.yeshi.fanli.entity.bus.user.TokenRecord.TokenTypeEnum;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
 import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
 import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
+import com.yeshi.fanli.entity.bus.user.UserSystemCouponGiveRecord;
 import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
+import com.yeshi.fanli.exception.tlj.UserTaoLiJinOriginException;
 import com.yeshi.fanli.exception.user.TokenRecordException;
+import com.yeshi.fanli.exception.user.UserInfoExtraException;
 import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
+import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
 import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinGiveRecordService;
+import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
 import com.yeshi.fanli.service.inter.user.TokenRecordService;
 import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
 import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -32,6 +41,9 @@
 @Service
 public class TokenRecordServiceImpl implements TokenRecordService {
 
+	@Resource(name = "taskExecutor")
+	private TaskExecutor executor;
+	
 	@Resource
 	private TokenRecordMapper tokenRecordMapper;
 
@@ -53,6 +65,13 @@
 	@Resource
 	private UserSystemCouponGiveRecordService userSystemCouponGiveRecordService;
 	
+	@Resource
+	private UserTaoLiJinOriginService userTaoLiJinOriginService;
+	
+	@Resource
+	private UserOtherMsgNotificationService userOtherMsgNotificationService;
+	
+	
 	@Override
 	public void insertSelective(TokenRecord record) {
 		record.setCreateTime(new Date());
@@ -73,6 +92,15 @@
 		TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
 		if (rokenRecord == null)
 			throw new TokenRecordException(1, "鍙d护涓嶅瓨鍦�");
+		
+		Date now = new Date();
+		Date endTimeToken = rokenRecord.getEndTime();
+		if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
+			throw new TokenRecordException(1, "鍙d护宸插け鏁�");
+		
+		Integer stateTtoken = rokenRecord.getState();
+		if (stateTtoken != null && stateTtoken == 1)
+			throw new TokenRecordException(1, "鍙d护宸插け鏁�");
 
 		String nickName = null;
 		String portrait = null;
@@ -95,11 +123,8 @@
 			nickName = nickName.substring(0, 6) + "...";
 		}
 
-		Integer stateTtoken = rokenRecord.getState();
-		if (stateTtoken == 1)
-			throw new TokenRecordException(1, "鍙d护宸插け鏁�");
-
 		TokenVO tokenVO = new TokenVO();
+		tokenVO.setToken(token);
 		tokenVO.setNickName(nickName);
 		tokenVO.setPortrait(portrait);
 
@@ -221,23 +246,32 @@
 			throw new TokenRecordException(1, "鍙d护涓嶈兘涓虹┖");
 
 		TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
-		if (rokenRecord == null || rokenRecord.getState() == 1)
+		if (rokenRecord == null)
 			throw new TokenRecordException(1, "鍙d护宸插け鏁�");
-
+		
+		Date now = new Date();
+		Date endTimeToken = rokenRecord.getEndTime();
+		if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
+			throw new TokenRecordException(1, "鍙d护宸插け鏁�");
+		
+		Integer stateTtoken = rokenRecord.getState();
+		if (stateTtoken != null && stateTtoken == 1)
+			throw new TokenRecordException(1, "鍙d护宸插け鏁�");
+		
 		String identify = rokenRecord.getIdentify();
 		if (StringUtil.isNullOrEmpty(identify))
 			throw new TokenRecordException(1, "鍙d护鏍囪瘑涓嶅瓨鍦�");
 		
-		Long giveUid = rokenRecord.getUid();
 		
+		Long giveUid = rokenRecord.getUid();
 		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
 		if (userInfoExtra == null) 
 			throw new TokenRecordException(1, "鐢ㄦ埛淇℃伅缂哄け");
 			
-		boolean receive = false;
 		TokenTypeEnum tokenType = rokenRecord.getType();
 		if (tokenType == TokenTypeEnum.freeCoupon) {
-			UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
+			long couponId = Long.parseLong(identify);
+			UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(couponId);
 			if (userCoupon == null)
 				throw new TokenRecordException(1, "璇ュ埜涓嶅瓨鍦�");
 
@@ -255,6 +289,14 @@
 				throw new TokenRecordException(1, "鍙d护宸插け鏁�");
 			
 			if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
+				UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(giveUid, couponId);
+				if (giveRecord == null ) 
+					throw new TokenRecordException(1, "鍙d护宸插け鏁�");
+				
+				Date endTime = giveRecord.getEndTime();
+				if (endTime != null && endTime.getTime() < now.getTime())
+					throw new TokenRecordException(1, "鍝庡憖锛岃繖寮犲厤鍗曞埜宸插け鏁堜簡锛�");
+				
 				try {
 					userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.freeCoupon.name(), UserSystemCoupon.SOURCE_GIVE);
 				} catch (Exception e) {
@@ -262,17 +304,46 @@
 					throw new TokenRecordException(1, "棰嗗彇澶辫触");
 				}
 				
-				// 鏇存柊璁板綍
-				userSystemCouponGiveRecordService.updateRecord(uid, giveUid, Long.parseLong(identify));
-				// 棰嗗彇鎴愬姛
-				receive = true;
+				// 鏇存柊璧犻�佸埜淇℃伅
+				userSystemCouponService.updateGiveRecord(uid, couponId, CouponTypeEnum.freeCoupon.name());
+				
+				// 鏇存柊璧犻�佽褰�
+				giveRecord.setReceiveUid(uid);
+				giveRecord.setReceiveTime(new Date());
+				userSystemCouponGiveRecordService.updateByPrimaryKeySelective(giveRecord);
+				
+				// 娑堟伅 + 闃熷憳
+				executor.execute(new Runnable() {
+					@Override
+					public void run() {
+						SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH.mm");
+						boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
+						String beiZhu = "鏃�";
+						if (addTeam)
+							beiZhu = "棰嗗彇浜哄凡缁忔垚涓轰綘鐨勪竴绾ч槦鍛�";
+						
+						String userName = "鏃�";
+						UserInfo user = userInfoService.selectByPKey(uid);
+						if (user != null && !StringUtil.isNullOrEmpty(user.getNickName()))
+							userName = user.getNickName();
+						
+						MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
+						msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
+						msgOther.setTitle("璧犻�佸厤鍗曞埜");
+						msgOther.setGiveType("浣犺禒閫佺殑鍏嶅崟鍒歌鎴愬姛棰嗗彇");
+						msgOther.setReceiveInfo("鏄电О:  " + userName +" ID:" + uid);
+						msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
+						msgOther.setReceiveTime(sd.format(new Date()));
+						userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
+					}
+				});
 			} else {
 				throw new TokenRecordException(1, "鍙d护宸插け鏁�");
 			}
 
 		} else if (tokenType == TokenTypeEnum.rebatePercentCoupon) {
-			UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
-
+			long couponId = Long.parseLong(identify);
+			UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(couponId);
 			if (userCoupon == null)
 				throw new TokenRecordException(1, "璇ュ埜涓嶅瓨鍦�");
 
@@ -290,14 +361,49 @@
 				throw new TokenRecordException(1, "鍙d护宸插け鏁�");
 			
 		    if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
+		    	UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(giveUid, couponId);
+				if (giveRecord == null ) 
+					throw new TokenRecordException(1, "鍙d护宸插け鏁�");
+				
+				Date endTime = giveRecord.getEndTime();
+				if (endTime != null && endTime.getTime() < now.getTime())
+					throw new TokenRecordException(1, "鍝庡憖锛岃繖寮犺繑鍒╁鍔卞埜宸插け鏁堜簡锛�");
+		    	
 		    	try {
 					userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(), UserSystemCoupon.SOURCE_GIVE);
 				} catch (Exception e) {
 					LogHelper.errorDetailInfo(e);
 					throw new TokenRecordException(1, "棰嗗彇澶辫触");
 				}
-				// 棰嗗彇鎴愬姛
-				receive = true;
+		    	
+		    	// 鏇存柊璧犻�佸埜淇℃伅
+				userSystemCouponService.updateGiveRecord(uid, couponId, CouponTypeEnum.rebatePercentCoupon.name());
+				
+				// 鏇存柊璧犻�佽褰�
+				giveRecord.setReceiveUid(uid);
+				giveRecord.setReceiveTime(new Date());
+				userSystemCouponGiveRecordService.updateByPrimaryKeySelective(giveRecord);
+				
+				// 娑堟伅 + 闃熷憳
+				executor.execute(new Runnable() {
+					@Override
+					public void run() {
+						SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH.mm");
+						boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
+						String beiZhu = "鏃�";
+						if (addTeam)
+							beiZhu = "棰嗗彇浜哄凡缁忔垚涓轰綘鐨勪竴绾ч槦鍛�";
+						
+						MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
+						msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
+						msgOther.setTitle("璧犻�佸鍔卞埜");
+						msgOther.setGiveType("浣犺禒閫佺殑濂栧姳鍒歌鎴愬姛棰嗗彇");
+						msgOther.setReceiveInfo("鏄电О:[鏄电О鏄电О鏄电О/鏃燷ID:[854245]");
+						msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
+						msgOther.setReceiveTime(sd.format(new Date()));
+						userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
+					}
+				});
 			} else {
 				throw new TokenRecordException(1, "鍙d护宸插け鏁�");
 			}
@@ -305,28 +411,61 @@
 			UserTaoLiJinGiveRecord giveRecord = userTaoLiJinGiveRecordService
 					.selectByPrimaryKey(Long.parseLong(identify));
 			if (giveRecord == null || giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_OVERDUE) {
-				throw new TokenRecordException(1, "鍝庡憖锛屽搸鍛�锛屾帹骞跨孩鍖呭凡澶辨晥浜嗭紒");
+				throw new TokenRecordException(1, "鍝庡憖锛屾帹骞跨孩鍖呭凡澶辨晥浜嗭紒");
 			} else if (giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_RECEIVE) {
 				throw new TokenRecordException(1, "鍝庡憖锛屾帹骞跨孩鍖呭凡琚鍙栦簡锛�");
 			} else {
-
+				Date endTime = giveRecord.getEndTime();
+				if (endTime != null && endTime.getTime() < now.getTime())
+					throw new TokenRecordException(1, "鍝庡憖锛屾帹骞跨孩鍖呭凡澶辨晥浜嗭紒");
 				
-				
-				
+				BigDecimal amount = giveRecord.getAmount();
+				try {
+					// 棰嗗彇
+					userTaoLiJinOriginService.receiveFriendsGive(uid, amount);
+					
+					// 鏇存柊璁板綍
+					UserTaoLiJinGiveRecord updateRecord = new UserTaoLiJinGiveRecord();
+					updateRecord.setId(Long.parseLong(identify));
+					updateRecord.setReceiveTime(new Date());
+					updateRecord.setReceiveUid(uid);
+					updateRecord.setState(UserTaoLiJinGiveRecord.STATE_RECEIVE);
+					userTaoLiJinGiveRecordService.updateByPrimaryKeySelective(updateRecord);
+				} catch (UserTaoLiJinOriginException e) {
+					e.printStackTrace();
+				}
 			}
 		} else {
 			throw new TokenRecordException(1, "鏃犲搴旂被鍨�");
 		}
 
-		if (receive) 
-			throw new TokenRecordException(1, "棰嗗彇澶辫触鍟�");
-		
-		if (StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
-			// 鎴愪负涓嬬骇
-			
-			
-		}
-
-		
 	}
+	
+	
+	/**
+	 *     鏈縺娲婚個璇风爜锛� 涓婁笅绾у叧绯荤‘绔�
+	 * @param uid
+	 * @param giveUid
+	 * @param userInfoExtra
+	 * @return
+	 */
+	public boolean addInviteTeam(Long uid, Long giveUid, UserInfoExtra userInfoExtra) {
+		boolean invite = false;
+		if (StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
+			UserInfoExtra userInfoExtraGive = userInfoExtraService.getUserInfoExtra(giveUid);
+			if (userInfoExtraGive != null ) {
+				String inviteCode = userInfoExtraGive.getInviteCode();
+				if(!StringUtil.isNullOrEmpty(inviteCode)) {
+					try {
+						userInfoExtraService.activateInviteCode(uid, inviteCode);
+						invite = true;
+					} catch (UserInfoExtraException e) {
+						e.printStackTrace();
+					}
+				}
+			}
+		}
+		return invite;
+	}
+	
 }

--
Gitblit v1.8.0