package jnpf.message.controller;
|
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
import com.alibaba.fastjson.JSONObject;
|
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameters;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import jakarta.validation.Valid;
|
import jnpf.base.ActionResult;
|
import jnpf.base.DictionaryDataApi;
|
import jnpf.base.UserInfo;
|
import jnpf.base.entity.DictionaryDataEntity;
|
import jnpf.base.entity.SuperBaseEntity;
|
import jnpf.base.entity.SystemEntity;
|
import jnpf.base.vo.PageListVO;
|
import jnpf.base.vo.PaginationVO;
|
import jnpf.constant.MsgCode;
|
import jnpf.exception.DataException;
|
import jnpf.message.NoticeApi;
|
import jnpf.message.entity.MessageEntity;
|
import jnpf.message.entity.MessageReceiveEntity;
|
import jnpf.message.model.*;
|
import jnpf.message.service.MessageService;
|
import jnpf.message.util.OnlineUserModel;
|
import jnpf.message.util.OnlineUserProvider;
|
import jnpf.message.util.SendFlowMsgUtil;
|
import jnpf.permission.UserApi;
|
import jnpf.permission.entity.UserEntity;
|
import jnpf.util.JsonUtil;
|
import jnpf.util.JsonUtilEx;
|
import jnpf.util.StringUtil;
|
import jnpf.util.UserProvider;
|
import jnpf.util.context.RequestContext;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.util.ObjectUtils;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 系统公告
|
*
|
* @author JNPF开发平台组
|
* @version V3.1.0
|
* @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
|
* @date 2019年9月27日 上午9:18
|
*/
|
@Tag(name = "系统公告", description = "Message")
|
@RestController
|
@RequestMapping
|
@Slf4j
|
public class NoticeController implements NoticeApi {
|
|
@Autowired
|
private MessageService messageService;
|
@Autowired
|
private SendFlowMsgUtil sendFlowMsgUtil;
|
@Autowired
|
private UserApi usersApi;
|
@Autowired
|
private DictionaryDataApi dictionaryDataApi;
|
|
/**
|
* 列表(通知公告)
|
*
|
* @param pagination 分页模型
|
* @return ignore
|
*/
|
@Operation(summary = "获取系统公告列表(带分页)")
|
@SaCheckPermission("msgCenter.notice")
|
@PostMapping("/Notice/List")
|
public ActionResult<PageListVO<MessageNoticeVO>> NoticeList(@RequestBody NoticePagination pagination) {
|
messageService.updateEnabledMark();
|
List<MessageEntity> list = messageService.getNoticeList(pagination);
|
List<UserEntity> userList = usersApi.getUserName(list.stream().map(MessageEntity::getCreatorUserId).collect(Collectors.toList()));
|
PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
|
List<DictionaryDataEntity> noticeType = dictionaryDataApi.getListByCode("NoticeType");
|
List<MessageNoticeVO> voList = new ArrayList<>();
|
// 判断是否过期
|
list.forEach(t -> {
|
MessageNoticeVO vo = JsonUtil.getJsonToBean(t, MessageNoticeVO.class);
|
// 处理是否过期
|
if (t.getExpirationTime() != null) {
|
// 已发布的情况下
|
if (t.getEnabledMark() == 1) {
|
if (t.getExpirationTime().getTime() < System.currentTimeMillis()) {
|
vo.setEnabledMark(2);
|
}
|
}
|
}
|
DictionaryDataEntity dictionaryDataEntity = noticeType.stream().filter(notice -> notice.getEnCode().equals(t.getCategory())).findFirst().orElse(new DictionaryDataEntity());
|
vo.setCategory(dictionaryDataEntity.getFullName());
|
// 转换创建人、发布人
|
UserEntity user = userList.stream().filter(ul -> ul.getId().equals(t.getCreatorUserId())).findFirst().orElse(null);
|
vo.setCreatorUser(user != null ? user.getRealName() + "/" + user.getAccount() : "");
|
if (t.getEnabledMark() != null && t.getEnabledMark() != 0) {
|
UserEntity entity = usersApi.getInfoById(t.getLastModifyUserId());
|
vo.setLastModifyUserId(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
|
vo.setReleaseTime(t.getLastModifyTime() != null ? t.getLastModifyTime().getTime() : null);
|
vo.setReleaseUser(vo.getLastModifyUserId());
|
}
|
voList.add(vo);
|
});
|
return ActionResult.page(voList, paginationVO);
|
}
|
|
/**
|
* 添加系统公告
|
*
|
* @param noticeCrForm 新建系统公告模型
|
* @return
|
*/
|
@Operation(summary = "添加系统公告")
|
@Parameters({
|
@Parameter(name = "noticeCrForm", description = "新建系统公告模型", required = true)
|
})
|
@SaCheckPermission("msgCenter.notice")
|
@PostMapping("/Notice")
|
public ActionResult create(@RequestBody @Valid NoticeCrForm noticeCrForm) {
|
MessageEntity entity = JsonUtil.getJsonToBean(noticeCrForm, MessageEntity.class);
|
if (entity != null && StringUtil.isNotEmpty(entity.getBodyText()) && (entity.getBodyText().contains("<") || entity.getBodyText().contains("&lt;"))) {
|
return ActionResult.fail(MsgCode.MSERR112.get());
|
}
|
messageService.create(entity);
|
return ActionResult.success(MsgCode.SU001.get());
|
}
|
|
/**
|
* 修改系统公告
|
*
|
* @param id 主键
|
* @param messageUpForm 修改系统公告模型
|
* @return
|
*/
|
@Operation(summary = "修改系统公告")
|
@Parameters({
|
@Parameter(name = "id", description = "主键", required = true),
|
@Parameter(name = "messageUpForm", description = "修改系统公告模型", required = true)
|
})
|
@SaCheckPermission("msgCenter.notice")
|
@PutMapping("/Notice/{id}")
|
public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid NoticeUpForm messageUpForm) {
|
MessageEntity entity = JsonUtil.getJsonToBean(messageUpForm, MessageEntity.class);
|
if (entity != null && StringUtil.isNotEmpty(entity.getBodyText()) && (entity.getBodyText().contains("<") || entity.getBodyText().contains("&lt;"))) {
|
return ActionResult.fail(MsgCode.MSERR112.get());
|
}
|
boolean flag = messageService.update(id, entity);
|
if (flag == false) {
|
return ActionResult.fail(MsgCode.FA002.get());
|
}
|
return ActionResult.success(MsgCode.SU004.get());
|
}
|
|
/**
|
* 信息
|
*
|
* @param id 主键值
|
* @return
|
*/
|
@Operation(summary = "获取/查看系统公告信息")
|
@Parameters({
|
@Parameter(name = "id", description = "主键", required = true)
|
})
|
@SaCheckPermission("msgCenter.notice")
|
@GetMapping("/Notice/{id}")
|
public ActionResult<NoticeInfoVO> Info(@PathVariable("id") String id) throws DataException {
|
MessageEntity entity = messageService.getInfo(id);
|
NoticeInfoVO vo = null;
|
if (entity != null) {
|
UserEntity info = usersApi.getInfoById(entity.getCreatorUserId());
|
entity.setCreatorUserId(info != null ? info.getRealName() + "/" + info.getAccount() : "");
|
vo = JsonUtilEx.getJsonToBeanEx(entity, NoticeInfoVO.class);
|
vo.setReleaseUser(entity.getCreatorUserId());
|
vo.setReleaseTime(entity.getLastModifyTime() != null ? entity.getLastModifyTime().getTime() : null);
|
UserEntity userEntity = usersApi.getInfoById(entity.getLastModifyUserId());
|
if (userEntity != null && StringUtil.isNotEmpty(userEntity.getId())) {
|
String realName = userEntity.getRealName();
|
String account = userEntity.getAccount();
|
if (StringUtil.isNotEmpty(realName)) {
|
vo.setReleaseUser(realName + "/" + account);
|
}
|
}
|
}
|
return ActionResult.success(vo);
|
}
|
|
/**
|
* 删除
|
*
|
* @param id 主键值
|
* @return
|
*/
|
@Operation(summary = "删除系统公告")
|
@Parameters({
|
@Parameter(name = "id", description = "主键", required = true)
|
})
|
@SaCheckPermission("msgCenter.notice")
|
@DeleteMapping("/Notice/{id}")
|
public ActionResult delete(@PathVariable("id") String id) {
|
MessageEntity entity = messageService.getInfo(id);
|
if (entity != null) {
|
messageService.delete(entity);
|
return ActionResult.success(MsgCode.SU003.get());
|
}
|
return ActionResult.fail(MsgCode.FA003.get());
|
}
|
|
/**
|
* 发布公告
|
*
|
* @param id 主键值
|
* @return
|
*/
|
@Operation(summary = "发布系统公告")
|
@Parameters({
|
@Parameter(name = "id", description = "主键", required = true)
|
})
|
@SaCheckPermission("msgCenter.notice")
|
@PutMapping("/Notice/{id}/Actions/Release")
|
public ActionResult Release(@PathVariable("id") String id) {
|
MessageEntity entity = messageService.getInfo(id);
|
if (entity != null) {
|
List<String> userIds = null;
|
if (StringUtil.isNotEmpty(entity.getToUserIds())) {
|
userIds = Arrays.asList(entity.getToUserIds().split(","));
|
} else {
|
userIds = usersApi.getListId();
|
}
|
List<String> userIdList = usersApi.getUserIdList(userIds);
|
if (sendFlowMsgUtil.sentNotice(userIdList, entity)) {
|
return ActionResult.success(MsgCode.SU011.get());
|
}
|
}
|
return ActionResult.fail(MsgCode.FA011.get());
|
}
|
|
//=======================================站内消息、消息中心=================================================
|
|
|
/**
|
* 获取消息中心列表
|
*
|
* @param pagination 分页模型
|
* @return
|
*/
|
@Operation(summary = "列表(通知公告/系统消息/私信消息)")
|
@GetMapping
|
public ActionResult<PageListVO<MessageInfoVO>> messageList(PaginationMessage pagination) {
|
List<MessageInfoVO> listVO = new ArrayList<>();
|
List<MessageReceiveEntity> list = messageService.getMessageList3(pagination);
|
List<UserEntity> userList = usersApi.getUserName(list.stream().map(SuperBaseEntity.SuperCBaseEntity::getCreatorUserId).collect(Collectors.toList()));
|
list.forEach(t -> {
|
MessageInfoVO vo = JsonUtil.getJsonToBean(t, MessageInfoVO.class);
|
UserEntity user = userList.stream().filter(ul -> ul.getId().equals(t.getCreatorUserId())).findFirst().orElse(null);
|
if (user != null) {
|
vo.setReleaseTime(t.getCreatorTime() != null ? t.getCreatorTime().getTime() : null);
|
UserEntity entity = usersApi.getInfoById(t.getCreatorUserId());
|
vo.setReleaseUser(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
|
vo.setCreatorUser(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
|
}
|
listVO.add(vo);
|
});
|
PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
|
return ActionResult.page(listVO, paginationVO);
|
}
|
|
|
/**
|
* 读取消息
|
*
|
* @param id 主键值
|
* @return
|
*/
|
@Operation(summary = "读取消息")
|
@Parameters({
|
@Parameter(name = "id", description = "主键值", required = true)
|
})
|
@GetMapping("/ReadInfo/{id}")
|
public ActionResult<NoticeInfoVO> readInfo(@PathVariable("id") String id) throws DataException {
|
MessageReceiveEntity receive = messageService.messageRead(id);
|
if (receive != null) {
|
UserEntity user = usersApi.getInfoById(receive.getCreatorUserId());
|
receive.setCreatorUserId(user != null ? user.getRealName() + "/" + user.getAccount() : "");
|
receive.setBodyText(receive.getBodyText());
|
// if (entity.getType() == 2) {
|
// entity.setBodyText(receive.getBodyText());
|
// }
|
}
|
NoticeInfoVO vo = JsonUtil.getJsonToBean(receive, NoticeInfoVO.class);
|
if (Objects.equals(receive.getType(), 1)) {
|
try {
|
MessageEntity jsonToBean = JsonUtil.getJsonToBean(receive.getBodyText(), MessageEntity.class);
|
if (jsonToBean != null) {
|
vo.setCategory(jsonToBean.getCategory());
|
vo.setCoverImage(jsonToBean.getCoverImage());
|
vo.setExcerpt(jsonToBean.getExcerpt());
|
vo.setExpirationTime(jsonToBean.getExpirationTime() != null ? jsonToBean.getExpirationTime().getTime() : null);
|
vo.setFiles(jsonToBean.getFiles());
|
vo.setBodyText(jsonToBean.getBodyText());
|
if (jsonToBean.getId() != null) {
|
MessageEntity info = messageService.getInfo(jsonToBean.getId());
|
if (info != null) {
|
vo.setCategory(info.getCategory());
|
vo.setCoverImage(info.getCoverImage());
|
vo.setExcerpt(info.getExcerpt());
|
vo.setExpirationTime(info.getExpirationTime() != null ? info.getExpirationTime().getTime() : null);
|
vo.setFiles(info.getFiles());
|
vo.setBodyText(info.getBodyText());
|
}
|
}
|
}
|
} catch (Exception e) {
|
vo.setBodyText(receive.getBodyText());
|
}
|
}
|
vo.setReleaseTime(receive.getCreatorTime() != null ? receive.getCreatorTime().getTime() : null);
|
// UserEntity info = usersApi.getInfoById(receive.getCreatorUserId());
|
vo.setReleaseUser(receive.getCreatorUserId());
|
return ActionResult.success(vo);
|
}
|
|
|
/**
|
* 全部已读
|
*
|
* @param pagination 分页模型
|
* @return
|
*/
|
@Operation(summary = "全部已读")
|
@Parameters({
|
@Parameter(name = "pagination", description = "分页模型", required = true)
|
})
|
@PostMapping("/Actions/ReadAll")
|
public ActionResult allRead(@RequestBody PaginationMessage pagination) {
|
List<MessageReceiveEntity> list = messageService.getMessageList3(pagination);
|
if (list != null && list.size() > 0) {
|
List<String> idList = list.stream().map(SuperBaseEntity.SuperIBaseEntity::getId).collect(Collectors.toList());
|
messageService.messageRead(idList);
|
return ActionResult.success(MsgCode.SU005.get());
|
} else {
|
return ActionResult.fail(MsgCode.MSERR113.get());
|
}
|
}
|
|
/**
|
* app端获取未读数据
|
*
|
* @return
|
*/
|
@Operation(summary = "app端获取未读数据")
|
@GetMapping("/getUnReadMsgNum")
|
public ActionResult<Map<String, String>> getUnReadMsgNum() {
|
Map<String, String> map = new HashMap<>();
|
UserInfo userInfo = UserProvider.getUser();
|
Integer unReadMsg = messageService.getUnreadCount(userInfo.getUserId(), 2);
|
Integer unReadSchedule = messageService.getUnreadCount(userInfo.getUserId(), 4);
|
Integer unReadNotice = messageService.getUnreadCount(userInfo.getUserId(), 1);
|
Integer unReadSystemMsg = messageService.getUnreadCount(userInfo.getUserId(), 3);
|
Integer unReadNum = unReadMsg + unReadNotice + unReadSchedule + unReadSystemMsg;
|
map.put("unReadMsg", unReadMsg.toString());
|
map.put("unReadNotice", unReadNotice.toString());
|
map.put("unReadSchedule", unReadSchedule.toString());
|
map.put("unReadSystemMsg", unReadSystemMsg.toString());
|
map.put("unReadNum", unReadNum.toString());
|
return ActionResult.success(map);
|
}
|
|
/**
|
* 删除记录
|
*
|
* @param recordForm 已读模型
|
* @return
|
*/
|
@Operation(summary = "删除消息")
|
@Parameters({
|
@Parameter(name = "recordForm", description = "已读模型", required = true)
|
})
|
@DeleteMapping("/Record")
|
public ActionResult deleteRecord(@RequestBody MessageRecordForm recordForm) {
|
String[] id = recordForm.getIds().split(",");
|
List<String> list = Arrays.asList(id);
|
messageService.deleteRecord(list);
|
return ActionResult.success(MsgCode.SU003.get());
|
}
|
|
/**
|
* 列表(通知公告)
|
*
|
* @return
|
*/
|
@Override
|
@PostMapping("/GetNoticeList")
|
public List<NoticeVO> getNoticeList(@RequestBody List<String> list) {
|
List<MessageEntity> dashboardNoticeList = messageService.getDashboardNoticeList(list);
|
List<UserEntity> userList = usersApi.getUserName(dashboardNoticeList.stream().map(MessageEntity::getCreatorUserId).collect(Collectors.toList()));
|
List<DictionaryDataEntity> noticeType = dictionaryDataApi.getListByCode("NoticeType");
|
dashboardNoticeList.forEach(t -> {
|
// 转换创建人、发布人
|
UserEntity user = userList.stream().filter(ul -> ul.getId().equals(t.getCreatorUserId())).findFirst().orElse(null);
|
t.setCreatorUserId(user != null ? user.getRealName() + "/" + user.getAccount() : "");
|
if (t.getEnabledMark() != null && t.getEnabledMark() != 0) {
|
UserEntity entity = usersApi.getInfoById(t.getLastModifyUserId());
|
t.setLastModifyUserId(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
|
}
|
DictionaryDataEntity dictionaryDataEntity = noticeType.stream().filter(notice -> notice.getEnCode().equals(t.getCategory())).findFirst().orElse(new DictionaryDataEntity());
|
t.setCategory(dictionaryDataEntity.getFullName());
|
});
|
List<NoticeVO> jsonToList = new ArrayList<>();
|
dashboardNoticeList.forEach(t -> {
|
NoticeVO vo = JsonUtil.getJsonToBean(t, NoticeVO.class);
|
vo.setReleaseTime(t.getLastModifyTime() != null ? t.getLastModifyTime().getTime() : null);
|
vo.setReleaseUser(t.getLastModifyUserId());
|
vo.setCreatorUser(t.getCreatorUserId());
|
jsonToList.add(vo);
|
});
|
return jsonToList;
|
}
|
|
/**
|
* 发送消息
|
*
|
* @param sentMessageModel
|
* @return
|
*/
|
@Override
|
@PostMapping("/SentMessage")
|
public void sentMessage(@RequestBody SentMessageModel sentMessageModel) {
|
messageService.sentMessage(sentMessageModel.getToUserIds(), sentMessageModel.getTitle(), sentMessageModel.getBodyText());
|
}
|
|
@Override
|
@PostMapping("/autoSystem")
|
public void autoSystem(@RequestBody Map<String, Object> map) {
|
String message = map.get("message").toString();
|
SystemEntity entity = JsonUtil.getJsonToBean(map.get("entity"), SystemEntity.class);
|
// 如果禁用了系统,则需要将系统
|
List<OnlineUserModel> onlineUserList = OnlineUserProvider.getOnlineUserList();
|
for (OnlineUserModel item : onlineUserList) {
|
String systemId = item.getSystemId();
|
if (entity.getId().equals(systemId)) {
|
if (item.getWebSocket().isOpen()) {
|
Map<String, String> maps = new HashMap<>(1);
|
maps.put("method", "logout");
|
maps.put("msg", "应用已被禁用或删除");
|
if (StringUtil.isNotEmpty(UserProvider.getUser().getTenantId())) {
|
if (UserProvider.getUser().getTenantId().equals(item.getTenantId())) {
|
item.getWebSocket().getAsyncRemote().sendText(JsonUtil.getObjectToString(maps));
|
}
|
} else {
|
item.getWebSocket().getAsyncRemote().sendText(JsonUtil.getObjectToString(maps));
|
}
|
}
|
}
|
}
|
|
}
|
|
|
@Override
|
@PostMapping("/sendMessage")
|
public void sendMessage(@RequestBody Map<String, Object> objectMap) {
|
List<String> userIds = (List<String>) objectMap.get("ids");
|
String message = (String) objectMap.get("message");
|
//用户不强制下线,websokit推送刷新即可
|
List<OnlineUserModel> users = new ArrayList<>();
|
List<OnlineUserModel> onlineUserList = OnlineUserProvider.getOnlineUserList();
|
for (OnlineUserModel onlineUserModel : onlineUserList) {
|
String userId = onlineUserModel.getUserId();
|
if (userIds.contains(userId)) {
|
users.add(onlineUserModel);
|
}
|
}
|
if (!ObjectUtils.isEmpty(users)) {
|
for (OnlineUserModel user : users) {
|
JSONObject obj = new JSONObject();
|
obj.put("method", "refresh");
|
obj.put("msg", StringUtil.isEmpty(message) ? MsgCode.PS010.get() : message);
|
if (user != null) {
|
OnlineUserProvider.sendMessage(user, obj);
|
}
|
}
|
}
|
}
|
|
@Override
|
@GetMapping("/majorStandFreshUser")
|
public void majorStandFreshUser() {
|
UserInfo userInfo = UserProvider.getUser();
|
List<OnlineUserModel> onlineUserList = OnlineUserProvider.getOnlineUserList();
|
for (OnlineUserModel onlineUserModel : onlineUserList) {
|
String userId = onlineUserModel.getUserId();
|
//当前用户,同平台的其他客户端需要强制刷新
|
if (userInfo.getUserId().equals(userId)
|
&& Objects.equals(!RequestContext.isOrignPc(), onlineUserModel.getIsMobileDevice())) {
|
JSONObject obj = new JSONObject();
|
obj.put("method", "refresh");
|
obj.put("msg", MsgCode.PS010.get());
|
OnlineUserProvider.sendMessage(onlineUserModel, obj);
|
}
|
}
|
}
|
|
|
@Override
|
@PostMapping("/logoutUser")
|
public void logoutUser(@RequestBody Map<String, Object> objectMap) {
|
List<String> userIds = (List<String>) objectMap.get("ids");
|
String message = (String) objectMap.get("message");
|
//用户不强制下线
|
List<OnlineUserModel> users = new ArrayList<>();
|
List<OnlineUserModel> onlineUserList = OnlineUserProvider.getOnlineUserList();
|
for (OnlineUserModel onlineUserModel : onlineUserList) {
|
String userId = onlineUserModel.getUserId();
|
if (userIds.contains(userId) && !Objects.equals(UserProvider.getUser().getToken(), onlineUserModel.getToken())) {
|
users.add(onlineUserModel);
|
}
|
}
|
if (!ObjectUtils.isEmpty(users)) {
|
for (OnlineUserModel user : users) {
|
JSONObject obj = new JSONObject();
|
obj.put("method", "logout");
|
obj.put("msg", StringUtil.isEmpty(message) ? MsgCode.PS011.get() : message);
|
if (user != null) {
|
OnlineUserProvider.sendMessage(user, obj);
|
}
|
//先移除对象, 并推送下线信息, 避免网络原因导致就用户未断开 新用户连不上WebSocket
|
OnlineUserProvider.removeModel(user);
|
UserProvider.logoutByToken(user.getToken());
|
}
|
}
|
}
|
|
@Override
|
@GetMapping("/getUserMessageList")
|
public List<MessageInfoVO> getUserMessageList() {
|
return messageService.getUserMessageList();
|
}
|
|
}
|