商品 批量操作
This commit is contained in:
@@ -5,7 +5,10 @@ import com.alibaba.fastjson2.JSONWriter;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.core.Cursor;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -52,6 +55,34 @@ public class RedisService {
|
||||
return redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除某前缀Key
|
||||
* @param prefix
|
||||
*/
|
||||
public void deleteKeysByPrefixSafe(String prefix) {
|
||||
// 拼接匹配规则:前缀 + * → 比如传入 user → 匹配 user*
|
||||
String pattern = prefix + "*";
|
||||
|
||||
// redisTemplate.execute:使用 Redis 底层连接执行原生命令(性能更高)
|
||||
redisTemplate.execute((RedisCallback<Void>) connection -> {
|
||||
// try-with-resources:自动关闭游标,避免资源泄漏
|
||||
try (Cursor<byte[]> cursor = connection.scan(
|
||||
// SCAN 配置:匹配规则、每次扫描1000条
|
||||
ScanOptions.scanOptions().match(pattern).count(1000).build()
|
||||
)) {
|
||||
// 迭代遍历扫描到的所有 key
|
||||
while (cursor.hasNext()) {
|
||||
// 删除当前遍历到的 key
|
||||
connection.del(cursor.next());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 异常捕获打印,避免删除失败导致程序报错
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定缓存失效时间
|
||||
*
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.czg.product.param;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品批量操作
|
||||
*
|
||||
* @author ww
|
||||
*/
|
||||
@Data
|
||||
public class ProductBatchParam implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 商品id集合
|
||||
*/
|
||||
@NotNull(message = "商品id集合不能为空")
|
||||
private List<Long> ids;
|
||||
/**
|
||||
* 商品操作
|
||||
* category 修改分类
|
||||
* isSale 上下架
|
||||
* is_sold_stock 售罄
|
||||
* isAutoSoldStock 自动售罄
|
||||
*/
|
||||
@NotBlank(message = "操作类型不能为空")
|
||||
private String type;
|
||||
/**
|
||||
* 操作值
|
||||
*/
|
||||
@NotBlank(message = "操作值不能为空")
|
||||
private String value;
|
||||
/**
|
||||
* 店铺id
|
||||
*/
|
||||
private Long shopId;
|
||||
}
|
||||
@@ -110,7 +110,10 @@ public interface ProductService extends IService<Product> {
|
||||
* @param param 商品报损入参
|
||||
*/
|
||||
void reportDamage(ProductReportDamageParam param);
|
||||
|
||||
/**
|
||||
* 批量操作商品
|
||||
*/
|
||||
void batchOperate(ProductBatchParam param);
|
||||
/**
|
||||
* 商品统计
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user