| /** | 
|  * Copyright (c) 2019 人人开源 All rights reserved. | 
|  * | 
|  * https://www.renren.io | 
|  * | 
|  * 版权所有,侵权必究! | 
|  */ | 
| package com.zt.modules.message.service; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.stream.Collectors; | 
|   | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import com.zt.common.db.query.QueryFilter; | 
| import com.zt.common.service.BaseService; | 
| import com.zt.core.context.UserContext; | 
| import com.zt.core.sys.service.ISysUserService; | 
| import com.zt.modules.message.dao.SysNoticeDao; | 
| import com.zt.modules.message.enums.NoticeReadStatus; | 
| import com.zt.modules.message.enums.NoticeStatus; | 
| import com.zt.modules.message.enums.ReceiverType; | 
| import com.zt.modules.message.model.SysNotice; | 
| import com.zt.modules.message.model.SysNoticeUser; | 
| import com.zt.modules.message.websocket.WebSocketServer; | 
| import com.zt.modules.message.websocket.data.MessageData; | 
|   | 
| /** | 
|  * 通知管理 | 
|  * | 
|  * @author hehz | 
|  */ | 
| @Service | 
| public class SysNoticeService extends BaseService<SysNoticeDao, SysNotice> { | 
|     @Autowired | 
|     private SysNoticeUserService sysNoticeUserService; | 
|     @Autowired | 
|     private ISysUserService sysUserService; | 
|     @Autowired | 
|     private WebSocketServer webSocketServer; | 
|   | 
|     public List<SysNotice> page(QueryFilter queryFilter) { | 
|         return super.query(queryFilter); | 
|     } | 
|   | 
|     /** | 
|      * 获取我的通知列表 | 
|      */ | 
|     public List<SysNotice> getMyNoticePage(QueryFilter queryFilter) { | 
|         // 查询 | 
|         List<SysNotice> list = baseDao.getMyNoticeList(UserContext.getUserId()); | 
|   | 
|         return queryFilter.getPageList(list); | 
|     } | 
|   | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public void insert(SysNotice entity) { | 
|         // 更新发送者信息 | 
|         if (entity.getStatus() == NoticeStatus.SEND.value()) { | 
|             entity.setSenderId(UserContext.getUser().getId()); | 
|             entity.setSendDate(new Date()); | 
|         } | 
|         entity.setReceiverIds(StringUtils.join(entity.getReceiverList(), ",")); | 
|         baseDao.insert(entity); | 
|   | 
|         // 发送通知 | 
|         sendNotice(entity); | 
|     } | 
|   | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public void update(SysNotice entity) { | 
|         // 更新发送者信息 | 
|         if (entity.getStatus() == NoticeStatus.SEND.value()) { | 
|             entity.setSenderId(UserContext.getUser().getId()); | 
|             entity.setSendDate(new Date()); | 
|         } | 
|         entity.setReceiverIds(StringUtils.join(entity.getReceiverList(), ",")); | 
|         baseDao.updateById(entity); | 
|   | 
|         // 发送通知 | 
|         sendNotice(entity); | 
|     } | 
|   | 
|     /** | 
|      * 发送通知 | 
|      */ | 
|     public void sendNotice(SysNotice notice) { | 
|         // 如果是草稿,在不发送通知 | 
|         if (notice.getStatus() == NoticeStatus.DRAFT.value()) { | 
|             return; | 
|         } | 
|   | 
|         List<Long> userIdList = new ArrayList<>(); | 
|   | 
|         if (ReceiverType.DEPT.getValue().equals(notice.getReceiverType())) {// 部门 | 
|             userIdList.addAll(sysUserService.getByDeptIds(notice.getReceiverList()).stream().map(user -> user.getId()) | 
|                     .collect(Collectors.toList())); | 
|         } else { // 选中用户 | 
|             userIdList.addAll(notice.getReceiverList()); | 
|         } | 
|         if (userIdList.size() > 0) { | 
|             // 发送给用户 | 
|             userIdList.forEach(userId -> { | 
|                 SysNoticeUser noticeUser = new SysNoticeUser().setNoticeId(notice.getId()).setReceiverId(userId) | 
|                         .setStatus(NoticeReadStatus.UNREAD.value()); | 
|   | 
|                 sysNoticeUserService.insert(noticeUser); | 
|             }); | 
|   | 
|             // 通过WebSocket,提示选中用户,有新通知 | 
|             MessageData<String> message = new MessageData<String>().msg(notice.getTitle()); | 
|             webSocketServer.sendMessage(userIdList, message); | 
|         } | 
|     } | 
| } |