抽奖3次后 修改概率
redis 工具类 新增返回 map<String,List<T>> 格式
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.sqx.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.sqx.modules.redisService.RedisService;
|
||||
@@ -10,12 +11,14 @@ import org.springframework.stereotype.Component;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Redis工具类
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class RedisUtils {
|
||||
@@ -33,18 +36,30 @@ public class RedisUtils {
|
||||
private SetOperations<String, Object> setOperations;
|
||||
@Autowired
|
||||
private ZSetOperations<String, Object> zSetOperations;
|
||||
/** 默认过期时长,单位:秒 */
|
||||
/**
|
||||
* 默认过期时长,单位:秒
|
||||
*/
|
||||
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
|
||||
/** 不设置过期时长 */
|
||||
/**
|
||||
* 不设置过期时长
|
||||
*/
|
||||
public final static long NOT_EXPIRE = -1;
|
||||
private final static Gson Gson = new Gson();
|
||||
|
||||
/**
|
||||
* 获取缓存里的数据 如果不存在 则插入 并返回
|
||||
* @param key redis Key
|
||||
* @param clazz 返回类型
|
||||
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
|
||||
*/
|
||||
|
||||
public <T> Map<String, List<T>> getMapData(String key, String method, Class<T> clazz) {
|
||||
String jsonStr = getDate(key, method);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
JsonNode jsonNode = objectMapper.readTree(jsonStr);
|
||||
|
||||
return jsonNodeToMap(jsonNode, clazz);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> getListData(String key, Class<T> clazz, String method) {
|
||||
String jsonStr = getDate(key, method);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
@@ -59,6 +74,7 @@ public class RedisUtils {
|
||||
|
||||
/**
|
||||
* 获取缓存里的数据 如果不存在 则插入 并返回
|
||||
*
|
||||
* @param key redis Key
|
||||
* @param clazz 返回类型
|
||||
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
|
||||
@@ -178,4 +194,22 @@ public class RedisUtils {
|
||||
private <T> T fromJson(String json, Class<T> clazz) {
|
||||
return Gson.fromJson(json, clazz);
|
||||
}
|
||||
|
||||
|
||||
public <T> Map<String, List<T>> jsonNodeToMap(JsonNode jsonNode, Class<T> clazz) {
|
||||
Map<String, List<T>> resultMap = new HashMap<>();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (jsonNode.isObject()) {
|
||||
// 获取字段名(也就是键)的迭代器
|
||||
Iterator<String> fieldNames = jsonNode.fieldNames();
|
||||
while (fieldNames.hasNext()) {
|
||||
String key = fieldNames.next();
|
||||
JsonNode elementNode = jsonNode.get(key);
|
||||
resultMap.put(key, objectMapper.convertValue(elementNode,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)));
|
||||
}
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ public class AppController {
|
||||
.eq("zhi_fu_bao", zhiFuBao));
|
||||
|
||||
if (count > 0) {
|
||||
return Result.error("一个支付宝账号仅可绑定一个支付宝用户");
|
||||
return Result.error("一个支付宝账号仅可绑定一个用户");
|
||||
}
|
||||
if (!ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", 3, "month")) {
|
||||
return Result.error("每月仅支持修改三次,请联系管理员");
|
||||
|
||||
@@ -151,10 +151,10 @@ public class DiscSpinningController {
|
||||
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @Nullable @ApiIgnore @RequestBody Map maps) {
|
||||
double amount = 0;
|
||||
Long orderId = null;
|
||||
Integer i = recordService.countDraw(userId);
|
||||
if (maps == null || !maps.containsKey("source") || !"task".equals(maps.get("source"))) {
|
||||
//任务抽奖
|
||||
int drawCount = Integer.parseInt(commonRepository.findOne(901).getValue());
|
||||
Integer i = recordService.countDraw(userId);
|
||||
if (i != null && i >= drawCount) {
|
||||
return Result.error("当日可抽奖次数已超限");
|
||||
}
|
||||
@@ -166,7 +166,7 @@ public class DiscSpinningController {
|
||||
}
|
||||
}
|
||||
return new Result().put("data",
|
||||
discSpinningService.draws(amount, orderId, userId, maps == null || maps.get("source") == null ? "order" : maps.get("source").toString()));
|
||||
discSpinningService.draws(i == null ? 1 : i + 1, amount, orderId, userId, maps == null || maps.get("source") == null ? "order" : maps.get("source").toString()));
|
||||
}
|
||||
|
||||
@ApiOperation("大转盘奖项领取")
|
||||
|
||||
@@ -18,6 +18,8 @@ import lombok.Data;
|
||||
public class DiscSpinningAmount extends Model<DiscSpinningAmount> {
|
||||
@ApiModelProperty("主键id")
|
||||
private Long id;
|
||||
@ApiModelProperty("从第几次开始变化")
|
||||
private Integer num;
|
||||
@ApiModelProperty("描述")
|
||||
private String name;
|
||||
@ApiModelProperty("0-1 小于 多少为该奖励")
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.sqx.modules.discSpinning.entity.DiscSpinningRecord;
|
||||
public interface DiscSpinningService extends IService<DiscSpinning> {
|
||||
|
||||
//抽奖
|
||||
DiscSpinningRecord draws(double orderAmount, Long orderId, Long userId, String source);
|
||||
DiscSpinningRecord draws(int drawCount, double orderAmount, Long orderId, Long userId, String source);
|
||||
|
||||
//领奖
|
||||
void receiveAsync(DiscSpinningRecord receive);
|
||||
|
||||
@@ -33,10 +33,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSpinning> implements DiscSpinningService {
|
||||
@@ -142,7 +139,7 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public DiscSpinningRecord draws(double orderAmount, Long orderId, Long userId, String source) {
|
||||
public DiscSpinningRecord draws(int drawCount, double orderAmount, Long orderId, Long userId, String source) {
|
||||
DiscSpinning result = new DiscSpinning("谢谢惠顾", 1, null);
|
||||
List<DiscSpinning> prizes = baseMapper.selectList(new QueryWrapper<DiscSpinning>().eq("disc_type", "order".equals(source) ? 1 : 2).orderByAsc("odds"));
|
||||
|
||||
@@ -153,7 +150,14 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
|
||||
} while (randomDouble == 0);
|
||||
BigDecimal randomNum = new BigDecimal(randomDouble).multiply(new BigDecimal(10000)).divide(new BigDecimal(100));
|
||||
|
||||
List<DiscSpinningAmount> amounts = redisUtils.getListData(RedisKeys.getDateKey("spinning:amount"), DiscSpinningAmount.class, "setDiscSpinningAmounts");
|
||||
List<DiscSpinningAmount> amounts = new ArrayList<>();
|
||||
Map<String, List<DiscSpinningAmount>> amountMaps = redisUtils.getMapData(RedisKeys.getDateKey("spinning:amount"), "setDiscSpinningAmounts", DiscSpinningAmount.class);
|
||||
for (int i = drawCount; i >= 0; i--) {
|
||||
if (amountMaps.containsKey(i + "")) {
|
||||
amounts = amountMaps.get(i + "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (DiscSpinning prize : prizes) {
|
||||
if (randomNum.compareTo(prize.getNumber()) < 0) {
|
||||
if (prize.getType() == 2) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.sqx.modules.redisService.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.sqx.common.utils.RedisKeys;
|
||||
import com.sqx.common.utils.RedisUtils;
|
||||
@@ -14,8 +13,10 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class RedisServiceImpl implements RedisService {
|
||||
@Lazy
|
||||
@@ -27,7 +28,11 @@ public class RedisServiceImpl implements RedisService {
|
||||
@Override
|
||||
public void setDiscSpinningAmounts(String key) {
|
||||
List<DiscSpinningAmount> amounts = amountService.list(new QueryWrapper<DiscSpinningAmount>().eq("status", 1).orderByAsc("max_amount"));
|
||||
redisUtils.set(key, amounts);
|
||||
Map<Integer, List<DiscSpinningAmount>> map =
|
||||
amounts.stream().collect(Collectors.groupingBy(
|
||||
disc -> disc.getNum() == null ? 0 : disc.getNum()
|
||||
));
|
||||
redisUtils.set(key, map);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user