1. 程式人生 > >APP轉盤抽獎Java服務端介面

APP轉盤抽獎Java服務端介面

應公司需求開發一個微信公眾號中抽獎活動

功能:獎品及中獎概率可在後臺配置,滾動重新整理中獎名單,控制使用者每日抽獎次數等。

規則:在活動期間,每日可抽獎一次,中獎後填寫個人資訊以便獎品的配送。

/**
 * 獲取抽獎頁面資料
 * @param request
 * @param response
 * @return
 * @throws ServletException
 * @throws IOException
 */
@RequestMapping(value="/queryLotteryActivity")
@ResponseBody
public AppIntReturn queryLotteryActivity(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	AppIntReturn res = new AppIntReturn();

	// 使用者同意授權後,能獲取到code
	String code = request.getParameter("code");

	// 使用者同意授權
	if (!"authdeny".equals(code)) {
		// 獲取網頁授權access_token
		WeixinOauth2Token weixinOauth2Token = CommonUtil
				.getOauth2AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code);
		// 使用者標識
		String openId = weixinOauth2Token.getOpenId();
		
		if(!StringUtil.isEmpty(openId)){
			
			// 查詢使用者資訊
			List<CxhWechatMember> memberList = appLotteryService.getMemberList(openId);
			// 操作次數
			int operNum = 1;  // 可寫成後臺可配置的
			if(memberList != null && memberList.size() > 0){
				operNum = operNum - memberList.size();
				/*// 獲取使用者資訊
				String accessToken = CommonUtil.getAccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT).getToken();
				cxhWechatMember = CommonUtil.getWeChatMemberInfo(accessToken, openId);
				// 儲存使用者資訊
				appLotteryService.saveMemberInfo(cxhWechatMember);*/
			}
			
			if (null == request.getParameter("activityId") || "".equals(request.getParameter("activityId"))){
				res.setResult("-2");
				res.setMsg("引數錯誤");
				return res;
			}
			// 查詢活動資訊
			CxhVoteActivity cxhVoteActivity = appLotteryService.getActivityInfo(request.getParameter("activityId"));
			if (null == cxhVoteActivity){
				res.setResult("-3");
				res.setMsg("暫無該類活動");
				return res;
			}
			CxhVoteAward cxhVoteAward = new CxhVoteAward();
			cxhVoteAward.setCxhVoteActivity(cxhVoteActivity);
			// 查詢獎品列表
			List<CxhVoteAward> awardList = appLotteryService.findAwardList(cxhVoteAward);
			
			// 返回Map
			Map<String, Object> rtnMap = new HashMap<String, Object>();
			rtnMap.put("activity", cxhVoteActivity);
			rtnMap.put("awardList", awardList);
			rtnMap.put("operNum", operNum);
			rtnMap.put("openId", openId);
			
			res.setResult("0");
			res.setMsg("請求成功");
			res.setData(rtnMap);
		}else{
			res.setResult("-1");
			res.setMsg("授權失敗");
		}
		
	}else{
		res.setResult("-1");
		res.setMsg("授權失敗");
	}
	return res;
}

/**
 * 中獎名單介面
 * @author lee
 * @return
 */
@ResponseBody
@RequestMapping(value = "/winningMemberList")
public Object queryWinningMemberList(HttpServletRequest request, HttpServletResponse response) {
	AppListReturn appResult = new AppListReturn();
	try {
		CxhWechatMember cxhWechatMember = new CxhWechatMember();
		cxhWechatMember.setIswinning("1"); // 中獎
		// 查詢中獎使用者名稱單(分頁)
		Page<CxhWechatMember> pageList = appLotteryService.findPage(new Page<CxhWechatMember>(request, response), cxhWechatMember);
		appResult.setData(pageList.getList());
		appResult.setPageNumber(pageList.getPageNo());
		appResult.setPageSize(pageList.getPageSize());
		appResult.setTotal((int) pageList.getCount());
		appResult.setTotalPages(pageList.getTotalPage());
		appResult.setResult(0);
		appResult.setMsg("成功");
	} catch (Exception e) {
		appResult.setResult(-9);
		appResult.setMsg("系統異常");
		logger.info(e.toString(), e);
	}
	return appResult;
}

/**
 * 抽獎介面
 * @author lee
 * @return
 */
@ResponseBody
@RequestMapping(value = "/doLottery")
public Object doLottery(HttpServletRequest request, HttpServletResponse response) {
	AppListReturn appResult = new AppListReturn();
	// 返回Map
	Map<String, Object> rtnMap = new HashMap<String, Object>();
	String activityId = request.getParameter("activityId");
	String openId = request.getParameter("openId");
	try {
		
		if (null == activityId || "".equals(activityId) || null == openId || "".equals(openId)){
			appResult.setResult(-2);
			appResult.setMsg("引數錯誤");
			return appResult;
		}
		// 查詢活動資訊
		CxhVoteActivity cxhVoteActivity = appLotteryService.getActivityInfo(request.getParameter("activityId"));
		if (null == cxhVoteActivity){
			appResult.setResult(-3);
			appResult.setMsg("暫無該類活動");
			return appResult;
		}
		CxhVoteAward cxhVoteAward = new CxhVoteAward();
		cxhVoteAward.setCxhVoteActivity(cxhVoteActivity);
		// 查詢獎品列表
		List<CxhVoteAward> awardList = appLotteryService.findAwardList(cxhVoteAward);
		
		Random rd = new Random();
		double dd = rd.nextDouble();
		double before = 0;
		double end = 0;
		cxhVoteAward.setLevel("5");  // 5-未中獎
		
		// 計算中獎概率
		for (int i = 0; i < awardList.size(); i++) {
			if(i > 0){
				before += awardList.get(i-1).getRate().doubleValue();
			}
			end += awardList.get(i).getRate().doubleValue();
			if(dd >= before && dd < end){
				if(awardList.get(i).getLeftnum() > 0){
					// 中獎獎品
					cxhVoteAward = awardList.get(i);
					// 修改獎品剩餘數量
					cxhVoteAward.setLeftnum(cxhVoteAward.getLeftnum() - 1);
					appLotteryService.updateAwardNumber(cxhVoteAward);
				}
				break;
			}
		}
		
		// 新增使用者操作記錄
		String accessToken = CommonUtil.getAccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT).getToken();
		CxhWechatMember cxhWechatMember = CommonUtil.getWeChatMemberInfo(accessToken, openId);
		cxhWechatMember.setId(IdGen.uuid());
		cxhWechatMember.setJoindate(new Date());
		cxhWechatMember.setDelFlag("0");
		// 儲存使用者資訊
		appLotteryService.saveMemberInfo(cxhWechatMember);
		
		rtnMap.put("awardLevel", cxhVoteAward.getLevel());
		rtnMap.put("awardId", cxhVoteAward.getId());
		appResult.setData(rtnMap);
		appResult.setResult(0);
		appResult.setMsg("成功");
	} catch (Exception e) {
		appResult.setResult(-9);
		appResult.setMsg("系統異常");
		logger.info(e.toString(), e);
	}
	return appResult;
}
/**
 * 儲存中獎使用者資訊的介面
 * @author lee
 * @return
 */
@ResponseBody
@RequestMapping(value = "/saveMemberInfo")
public Object saveMemberInfo(HttpServletRequest request, HttpServletResponse response) {
	AppListReturn appResult = new AppListReturn();
	try {
		
		// 使用者同意授權後,能獲取到code
		String openId = request.getParameter("openId");
		String username = request.getParameter("username");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String awardLevel = request.getParameter("awardLevel");
		String awardId = request.getParameter("awardId");

		if (null == username || "".equals(username)
				|| null == phone || "".equals(phone)
				|| null == address || "".equals(address)
				|| null == openId || "".equals(openId)
				|| null == awardLevel || "".equals(awardLevel)
				|| null == awardId || "".equals(awardId)){
			appResult.setResult(-2);
			appResult.setMsg("引數錯誤");
			return appResult;
		}
			
		// 查詢使用者資訊
		List<CxhWechatMember> memberList = appLotteryService.getMemberList(openId);
		CxhWechatMember cxhWechatMember = memberList.get(0);
		cxhWechatMember.setUsername(username);
		cxhWechatMember.setPhone(phone);
		cxhWechatMember.setAddress(address);
		cxhWechatMember.setIswinning(awardLevel == "5" ? "0" : "1");
		cxhWechatMember.setAwardid(awardId);
		appLotteryService.update(cxhWechatMember);
		
		appResult.setResult(0);
		appResult.setMsg("成功");
		
	} catch (Exception e) {
		appResult.setResult(-9);
		appResult.setMsg("系統異常");
		logger.info(e.toString(), e);
	}
	return appResult;
}