123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package com.flow.service.impl;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.flow.common.core.model.CursorResult;
- import com.flow.common.core.model.PageResult;
- import com.flow.common.mybatis.service.impl.BaseServiceImpl;
- import com.flow.common.oauth2.utils.SecurityContextUtil;
- import com.flow.dao.NotifyDao;
- import com.flow.entity.MessageEntity;
- import com.flow.entity.Notify;
- import com.flow.mapstruct.NotifyMapper;
- import com.flow.model.NotifyQuery;
- import com.flow.service.NotifyService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.messaging.simp.SimpMessagingTemplate;
- import org.springframework.stereotype.Service;
- import java.util.List;
- @Service
- public class NotifyServiceImpl extends BaseServiceImpl<NotifyDao, Notify> implements NotifyService {
- @Autowired
- private NotifyDao notifyDao;
- @Autowired
- private NotifyMapper notifyMapper;
- @Autowired
- private SimpMessagingTemplate messagingTemplate;
- @Override
- public Notify notify(Notify notify) {
- int insert = notifyDao.insert(notify);
- if (insert > 0) {
- MessageEntity<Notify> messageEntity = new MessageEntity<>(
- notify.getSender(),
- notify.getReceiver(),
- notify
- );
- messagingTemplate.convertAndSendToUser(notify.getReceiver(), "/topic/notify", messageEntity);
- }
- return notify;
- }
- @Override
- public PageResult<Notify> getList(NotifyQuery query) {
- query.setReceiver(SecurityContextUtil.getUserId());
- Notify notify = notifyMapper.toEntity(query);
- Page<Notify> page = notifyDao.lambdaQueryChain()
- .setEntity(notify)
- .orderByDesc(Notify::getId)
- .page(new Page<>(query.getPage(), query.getLimit()));
- return new PageResult<>(page.getTotal(), page.getRecords());
- }
- @Override
- public CursorResult<Notify> cursor(NotifyQuery query) {
- query.setReceiver(SecurityContextUtil.getUserId());
- List<Notify> list = notifyDao.cursor(query);
- CursorResult<Notify> cursorResult = new CursorResult<>();
- cursorResult.setRows(list);
- if (CollectionUtils.isNotEmpty(list)) {
- cursorResult.setCursor(list.get(list.size() - 1).getId());
- }
- return cursorResult;
- }
- @Override
- public void read(List<Long> ids) {
- notifyDao.lambdaUpdateChain()
- .in(Notify::getId, ids)
- .set(Notify::getIsRead, true)
- .update();
- }
- @Override
- public void readAll() {
- String userId = SecurityContextUtil.getUserId();
- notifyDao.lambdaUpdateChain()
- .eq(Notify::getReceiver, userId)
- .set(Notify::getIsRead, true)
- .update();
- }
- @Override
- public void delete(List<Long> ids) {
- notifyDao.lambdaUpdateChain()
- .in(Notify::getId, ids)
- .remove();
- }
- @Override
- public Long getUnreadCount() {
- String userId = SecurityContextUtil.getUserId();
- return notifyDao.lambdaQueryChain()
- .select(Notify::getId)
- .eq(Notify::getReceiver, userId)
- .eq(Notify::getIsRead, false)
- .count();
- }
- }
|