|
|
package com.yoho.activity.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.yoho.activity.common.ApiResponse;
|
|
|
import com.yoho.activity.common.bo.ActPrizeProductBo;
|
|
|
import com.yoho.activity.common.bo.ActPrizeProductUserBo;
|
|
|
import com.yoho.activity.common.convert.ActPrizeProductConvert;
|
|
|
import com.yoho.activity.common.convert.ActPrizeProductUserConvert;
|
|
|
import com.yoho.activity.common.redis.CacheKeyHelper;
|
|
|
import com.yoho.activity.common.vo.YoLuckReq;
|
|
|
import com.yoho.activity.dal.model.ActPrizeProduct;
|
|
|
import com.yoho.activity.dal.model.ActPrizeProductUser;
|
|
|
import com.yoho.activity.service.IYoLuckService;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.core.redis.cluster.annotation.Redis;
|
...
|
...
|
@@ -19,13 +24,15 @@ import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.yoho.activity.dal.ActPrizeProductMapper;
|
|
|
import com.yoho.activity.dal.ActPrizeProductUserMapper;
|
|
|
import com.yoho.activity.common.enums.YoLockActivityStatusEnum;
|
|
|
import com.yoho.activity.common.enums.YoLockActivityListTypeEnum;
|
|
|
import com.yoho.activity.common.constatns.Constant;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
|
|
|
@Service
|
...
|
...
|
@@ -36,6 +43,10 @@ public class YoLuckServiceImpl implements IYoLuckService { |
|
|
@Resource
|
|
|
private ActPrizeProductMapper actPrizeProductMapper;
|
|
|
|
|
|
@Resource
|
|
|
private ActPrizeProductUserMapper actPrizeProductUserMapper;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
RedisValueHelper redisValueHelper;
|
|
|
|
...
|
...
|
@@ -93,6 +104,7 @@ public class YoLuckServiceImpl implements IYoLuckService { |
|
|
/**
|
|
|
* 已参加的活动列表
|
|
|
*/
|
|
|
@Override
|
|
|
public List<ActPrizeProductBo> participationList(YoLuckReq request, PageRequest page) throws ServiceException {
|
|
|
log.info("Enter YoLuckServiceImpl participationList YoLuckReq request = {}, PageRequest = {}", request, page);
|
|
|
List<ActPrizeProductBo> resultList = new ArrayList<>();
|
...
|
...
|
@@ -121,6 +133,7 @@ public class YoLuckServiceImpl implements IYoLuckService { |
|
|
* @return
|
|
|
* @throws ServiceException
|
|
|
*/
|
|
|
@Override
|
|
|
public ActPrizeProductBo getContentDetail(YoLuckReq request) throws ServiceException {
|
|
|
log.info("Enter YoLuckServiceImpl getContentDetail request = {}", request);
|
|
|
ActPrizeProductBo content = null;
|
...
|
...
|
@@ -129,36 +142,9 @@ public class YoLuckServiceImpl implements IYoLuckService { |
|
|
// 参数错误
|
|
|
throw new ServiceException(401, "参数不合法");
|
|
|
}
|
|
|
do {
|
|
|
|
|
|
// 读取缓存
|
|
|
RedisKeyBuilder key = getContentCacheKey(request.getActPrizeId());
|
|
|
String value = yhValueOperations.get(key);
|
|
|
ActPrizeProductBo redisContent = CacheKeyHelper.string2Value(value, ActPrizeProductBo.class);
|
|
|
if (null != redisContent) {
|
|
|
content = redisContent;
|
|
|
log.info("getActivityList with result from redis.");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 读取数据库
|
|
|
content = ActPrizeProductConvert.db2bo(actPrizeProductMapper.selectContentDetail(actPrizeId));
|
|
|
if (content == null) {
|
|
|
log.warn("Enter YoLuckServiceImpl getContentDetail actPrizeId = {}", actPrizeId);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 获取活动状态
|
|
|
content.setStatus(getActivityStatus(content));
|
|
|
|
|
|
// 写入缓存
|
|
|
try {
|
|
|
yhValueOperations.set(key, CacheKeyHelper.value2String(content), 10, TimeUnit.SECONDS);
|
|
|
} catch (Exception e) {
|
|
|
log.warn("setContentDetail redis cache fail err={}.", e);
|
|
|
}
|
|
|
|
|
|
} while (false);
|
|
|
|
|
|
// 获取活动基本信息
|
|
|
content = getActProBaseInfo(actPrizeId);
|
|
|
|
|
|
// 用户抽奖码数量
|
|
|
if (request.getUid() != null) {
|
...
|
...
|
@@ -167,6 +153,187 @@ public class YoLuckServiceImpl implements IYoLuckService { |
|
|
|
|
|
return content;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 最近参加活动用户
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws ServiceException
|
|
|
*/
|
|
|
@Override
|
|
|
public List<ActPrizeProductUserBo> recentUsers(YoLuckReq request) throws ServiceException {
|
|
|
log.info("Enter YoLuckServiceImpl recentUsers request = {}", request);
|
|
|
List<ActPrizeProductUserBo> result = new ArrayList<>();
|
|
|
Integer actPrizeId = request.getActPrizeId();
|
|
|
if (actPrizeId == null) {
|
|
|
// 参数错误
|
|
|
throw new ServiceException(401, "参数不合法");
|
|
|
}
|
|
|
|
|
|
do {
|
|
|
// 读取缓存
|
|
|
RedisKeyBuilder key = RedisKeyBuilder.newInstance().appendFixed(Constant.YOLUCK_ACTIVITY_RECENT_USERS_CACHE_PREFIX).appendVar(actPrizeId);
|
|
|
String value = yhValueOperations.get(key);
|
|
|
List<ActPrizeProductUserBo> redisResult = CacheKeyHelper.string2Value(value, List.class);
|
|
|
if (null != redisResult) {
|
|
|
result = redisResult;
|
|
|
log.info("getActivityList with result from redis.");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 读取数据库
|
|
|
List<ActPrizeProductUser> dbList = actPrizeProductUserMapper.selectRecentUsers(actPrizeId);
|
|
|
for (ActPrizeProductUser item : dbList) {
|
|
|
result.add(ActPrizeProductUserConvert.db2bo(item));
|
|
|
}
|
|
|
|
|
|
if (result == null) {
|
|
|
log.warn("Enter YoLuckServiceImpl getContentDetail actPrizeId = {}", actPrizeId);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 写入缓存
|
|
|
try {
|
|
|
yhValueOperations.set(key, CacheKeyHelper.value2String(result), 10, TimeUnit.SECONDS);
|
|
|
} catch (Exception e) {
|
|
|
log.warn("setContentDetail redis cache fail err={}.", e);
|
|
|
}
|
|
|
|
|
|
} while (false);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Boolean getCode(YoLuckReq request) throws ServiceException {
|
|
|
log.info("Enter YoLuckServiceImpl getCode");
|
|
|
Integer actPrizeId = request.getActPrizeId();
|
|
|
Integer uid = request.getUid();
|
|
|
Integer shareUid = request.getShareUid();
|
|
|
String userName = request.getUserName();
|
|
|
String userthumb = request.getUserThumb();
|
|
|
String unionId = request.getUnionId();
|
|
|
Integer isShareTake = request.getIsShareTake();
|
|
|
|
|
|
|
|
|
if (actPrizeId == null || uid == null || null == userName || null == userthumb) {
|
|
|
// 参数错误
|
|
|
log.warn("params error width request = {}", request);
|
|
|
throw new ServiceException(401, "参数不合法");
|
|
|
}
|
|
|
|
|
|
// 查询活动状态: 活动结束
|
|
|
ActPrizeProductBo actInfo = getActProBaseInfo(actPrizeId);
|
|
|
if (actInfo.getStatus() >= 3) {
|
|
|
log.warn("getCode act is over");
|
|
|
throw new ServiceException(401, "活动已经结束");
|
|
|
}
|
|
|
|
|
|
|
|
|
// 查询用户获得的抽奖码总数
|
|
|
ActPrizeProductUser record = new ActPrizeProductUser();
|
|
|
record.setActPrizeId(actPrizeId);
|
|
|
record.setUid(uid);
|
|
|
record.setIsShareTake(isShareTake);
|
|
|
record.setPrizeCode(createPrizeCode());
|
|
|
record.setShareUid(shareUid);
|
|
|
record.setUserName(userName);
|
|
|
record.setUserThumb(userthumb);
|
|
|
record.setCreateTime(new Date());
|
|
|
|
|
|
Integer userCodeTotal = actPrizeProductUserMapper.getActUserCodeCount(record);
|
|
|
|
|
|
if (userCodeTotal > 0) {
|
|
|
log.warn("Uid has got code before!");
|
|
|
throw new ServiceException(401, "您已获得抽奖码,分享好友获取更多抽奖码");
|
|
|
}
|
|
|
|
|
|
// 登陆用户获取抽奖码
|
|
|
Integer id = actPrizeProductUserMapper.insertSelective(record);
|
|
|
|
|
|
/**
|
|
|
* 如果分享的用户和登陆用户相同: 您已获得抽奖码,分享好友获取更多抽奖码
|
|
|
*/
|
|
|
if (shareUid != null && uid == shareUid) {
|
|
|
log.warn("Uid has got code before!");
|
|
|
throw new ServiceException(401, "您已获得抽奖码,分享好友获取更多抽奖码");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 分享用户获取抽奖码
|
|
|
* 1、查询用户参加次数,是否已达到最多限制
|
|
|
*/
|
|
|
if (null != id && id > 0 && null != shareUid) {
|
|
|
ActPrizeProductUser shareRecord = new ActPrizeProductUser();
|
|
|
shareRecord.setActPrizeId(actPrizeId);
|
|
|
shareRecord.setUid(shareUid);
|
|
|
shareRecord.setIsShareTake(1);
|
|
|
shareRecord.setPrizeCode(createPrizeCode());
|
|
|
shareRecord.setShareUid(0);
|
|
|
shareRecord.setCreateTime(new Date());
|
|
|
Integer shareUserCodeTotal = actPrizeProductUserMapper.getActUserCodeCount(record);
|
|
|
if (shareUserCodeTotal < Constant.YOLUCK_ACTIVITY_PRIZE_CODE_MAX_NUM) {
|
|
|
actPrizeProductUserMapper.insertShareUserCode(shareRecord);
|
|
|
} else {
|
|
|
log.warn("share uid join limit with total code number = {}", shareUserCodeTotal);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 查询活动基本信息
|
|
|
*/
|
|
|
private ActPrizeProductBo getActProBaseInfo(Integer id) {
|
|
|
|
|
|
// 读取缓存
|
|
|
RedisKeyBuilder key = getContentCacheKey(id);
|
|
|
String value = yhValueOperations.get(key);
|
|
|
ActPrizeProductBo content = CacheKeyHelper.string2Value(value, ActPrizeProductBo.class);
|
|
|
if (null != content) {
|
|
|
log.info("getActivityList from redis.");
|
|
|
return content;
|
|
|
}
|
|
|
|
|
|
// 读取数据库
|
|
|
content = ActPrizeProductConvert.db2bo(actPrizeProductMapper.selectContentDetail(id));
|
|
|
if (content == null) {
|
|
|
log.warn("Enter YoLuckServiceImpl getContentDetail actPrizeId = {}", id);
|
|
|
return content;
|
|
|
}
|
|
|
|
|
|
// 获取活动状态
|
|
|
content.setStatus(getActivityStatus(content));
|
|
|
|
|
|
// 写入缓存
|
|
|
try {
|
|
|
yhValueOperations.set(key, CacheKeyHelper.value2String(content), 5, TimeUnit.MINUTES);
|
|
|
} catch (Exception e) {
|
|
|
log.warn("setContentDetail redis cache fail err={}.", e);
|
|
|
}
|
|
|
return content;
|
|
|
}
|
|
|
/**
|
|
|
* 生成抽奖码
|
|
|
* @return
|
|
|
*/
|
|
|
private String createPrizeCode() {
|
|
|
return createPrizeCode(8);
|
|
|
}
|
|
|
private String createPrizeCode(Integer length) {
|
|
|
String codeMap = Constant.YOLUCK_ACTIVITY_PRIZE_CODE_STRING;
|
|
|
StringBuilder code = new StringBuilder();
|
|
|
for(int i=0; i< length; i++) {
|
|
|
int startIndex = (int) (Math.random() * codeMap.length());
|
|
|
String codeItem = codeMap.substring(startIndex, startIndex+1);
|
|
|
code.append(codeItem);
|
|
|
}
|
|
|
return code.toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 格式化活动列表数据
|
|
|
* @param list
|
...
|
...
|
@@ -227,4 +394,6 @@ public class YoLuckServiceImpl implements IYoLuckService { |
|
|
private RedisKeyBuilder getContentCacheKey(int actPrizeId){
|
|
|
return RedisKeyBuilder.newInstance().appendFixed(Constant.YOLUCK_ACTIVITY_CONTENT_DETAIL_CACHE_PREFIX).appendVar(actPrizeId);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|