parent
38decaa8f1
commit
48ed1e3d7a
|
|
@ -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,19 +36,31 @@ 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> List<T> getListData(String key, Class<T> clazz,String method) {
|
||||
|
||||
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();
|
||||
try {
|
||||
|
|
@ -59,26 +74,27 @@ public class RedisUtils {
|
|||
|
||||
/**
|
||||
* 获取缓存里的数据 如果不存在 则插入 并返回
|
||||
* @param key redis Key
|
||||
* @param clazz 返回类型
|
||||
*
|
||||
* @param key redis Key
|
||||
* @param clazz 返回类型
|
||||
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
|
||||
*/
|
||||
public <T> T getObjectDate(String key, Class<T> clazz,String method) {
|
||||
public <T> T getObjectDate(String key, Class<T> clazz, String method) {
|
||||
String jsonStr = getDate(key, method);
|
||||
return this.fromJson(jsonStr, clazz);
|
||||
}
|
||||
|
||||
public String getDate(String key, String method) {
|
||||
public String getDate(String key, String method) {
|
||||
if (!this.hasKey(key)) {
|
||||
try {
|
||||
// 获取Lookup对象
|
||||
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
||||
// 构建方法类型(这里假设方法无参数,返回类型为void)
|
||||
MethodType methodType = MethodType.methodType(void.class , String.class);
|
||||
MethodType methodType = MethodType.methodType(void.class, String.class);
|
||||
// 获取方法句柄
|
||||
MethodHandle methodHandle = lookup.findVirtual(redisService.getClass(), method, methodType);
|
||||
// 调用方法句柄
|
||||
methodHandle.invoke(redisService,key);
|
||||
methodHandle.invoke(redisService, key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
|
|
@ -93,9 +109,9 @@ public class RedisUtils {
|
|||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
public void set(String key, Object value, long expire){
|
||||
public void set(String key, Object value, long expire) {
|
||||
valueOperations.set(key, toJson(value));
|
||||
if(expire != NOT_EXPIRE){
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
|
@ -114,13 +130,13 @@ public class RedisUtils {
|
|||
|
||||
}
|
||||
|
||||
public void set(String key, Object value){
|
||||
public void set(String key, Object value) {
|
||||
set(key, value, DEFAULT_EXPIRE);
|
||||
}
|
||||
|
||||
public <T> T get(String key, Class<T> clazz, long expire) {
|
||||
String value = valueOperations.get(key);
|
||||
if(expire != NOT_EXPIRE){
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
return value == null ? null : fromJson(value, clazz);
|
||||
|
|
@ -132,7 +148,7 @@ public class RedisUtils {
|
|||
|
||||
public String get(String key, long expire) {
|
||||
String value = valueOperations.get(key);
|
||||
if(expire != NOT_EXPIRE){
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
return value;
|
||||
|
|
@ -164,9 +180,9 @@ public class RedisUtils {
|
|||
/**
|
||||
* Object转成JSON数据
|
||||
*/
|
||||
private String toJson(Object object){
|
||||
if(object instanceof Integer || object instanceof Long || object instanceof Float ||
|
||||
object instanceof Double || object instanceof Boolean || object instanceof String){
|
||||
private String toJson(Object object) {
|
||||
if (object instanceof Integer || object instanceof Long || object instanceof Float ||
|
||||
object instanceof Double || object instanceof Boolean || object instanceof String) {
|
||||
return String.valueOf(object);
|
||||
}
|
||||
return Gson.toJson(object);
|
||||
|
|
@ -175,7 +191,25 @@ public class RedisUtils {
|
|||
/**
|
||||
* JSON数据,转成Object
|
||||
*/
|
||||
private <T> T fromJson(String json, Class<T> clazz){
|
||||
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 {
|
||||
|
|
@ -125,11 +122,11 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
|
|||
cashOut.setState(2);
|
||||
if (baseResp.getErrorMsg().contains("收款人账户号出款属性不匹配")) {
|
||||
cashOut.setRefund("提现失败,请检查支付宝账号与收款人姓名后,重试。");
|
||||
}else {
|
||||
} else {
|
||||
cashOut.setRefund(baseResp.getErrorMsg());
|
||||
}
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
UserMoneyDetails userMoneyDetails = new UserMoneyDetails(
|
||||
userInfo.getUserId(), null, null, "[现金大转盘]", 4, 2, 1,
|
||||
new BigDecimal(money), "现金红包自动提现" + money + "元", 1);
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue