From 5c4ecf9e8b47efbbf5d21c9e109be0a60cb7e26d Mon Sep 17 00:00:00 2001
From: yujian <yujian>
Date: 星期一, 08 四月 2019 15:15:09 +0800
Subject: [PATCH] 冲突解决

---
 fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java |  177 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 126 insertions(+), 51 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 011d4dc..39825bf 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
@@ -17,10 +17,12 @@
 import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
 import com.yeshi.fanli.entity.bus.user.DeviceLotteryRecord;
 import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
+import com.yeshi.fanli.entity.bus.user.UserLotteryRecord;
 import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
 import com.yeshi.fanli.entity.system.SystemCoupon;
 import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
 import com.yeshi.fanli.exception.user.UserInfoExtraException;
+import com.yeshi.fanli.exception.user.UserLotteryRecordException;
 import com.yeshi.fanli.exception.user.UserSystemCouponException;
 import com.yeshi.fanli.service.inter.config.ConfigService;
 import com.yeshi.fanli.service.inter.config.SystemCouponService;
@@ -30,6 +32,7 @@
 import com.yeshi.fanli.service.inter.order.CommonOrderService;
 import com.yeshi.fanli.service.inter.user.DeviceLotteryRecordService;
 import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
+import com.yeshi.fanli.service.inter.user.UserLotteryRecordService;
 import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.vo.user.UserSystemCouponRecordVO;
@@ -79,9 +82,11 @@
 
 	@Resource
 	private ThreeSaleSerivce threeSaleSerivce;
+	
+	@Resource
+	private UserLotteryRecordService userLotteryRecordService;
+	
 
-	// 鏈�澶ф娊濂栨鏁�
-	private static int MAX_COUNT = 5;
 
 	/**
 	 * 鐢ㄦ埛鍒稿垪琛ㄦ煡璇�
@@ -435,11 +440,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;
@@ -458,45 +469,52 @@
 					JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("璁惧涓嶅瓨鍦�"));
 					return;
 				}
-				int countPrize = 0;
 
 				List<DeviceLotteryRecord> list = deviceLotteryRecordService.listByPlatformAndDevice(platformType,
 						device);
-				if (list != null && list.size() == MAX_COUNT) { // 鏃犳娊濂栨満浼�
+				if (list != null && list.size() >= Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 鏃犳娊濂栨満浼�
 					JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(2, "鎶藉娆℃暟涓嶈冻"));
 					return;
-				} else if (list != null && list.size() < MAX_COUNT) { // 鎷ユ湁鎶藉鏈轰細
-
+				} 
+				
+				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) {
-							SystemCoupon coupon = systemCouponService.selectByPrimaryKey(systemCouponId);
-							hasPrize = coupon.getType().name();
-						}
 					}
-
-					count = MAX_COUNT - 1 - list.size();
-
-				} else {
-					count = MAX_COUNT - 1;
+					count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1 - list.size();
 				}
-
-				// 濂栧搧
-				prize = getLotteryPrize(MAX_COUNT, 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();
+						}
 					}
 				}
 
@@ -509,41 +527,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();
+						UserSystemCoupon.SOURCE_NEWBIES);
+				
+				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(MAX_COUNT, list.size(), MAX_COUNT - 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);
+						userSystemCouponService.insertUserCoupon(uid, prize, UserSystemCoupon.SOURCE_NEWBIES);
 					}
 				}
-
+				
 				// 鍓╀綑娆℃暟
 				userInfoExtra.setLotteryNewbies(count);
 				userInfoExtraService.saveUserInfoExtra(userInfoExtra);
@@ -574,7 +613,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(); // 绂忓埄鍏嶈垂鍒�
@@ -582,16 +621,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) {
@@ -602,7 +635,7 @@
 			} else {
 				prize = rebateCoupon; // 濂栧姳鍒�
 			}
-
+			
 		} else { // 澶т簬3娆℃満浼�
 
 			if (countPrize < 2) { // 涓嶈冻涓や釜濂栧搧
@@ -614,8 +647,50 @@
 				}
 			}
 		}
-
 		return prize;
 	}
 
+	
+	/**
+	 * 鑾峰彇鎶藉娆℃暟- 澶╁ぉ鎶藉鍔卞埜
+	 * 
+	 * @param callback
+	 * @param acceptData
+	 * @param uid
+	 * @param out
+	 */
+	@RequestMapping(value = "getDailyCount")
+	public void getDailyCount(String callback, AcceptData acceptData, Long uid, PrintWriter out) {
+		try {
+			JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult( userLotteryRecordService.getLotteryCountDaily(uid)));
+		} catch (UserLotteryRecordException e) {
+			JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getCode(),e.getMsg()));
+		} catch (Exception e) {
+			JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("淇℃伅鑾峰彇澶辫触"));
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * 鑾峰彇鎶藉缁撴灉 - 澶╁ぉ鎶藉鍔卞埜`
+	 * 
+	 * 
+	 * 
+	 * @param callback
+	 * @param acceptData
+	 * @param uid
+	 * @param out
+	 */
+	@RequestMapping(value = "getDailyResult")
+	public void getDailyResult(String callback, AcceptData acceptData, Long uid, PrintWriter out) {
+		try {
+			JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult( userLotteryRecordService.executeLotteryDaily(uid)));
+		} catch (UserLotteryRecordException e) {
+			JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getCode(),e.getMsg()));
+		} catch (Exception e) {
+			JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("鎶藉澶辫触"));
+			e.printStackTrace();
+		}
+	}
+
 }

--
Gitblit v1.8.0