From 2e5c6b46697f7b96460ad0c356740df52bf056d2 Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期三, 14 八月 2019 09:51:07 +0800
Subject: [PATCH] 金币兑换

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java |  238 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 194 insertions(+), 44 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java
index 848d494..220ef2c 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java
@@ -1,5 +1,7 @@
 package com.yeshi.fanli.service.impl.integral;
 
+import java.math.BigDecimal;
+import java.util.Date;
 import java.util.List;
 
 import javax.annotation.Resource;
@@ -7,12 +9,26 @@
 import org.springframework.stereotype.Service;
 
 import com.yeshi.fanli.dao.mybatis.integral.IntegralExchangeMapper;
+import com.yeshi.fanli.entity.bus.user.HongBaoV2;
+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.integral.CodePublishRecord;
 import com.yeshi.fanli.entity.integral.IntegralExchange;
 import com.yeshi.fanli.entity.integral.IntegralExchange.ExchangeTypeEnum;
+import com.yeshi.fanli.entity.money.UserMoneyDetail;
 import com.yeshi.fanli.exception.integral.IntegralExchangeException;
+import com.yeshi.fanli.log.LogHelper;
+import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
+import com.yeshi.fanli.service.inter.integral.CodePublishRecordService;
 import com.yeshi.fanli.service.inter.integral.IntegralExchangeService;
+import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
 import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
+import com.yeshi.fanli.service.inter.user.UserInfoService;
+import com.yeshi.fanli.service.inter.user.UserMoneyDetailService;
+import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
 import com.yeshi.fanli.util.StringUtil;
+import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
 import com.yeshi.fanli.vo.integral.ExchangeTipVO;
 import com.yeshi.fanli.vo.user.UserInfoExtraVO;
 
@@ -24,6 +40,25 @@
 	
 	@Resource
 	private UserInfoExtraService userInfoExtraService;
+	
+	@Resource
+	private UserSystemCouponService userSystemCouponService;
+	
+	@Resource
+	private UserTaoLiJinOriginService userTaoLiJinOriginService;
+	
+	@Resource
+	private HongBaoV2Service hongBaoV2Service;
+	
+	@Resource
+	private UserMoneyDetailService userMoneyDetailService;
+	
+	@Resource
+	private UserInfoService userInfoService;
+	
+	@Resource
+	private CodePublishRecordService codePublishRecordService;
+	
 	
 
 	@Override
@@ -53,9 +88,18 @@
 		if (exchange == null)
 			throw new IntegralExchangeException(1, "鍏戞崲鏂瑰紡涓嶅瓨鍦�");
 		
+		Integer goldCoin = exchange.getGoldCoin();
 		
 		ExchangeTipVO exchangeTip = new ExchangeTipVO();
-		Integer goldCoin = exchange.getGoldCoin();
+		exchangeTip.setId(id);
+		ExchangeTypeEnum type = exchange.getType();
+		if (ExchangeTypeEnum.inviteCodeActivate == type) {
+			//exchangeTip.setTip("娉細鍏戞崲鎴愬姛鍚庤鍒扳�滄秷鎭�-绯荤粺娑堟伅鈥濇煡鐪�");
+			//exchangeTip.setGoldCoin(goldCoin + "閲戝竵");
+			exchangeTip.setType(type.name());
+			return exchangeTip;
+		}
+		
 		Integer goldCoinHas = extraVO.getGoldCoin();
 		if (goldCoin.intValue() > goldCoinHas.intValue()) {
 			exchangeTip.setType("notEnough");
@@ -64,16 +108,15 @@
 			return exchangeTip;
 		}
 		
-		ExchangeTypeEnum type = exchange.getType();
 		if (ExchangeTypeEnum.freeCouponBuy == type) {
 			exchangeTip.setTip("鑷喘鍏嶅崟鍒镐粎鑳借嚜宸变娇鐢紝涓旀瘡涓敤鎴稩D鍙兘鍏戞崲涓�娆°�俓r\n娉�:鍏戞崲鎴愬姛鍚庤鍒扳�滄垜鐨�-绂忓埄涓績鈥濅腑鏌ョ湅");
 		} else if (ExchangeTypeEnum.freeCouponGive == type) {
 			exchangeTip.setTip("璧犻�佸厤鍗曞埜鍏戞崲娆℃暟涓嶉檺锛岃禒閫佹鏁颁笉闄愶紝鍙楄禒浜鸿嫢鏃犫�滈個璇蜂汉鈥濇垚鍔熼鍙栧悗灏嗘垚涓轰綘鐨勪竴绾ч槦鍛樸�俓r\n娉�:鍏戞崲鎴愬姛鍚庤鍒扳�滄垜鐨�-绂忓埄涓績鈥濅腑鏌ョ湅");
-		} else if (ExchangeTypeEnum.rebateCoupon == type) {
+		} else if (ExchangeTypeEnum.rebatePercentCoupon == type) {
 			exchangeTip.setTip("杩斿埄濂栧姳鍒稿厬鎹㈡鏁颁笉闄愶紝璧犻�佹鏁颁笉闄愶紝鍙楄禒浜鸿嫢鏃犫�滈個璇蜂汉鈥濇垚鍔熼鍙栧悗灏嗘垚涓轰綘鐨勪竴绾ч槦鍛樸�俓r\n娉�:鍏戞崲鎴愬姛鍚庤鍒扳�滄垜鐨�-绂忓埄涓績鈥濅腑鏌ョ湅");
-		} else if (ExchangeTypeEnum.inviteCodeActivate == type) {
-			exchangeTip.setTip("娉細鍏戞崲鎴愬姛鍚庤鍒扳�滄秷鎭�-绯荤粺娑堟伅鈥濇煡鐪�");
 		} else if (ExchangeTypeEnum.inviteCodePublish == type) {
+			if(codePublishRecordService.countValidRecord(uid) > 0)
+				throw new IntegralExchangeException(1, "涓夊ぉ涔嬪唴涓嶅彲閲嶅鍏戞崲");
 			exchangeTip.setInviteCode(extraVO.getInviteCode());
 			exchangeTip.setTip("鍏戞崲鎴愬姛鍚庯紝灏嗗彂甯冧簬鈥滄縺娲婚個璇风爜鍏戞崲鍔熻兘涓�濓紝闇�婵�娲婚個璇风殑鐢ㄦ埛鍙敤閲戝竵鍏戞崲锛屾湰娆″睍绀烘湁鏁堟湡涓�3澶┿��");
 		} else if (ExchangeTypeEnum.taoLiJin == type) {
@@ -91,64 +134,171 @@
 		return exchangeTip;
 	}
 	
-	
 	@Override
-	public void exchange(Long uid, Long id, Long inviteId) throws IntegralExchangeException{
-		if (uid == null || uid <= 0) 
+	public IntegralExchange exchange(Long uid, Long id) throws IntegralExchangeException {
+		if (uid == null || uid <= 0)
 			throw new IntegralExchangeException(1, "鐢ㄦ埛鏈櫥褰�");
-		
-		if (id == null || id <= 0) 
+
+		if (id == null || id <= 0)
 			throw new IntegralExchangeException(1, "鍏戞崲id涓嶆纭�");
-		
-		UserInfoExtraVO extraVO = userInfoExtraService.getInfoExtraVOByUid(uid);
+
+		UserInfoExtra extraVO = userInfoExtraService.getUserInfoExtra(uid);
 		if (extraVO == null)
 			throw new IntegralExchangeException(1, "鐢ㄦ埛鐩稿叧淇℃伅涓嶅瓨鍦�");
-		
+
 		IntegralExchange exchange = integralExchangeMapper.selectByPrimaryKey(id);
 		if (exchange == null)
 			throw new IntegralExchangeException(1, "鍏戞崲鏂瑰紡涓嶅瓨鍦�");
-		
+
 		Integer goldCoin = exchange.getGoldCoin();
 		Integer goldCoinHas = extraVO.getGoldCoin();
 		if (goldCoin.intValue() > goldCoinHas.intValue()) {
 			throw new IntegralExchangeException(1, "褰撳墠璐︽埛涓彲鐢ㄩ噾甯佷笉瓒筹紝鏃犳硶鍏戞崲璇ュ鍝侊紒");
 		}
 		
-		ExchangeTypeEnum type = exchange.getType();
-		if (ExchangeTypeEnum.freeCouponBuy == type) {
-			// 鑷喘鍏嶅崟鍒�
+		try {
+			ExchangeTypeEnum type = exchange.getType();
+			if (ExchangeTypeEnum.freeCouponBuy == type) {
+				// 鑷喘鍏嶅崟鍒�
+				userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, false, null);
+			} else if (ExchangeTypeEnum.freeCouponGive == type) {
+				// 璧犻�佸厤鍗曞埜
+				userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, true, null);
+			} else if (ExchangeTypeEnum.rebatePercentCoupon == type) {
+				// 濂栧姳鍏嶅崟鍒�
+				userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, true,
+						new BigDecimal(10));
+			} else if (ExchangeTypeEnum.inviteCodePublish == type) {
+				// 閭�璇风爜鍙戝竷
+				if(codePublishRecordService.countValidRecord(uid) > 0)
+					throw new IntegralExchangeException(1, "涓夊ぉ涔嬪唴涓嶅彲閲嶅鍏戞崲");
+				codePublishRecordService.publishInviteCode(uid);
+			} else if (ExchangeTypeEnum.taoLiJin == type) {
+				// 鎺ㄥ箍绾㈠寘
+				userTaoLiJinOriginService.exchangeMoney(uid, exchange.getAmount());
+			} else if (ExchangeTypeEnum.cash == type) {
+				// 鐜伴噾绾㈠寘
+				BigDecimal money = exchange.getAmount();
+
+				// 1銆佹彃鍏ョ孩鍖呮暟鎹�
+				HongBaoV2 hongBaoV2 = new HongBaoV2();
+				hongBaoV2.setMoney(money);
+				hongBaoV2.setType(HongBaoV2.TYPE_EXCHANGE);
+				hongBaoV2.setState(HongBaoV2.STATE_YILINGQU);
+				hongBaoV2.setVersion(2);
+				hongBaoV2.setCreateTime(new Date());
+				hongBaoV2.setUpdateTime(new Date());
+				hongBaoV2.setUserInfo(new UserInfo(uid));
+				hongBaoV2.setPreGetTime(new Date());
+				hongBaoV2.setGetTime(new Date());
+				hongBaoV2Service.insertSelective(hongBaoV2);
+
+				// 2.鎻掑叆璧勯噾鏄庣粏,鐢ㄦ埛浣欓
+				UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createScoreConvert(money, new UserInfo(uid));
+				userMoneyDetailService.addUserMoneyDetail(userMoneyDetail);
+				userInfoService.addMoney(new UserInfo(uid), money);
+			} else {
+				throw new IntegralExchangeException(1, "鍏戞崲鏂瑰紡涓嶅瓨鍦�");
+			}
 			
+			String progress = exchange.getProgress();
+			if (!StringUtil.isNullOrEmpty(progress))
+				progress = progress.replace("{宸插厬鎹", 1 + "").replace("{涓婇檺鏁皚", exchange.getUpperLimit() + "");
+			exchange.setProgress(progress);
 			
-		} else if (ExchangeTypeEnum.freeCouponGive == type) {
-			// 璧犻�佸厤鍗曞埜
-			
-			
-		} else if (ExchangeTypeEnum.rebateCoupon == type) {
-			// 濂栧姳鍏嶅崟鍒�
-			
-			
-		} else if (ExchangeTypeEnum.inviteCodeActivate == type) {
-			if (inviteId == null || inviteId <= 0) 
-				throw new IntegralExchangeException(1, "浼犻�掑弬鏁颁笉姝g‘");
-			
-			// 婵�娲婚個璇风爜
-			UserInfoExtraVO inviteExtra = userInfoExtraService.getInfoExtraVOByUid(inviteId);
+			// 鏇存柊閲戝竵
+			UserInfoExtraVO extraUpdate = new UserInfoExtraVO();
+			extraUpdate.setId(extraVO.getId());
+			extraUpdate.setGoldCoin(goldCoinHas.intValue() - goldCoin.intValue());
+			userInfoExtraService.saveUserInfoExtra(extraUpdate);
+			return exchange;
+		} catch (Exception e) {
+			LogHelper.errorDetailInfo(e);
+			throw new IntegralExchangeException(1, "鍏戞崲寮傚父");
+		}
+	}
+	
+	
+	
+	@Override
+	public ExchangeTipVO verifyInviteCode(Long uid, Long id) throws IntegralExchangeException{
+		if (uid == null || uid <= 0)
+			throw new IntegralExchangeException(1, "鐢ㄦ埛鏈櫥褰�");
+
+		if (id == null || id <= 0)
+			throw new IntegralExchangeException(1, "鍏戞崲id涓嶆纭�");
+
+		UserInfoExtra extraVO = userInfoExtraService.getUserInfoExtra(uid);
+		if (extraVO == null)
+			throw new IntegralExchangeException(1, "鐢ㄦ埛鐩稿叧淇℃伅涓嶅瓨鍦�");
+
+		if (!StringUtil.isNullOrEmpty(extraVO.getInviteCode())) 
+			throw new IntegralExchangeException(1, "閭�璇风爜宸叉縺娲伙紝鏃犻渶鍏戞崲");
+		
+		CodePublishRecord record = codePublishRecordService.selectByPrimaryKey(id);
+		if (record == null)
+			throw new IntegralExchangeException(1, "鍏戞崲璁板綍涓嶅瓨鍦�");
+
+		IntegralExchange exchange = integralExchangeMapper.getValidByType(ExchangeTypeEnum.inviteCodeActivate.name());
+		if (exchange == null)
+			throw new IntegralExchangeException(1, "鍏戞崲鏂瑰紡涓嶅瓨鍦�");
+
+		ExchangeTipVO exchangeTip = new ExchangeTipVO();
+		exchangeTip.setId(id);
+		Integer goldCoin = exchange.getGoldCoin();
+		Integer goldCoinHas = extraVO.getGoldCoin();
+		if (goldCoin.intValue() > goldCoinHas.intValue()) {
+			exchangeTip.setType("notEnough");
+			exchangeTip.setTip("褰撳墠璐︽埛涓彲鐢ㄩ噾甯佷笉瓒筹紝鏃犳硶鍏戞崲璇ュ鍝侊紒");
+			exchangeTip.setGoldCoin((goldCoin.intValue() - goldCoinHas.intValue()) + "閲戝竵");
+			return exchangeTip;
+		} 
+		
+		exchangeTip.setTip("娉細鍏戞崲鎴愬姛鍚庤鍒扳�滄秷鎭�-绯荤粺娑堟伅鈥濇煡鐪�");
+		exchangeTip.setGoldCoin(goldCoin + "閲戝竵");
+		exchangeTip.setType(ExchangeTypeEnum.inviteCodeActivate.name());
+		return exchangeTip;
+	}
+	
+	
+	@Override
+	public void exchangeInviteCode(Long uid, Long id) throws IntegralExchangeException {
+		if (uid == null || uid <= 0)
+			throw new IntegralExchangeException(1, "鐢ㄦ埛鏈櫥褰�");
+
+		if (id == null || id <= 0)
+			throw new IntegralExchangeException(1, "鍏戞崲id涓嶆纭�");
+
+		UserInfoExtra extraVO = userInfoExtraService.getUserInfoExtra(uid);
+		if (extraVO == null)
+			throw new IntegralExchangeException(1, "鐢ㄦ埛鐩稿叧淇℃伅涓嶅瓨鍦�");
+
+		if (!StringUtil.isNullOrEmpty(extraVO.getInviteCode())) 
+			throw new IntegralExchangeException(1, "閭�璇风爜宸叉縺娲伙紝鏃犻渶鍏戞崲");
+		
+		CodePublishRecord record = codePublishRecordService.selectByPrimaryKey(id);
+		if (record == null)
+			throw new IntegralExchangeException(1, "鍏戞崲璁板綍涓嶅瓨鍦�");
+
+		IntegralExchange exchange = integralExchangeMapper.getValidByType(ExchangeTypeEnum.inviteCodeActivate.name());
+		if (exchange == null)
+			throw new IntegralExchangeException(1, "鍏戞崲鏂瑰紡涓嶅瓨鍦�");
+
+		Integer goldCoin = exchange.getGoldCoin();
+		Integer goldCoinHas = extraVO.getGoldCoin();
+		if (goldCoin.intValue() > goldCoinHas.intValue()) {
+			throw new IntegralExchangeException(1, "褰撳墠璐︽埛涓彲鐢ㄩ噾甯佷笉瓒筹紝鏃犳硶鍏戞崲璇ュ鍝侊紒");
+		} 
+		
+		try {
+			UserInfoExtraVO inviteExtra = userInfoExtraService.getInfoExtraVOByUid(record.getUid());
 			if (inviteExtra == null || StringUtil.isNullOrEmpty(inviteExtra.getInviteCode()))
 				throw new IntegralExchangeException(1, "鍏戞崲澶辫触锛岃鐢ㄦ埛閭�璇风爜涓嶅瓨鍦�");
 			
-		} else if (ExchangeTypeEnum.inviteCodePublish == type) {
-			// 閭�璇风爜鍙戝竷
-			
-		
-		} else if (ExchangeTypeEnum.taoLiJin == type) {
-			// 鎺ㄥ箍绾㈠寘
-			
-		
-		} else if (ExchangeTypeEnum.cash == type) {	
-			// 鐜伴噾绾㈠寘
-			
-		} else {
-			throw new IntegralExchangeException(1, "鍏戞崲鏂瑰紡涓嶅瓨鍦�");
+			userInfoExtraService.activateInviteCode(uid, inviteExtra.getInviteCode());
+		} catch (Exception e) {
+			LogHelper.errorDetailInfo(e);
+			throw new IntegralExchangeException(1, "鍏戞崲寮傚父");
 		}
 	}
 }

--
Gitblit v1.8.0