From cdcbed9af813b2a02cdc01eefa24db8bec6b51a9 Mon Sep 17 00:00:00 2001
From: yujian <yujian>
Date: 星期三, 27 三月 2019 12:17:33 +0800
Subject: [PATCH] 主分类 + 子分类 DAO改造

---
 fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java |  120 ++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 73 insertions(+), 47 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java b/fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java
index d21e90f..73c27c9 100644
--- a/fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java
+++ b/fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java
@@ -433,11 +433,17 @@
 	public void getLotteryResult(String callback, AcceptData acceptData, Long uid, PrintWriter out) {
 
 		try {
-			// 濂栧搧
+			// 鍓╀綑鎶藉娆℃暟
 			int count = 0;
-			String prize = "";
-			String hasPrize = "";
-			Long couponId = null;
+			// 鏈鎶戒腑鐨勫鍝�
+			String prize = null;
+			
+			// 宸蹭娇鐢ㄦ娊濂栨鏁�
+			int countUsed = 0;
+			// 宸叉嫢鏈夊鍝佹暟閲�
+			int countPrize = 0;
+			// 宸叉湁鍒哥被鍨�
+			String couponType = null;
 
 			if (uid == null || uid == 0) { // 鏈櫥褰�
 				int platformType = 0;
@@ -456,46 +462,52 @@
 					JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("璁惧涓嶅瓨鍦�"));
 					return;
 				}
-				int countPrize = 0;
 
 				List<DeviceLotteryRecord> list = deviceLotteryRecordService.listByPlatformAndDevice(platformType,
 						device);
-				if (list != null && list.size() == Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 鏃犳娊濂栨満浼�
+				if (list != null && list.size() >= Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 鏃犳娊濂栨満浼�
 					JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(2, "鎶藉娆℃暟涓嶈冻"));
 					return;
-				} else if (list != null && list.size() < Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 鎷ユ湁鎶藉鏈轰細
-
+				} 
+				
+				Long couponId = null;
+				
+				if (list == null || list.size() == 0) { // 鎷ユ湁鎶藉鏈轰細
+					count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1;
+				} else {
+					
 					for (DeviceLotteryRecord deviceLotteryRecord : list) {
 						Long systemCouponId = deviceLotteryRecord.getSystemCouponId();
 						if (systemCouponId != null) {
 							countPrize++;
 							couponId = systemCouponId;
 						}
-
 					}
-					
-					if (countPrize == 1 && couponId != null) {
-						SystemCoupon coupon = systemCouponService.selectByPrimaryKey(couponId);
-						hasPrize = coupon.getType().name();
-					}
-
 					count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1 - list.size();
-
-				} else {
-					count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1;
 				}
-
-				// 濂栧搧
-				prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize, list.size(), couponId);
+				
+				// 濂栧搧灏忎簬2鏃舵墠鎶藉
+				if (countPrize < 2) {
+					if (couponId != null) {
+						SystemCoupon coupon = systemCouponService.selectByPrimaryKey(couponId);
+						// 宸叉湁鍒�
+						couponType = coupon.getType().name();
+					}
+					// 濂栧搧
+					prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize, list.size(), couponType);
+				}
+				
 				// 鎶戒腑
 				Long newCouponId = null;
 				if (prize != null && prize.trim().length() > 0) {
 					// 宸插瓨鍦ㄦ鍒�
-					if (hasPrize.equals(prize)) {
+					if (prize.equals(couponType)) {
 						prize = null;
 					} else {
 						SystemCoupon coupon = systemCouponService.getCouponByType(prize);
-						newCouponId = coupon.getId();
+						if (coupon != null) {
+							newCouponId = coupon.getId();
+						}
 					}
 				}
 
@@ -508,41 +520,62 @@
 				deviceLotteryRecordService.insertSelective(record);
 
 			} else { // 宸茬櫥褰�
+				
 				UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
 				if (userInfoExtra == null) {
 					JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("淇℃伅涓嶆纭�"));
 					return;
 				}
 
+				// 鍓╀綑鎶藉娆℃暟
 				Integer lotteryNewbies = userInfoExtra.getLotteryNewbies();
 				if (lotteryNewbies == null || lotteryNewbies < 1) { // 娆℃暟宸茬敤鍏�
 					JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(2, "鎶藉娆℃暟涓嶈冻"));
 					return;
-				} else {
-					count = lotteryNewbies - 1; // 鍓╀綑娆℃暟
-				}
+				}  
+				
+				// 鍓╀綑娆℃暟
+				count = lotteryNewbies - 1; 
+				// 宸蹭娇鐢ㄦ鏁�
+				countUsed = Constant.MAX_COUNT_LOTTERY_NEWBIES - lotteryNewbies;
+				
 
+				// 宸叉娊涓殑濂栧搧
 				List<UserSystemCoupon> list = userSystemCouponService.getUserCouponBySource(uid,
 						UserSystemCoupon.SOURCE_CHOUJIANG);
-				if (list != null && list.size() == 1) {
-					couponId = list.get(0).getSystemCoupon().getId();
-					SystemCoupon coupon = systemCouponService.selectByPrimaryKey(couponId);
-					hasPrize = coupon.getType().name();
+				
+				if (list == null || list.size() == 0) {
+					// 鎶藉
+					prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize , countUsed, couponType);
+					
+				} else if (list.size() == 1) {
+					UserSystemCoupon userSystemCoupon = list.get(0);
+					
+					SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon();
+					if (systemCoupon != null) {
+						SystemCoupon coupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
+						// 宸叉湁鍒�
+						couponType = coupon.getType().name();
+					}
+					
+					// 鎷ユ湁濂栧搧鏁伴噺
+					countPrize = 1;
+					
+					// 鎶藉
+					prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize, countUsed, couponType);
 				}
 
-				// 濂栧搧
-				prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, list.size(), Constant.MAX_COUNT_LOTTERY_NEWBIES - lotteryNewbies, couponId);
 				// 鎶戒腑
 				if (prize != null && prize.trim().length() > 0) {
-
-					if (hasPrize.equals(prize)) {
+					if (prize.equals(couponType)) {
+						// 宸叉湁姝ゅ埜
 						prize = null;
 					} else {
 						// 鎻掑叆鍒镐俊鎭�
 						userSystemCouponService.insertUserCoupon(uid, prize, UserSystemCoupon.SOURCE_CHOUJIANG);
 					}
 				}
-
+				
 				// 鍓╀綑娆℃暟
 				userInfoExtra.setLotteryNewbies(count);
 				userInfoExtraService.saveUserInfoExtra(userInfoExtra);
@@ -573,7 +606,7 @@
 	 * @param couponId 鍒竔d
 	 * @return
 	 */
-	public String getLotteryPrize(int maxCount, int countPrize, int record, Long couponId) {
+	public String getLotteryPrize(int maxCount, int countPrize, int record, String couponType) {
 
 		String prize = null;
 		String freeCoupon = CouponTypeEnum.welfareFreeCoupon.name(); // 绂忓埄鍏嶈垂鍒�
@@ -581,16 +614,10 @@
 
 		if (countPrize == 1 && record == maxCount - 1) {
 			// 鏈�鍚庝竴娆★細 鍙敹鍒颁竴涓鍝�
-			SystemCoupon systemCoupon = systemCouponService.selectByPrimaryKey(couponId);
-			if (systemCoupon != null) {
-				CouponTypeEnum type = systemCoupon.getType();
-				if (type.name() == freeCoupon) {
-					prize = rebateCoupon; // 濂栧姳鍒�
-				} else {
-					prize = freeCoupon; // 绂忓埄鍏嶈垂鍒�
-				}
+			if (rebateCoupon.equals(couponType)) {
+				prize = freeCoupon; // 绂忓埄鍏嶈垂鍒�
 			} else {
-				prize = rebateCoupon; // 濂栧姳鍒�
+				prize = rebateCoupon;  // 濂栧姳鍒�
 			}
 
 		} else if (countPrize == 0 && record == maxCount - 2) {
@@ -601,7 +628,7 @@
 			} else {
 				prize = rebateCoupon; // 濂栧姳鍒�
 			}
-
+			
 		} else { // 澶т簬3娆℃満浼�
 
 			if (countPrize < 2) { // 涓嶈冻涓や釜濂栧搧
@@ -613,7 +640,6 @@
 				}
 			}
 		}
-
 		return prize;
 	}
 

--
Gitblit v1.8.0