diff --git a/bnyer-common/bnyer-common-redis/src/main/java/com/bnyer/common/redis/service/RedisService.java b/bnyer-common/bnyer-common-redis/src/main/java/com/bnyer/common/redis/service/RedisService.java index 1cfe00a..69d5b3f 100644 --- a/bnyer-common/bnyer-common-redis/src/main/java/com/bnyer/common/redis/service/RedisService.java +++ b/bnyer-common/bnyer-common-redis/src/main/java/com/bnyer/common/redis/service/RedisService.java @@ -7,15 +7,12 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.BoundSetOperations; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; +import org.springframework.data.redis.core.*; import org.springframework.stereotype.Component; /** * spring redis 工具类 - * + * * @author ruoyi **/ @SuppressWarnings(value = { "unchecked", "rawtypes" }) @@ -245,7 +242,7 @@ public class RedisService /** * 获得缓存的基本对象列表 - * + * * @param pattern 字符串前缀 * @return 对象列表 */ @@ -273,4 +270,44 @@ public class RedisService public Long decr(final String key,final long delta){ return redisTemplate.opsForValue().decrement(key, delta); } + + /** + * 获取hashScan结果 + * @param key 键值 + * @return - + */ + public Cursor> getHashScan(String key){ + return redisTemplate.opsForHash().scan(key,ScanOptions.NONE); + } + + /** + * 删除hashkey + * @param key 键值 + * @param hashKey hash键值 + */ + public void deleteHashKey(final String key,final String hashKey) + { + redisTemplate.opsForHash().delete(key, hashKey); + } + + /** + * 判断是否存在hashkey + * @param key 键值 + * @param hashKey hash键值 + */ + public boolean hasHashKey(final,String key,final String hashKey) + { + return redisTemplate.opsForHash().hasKey(key,hashKey); + } + + /** + * hash数据递增 + * @param key 键 + * @param hashKey hash键 + * @param delta 步长 + * @return - + */ + public Long hashIncr(final String key,final String hashKey,final long delta){ + return redisTemplate.opsForHash().increment(key, hashKey,delta); + } } diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java index 6acb3a0..faff7d8 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java @@ -6,4 +6,14 @@ public class RedisKeyConstant { * 小程序用户图片首页键 */ public static final String TIKTOK_USER_IMG_KEY = "bnyer.tiktok.userImg"; + + /** + * 抖音小程序用户收藏图片键 + */ + public static final String TIKTOK_USER_COLLECT_KEY = "bnyer.tiktok.collect"; + + /** + * 抖音小程序图片收藏数量键 + */ + public static final String TIKTOK_IMG_COLLECT_NUM_KEY = "bnyer.tiktok.collectNum"; } diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java index c5def72..65a60c9 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java @@ -38,6 +38,9 @@ public class TiktokMiniController extends BaseController { @Autowired private TiktokUserService tiktokUserService; + @Autowired + private TiktokCollectionService tiktokCollectionService; + @ApiOperation(value="查询banner列表") @GetMapping(value = "/listBanner") public AjaxResult listBanner(){ @@ -107,4 +110,12 @@ public class TiktokMiniController extends BaseController { public AjaxResult tiktokUserImgs(){ return AjaxResult.success(tiktokUserService.queryUserImgList()); } + + @ApiOperation(value="添加收藏/取消收藏") + @PostMapping(value = "/collect") + public AjaxResult insert(@Validated @RequestBody @ApiParam("收藏对象") CollectionDto dto){ + log.debug("添加收藏/取消收藏参数为:{}", JSON.toJSONString(dto)); + tiktokCollectionService.collect(dto.getUserId(),dto.getImgId()); + return AjaxResult.success(); + } } diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/dto/CollectionDto.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/dto/CollectionDto.java new file mode 100644 index 0000000..0f53257 --- /dev/null +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/dto/CollectionDto.java @@ -0,0 +1,26 @@ +package com.bnyer.img.dto; + +import com.bnyer.common.core.utils.bean.BeanUtils; +import com.bnyer.img.domain.Banner; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.Setter; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + + +@Getter +@Setter +@ApiModel("收藏接收类") +public class CollectionDto implements Serializable { + + @NotNull(message = "用户id不能为空!") + @ApiModelProperty(value="用户id") + private Long userId; + + @NotNull(message = "图片id不能为空!") + @ApiModelProperty(value="图片id") + private Long imgId; +} diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokCollectionService.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokCollectionService.java index 8d8ee74..c57deeb 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokCollectionService.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokCollectionService.java @@ -7,18 +7,9 @@ import java.util.List; public interface TiktokCollectionService { /** - * 新增收藏 - * @param tiktokCollection 收藏对象 - * @return - - */ - int insert(TiktokCollection tiktokCollection); - - /** - * 修改收藏 - * @param tiktokCollection 收藏对象 - * @return - + * 同步redis收藏到db中 */ - int update(TiktokCollection tiktokCollection); + void insert(); /** * 批量删除收藏 @@ -34,4 +25,11 @@ public interface TiktokCollectionService { */ Integer getCollectionCount(Long imgId); + /** + * 收藏/取消收藏 + * @param userId 用户Id + * @param imgId 图片Id + */ + void collect(Long userId, Long imgId); + } diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java index 1073ea4..bcf5a5d 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java @@ -1,15 +1,20 @@ package com.bnyer.img.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.bnyer.common.redis.service.RedisService; +import com.bnyer.img.constants.RedisKeyConstant; import com.bnyer.img.domain.TiktokCollection; +import com.bnyer.img.enums.TiktokCollectionEnum; import com.bnyer.img.mapper.TiktokCollectionMapper; import com.bnyer.img.service.TiktokCollectionService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.Cursor; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; +import java.util.Map; @Service @Slf4j @@ -18,17 +23,38 @@ public class TiktokCollectionServiceImpl implements TiktokCollectionService { @Autowired private TiktokCollectionMapper tiktokCollectionMapper; - @Override - public int insert(TiktokCollection tiktokCollection) { - tiktokCollection.setCreateTime(new Date()); - tiktokCollection.setUpdateTime(new Date()); - return tiktokCollectionMapper.insert(tiktokCollection); + @Autowired + private RedisService redisService; + + private String getHashKey(Long userId, Long imgId){ + return userId+":"+imgId; } @Override - public int update(TiktokCollection tiktokCollection) { - tiktokCollection.setUpdateTime(new Date()); - return tiktokCollectionMapper.updateById(tiktokCollection); + public void insert() { + log.info("==============redis同步收藏记录到db数据开始!==============="); + //拿缓存 + String redisKey = RedisKeyConstant.TIKTOK_USER_COLLECT_KEY; + Cursor> collection = redisService.getHashScan(redisKey); + while (collection.hasNext()){ + Map.Entry next = collection.next(); + String key = (String) next.getKey(); + //分离出 userId,imgId + String[] split = key.split(":"); + String userId = split[0]; + Long userLongId = Long.parseLong(userId); + String imgId = split[1]; + Long imgLongId = Long.parseLong(imgId); + TiktokCollection tiktokCollection = new TiktokCollection(); + tiktokCollection.setUserId(userLongId); + tiktokCollection.setImgId(imgLongId); + tiktokCollection.setCreateTime(new Date()); + tiktokCollection.setUpdateTime(new Date()); + tiktokCollectionMapper.insert(tiktokCollection); + //保存后从 Redis 中删除 + redisService.deleteHashKey(redisKey,key); + } + log.info("==============redis同步收藏记录到db数据完成!==============="); } @Override @@ -44,4 +70,29 @@ public class TiktokCollectionServiceImpl implements TiktokCollectionService { Long num = tiktokCollectionMapper.selectCount(wrapper); return num.intValue(); } + + @Override + public void collect(Long userId, Long imgId) { + //查询缓存中是否存在该用户和图片组装成的结果 + String hashKey = getHashKey(userId,imgId); + String redisKey = RedisKeyConstant.TIKTOK_USER_COLLECT_KEY; + String imgCollectNumKey = RedisKeyConstant.TIKTOK_IMG_COLLECT_NUM_KEY; + if(!redisService.hasKey(redisKey)){ + //无则插入redis,状态置为1,图片数量+1 + redisService.setCacheMapValue(redisKey,hashKey, TiktokCollectionEnum.COLLECTE.getCode()); + redisService.hashIncr(imgCollectNumKey,String.valueOf(imgId), 1); + log.info("用户【{}】收藏了图片【{}】,收藏量增加1",userId,imgId); + }else{ + //有则删除,图片数量-1 + if(redisService.hasHashKey(redisKey,hashKey)){ + redisService.deleteHashKey(redisKey,hashKey); + redisService.hashIncr(imgCollectNumKey,String.valueOf(imgId), -1); + log.info("用户【{}】取消收藏了图片【{}】,收藏量减少1",userId,imgId); + }else{ + //不存在 + log.error("用户【{}】取消收藏图片【{}】失败,hashKey【{}】不存在!",userId,imgId,hashKey); + } + } + + } }