MessageController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package jnpf.message.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import io.swagger.v3.oas.annotations.Parameter;
  4. import io.swagger.v3.oas.annotations.Parameters;
  5. import jnpf.base.controller.SuperController;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import jnpf.base.ActionResult;
  9. import jnpf.base.UserInfo;
  10. import jnpf.base.entity.DictionaryDataEntity;
  11. import jnpf.base.entity.SuperBaseEntity;
  12. import jnpf.base.service.DictionaryDataService;
  13. import jnpf.base.service.DictionaryTypeService;
  14. import jnpf.base.vo.PageListVO;
  15. import jnpf.base.vo.PaginationVO;
  16. import jnpf.constant.MsgCode;
  17. import jnpf.message.entity.MessageEntity;
  18. import jnpf.exception.DataException;
  19. import jnpf.message.entity.MessageReceiveEntity;
  20. import jnpf.message.model.NoticePagination;
  21. import jnpf.message.service.MessageService;
  22. import jnpf.message.model.message.*;
  23. import jnpf.message.service.UserDeviceService;
  24. import jnpf.message.util.unipush.UinPush;
  25. import jnpf.permission.entity.UserEntity;
  26. import jnpf.permission.service.UserService;
  27. import jnpf.util.JsonUtil;
  28. import jnpf.util.JsonUtilEx;
  29. import jnpf.util.StringUtil;
  30. import jnpf.util.UserProvider;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.web.bind.annotation.*;
  33. import jakarta.validation.Valid;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. /**
  37. * 系统公告
  38. *
  39. * @author JNPF开发平台组
  40. * @version V3.1.0
  41. * @copyright 引迈信息技术有限公司
  42. * @date 2019年9月27日 上午9:18
  43. */
  44. @Tag(name = "系统公告", description = "Message")
  45. @RestController
  46. @RequestMapping("/api/message")
  47. public class MessageController extends SuperController<MessageService, MessageEntity> {
  48. @Autowired
  49. private MessageService messageService;
  50. @Autowired
  51. private UserService userApi;
  52. @Autowired
  53. private UinPush uinPush;
  54. @Autowired
  55. private UserDeviceService userDeviceService;
  56. @Autowired
  57. private DictionaryDataService dictionaryDataApi;
  58. @Autowired
  59. private DictionaryTypeService dictionaryTypeService;
  60. /**
  61. * 列表(通知公告)
  62. *
  63. * @param pagination
  64. * @return
  65. */
  66. @Operation(summary = "获取系统公告列表(带分页)")
  67. @SaCheckPermission("msgCenter.notice")
  68. @PostMapping("/Notice/List")
  69. public ActionResult<PageListVO<MessageNoticeVO>> NoticeList(@RequestBody NoticePagination pagination) {
  70. messageService.updateEnabledMark();
  71. List<MessageEntity> list = messageService.getNoticeList(pagination);
  72. List<UserEntity> userList = userApi.getUserName(list.stream().map(MessageEntity::getCreatorUserId).collect(Collectors.toList()));
  73. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  74. List<DictionaryDataEntity> noticeType = dictionaryDataApi.getListByTypeDataCode("NoticeType");
  75. List<MessageNoticeVO> voList = new ArrayList<>();
  76. // 判断是否过期
  77. list.forEach(t -> {
  78. MessageNoticeVO vo = JsonUtil.getJsonToBean(t, MessageNoticeVO.class);
  79. // 处理是否过期
  80. if (t.getExpirationTime() != null) {
  81. // 已发布的情况下
  82. if (t.getEnabledMark() == 1) {
  83. if (t.getExpirationTime().getTime() < System.currentTimeMillis()) {
  84. vo.setEnabledMark(2);
  85. }
  86. }
  87. }
  88. DictionaryDataEntity dictionaryDataEntity = noticeType.stream().filter(notice -> notice.getEnCode().equals(t.getCategory())).findFirst().orElse(new DictionaryDataEntity());
  89. vo.setCategory(dictionaryDataEntity.getFullName());
  90. // 转换创建人、发布人
  91. UserEntity user = userList.stream().filter(ul -> ul.getId().equals(t.getCreatorUserId())).findFirst().orElse(null);
  92. vo.setCreatorUser(user != null ? user.getRealName() + "/" + user.getAccount() : "");
  93. if (t.getEnabledMark() != null && t.getEnabledMark() != 0) {
  94. UserEntity entity = userApi.getInfo(t.getLastModifyUserId());
  95. vo.setLastModifyUserId(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
  96. vo.setReleaseTime(t.getLastModifyTime() != null ? t.getLastModifyTime().getTime() : null);
  97. vo.setReleaseUser(vo.getLastModifyUserId());
  98. }
  99. voList.add(vo);
  100. });
  101. return ActionResult.page(voList, paginationVO);
  102. }
  103. /**
  104. * 添加系统公告
  105. *
  106. * @param noticeCrForm 实体对象
  107. * @return
  108. */
  109. @Operation(summary = "添加系统公告")
  110. @Parameters({
  111. @Parameter(name = "noticeCrForm", description = "新建系统公告模型", required = true)
  112. })
  113. @SaCheckPermission("msgCenter.notice")
  114. @PostMapping("/Notice")
  115. public ActionResult create(@RequestBody @Valid NoticeCrForm noticeCrForm) {
  116. MessageEntity entity = JsonUtil.getJsonToBean(noticeCrForm, MessageEntity.class);
  117. if(entity != null && StringUtil.isNotEmpty(entity.getBodyText()) && (entity.getBodyText().contains("&lt;") || entity.getBodyText().contains("&amp;lt;"))){
  118. return ActionResult.fail(MsgCode.MSERR112.get());
  119. }
  120. messageService.create(entity);
  121. return ActionResult.success(MsgCode.SU001.get());
  122. }
  123. /**
  124. * 修改系统公告
  125. *
  126. * @param id 主键值
  127. * @param messageUpForm 实体对象
  128. * @return
  129. */
  130. @Operation(summary = "修改系统公告")
  131. @Parameters({
  132. @Parameter(name = "id", description = "主键", required = true),
  133. @Parameter(name = "messageUpForm", description = "修改系统公告模型", required = true)
  134. })
  135. @SaCheckPermission("msgCenter.notice")
  136. @PutMapping("/Notice/{id}")
  137. public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid NoticeUpForm messageUpForm) {
  138. MessageEntity entity = JsonUtil.getJsonToBean(messageUpForm, MessageEntity.class);
  139. if(entity != null && StringUtil.isNotEmpty(entity.getBodyText()) && (entity.getBodyText().contains("&lt;") || entity.getBodyText().contains("&amp;lt;"))){
  140. return ActionResult.fail(MsgCode.MSERR112.get());
  141. }
  142. boolean flag = messageService.update(id, entity);
  143. if (flag == false) {
  144. return ActionResult.fail(MsgCode.FA002.get());
  145. }
  146. return ActionResult.success(MsgCode.SU004.get());
  147. }
  148. /**
  149. * 信息
  150. *
  151. * @param id 主键值
  152. * @return
  153. */
  154. @Operation(summary = "获取/查看系统公告信息")
  155. @Parameters({
  156. @Parameter(name = "id", description = "主键", required = true)
  157. })
  158. @SaCheckPermission("msgCenter.notice")
  159. @GetMapping("/Notice/{id}")
  160. public ActionResult<NoticeInfoVO> Info(@PathVariable("id") String id) throws DataException {
  161. MessageEntity entity = messageService.getInfo(id);
  162. NoticeInfoVO vo = null;
  163. if (entity != null) {
  164. UserEntity info = userApi.getInfo(entity.getCreatorUserId());
  165. entity.setCreatorUserId(info != null ? info.getRealName() + "/" + info.getAccount() : "");
  166. vo = JsonUtilEx.getJsonToBeanEx(entity, NoticeInfoVO.class);
  167. vo.setReleaseUser(entity.getCreatorUserId());
  168. vo.setReleaseTime(entity.getLastModifyTime() != null ? entity.getLastModifyTime().getTime() : null);
  169. UserEntity userEntity = userApi.getInfo(entity.getLastModifyUserId());
  170. if (userEntity != null && StringUtil.isNotEmpty(userEntity.getId())) {
  171. String realName = userEntity.getRealName();
  172. String account = userEntity.getAccount();
  173. if (StringUtil.isNotEmpty(realName)) {
  174. vo.setReleaseUser(realName + "/" + account);
  175. }
  176. }
  177. }
  178. return ActionResult.success(vo);
  179. }
  180. /**
  181. * 删除
  182. *
  183. * @param id 主键值
  184. * @return
  185. */
  186. @Operation(summary = "删除系统公告")
  187. @Parameters({
  188. @Parameter(name = "id", description = "主键", required = true)
  189. })
  190. @SaCheckPermission("msgCenter.notice")
  191. @DeleteMapping("/Notice/{id}")
  192. public ActionResult delete(@PathVariable("id") String id) {
  193. MessageEntity entity = messageService.getInfo(id);
  194. if (entity != null) {
  195. messageService.delete(entity);
  196. return ActionResult.success(MsgCode.SU003.get());
  197. }
  198. return ActionResult.fail(MsgCode.FA003.get());
  199. }
  200. /**
  201. * 发布公告
  202. *
  203. * @param id 主键值
  204. * @return
  205. */
  206. @Operation(summary = "发布系统公告")
  207. @Parameters({
  208. @Parameter(name = "id", description = "主键", required = true)
  209. })
  210. @SaCheckPermission("msgCenter.notice")
  211. @PutMapping("/Notice/{id}/Actions/Release")
  212. public ActionResult release(@PathVariable("id") String id) {
  213. MessageEntity entity = messageService.getInfo(id);
  214. if (entity != null) {
  215. List<String> userIds = null;
  216. if (StringUtil.isNotEmpty(entity.getToUserIds())) {
  217. userIds = Arrays.asList(entity.getToUserIds().split(","));
  218. } else {
  219. userIds = userApi.getListId();
  220. }
  221. List<String> userIdList = userApi.getUserIdList(userIds);
  222. if (messageService.sentNotice(userIdList, entity)) {
  223. /*if(userIdList != null && userIdList.size()>0) {
  224. for (String userId : userIdList) {
  225. List<String> cidList = userDeviceService.getCidList(userId);
  226. if(cidList != null && cidList.size()>0){
  227. JSONObject jsonObject = new JSONObject();
  228. jsonObject.put("type","1");
  229. jsonObject.put("id",entity.getId());
  230. jsonObject.put("title",entity.getTitle());
  231. String text = JSONObject.toJSONString(jsonObject);
  232. byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
  233. text = Base64.getEncoder().encodeToString(bytes);
  234. uinPush.sendUniPush(cidList, entity.getTitle(), "你有一条公告消息", "1", text);
  235. }
  236. }
  237. }*/
  238. return ActionResult.success(MsgCode.SU011.get());
  239. }
  240. }
  241. return ActionResult.fail(MsgCode.FA011.get());
  242. }
  243. //=======================================站内消息、消息中心=================================================
  244. /**
  245. * 获取消息中心列表
  246. *
  247. * @param pagination
  248. * @return
  249. */
  250. @Operation(summary = "列表(通知公告/系统消息/私信消息)")
  251. @GetMapping
  252. public ActionResult<PageListVO<MessageInfoVO>> messageList(PaginationMessage pagination) {
  253. List<MessageInfoVO> listVO = new ArrayList<>();
  254. List<MessageReceiveEntity> list = messageService.getMessageList3(pagination);
  255. List<UserEntity> userList = userApi.getUserName(list.stream().map(SuperBaseEntity.SuperCBaseEntity::getCreatorUserId).collect(Collectors.toList()));
  256. list.forEach(t -> {
  257. MessageInfoVO vo = JsonUtil.getJsonToBean(t, MessageInfoVO.class);
  258. UserEntity user = userList.stream().filter(ul -> ul.getId().equals(t.getCreatorUserId())).findFirst().orElse(null);
  259. if (user != null) {
  260. vo.setReleaseTime(t.getCreatorTime() != null ? t.getCreatorTime().getTime() : null);
  261. UserEntity entity = userApi.getInfo(t.getCreatorUserId());
  262. vo.setReleaseUser(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
  263. vo.setCreatorUser(entity != null ? entity.getRealName() + "/" + entity.getAccount() : "");
  264. }
  265. listVO.add(vo);
  266. });
  267. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  268. return ActionResult.page(listVO, paginationVO);
  269. }
  270. /**
  271. * 读取消息
  272. *
  273. * @param id 主键值
  274. * @return
  275. */
  276. @Operation(summary = "读取消息")
  277. @Parameters({
  278. @Parameter(name = "id", description = "主键值", required = true)
  279. })
  280. @GetMapping("/ReadInfo/{id}")
  281. public ActionResult<NoticeInfoVO> readInfo(@PathVariable("id") String id) throws DataException {
  282. MessageReceiveEntity receive = messageService.messageRead(id);
  283. if (receive != null) {
  284. UserEntity user = userApi.getInfo(receive.getCreatorUserId());
  285. receive.setCreatorUserId(user != null ? user.getRealName() + "/" + user.getAccount() : "");
  286. receive.setBodyText(receive.getBodyText());
  287. // if (entity.getType() == 2) {
  288. // entity.setBodyText(receive.getBodyText());
  289. // }
  290. }
  291. NoticeInfoVO vo = JsonUtil.getJsonToBean(receive, NoticeInfoVO.class);
  292. if (Objects.equals(receive.getType() , 1)) {
  293. try{
  294. MessageEntity jsonToBean = JsonUtil.getJsonToBean(receive.getBodyText(), MessageEntity.class);
  295. if (jsonToBean !=null) {
  296. vo.setCategory(jsonToBean.getCategory());
  297. vo.setCoverImage(jsonToBean.getCoverImage());
  298. vo.setExcerpt(jsonToBean.getExcerpt());
  299. vo.setExpirationTime(jsonToBean.getExpirationTime() != null ? jsonToBean.getExpirationTime().getTime() : null);
  300. vo.setFiles(jsonToBean.getFiles());
  301. vo.setBodyText(jsonToBean.getBodyText());
  302. if (jsonToBean.getId() != null) {
  303. MessageEntity info = messageService.getInfo(jsonToBean.getId());
  304. if (info != null) {
  305. vo.setCategory(info.getCategory());
  306. vo.setCoverImage(info.getCoverImage());
  307. vo.setExcerpt(info.getExcerpt());
  308. vo.setExpirationTime(info.getExpirationTime() != null ? info.getExpirationTime().getTime() : null);
  309. vo.setFiles(info.getFiles());
  310. vo.setBodyText(info.getBodyText());
  311. }
  312. }
  313. }
  314. }catch (Exception e){
  315. vo.setBodyText(receive.getBodyText());
  316. }
  317. }
  318. vo.setReleaseTime(receive.getCreatorTime() != null ? receive.getCreatorTime().getTime() : null);
  319. // UserEntity info = usersApi.getInfoById(receive.getCreatorUserId());
  320. vo.setReleaseUser(receive.getCreatorUserId());
  321. return ActionResult.success(vo);
  322. }
  323. /**
  324. * 全部已读
  325. *
  326. * @param pagination 分页模型
  327. * @return
  328. */
  329. @Operation(summary = "全部已读")
  330. @Parameters({
  331. @Parameter(name = "pagination", description = "分页模型", required = true)
  332. })
  333. @PostMapping("/Actions/ReadAll")
  334. public ActionResult allRead(@RequestBody PaginationMessage pagination) {
  335. List<MessageReceiveEntity> list = messageService.getMessageList3(pagination);
  336. if(list != null && list.size()>0) {
  337. List<String> idList = list.stream().map(SuperBaseEntity.SuperIBaseEntity::getId).collect(Collectors.toList());
  338. messageService.messageRead(idList);
  339. return ActionResult.success(MsgCode.SU005.get());
  340. }else {
  341. return ActionResult.fail(MsgCode.MSERR113.get());
  342. }
  343. }
  344. /**
  345. * app端获取未读数据
  346. *
  347. * @return
  348. */
  349. @Operation(summary = "app端获取未读数据")
  350. @GetMapping("/getUnReadMsgNum")
  351. public ActionResult getUnReadMsgNum() {
  352. Map<String, String> map = new HashMap<>();
  353. UserInfo userInfo = UserProvider.getUser();
  354. Integer unReadMsg = messageService.getUnreadCount(userInfo.getUserId(), 2);
  355. Integer unReadSchedule = messageService.getUnreadCount(userInfo.getUserId(),4);
  356. Integer unReadNotice = messageService.getUnreadCount(userInfo.getUserId(), 1);
  357. Integer unReadSystemMsg = messageService.getUnreadCount(userInfo.getUserId(), 3);
  358. Integer unReadNum = unReadMsg+unReadNotice+unReadSchedule+unReadSystemMsg;
  359. map.put("unReadMsg",unReadMsg.toString());
  360. map.put("unReadNotice",unReadNotice.toString());
  361. map.put("unReadSchedule",unReadSchedule.toString());
  362. map.put("unReadSystemMsg",unReadSystemMsg.toString());
  363. map.put("unReadNum",unReadNum.toString());
  364. return ActionResult.success(map);
  365. }
  366. /**
  367. * 删除记录
  368. *
  369. * @param recordForm 已读模型
  370. * @return
  371. */
  372. @Operation(summary = "删除消息")
  373. @Parameters({
  374. @Parameter(name = "recordForm", description = "已读模型", required = true)
  375. })
  376. @DeleteMapping("/Record")
  377. public ActionResult deleteRecord(@RequestBody MessageRecordForm recordForm) {
  378. String[] id = recordForm.getIds().split(",");
  379. List<String> list = Arrays.asList(id);
  380. messageService.deleteRecord(list);
  381. return ActionResult.success(MsgCode.SU003.get());
  382. }
  383. }