Authored by 周少峰

'addCode'

package com.yoho.activity.common.bo;
import com.alibaba.fastjson.JSONArray;
import com.yoho.queue.dal.model.BaseModel;
import java.util.Date;
... ... @@ -41,7 +42,7 @@ public class ActPrizeProductBo extends BaseModel {
private Integer lotteryTime;
private String content;
private JSONArray content;
private Integer myCodeNum = 0;
... ... @@ -173,11 +174,11 @@ public class ActPrizeProductBo extends BaseModel {
this.lotteryTime = lotteryTime;
}
public String getContent() {
public JSONArray getContent() {
return content;
}
public void setContent(String content) {
public void setContent(JSONArray content) {
this.content = content;
}
... ...
package com.yoho.activity.common.bo;
import com.yoho.queue.dal.model.BaseModel;
import java.util.Date;
public class ActPrizeProductUserBo extends BaseModel {
/**
*
*/
private static final long serialVersionUID = 1704484697194941946L;
private Integer id;
private Integer actId;
private Integer actPrizeId;
private Integer uid;
private String userName;
private String userThumb;
private String unionId;
private String prizeCode;
private Byte isShareTake;
private Integer shareUid;
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getActId() {
return actId;
}
public void setActId(Integer actId) {
this.actId = actId;
}
public Integer getActPrizeId() {
return actPrizeId;
}
public void setActPrizeId(Integer actPrizeId) {
this.actPrizeId = actPrizeId;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getUserThumb() {
return userThumb;
}
public void setUserThumb(String userThumb) {
this.userThumb = userThumb == null ? null : userThumb.trim();
}
public String getUnionId() {
return unionId;
}
public void setUnionId(String unionId) {
this.unionId = unionId == null ? null : unionId.trim();
}
public String getPrizeCode() {
return prizeCode;
}
public void setPrizeCode(String prizeCode) {
this.prizeCode = prizeCode == null ? null : prizeCode.trim();
}
public Byte getIsShareTake() {
return isShareTake;
}
public void setIsShareTake(Byte isShareTake) {
this.isShareTake = isShareTake;
}
public Integer getShareUid() {
return shareUid;
}
public void setShareUid(Integer shareUid) {
this.shareUid = shareUid;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
\ No newline at end of file
... ...
... ... @@ -51,4 +51,15 @@ public class Constant {
// Yo!Luck 活动详情缓存前缀
public final static String YOLUCK_ACTIVITY_CONTENT_DETAIL_CACHE_PREFIX = "yh:yoluck:activity_content_detail_cache_prefix_";
// Yo!Luck 活动基本信息详情缓存前缀
public final static String YOLUCK_ACTIVITY_CONTENT_BASE_CACHE_PREFIX = "yh:yoluck:activity_content_base_cache_prefix_";
// 最近参加活动的用户
public final static String YOLUCK_ACTIVITY_RECENT_USERS_CACHE_PREFIX = "yh:yoluck:activity_recent_users_cache_prefix_";
// Yo!Luck 抽奖码组合字符
public final static String YOLUCK_ACTIVITY_PRIZE_CODE_STRING = "QQWERTYUIOPASDFGHJKLZXCVBNM12345678900";
// Yo!Luck 单个活动用户最多可活动抽奖码数量
public final static int YOLUCK_ACTIVITY_PRIZE_CODE_MAX_NUM = 100;
}
... ...
package com.yoho.activity.common.convert;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.activity.common.bo.ActPrizeProductBo;
import com.yoho.activity.common.vo.YoLuckReq;
import com.yoho.activity.dal.model.ActPrizeProduct;
... ... @@ -20,10 +22,21 @@ public class ActPrizeProductConvert {
ActPrizeProductBo bo=new ActPrizeProductBo();
BeanUtils.copyProperties(db, bo);
// 图片地址换成https
bo.setCoverImg(StringUtils.replace(db.getCoverImg(),"http:" , "https:"));
if (null != db.getCoverImg()) {
bo.setCoverImg(StringUtils.replace(db.getCoverImg(),"http:" , "https:"));
}
// 价格保留2位小数,添加人民币符号
StringBuilder priceBuffer = new StringBuilder();
bo.setPrice(priceBuffer.append("¥").append(String.valueOf(db.getPrice() / 100d)).toString());
if (null != db.getPrice()) {
StringBuilder priceBuffer = new StringBuilder();
bo.setPrice(priceBuffer.append("¥").append(String.valueOf(db.getPrice() / 100d)).toString());
}
// 格式化内容
if (db.getContent() != null) {
bo.setContent(JSONObject.parseArray(db.getContent()));
}
return bo;
}
... ...
package com.yoho.activity.common.convert;
import com.yoho.activity.common.bo.ActPrizeProductUserBo;
import com.yoho.activity.common.vo.YoLuckReq;
import com.yoho.activity.dal.model.ActPrizeProductUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
public class ActPrizeProductUserConvert {
/**
* 数据库对象转为bo对象返回
* @param db
* @return
*/
public static ActPrizeProductUserBo db2bo(ActPrizeProductUser db){
if (db == null) {
return null;
}
ActPrizeProductUserBo bo=new ActPrizeProductUserBo();
BeanUtils.copyProperties(db, bo);
return bo;
}
}
... ...
... ... @@ -18,6 +18,15 @@ public class YoLuckReq {
private Integer actPrizeId;
private String userName;
private String userThumb = "https://img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100";
private Integer shareUid;
private String unionId;
private Integer isShareTake;
public Integer getActId() {
return actId;
... ... @@ -58,4 +67,44 @@ public class YoLuckReq {
public void setActPrizeId(Integer actPrizeId) {
this.actPrizeId = actPrizeId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserThumb() {
return userThumb;
}
public void setUserThumb(String userThumb) {
this.userThumb = userThumb;
}
public Integer getShareUid() {
return shareUid;
}
public void setShareUid(Integer shareUid) {
this.shareUid = shareUid;
}
public String getUnionId() {
return unionId;
}
public void setUnionId(String unionId) {
this.unionId = unionId;
}
public Integer getIsShareTake() {
return isShareTake;
}
public void setIsShareTake(Integer isShareTake) {
this.isShareTake = isShareTake;
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.yoho.activity.controller;
import com.yoho.activity.common.ApiResponse;
import com.yoho.activity.common.bo.ActPrizeProductBo;
import com.yoho.activity.common.bo.ActPrizeProductUserBo;
import com.yoho.activity.service.IYoLuckService;
import com.yoho.error.exception.ServiceException;
import org.slf4j.Logger;
... ... @@ -70,9 +71,9 @@ public class YoLuckController {
*/
@RequestMapping("/code/recent")
@ResponseBody
public List<ActPrizeProductBo> recent(YoLuckReq request, PageRequest page) throws ServiceException {
log.info("Enter YoLuckController participationList YoLuckReq request = {}, PageRequest = {}", request, page);
return yoLuckService.participationList(request, page);
public List<ActPrizeProductUserBo> recent(YoLuckReq request) throws ServiceException {
log.info("Enter YoLuckController recent YoLuckReq request = {}", request);
return yoLuckService.recentUsers(request);
}
/**
* 我的列表
... ... @@ -87,16 +88,14 @@ public class YoLuckController {
}
/**
* 获得分享
* 获得抽奖
* @return
* @throws ServiceException
*/
@RequestMapping("/code/gain")
@ResponseBody
public ApiResponse getShareCode(@RequestParam(value="uid")int uid, @RequestParam(value="year")String year) throws ServiceException {
log.info("");
ApiResponse apiResponse = new ApiResponse();
log.info("");
return apiResponse;
public ApiResponse getCode(YoLuckReq request) throws ServiceException {
log.info("Enter YoLuckController getCode YoLuckReq request = {}", request);
return yoLuckService.getCode(request);
}
}
... ...
package com.yoho.activity.dal;
import com.yoho.activity.dal.model.ActPrizeProductUser;
import java.util.List;
public interface ActPrizeProductUserMapper {
int deleteByPrimaryKey(Integer id);
... ... @@ -14,4 +15,23 @@ public interface ActPrizeProductUserMapper {
int updateByPrimaryKeySelective(ActPrizeProductUser record);
int updateByPrimaryKey(ActPrizeProductUser record);
/**
* 最近参加活动的用户信息(10个)
* @param actPrizeId
* @return
*/
List<ActPrizeProductUser> selectRecentUsers(Integer actPrizeId);
//
/**
* 查询用户的抽奖码总数
* @param record
* @return
*/
Integer getActUserCodeCount(ActPrizeProductUser record);
// 给shareUid添加抽奖码
Integer insertShareUserCode(ActPrizeProductUser record);
}
\ No newline at end of file
... ...
... ... @@ -21,7 +21,7 @@ public class ActPrizeProductUser extends BaseModel {
private String prizeCode;
private Byte isShareTake;
private Integer isShareTake;
private Integer shareUid;
... ... @@ -91,11 +91,11 @@ public class ActPrizeProductUser extends BaseModel {
this.prizeCode = prizeCode == null ? null : prizeCode.trim();
}
public Byte getIsShareTake() {
public Integer getIsShareTake() {
return isShareTake;
}
public void setIsShareTake(Byte isShareTake) {
public void setIsShareTake(Integer isShareTake) {
this.isShareTake = isShareTake;
}
... ...
... ... @@ -161,4 +161,25 @@
create_time = #{createTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectRecentUsers" resultMap="BaseResultMap">
select uid, user_name, user_thumb, create_time from act_prize_product_user
where act_prize_id = #{actPrizeId} and is_share_take=0 order by create_time desc limit 10
</select>
<select id="getActUserCodeCount" resultType="java.lang.Integer">
SELECT count(1) from act_prize_product_user where 1=1
<if test="record.actPrizeId != null">
and act_prize_id = #{actPrizeId}
</if>
<if test="record.actPrizeId != null">
and uid = #{uid}
</if>
</select>
<insert id="insertSelective" parameterType="com.yoho.activity.dal.model.ActPrizeProductUser">
insert into act_prize_product_user (act_prize_id, uid, user_name, user_thumb, union_id, prize_code, is_share_take, share_uid)
select #{record.actPrizeId} as act_prize_id, #{record.uid} as uid, #{record.userName}, #{record.userThumb}, #{record.unionId}, #{record.prizeCode} as prize_code,
#{record.isShareTake} as is_share_take, #{record.shareUid} as share_uid from act_prize_product_user where uid = #{record.uid} and is_share_take=0 limit 1;
</insert>
</mapper>
\ No newline at end of file
... ...
# 详情页调整
- 内容详情 楼层删除 act_id、id, act_prize_id 、sort,只保留floor_type, content.后台保存的时候已经排序。
... ...
... ... @@ -5,6 +5,7 @@ package com.yoho.activity.service;
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.vo.YoLuckReq;
import com.yoho.error.exception.ServiceException;
import com.yoho.tools.common.beans.page.PageRequest;
... ... @@ -36,5 +37,28 @@ public interface IYoLuckService {
*/
public List participationList(YoLuckReq request, PageRequest page) throws ServiceException;
/**
* 活动详情
* @param request
* @return
* @throws ServiceException
*/
public ActPrizeProductBo getContentDetail(YoLuckReq request) throws ServiceException;
/**
* 最近参与的用户信息
* @param request
* @return
* @throws ServiceException
*/
public List<ActPrizeProductUserBo> recentUsers(YoLuckReq request) throws ServiceException;
/**
* 获取抽奖码
* @param request
* @return
* @throws ServiceException
*/
public Boolean getCode(YoLuckReq request) throws ServiceException;
}
... ...
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);
}
}
... ...