|
|
package com.yohoufo.user.service.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yohobuy.ufo.model.user.req.FavoriteSkuRequestVO;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.dal.user.IUserFavoriteSkuDao;
|
|
|
import com.yohoufo.dal.user.model.UserFavorite;
|
|
|
import com.yohoufo.dal.user.model.UserFavoriteSku;
|
...
|
...
|
@@ -40,37 +42,32 @@ public class FavoriteSkuService { |
|
|
}
|
|
|
|
|
|
if(!cacheService.isExistUserProductFavoriteByProductId(vo.getUid(), vo.getProductId(), false)){
|
|
|
//收藏
|
|
|
LocalDateTime now=LocalDateTime.now();
|
|
|
long ts=now.toEpochSecond(ZoneOffset.of("+8"));
|
|
|
UserFavorite userFavorite=new UserFavorite();
|
|
|
userFavorite.setUid(vo.getUid());
|
|
|
userFavorite.setProductId(vo.getProductId());
|
|
|
userFavorite.setValidStatus(1);
|
|
|
userFavorite.setCreateTime(ts);
|
|
|
userFavorite.setUpdateTime(ts);
|
|
|
logger.info("RealNameAuthorizeServiceImpl addFavorite userFavorite {}",userFavorite);
|
|
|
userFavoriteSkuDao.insert(getTableIndex(vo.getUid()), userFavorite);
|
|
|
//放到缓存
|
|
|
long ts = DateUtil.getEpochSecond();
|
|
|
UserFavoriteSku userFavoriteSku = UserFavoriteSku.builder()
|
|
|
.uid(vo.getUid()).productId(vo.getProductId())
|
|
|
.storageId(vo.getStorageId()).validStatus(1)
|
|
|
.createTime(ts).updateTime(ts)
|
|
|
.build();
|
|
|
logger.info("FavoriteSkuService addFavorite userFavoriteSku {}", userFavoriteSku);
|
|
|
|
|
|
userFavoriteSkuDao.insertBatch(getTableIndex(vo.getUid()), Lists.newArrayList(userFavoriteSku));
|
|
|
cacheService.addFavoriteProductKey(vo.getUid(), vo.getProductId(), ts);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void cancelFavorite(FavoriteRequestVO vo){
|
|
|
LocalDateTime now=LocalDateTime.now();
|
|
|
long ts=now.toEpochSecond(ZoneOffset.of("+8"));
|
|
|
//删除用户的缓存
|
|
|
|
|
|
|
|
|
public void cancelFavorite(FavoriteSkuRequestVO vo){
|
|
|
cacheService.removeUserFavoriteProduct(vo.getUid(),vo.getProductId());
|
|
|
//删除数据库,逻辑删除
|
|
|
userFavoriteDao.updateFavoriteInvalid(getTableIndex(vo.getUid()),vo.getUid(), vo.getProductId(),ts);
|
|
|
userFavoriteSkuDao.updateFavoriteInvalid(getTableIndex(vo.getUid()), vo.getUid(), vo.getStorageId(), DateUtil.getEpochSecond()); //删除数据库,逻辑删除
|
|
|
}
|
|
|
|
|
|
public List<String> listFavorite(FavoriteRequestVO vo){
|
|
|
List<String> favoriteProducts = new ArrayList<>();
|
|
|
int uid = vo.getUid();
|
|
|
int startIndex=vo.getLimit()*(vo.getPage()-1);
|
|
|
int endIndex=vo.getLimit()*vo.getPage();
|
|
|
int startIndex = vo.getLimit() * (vo.getPage()-1);
|
|
|
int endIndex = vo.getLimit() * vo.getPage();
|
|
|
// 查询缓存
|
|
|
Set<String> favoriteProductIds = cacheService.queryAllProductFavoriteByUid(uid, startIndex, endIndex-1);
|
|
|
if(CollectionUtils.isNotEmpty(favoriteProductIds)){
|
...
|
...
|
@@ -79,11 +76,11 @@ public class FavoriteSkuService { |
|
|
return favoriteProducts;
|
|
|
}
|
|
|
|
|
|
List<UserFavorite> favoriteList = userFavoriteDao.selectValidFavoriteByUid(getTableIndex(uid), uid);
|
|
|
cacheService.batchSetFavoriteProduct(uid, favoriteList);
|
|
|
int count=0;
|
|
|
for (UserFavorite favorite : favoriteList) {
|
|
|
if(count>=startIndex && count<endIndex){
|
|
|
List<UserFavoriteSku> favoriteList = userFavoriteSkuDao.selectValidFavoriteByUid(getTableIndex(uid), uid);
|
|
|
cacheService.batchSetFavoriteSku(uid, favoriteList);
|
|
|
int count = 0;
|
|
|
for (UserFavoriteSku favorite : favoriteList) {
|
|
|
if(count >= startIndex && count < endIndex){
|
|
|
favoriteProducts.add(String.valueOf(favorite.getProductId()));
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -99,13 +96,13 @@ public class FavoriteSkuService { |
|
|
return false;
|
|
|
}
|
|
|
try {
|
|
|
boolean existFlag = cacheService.isExistUserProductFavoriteByProductId(vo.getUid(), vo.getProductId(),false);
|
|
|
boolean existFlag = cacheService.isExistUserProductFavoriteByProductId(vo.getUid(), vo.getProductId(), false);
|
|
|
if (existFlag) {
|
|
|
return true;
|
|
|
} else {
|
|
|
if (!cacheService.hasUserFavoriteProductKey(vo.getUid(),false)) {
|
|
|
List<UserFavorite> favoriteList = userFavoriteDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid());
|
|
|
cacheService.batchSetFavoriteProduct(vo.getUid(), favoriteList);
|
|
|
List<UserFavoriteSku> favoriteList = userFavoriteSkuDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid());
|
|
|
cacheService.batchSetFavoriteSku(vo.getUid(), favoriteList);
|
|
|
return containsProductSku(favoriteList, vo.getProductId());
|
|
|
}
|
|
|
return false;
|
...
|
...
|
@@ -117,6 +114,10 @@ public class FavoriteSkuService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean containsProductSku(List<UserFavoriteSku> favoriteList, Integer productId) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
private String getTableIndex(Integer uid) {
|
|
|
return String.valueOf(uid % 10);
|
...
|
...
|
|