记得上下班打卡 | git大法好,push需谨慎

Commit ec888aaa authored by 胡佳晨's avatar 胡佳晨

抽奖修改数据库

parent e673bff3
......@@ -35,6 +35,14 @@ public class RandomUtil {
return getRandom().nextInt(max - min) + min;
}
/**
* 从[min,max)中随机一个整数
*/
public static int getRandomIntTime(int min, int max) {
Random random = new Random(System.currentTimeMillis()-(new Random().nextInt()));
return random.nextInt(max - min) + min;
}
/**
* 获取随机boolean型
*
......
......@@ -13,11 +13,10 @@ public class SweetConstant {
public final static String REDIS_KEY_SWEET_ARTISTS_RELATION = "sweet:artists:relation:uid:";
public final static String REDIS_KEY_SWEET_ARTISTS_DETAILS = "sweet:artists:details:";
public final static String REDIS_KEY_SWEET_SHOP = "sweet:artists:shop:manual:";
public final static String REDIS_KEY_SWEET_LUCK_DRAW = "sweet:artists:luckDraw:uid:";
public final static String REDIS_KEY_SWEET_LUCK_DRAW_SURPLUS = "sweet:artists:luckDraw:surplus:num:";//中奖库存
public final static String REDIS_KEY_SWEET_LUCK_DRAW_ALL = "sweet:artists:luckDraw:all:num:";//中奖总量
public final static String REDIS_KEY_SWEET_LUCK_DRAW = "sweet:luckDraw:mobile:";
public final static String REDIS_KEY_SWEET_LUCK_DRAW_SURPLUS = "sweet:luckDraw:num:";//中奖库存
public final static String REDIS_KEY_SWEET_LUCK_DRAW_ALL = "sweet:luckDraw:num:";//中奖总量
public final static String REDIS_KEY_SWEET_LUCK_DRAW_PRESENT_MOBILE = "sweet:luckDraw:present:mobile:";//中奖库存
public final static String REDIS_KEY_SWEET_WECHAT_USER_INFO="sweet:wechatUser:unionId:";
......
......@@ -76,7 +76,7 @@ public class SweetDataController {
@ApiOperation("增加可中奖数量1最大6(20210820)")
public ResponseDto<Boolean> luckDrawAllAdd() {
try {
String redisKey = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_ALL.concat("20210820");
String redisKey = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_ALL.concat("20210820").concat(":all");
int count = (int) redisUtil.get(redisKey);
if (count >= 6) {
redisUtil.set(redisKey, 6);
......
......@@ -17,6 +17,7 @@ import com.liquidnet.service.sweet.vo.SweetAnswerVo;
import com.liquidnet.service.sweet.vo.SweetArtistsRelationVo;
import com.liquidnet.service.sweet.vo.SweetPrizeVo;
import com.liquidnet.service.sweet.vo.SweetRemindVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -27,6 +28,7 @@ import static com.liquidnet.service.sweet.constant.SweetConstant.REDIS_KEY_SWEET
import static com.liquidnet.service.sweet.constant.SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_SURPLUS;
@Component
@Slf4j
public class RedisDataUtils {
@Autowired
......@@ -393,7 +395,13 @@ public class RedisDataUtils {
//获取抽奖状态
public int getLuckDrawStatus(String mobile, String luckDrawNum) {
String redisKey = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW.concat(mobile).concat(":luckDrawNum:" + luckDrawNum);
return (int) redisUtil.get(redisKey);
Object obj = redisUtil.get(redisKey);
if(obj==null){
redisUtil.set(redisKey,0);
return 0;
}else{
return (int) redisUtil.get(redisKey);
}
}
//修改抽奖状态
......@@ -405,22 +413,22 @@ public class RedisDataUtils {
SweetPrizeVo vo = SweetPrizeVo.getNew();
String redisKey = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW.concat(mobile).concat(":luckDrawNum:" + luckDrawNum);
String redisSurplus = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_SURPLUS.concat(luckDrawNum);
String redisAll = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_ALL.concat(luckDrawNum);
String redisSurplus = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_SURPLUS.concat(luckDrawNum).concat(":surplus");
String redisAll = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_ALL.concat(luckDrawNum).concat(":all");
String redisPresent = SweetConstant.REDIS_KEY_SWEET_LUCK_DRAW_PRESENT_MOBILE.concat(mobile);
int surplus = (int) redisUtil.get(redisSurplus);//当前中奖数量
int prize = (int) redisUtil.get(redisAll);//总量
int prizeALl = (int) redisUtil.get(redisAll) + 1;//不变的总量
int prize = 0;//总量
int prizeALl = (int) redisUtil.get(redisAll);//不变的总量
int count = (int) redisUtil.incr(redisKey, 1);
if (count > 2) {
vo.setPrize(prizeALl);
vo.setPrize(prize);
vo.setPrizeAll(prizeALl);
return vo;
}
if (surplus < prize) {
if (surplus < prizeALl) {
//随机算法
Object obj = redisUtil.get(redisPresent);
int present;
......@@ -429,12 +437,12 @@ public class RedisDataUtils {
} else {
present = (int) obj;
}
int random1 = RandomUtil.getRandomInt(present, 100);
int random2 = RandomUtil.getRandomInt(present, 100);
int random1 = RandomUtil.getRandomIntTime(present, 101);
int random2 = RandomUtil.getRandomIntTime(present, 101);
if (random1 == random2) {
surplus = (int) redisUtil.get(redisSurplus);
if (surplus < prize) {
surplus = (int) redisUtil.incr(redisSurplus, 1);
if (surplus < prizeALl) {
prize = (int) redisUtil.incr(redisSurplus, 1);
redisUtil.set(redisKey, 2);
}
}
......@@ -442,12 +450,12 @@ public class RedisDataUtils {
sqls.add(SqlMapping.get("sweet_luck_draw.insert"));
sqlsDataA.add(new Object[]{
mobile,unionId, luckDrawNum, surplus, prize, now
mobile,unionId, luckDrawNum, prize, prizeALl, now
});
queueUtils.sendMsgByRedis(MQConst.SweetQueue.LUCK_DRAW.getKey(),
SqlMapping.gets(sqls, sqlsDataA));
vo.setPrize(surplus);
vo.setPrize(prize);
vo.setPrizeAll(prizeALl);
return vo;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment