FavoriteServiceImpl.java
5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.yohoufo.user.service.impl;
import com.yohoufo.dal.user.IUserFavoriteDao;
import com.yohoufo.dal.user.model.UserFavorite;
import com.yohoufo.user.cache.CacheService;
import com.yohoufo.user.requestVO.FavoriteRequestVO;
import com.yohoufo.user.service.IFavoriteService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Service("favoriteServiceImpl")
public class FavoriteServiceImpl implements IFavoriteService {
private Logger logger = LoggerFactory.getLogger(FavoriteServiceImpl.class);
@Autowired
private IUserFavoriteDao userFavoriteDao;
@Autowired
private CacheService cacheService;
public void addFavorite(FavoriteRequestVO vo){
logger.info("RealNameAuthorizeServiceImpl addFavorite vo {}",vo);
//判断redis是否存在该key,如果不存在先获取所有商品并放入缓存
boolean isHas = cacheService.hasUserFavoriteProductKey(vo.getUid(),true);
if(!isHas){
List<UserFavorite> favoriteList = userFavoriteDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid());
cacheService.batchSetFavoriteProduct(vo.getUid(), favoriteList);
}
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);
userFavoriteDao.insert(getTableIndex(vo.getUid()),userFavorite);
//放到缓存
cacheService.addFavoriteProductKey(vo.getUid(), vo.getProductId(), ts);
}
}
public void cancelFavorite(FavoriteRequestVO vo){
LocalDateTime now=LocalDateTime.now();
long ts=now.toEpochSecond(ZoneOffset.of("+8"));
//删除用户的缓存
cacheService.removeUserFavoriteProduct(vo.getUid(),vo.getProductId());
//删除数据库,逻辑删除
userFavoriteDao.updateFavoriteInvalid(getTableIndex(vo.getUid()),vo.getUid(), vo.getProductId(),ts);
}
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();
// 查询缓存
Set<String> favoriteProductIds = cacheService.queryAllProductFavoriteByUid(uid, startIndex, endIndex-1);
if(CollectionUtils.isNotEmpty(favoriteProductIds)){
favoriteProductIds.remove("0");
favoriteProducts.addAll(favoriteProductIds);
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){
favoriteProducts.add(String.valueOf(favorite.getProductId()));
}
}
return favoriteProducts;
}
/**
* 查询某用户是否已收藏某商品 (如果存在则将失效时间延期)
*/
public boolean isFavorite(FavoriteRequestVO vo){
try {
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);
return containsProductSku(favoriteList, vo.getProductId());
}
return false;
}
}
catch(Exception e) {
logger.warn("isFavorite failed userId is:{},product sku is:{}",vo.getUid(),vo.getProductId());
return false; //无关紧要的逻辑抛了异常也不能影响
}
}
// 是否已经收藏商品
private boolean containsProductSku(List<UserFavorite> favoriteList, Integer productId) {
boolean curEquals = false;
for (UserFavorite favorite : favoriteList) { // 数据库查询出来的list肯定不为null
curEquals = productId.equals(favorite.getProductId());
if (curEquals) {
return curEquals;
}
}
return curEquals;
}
public int numFavorite(FavoriteRequestVO vo){
int count=(int)cacheService.queryUserProductFavoriteCount(vo.getUid());
if(count > 0){
return count -1;
}
List<UserFavorite> favoriteList = userFavoriteDao.selectValidFavoriteByUid(getTableIndex(vo.getUid()), vo.getUid());
cacheService.batchSetFavoriteProduct(vo.getUid(), favoriteList);
count = favoriteList.size();
return count;
}
private String getTableIndex(Integer uid) {
return String.valueOf(uid % 10);
}
}