今天主要是增加日程安排的消息推送功能,同时对增加修改删除等都对相应的消息推送数据进行更新。
1、日程添加完整代码
@Override @Transactional(rollbackFor=Exception.class) public void saveAndAddSchedule(OaSchedule oaSchedule) { SysUser loginUser = iEstarThirdService.getLoginUser(); String userId = loginUser.getUsername(); if(StringUtils.isEmpty(oaSchedule.getTaker())){ oaSchedule.setTaker(userId); } if(StringUtils.isEmpty(oaSchedule.getOwner())){ oaSchedule.setOwner(userId); } oaScheduleService.save(oaSchedule); if (oaSchedule.getRemind() != 0) {//要提醒 SysMessageVo sysMessageVo= new SysMessageVo(); sysMessageVo.setEsContent(oaSchedule.getContent()); sysMessageVo.setEsTitle(oaSchedule.getTitle()); sysMessageVo.setEsReceiver(oaSchedule.getTaker()); sysMessageVo.setEsType(oaSchedule.getRemindType()); String reminddate = DateUtil.getPreTime(DateUtil.getDateString(oaSchedule.getStartTime(), "yyyy-MM-dd HH:mm:ss") ,String.valueOf(0-oaSchedule.getRemind())); sysMessageVo.setEsSendTime(DateUtil.stringToDate(reminddate)); sysMessageVo.setEsSendStatus("0"); //SendMsgStatusEnum.WAIT.getCode() sysMessageVo.setEsSendNum(0); String sysMsgId = iEstarThirdService.saveSysMessage(sysMessageVo); oaSchedule.setMsgId(sysMsgId); oaScheduleService.updateById(oaSchedule); } }
2、日程删除
@Override @Transactional(rollbackFor=Exception.class) public boolean removeSchedule(String id) { OaSchedule oaSchedule = oaScheduleService.getById(id); String msgId = oaSchedule.getMsgId(); String sendStatus = iEstarThirdService.getMsgSendStatus(msgId); if (oaSchedule.getRemind() != 0 ) {//有消息提醒 if(StringUtils.equals(sendStatus,"0")) { oaScheduleService.removeById(id); iEstarThirdService.removeSysMessage(msgId); return true; } else { return false; } } else { oaScheduleService.removeById(id); return true; } }
3、日程更新
@Override @Transactional(rollbackFor=Exception.class) public boolean updateSchedule(OaSchedule oaSchedule) { String msgId = oaSchedule.getMsgId(); String sendStatus = iEstarThirdService.getMsgSendStatus(msgId); if (oaSchedule.getRemind() != 0) {//有消息提醒 if(StringUtils.equals(sendStatus,"0")) { oaScheduleService.updateById(oaSchedule); SysMessageVo sysMessageVo= new SysMessageVo(); sysMessageVo.setId(msgId); sysMessageVo.setEsContent(oaSchedule.getContent()); sysMessageVo.setEsTitle(oaSchedule.getTitle()); sysMessageVo.setEsReceiver(oaSchedule.getTaker()); sysMessageVo.setEsType(oaSchedule.getRemindType()); String reminddate = DateUtil.getPreTime(DateUtil.getDateString(oaSchedule.getStartTime(), "yyyy-MM-dd HH:mm:ss") ,String.valueOf(0-oaSchedule.getRemind())); sysMessageVo.setEsSendTime(DateUtil.stringToDate(reminddate)); iEstarThirdService.updateSysMessage(sysMessageVo); return true; } else { return false; } } return true; }
4、发送消息调整如下:
/** * @Description: 发送websocket消息job * @author: nbacheng * @date: 2023-05-08 * @version: V1.0 */ @Slf4j public class SendWebMsgJob implements Job { @Autowired private ISysMessageService sysMessageService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { log.info(String.format(" Nbcio-Boot 发送消息任务 SendWebMsgJob ! 时间:" + DateUtils.getTimestamp())); // 1.读取消息中心数据,只查询未发送的和发送失败不超过次数的 QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<SysMessage>(); queryWrapper.eq("es_send_status", SendMsgStatusEnum.WAIT.getCode()) .or(i -> i.eq("es_send_status", SendMsgStatusEnum.FAIL.getCode()).lt("es_send_num", 6)); List<SysMessage> sysMessages = sysMessageService.list(queryWrapper); System.out.println(sysMessages); // 2.根据不同的类型走不通的发送实现类 for (SysMessage sysMessage : sysMessages) { ISendMsgHandle sendMsgHandle = null; try { if (sysMessage.getEsType().equals(SendMsgTypeEnum.WEBMSG.getType())) {//目前只发送websocket消息 sendMsgHandle = (ISendMsgHandle) Class.forName(SendMsgTypeEnum.WEBMSG.getImplClass()).newInstance(); } } catch (Exception e) { log.error(e.getMessage(),e); } Integer sendNum = sysMessage.getEsSendNum(); try { sendMsgHandle.SendMsg(sysMessage.getEsReceiver(), sysMessage.getEsTitle(), sysMessage.getEsContent().toString(),sysMessage.getCreateBy()); // 发送消息成功 sysMessage.setEsSendStatus(SendMsgStatusEnum.SUCCESS.getCode()); } catch (Exception e) { e.printStackTrace(); // 发送消息出现异常 sysMessage.setEsSendStatus(SendMsgStatusEnum.FAIL.getCode()); } sysMessage.setEsSendNum(++sendNum); // 发送结果回写到数据库 sysMessageService.updateById(sysMessage); } } }
5、效果图