119 lines
4.5 KiB
Java
119 lines
4.5 KiB
Java
package com.yunhai.userhome.service.impl;
|
|
|
|
import com.yunhai.userhome.entity.UserMessage;
|
|
import com.yunhai.userhome.repository.UserMessageRepository;
|
|
import com.yunhai.userhome.service.IUserMessageService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
import javax.persistence.criteria.Predicate;
|
|
import javax.persistence.criteria.Root;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* 用户消息服务:行为对齐旧 UserHome UserMessageService(本地 user_message 表)。
|
|
* WS 推送态由前端轮询兜底,本服务不消费 Redis 缓存(旧工程依赖 Redis,新工程未引入)。
|
|
*/
|
|
@Service
|
|
public class UserMessageServiceImpl implements IUserMessageService {
|
|
|
|
@Autowired
|
|
private UserMessageRepository userMessageRepository;
|
|
|
|
@Override
|
|
public List<UserMessage> getUserMessageAppIds(Integer userId, Integer schId) {
|
|
return userMessageRepository.getUserMessageAppList(userId, schId);
|
|
}
|
|
|
|
@Override
|
|
public Page<UserMessage> findUserMessageByAppId(UserMessage userMessage, Pageable pageable) {
|
|
return userMessageRepository.findAll(select(userMessage), pageable);
|
|
}
|
|
|
|
@Override
|
|
public Page<UserMessage> findUserMessageByUserId(Integer userId, Pageable pageable) {
|
|
return userMessageRepository.findAll(select(userId, null), pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<UserMessage> findUserMessageByUserId(Integer userId) {
|
|
// 旧版优先读 Redis 缓存,缓存缺失再查库;新工程无 Redis,直接查库(行为等价:返回该用户全部消息)
|
|
return userMessageRepository.findAll(select(userId, null), PageRequest.of(0, Integer.MAX_VALUE, Sort.by(Sort.Direction.DESC, "cDate"))).getContent();
|
|
}
|
|
|
|
@Override
|
|
public Boolean findUserMessageUnRead(Integer userId) {
|
|
return userMessageRepository.count(select(userId, false)) > 0;
|
|
}
|
|
|
|
@Override
|
|
public void updateRead(Integer userId, Integer appId) {
|
|
userMessageRepository.updateRead(userId, appId);
|
|
}
|
|
|
|
@Override
|
|
public void updateUserRead(Integer id, Integer userId) {
|
|
// 单条已读:前端按 id 移除本地列表后再调;服务端标记该条已读
|
|
userMessageRepository.updateReadById(id);
|
|
}
|
|
|
|
@Override
|
|
public void updateReadAll(Integer userId) {
|
|
userMessageRepository.updateReadAll(userId);
|
|
}
|
|
|
|
@Override
|
|
public Page<UserMessage> findUserMessageById(Integer userId, Integer page, Integer count) {
|
|
Sort sort = Sort.by(Sort.Direction.DESC, "id");
|
|
Pageable pageable = PageRequest.of(page - 1, count, sort);
|
|
return userMessageRepository.findAll(select(userId, null), pageable);
|
|
}
|
|
|
|
@Override
|
|
public void readMsgById(Integer msgId) {
|
|
userMessageRepository.updateReadById(msgId);
|
|
}
|
|
|
|
@Override
|
|
public void readMsgAll(Integer userId) {
|
|
userMessageRepository.updateReadAll(userId);
|
|
}
|
|
|
|
private Specification<UserMessage> select(UserMessage userMessage) {
|
|
return (Root<UserMessage> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
if (userMessage != null) {
|
|
if (userMessage.getUserId() != null) {
|
|
predicates.add(cb.equal(root.get("userId"), userMessage.getUserId()));
|
|
}
|
|
if (userMessage.getAppId() != null) {
|
|
predicates.add(cb.equal(root.get("appId"), userMessage.getAppId()));
|
|
}
|
|
}
|
|
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
|
};
|
|
}
|
|
|
|
private Specification<UserMessage> select(Integer userId, Boolean isRead) {
|
|
return (Root<UserMessage> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
if (userId != null) {
|
|
predicates.add(cb.equal(root.get("userId"), userId));
|
|
}
|
|
if (isRead != null) {
|
|
predicates.add(cb.equal(root.get("isRead"), isRead));
|
|
}
|
|
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
|
};
|
|
}
|
|
}
|