19 changed files with 424 additions and 56 deletions
@ -0,0 +1,20 @@ |
|||
package cc.hiver.mall.service; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public interface HomeReminderService { |
|||
|
|||
void incrementIeUnread(Long userId); |
|||
|
|||
Integer getIeUnreadCount(Long userId); |
|||
|
|||
void setIeUnreadCount(Long userId, int count); |
|||
|
|||
void clearIeUnread(Long userId); |
|||
|
|||
void addPlanetReward(String userId, String regionId, String type, String title, String recordId); |
|||
|
|||
void clearPlanetReminders(String userId, String regionId); |
|||
|
|||
Map<String, Object> homeReminders(String userId, String regionId); |
|||
} |
|||
@ -0,0 +1,138 @@ |
|||
package cc.hiver.mall.serviceimpl; |
|||
|
|||
import cc.hiver.mall.service.HomeReminderService; |
|||
import cn.hutool.core.util.StrUtil; |
|||
import cn.hutool.json.JSONUtil; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.redis.core.StringRedisTemplate; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Service |
|||
public class HomeReminderServiceImpl implements HomeReminderService { |
|||
|
|||
private static final String KEY_PREFIX = "home:reminder:"; |
|||
private static final String FIELD_IE_UNREAD = "ieUnread"; |
|||
private static final String FIELD_PLANET_DRAW = "planetDraw"; |
|||
private static final String FIELD_PLANET_RANK_TOP = "planetRankTop"; |
|||
private static final long TTL_DAYS = 14L; |
|||
|
|||
@Autowired |
|||
private StringRedisTemplate stringRedisTemplate; |
|||
|
|||
@Override |
|||
public void incrementIeUnread(Long userId) { |
|||
if (userId == null) { |
|||
return; |
|||
} |
|||
String key = userKey(String.valueOf(userId)); |
|||
stringRedisTemplate.opsForHash().increment(key, FIELD_IE_UNREAD, 1); |
|||
stringRedisTemplate.expire(key, TTL_DAYS, TimeUnit.DAYS); |
|||
} |
|||
|
|||
@Override |
|||
public Integer getIeUnreadCount(Long userId) { |
|||
if (userId == null) { |
|||
return 0; |
|||
} |
|||
Object value = stringRedisTemplate.opsForHash().get(userKey(String.valueOf(userId)), FIELD_IE_UNREAD); |
|||
return parseInt(value); |
|||
} |
|||
|
|||
@Override |
|||
public void setIeUnreadCount(Long userId, int count) { |
|||
if (userId == null) { |
|||
return; |
|||
} |
|||
String key = userKey(String.valueOf(userId)); |
|||
if (count <= 0) { |
|||
stringRedisTemplate.opsForHash().delete(key, FIELD_IE_UNREAD); |
|||
return; |
|||
} |
|||
stringRedisTemplate.opsForHash().put(key, FIELD_IE_UNREAD, String.valueOf(count)); |
|||
stringRedisTemplate.expire(key, TTL_DAYS, TimeUnit.DAYS); |
|||
} |
|||
|
|||
@Override |
|||
public void clearIeUnread(Long userId) { |
|||
if (userId == null) { |
|||
return; |
|||
} |
|||
stringRedisTemplate.opsForHash().delete(userKey(String.valueOf(userId)), FIELD_IE_UNREAD); |
|||
} |
|||
|
|||
@Override |
|||
public void addPlanetReward(String userId, String regionId, String type, String title, String recordId) { |
|||
if (StrUtil.isBlank(userId)) { |
|||
return; |
|||
} |
|||
String field = "rankTop".equals(type) ? FIELD_PLANET_RANK_TOP : FIELD_PLANET_DRAW; |
|||
Map<String, Object> payload = new HashMap<>(); |
|||
payload.put("type", type); |
|||
payload.put("regionId", regionId); |
|||
payload.put("title", title); |
|||
payload.put("recordId", recordId); |
|||
payload.put("time", System.currentTimeMillis()); |
|||
String key = userKey(userId); |
|||
stringRedisTemplate.opsForHash().put(key, field, JSONUtil.toJsonStr(payload)); |
|||
stringRedisTemplate.expire(key, TTL_DAYS, TimeUnit.DAYS); |
|||
} |
|||
|
|||
@Override |
|||
public void clearPlanetReminders(String userId, String regionId) { |
|||
if (StrUtil.isBlank(userId)) { |
|||
return; |
|||
} |
|||
stringRedisTemplate.opsForHash().delete(userKey(userId), FIELD_PLANET_DRAW, FIELD_PLANET_RANK_TOP); |
|||
} |
|||
|
|||
@Override |
|||
public Map<String, Object> homeReminders(String userId, String regionId) { |
|||
Map<String, Object> result = new HashMap<>(); |
|||
if (StrUtil.isBlank(userId)) { |
|||
result.put("ieUnreadCount", 0); |
|||
result.put("ie", false); |
|||
result.put("planet", false); |
|||
return result; |
|||
} |
|||
String key = userKey(userId); |
|||
Integer ieUnread = parseInt(stringRedisTemplate.opsForHash().get(key, FIELD_IE_UNREAD)); |
|||
Object draw = stringRedisTemplate.opsForHash().get(key, FIELD_PLANET_DRAW); |
|||
Object rankTop = stringRedisTemplate.opsForHash().get(key, FIELD_PLANET_RANK_TOP); |
|||
result.put("ieUnreadCount", ieUnread); |
|||
result.put("ie", ieUnread > 0); |
|||
result.put("planet", draw != null || rankTop != null); |
|||
result.put("planetDraw", parseJson(draw)); |
|||
result.put("planetRankTop", parseJson(rankTop)); |
|||
return result; |
|||
} |
|||
|
|||
private String userKey(String userId) { |
|||
return KEY_PREFIX + userId; |
|||
} |
|||
|
|||
private Integer parseInt(Object value) { |
|||
if (value == null) { |
|||
return 0; |
|||
} |
|||
try { |
|||
return Math.max(0, Integer.parseInt(String.valueOf(value))); |
|||
} catch (Exception ignored) { |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
private Object parseJson(Object value) { |
|||
if (value == null || StrUtil.isBlank(String.valueOf(value))) { |
|||
return null; |
|||
} |
|||
try { |
|||
return JSONUtil.parseObj(String.valueOf(value)); |
|||
} catch (Exception ignored) { |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
ALTER TABLE ie_user_profile |
|||
ADD COLUMN companion_intent VARCHAR(80) NULL COMMENT '用户自由输入的找搭子内容'; |
|||
|
|||
ALTER TABLE ie_user_profile |
|||
ADD INDEX idx_ie_profile_companion_latest (profile_completed, is_deleted, update_time); |
|||
Loading…
Reference in new issue