库存记录 存入redis
This commit is contained in:
parent
b590a3260a
commit
ce3ef6b8d3
|
|
@ -60,4 +60,8 @@ public interface CacheKey {
|
||||||
* 数据字典
|
* 数据字典
|
||||||
*/
|
*/
|
||||||
String DICT_NAME = "dict:name:";
|
String DICT_NAME = "dict:name:";
|
||||||
|
/**
|
||||||
|
* 库存 PRODUCT:shopid:skuid
|
||||||
|
*/
|
||||||
|
String PRODUCT_SKU = "PRODUCT:";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.connection.RedisConnection;
|
import org.springframework.data.redis.connection.RedisConnection;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.core.*;
|
import org.springframework.data.redis.core.*;
|
||||||
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
|
@ -693,6 +694,10 @@ public class RedisUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void incrBy(String key, double delta) {
|
||||||
|
redisTemplate.opsForValue().increment(key, delta);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param prefix 前缀
|
* @param prefix 前缀
|
||||||
* @param ids id
|
* @param ids id
|
||||||
|
|
@ -709,4 +714,32 @@ public class RedisUtils {
|
||||||
log.debug("缓存删除数量:" + count + "个");
|
log.debug("缓存删除数量:" + count + "个");
|
||||||
log.debug("--------------------------------------------");
|
log.debug("--------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delByIntKey(String prefix, Set<Integer> ids) {
|
||||||
|
Set<Object> keys = new HashSet<>();
|
||||||
|
for (Integer id : ids) {
|
||||||
|
keys.addAll(redisTemplate.keys(new StringBuffer(prefix).append(id).toString()));
|
||||||
|
}
|
||||||
|
long count = redisTemplate.delete(keys);
|
||||||
|
// 此处提示可自行删除
|
||||||
|
log.debug("--------------------------------------------");
|
||||||
|
log.debug("成功删除缓存:" + keys.toString());
|
||||||
|
log.debug("缓存删除数量:" + count + "个");
|
||||||
|
log.debug("--------------------------------------------");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* redis 库存操作
|
||||||
|
* @param type 1为修改库存 2为移除库存
|
||||||
|
* @param map k:
|
||||||
|
*/
|
||||||
|
public void redisUp(Integer type,String shopId,Map<Integer,Double> map) {
|
||||||
|
String redisKey= CacheKey.PRODUCT_SKU+shopId+ ":";
|
||||||
|
if(type==1) {
|
||||||
|
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
|
||||||
|
incrBy(redisKey+entry.getKey(),entry.getValue());
|
||||||
|
}
|
||||||
|
} else if (type == 2) {
|
||||||
|
delByIntKey(redisKey,map.keySet());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
@ -69,6 +70,8 @@ public class TbProductServiceImpl implements TbProductService {
|
||||||
private final TbProductSpecRepository tbProductSpecRepository;
|
private final TbProductSpecRepository tbProductSpecRepository;
|
||||||
private final TbProductSkuResultRepository tbProductSkuResultRepository;
|
private final TbProductSkuResultRepository tbProductSkuResultRepository;
|
||||||
|
|
||||||
|
private final RedisUtils redisUtils;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String,Object> queryAll(TbProductQueryCriteria criteria){
|
public Map<String,Object> queryAll(TbProductQueryCriteria criteria){
|
||||||
|
|
@ -261,7 +264,10 @@ public class TbProductServiceImpl implements TbProductService {
|
||||||
sku.setUpdatedAt(Instant.now().toEpochMilli());
|
sku.setUpdatedAt(Instant.now().toEpochMilli());
|
||||||
skuList.add(sku);
|
skuList.add(sku);
|
||||||
}
|
}
|
||||||
tbProductSkuRepository.saveAll(skuList);
|
List<TbProductSku> tbProductSkus = tbProductSkuRepository.saveAll(skuList);
|
||||||
|
Map<Integer, Double> idStockMap = tbProductSkus.stream()
|
||||||
|
.collect(Collectors.toMap(TbProductSku::getId, TbProductSku::getStockNumber));
|
||||||
|
redisUtils.redisUp(1,save.getShopId(),idStockMap);
|
||||||
}
|
}
|
||||||
//保存到sku_result
|
//保存到sku_result
|
||||||
if ("sku".equals(resources.getTypeEnum())){
|
if ("sku".equals(resources.getTypeEnum())){
|
||||||
|
|
@ -325,6 +331,12 @@ public class TbProductServiceImpl implements TbProductService {
|
||||||
public void deleteAll(Integer[] ids) {
|
public void deleteAll(Integer[] ids) {
|
||||||
List<Integer> list = Arrays.asList(ids);
|
List<Integer> list = Arrays.asList(ids);
|
||||||
tbProductRepository.updateByStatus(list);
|
tbProductRepository.updateByStatus(list);
|
||||||
|
list.forEach(productId->{
|
||||||
|
List<TbProductSku> tbProductSkus = tbProductSkuRepository.searchSku(productId.toString());
|
||||||
|
Map<Integer, Double> idStockMap = tbProductSkus.stream()
|
||||||
|
.collect(Collectors.toMap(TbProductSku::getId, TbProductSku::getStockNumber));
|
||||||
|
redisUtils.redisUp(2,productId.toString(),idStockMap);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue