diff --git a/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.java b/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.java index 9ada037d6b684c6a1b36c8f162837c2456ba1738..3e8bc50705165e397938a515d14bfba28f7fd8e1 100644 --- a/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.java +++ b/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.java @@ -49,6 +49,11 @@ public interface UserSessionMapper { @Param("pageSize") Integer pageSize ); + List getOnlineUserUuidListByUserUuidListAndGreaterThanSessionTime( + @Param("userUuidList") List userUuidList, + @Param("sessionTime") Date sessionTime + ); + int getUserSessionCountByDate(String limitDate); int insertUserSession(@Param("userUuid") String userUuid, @Param("tokenHash") String tokenHash, @Param("tokenCreateTime") Long tokenCreateTime, @Param("authInfoHash") String authInfoHash); diff --git a/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.xml b/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.xml index d684e43ac866ef862a96644d8e5dfac504c75884..b26db4d938736c2dfafa781bb46cd44ba2f8d28e 100644 --- a/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.xml +++ b/src/main/java/neatlogic/framework/dao/mapper/UserSessionMapper.xml @@ -121,6 +121,18 @@ along with this program. If not, see .--> + + + + + + update `system_notice` @@ -435,6 +451,19 @@ where `id` = #{id} + + update `system_notice_user` @@ -553,6 +582,24 @@ + + INSERT INTO `system_notice_user` ( + `system_notice_id`, + `user_uuid`, + `is_read` + ) + SELECT DISTINCT + a.`id`, '${userUuid}', 0 + FROM `system_notice` a + JOIN `system_notice_recipient` b ON a.`id` = b.`system_notice_id` + WHERE a.`status` = 'issued' + AND a.`id` NOT IN (SELECT `system_notice_id` FROM `system_notice_user` WHERE `user_uuid` = #{userUuid}) + AND b.`uuid` IN + + #{uuid} + + + delete from `system_notice` @@ -576,4 +623,15 @@ + + + + DELETE FROM `system_notice_user` WHERE `system_notice_id` = #{value} + + diff --git a/src/main/java/neatlogic/framework/systemnotice/service/ISystemNoticeCrossoverService.java b/src/main/java/neatlogic/framework/systemnotice/service/ISystemNoticeCrossoverService.java new file mode 100644 index 0000000000000000000000000000000000000000..407e6cb7a36f15b418eb1f730febbfacbf4a796c --- /dev/null +++ b/src/main/java/neatlogic/framework/systemnotice/service/ISystemNoticeCrossoverService.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.systemnotice.service; + +import neatlogic.framework.crossover.ICrossoverService; +import neatlogic.framework.systemnotice.dto.SystemNoticeVo; + +public interface ISystemNoticeCrossoverService extends ICrossoverService { + + /** + * @Description: 清理掉system_notice_user中因删除公告或更改公告通知对象而遗留的记录 + * @Author: laiwt + * @Date: 2021/1/20 11:10 + * @Params: [] + * @Returns: void + **/ + public void clearSystemNoticeUser(); + + /** + * @Description: 检查是否存在【已下发却到了失效时间的】公告,如果有,则停用 + * @Author: laiwt + * @Date: 2021/1/20 11:12 + * @Params: [] + * @Returns: void + **/ + public void stopExpiredSystemNotice(); + + /** + * @Description: 在system_notice_user插入【当前用户可看的】、【已下发的】公告 + * @Author: laiwt + * @Date: 2021/1/20 11:12 + * @Params: [] + * @Returns: void + **/ + public void pullIssuedSystemNotice(); + + /** + * @Description: 检查是否存在【当前用户可看的】、【到了生效时间,却还没下发】公告, + * 如果有则下发给当前用户 + * @Author: laiwt + * @Date: 2021/1/20 11:13 + * @Params: [] + * @Returns: void + **/ + public void pullActiveSystemNotice(); + + /** + * 下发公告 + * @param systemNoticeVo + */ + boolean issueSystemNotice(SystemNoticeVo systemNoticeVo); +} diff --git a/src/main/java/neatlogic/module/framework/systemnotice/login/handler/LoginPullSystemNoticeProcessor.java b/src/main/java/neatlogic/module/framework/systemnotice/login/handler/LoginPullSystemNoticeProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..8ee6cbcd98458fd8d565ac23aa131a40b4f13484 --- /dev/null +++ b/src/main/java/neatlogic/module/framework/systemnotice/login/handler/LoginPullSystemNoticeProcessor.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.framework.systemnotice.login.handler; + +import neatlogic.framework.login.core.LoginPostProcessorBase; +import neatlogic.module.framework.systemnotice.service.SystemNoticeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @Title: LoginPullSystemNoticeProcessor + * @Package neatlogic.framework.message.login.handler + * @Description: 登录后拉取系统公告处理器 + * @Author: laiwt + * @Date: 2021/1/15 15:38 + **/ +@Service +public class LoginPullSystemNoticeProcessor extends LoginPostProcessorBase { + + @Autowired + private SystemNoticeService systemNoticeService; + + @Override + protected void myLoginAfterInitialization() { + /** 清理掉system_notice_user中因删除公告或更改公告通知对象而遗留的记录 **/ +// systemNoticeService.clearSystemNoticeUser(); + + /** 检查是否存在【已下发却到了失效时间的】公告,如果有,则停用 **/ +// systemNoticeService.stopExpiredSystemNotice(); + + /** 在system_notice_user插入【当前用户可看的】、【已下发的】公告 **/ + systemNoticeService.pullIssuedSystemNotice(); + + /** + * 检查是否存在【当前用户可看的】、【到了生效时间,却还没下发】公告,如果有,则下发给当前用户 + * 其他的通知用户,如果在线则由前端定时拉取,如果离线则登录时拉取 + **/ +// systemNoticeService.pullActiveSystemNotice(); + } + +} diff --git a/src/main/java/neatlogic/module/framework/systemnotice/schedule/IssueSystemNoticeJob.java b/src/main/java/neatlogic/module/framework/systemnotice/schedule/IssueSystemNoticeJob.java new file mode 100644 index 0000000000000000000000000000000000000000..b0ae2fd3beabb3331f26c4ea8cffd5ed66cc328b --- /dev/null +++ b/src/main/java/neatlogic/module/framework/systemnotice/schedule/IssueSystemNoticeJob.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.framework.systemnotice.schedule; + +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.scheduler.core.JobBase; +import neatlogic.framework.scheduler.dto.JobObject; +import neatlogic.framework.systemnotice.dao.mapper.SystemNoticeMapper; +import neatlogic.framework.systemnotice.dto.SystemNoticeVo; +import neatlogic.module.framework.systemnotice.service.SystemNoticeService; +import org.quartz.DisallowConcurrentExecution; +import org.quartz.JobExecutionContext; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Objects; + +@Component +@DisallowConcurrentExecution +public class IssueSystemNoticeJob extends JobBase { + + @Resource + private SystemNoticeMapper systemNoticeMapper; + + @Resource + private SystemNoticeService systemNoticeService; + + @Override + public String getGroupName() { + return TenantContext.get().getTenantUuid() + "-ISSUE-SYSTEMNOTICE"; + } + + @Override + public void reloadJob(JobObject jobObject) { + String tenantUuid = jobObject.getTenantUuid(); + TenantContext.get().switchTenant(tenantUuid); + Long noticeId = Long.valueOf(jobObject.getJobName()); + SystemNoticeVo systemNotice = systemNoticeMapper.getSystemNoticeById(noticeId); + if (systemNotice != null && Objects.equals(systemNotice.getStatus(), SystemNoticeVo.Status.NOTISSUED.getValue()) && systemNotice.getStartTime() != null) { + JobObject.Builder newJobObjectBuilder = new JobObject.Builder(noticeId.toString(), this.getGroupName(), this.getClassName(), TenantContext.get().getTenantUuid()) + .withBeginTime(systemNotice.getStartTime()) + .withIntervalInSeconds(60 * 60) + .withRepeatCount(0); + JobObject newJobObject = newJobObjectBuilder.build(); + schedulerManager.loadJob(newJobObject); + } + } + + @Override + public void initJob(String tenantUuid) { + List noticeIdList = systemNoticeMapper.getNotIssuedNoticeIdList(); + for(Long noticeId : noticeIdList) { + JobObject.Builder jobObjectBuilder = new JobObject.Builder(noticeId.toString(), this.getGroupName(), this.getClassName(), TenantContext.get().getTenantUuid()); + JobObject jobObject = jobObjectBuilder.build(); + this.reloadJob(jobObject); + } + + } + + @Override + protected Boolean isMyHealthy(JobObject jobObject) { + Long noticeId = Long.valueOf(jobObject.getJobName()); + SystemNoticeVo systemNotice = systemNoticeMapper.getSystemNoticeById(noticeId); + if (systemNotice != null && Objects.equals(systemNotice.getStatus(), SystemNoticeVo.Status.NOTISSUED.getValue()) && systemNotice.getStartTime() != null) { + return true; + } + return false; + } + + @Override + public void executeInternal(JobExecutionContext context, JobObject jobObject) throws Exception { + Long noticeId = Long.valueOf(jobObject.getJobName()); + SystemNoticeVo systemNotice = systemNoticeMapper.getSystemNoticeById(noticeId); + if (systemNotice != null && Objects.equals(systemNotice.getStatus(), SystemNoticeVo.Status.NOTISSUED.getValue()) && systemNotice.getStartTime() != null) { + systemNoticeService.issueSystemNotice(systemNotice); + systemNotice.setStatus(SystemNoticeVo.Status.ISSUED.getValue()); + systemNotice.setIssueTime(systemNotice.getStartTime()); + systemNoticeMapper.updateSystemNoticeIssueInfo(systemNotice); + } + } +} diff --git a/src/main/java/neatlogic/module/framework/systemnotice/schedule/StopSystemNoticeJob.java b/src/main/java/neatlogic/module/framework/systemnotice/schedule/StopSystemNoticeJob.java new file mode 100644 index 0000000000000000000000000000000000000000..028986f2e483f543bdd4a6cf80d45a9f6a09ed19 --- /dev/null +++ b/src/main/java/neatlogic/module/framework/systemnotice/schedule/StopSystemNoticeJob.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.framework.systemnotice.schedule; + +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.common.constvalue.SystemUser; +import neatlogic.framework.scheduler.core.JobBase; +import neatlogic.framework.scheduler.dto.JobObject; +import neatlogic.framework.systemnotice.dao.mapper.SystemNoticeMapper; +import neatlogic.framework.systemnotice.dto.SystemNoticeVo; +import org.quartz.DisallowConcurrentExecution; +import org.quartz.JobExecutionContext; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Objects; + +@Component +@DisallowConcurrentExecution +public class StopSystemNoticeJob extends JobBase { + + @Resource + private SystemNoticeMapper systemNoticeMapper; + + + @Override + public String getGroupName() { + return TenantContext.get().getTenantUuid() + "-STOP-SYSTEMNOTICE"; + } + + @Override + public void reloadJob(JobObject jobObject) { + String tenantUuid = jobObject.getTenantUuid(); + TenantContext.get().switchTenant(tenantUuid); + Long noticeId = Long.valueOf(jobObject.getJobName()); + SystemNoticeVo systemNotice = systemNoticeMapper.getSystemNoticeById(noticeId); + if (systemNotice != null && Objects.equals(systemNotice.getStatus(), SystemNoticeVo.Status.ISSUED.getValue()) && systemNotice.getEndTime() != null) { + JobObject.Builder newJobObjectBuilder = new JobObject.Builder(noticeId.toString(), this.getGroupName(), this.getClassName(), TenantContext.get().getTenantUuid()) + .withBeginTime(systemNotice.getEndTime()) + .withIntervalInSeconds(60 * 60) + .withRepeatCount(0); + JobObject newJobObject = newJobObjectBuilder.build(); + schedulerManager.loadJob(newJobObject); + } + } + + @Override + public void initJob(String tenantUuid) { + List noticeIdList = systemNoticeMapper.getIssuedNoticeIdList(); + for(Long noticeId : noticeIdList) { + JobObject.Builder jobObjectBuilder = new JobObject.Builder(noticeId.toString(), this.getGroupName(), this.getClassName(), TenantContext.get().getTenantUuid()); + JobObject jobObject = jobObjectBuilder.build(); + this.reloadJob(jobObject); + } + } + + @Override + protected Boolean isMyHealthy(JobObject jobObject) { + Long noticeId = Long.valueOf(jobObject.getJobName()); + SystemNoticeVo systemNotice = systemNoticeMapper.getSystemNoticeById(noticeId); + if (systemNotice != null && Objects.equals(systemNotice.getStatus(), SystemNoticeVo.Status.ISSUED.getValue()) && systemNotice.getEndTime() != null) { + return true; + } + return false; + } + + @Override + public void executeInternal(JobExecutionContext context, JobObject jobObject) throws Exception { + Long noticeId = Long.valueOf(jobObject.getJobName()); + SystemNoticeVo systemNotice = systemNoticeMapper.getSystemNoticeById(noticeId); + if (systemNotice != null && Objects.equals(systemNotice.getStatus(), SystemNoticeVo.Status.ISSUED.getValue()) && systemNotice.getStartTime() != null) { + systemNotice.setLcu(SystemUser.SYSTEM.getUserUuid()); + systemNoticeMapper.stopSystemNoticeById(systemNotice); + } + } +} diff --git a/src/main/java/neatlogic/module/framework/systemnotice/service/SystemNoticeService.java b/src/main/java/neatlogic/module/framework/systemnotice/service/SystemNoticeService.java new file mode 100644 index 0000000000000000000000000000000000000000..55eaa8177aa032013984babd0a77c269dc47a760 --- /dev/null +++ b/src/main/java/neatlogic/module/framework/systemnotice/service/SystemNoticeService.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.framework.systemnotice.service; + + +import neatlogic.framework.systemnotice.dto.SystemNoticeVo; + +/** + * @Title: SystemNoticeService + * @Package: neatlogic.framework.systemnotice.service + * @Description: + * @Author: laiwt + * @Date: 2021/1/20 11:00 + **/ +public interface SystemNoticeService { + + /** + * @Description: 清理掉system_notice_user中因删除公告或更改公告通知对象而遗留的记录 + * @Author: laiwt + * @Date: 2021/1/20 11:10 + * @Params: [] + * @Returns: void + **/ + public void clearSystemNoticeUser(); + + /** + * @Description: 检查是否存在【已下发却到了失效时间的】公告,如果有,则停用 + * @Author: laiwt + * @Date: 2021/1/20 11:12 + * @Params: [] + * @Returns: void + **/ + public void stopExpiredSystemNotice(); + + /** + * @Description: 在system_notice_user插入【当前用户可看的】、【已下发的】公告 + * @Author: laiwt + * @Date: 2021/1/20 11:12 + * @Params: [] + * @Returns: void + **/ + public void pullIssuedSystemNotice(); + + /** + * @Description: 检查是否存在【当前用户可看的】、【到了生效时间,却还没下发】公告, + * 如果有则下发给当前用户 + * @Author: laiwt + * @Date: 2021/1/20 11:13 + * @Params: [] + * @Returns: void + **/ + public void pullActiveSystemNotice(); + + /** + * 下发公告 + * @param systemNoticeVo + */ + boolean issueSystemNotice(SystemNoticeVo systemNoticeVo); +} diff --git a/src/main/java/neatlogic/module/framework/systemnotice/service/SystemNoticeServiceImpl.java b/src/main/java/neatlogic/module/framework/systemnotice/service/SystemNoticeServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..239f132ed0b044aa18532711ee4229a2d6268a3a --- /dev/null +++ b/src/main/java/neatlogic/module/framework/systemnotice/service/SystemNoticeServiceImpl.java @@ -0,0 +1,247 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.framework.systemnotice.service; + +import neatlogic.framework.asynchronization.thread.NeatLogicThread; +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.asynchronization.threadlocal.UserContext; +import neatlogic.framework.asynchronization.threadpool.CachedThreadPool; +import neatlogic.framework.common.config.Config; +import neatlogic.framework.common.constvalue.GroupSearch; +import neatlogic.framework.common.constvalue.UserType; +import neatlogic.framework.common.dto.BasePageVo; +import neatlogic.framework.common.util.PageUtil; +import neatlogic.framework.dao.mapper.RoleMapper; +import neatlogic.framework.dao.mapper.TeamMapper; +import neatlogic.framework.dao.mapper.UserSessionMapper; +import neatlogic.framework.scheduler.core.IJob; +import neatlogic.framework.scheduler.core.SchedulerManager; +import neatlogic.framework.scheduler.dto.JobObject; +import neatlogic.framework.scheduler.exception.ScheduleHandlerNotFoundException; +import neatlogic.framework.service.UserService; +import neatlogic.framework.systemnotice.dao.mapper.SystemNoticeMapper; +import neatlogic.framework.systemnotice.dto.SystemNoticeRecipientVo; +import neatlogic.framework.systemnotice.dto.SystemNoticeUserVo; +import neatlogic.framework.systemnotice.dto.SystemNoticeVo; +import neatlogic.framework.systemnotice.service.ISystemNoticeCrossoverService; +import neatlogic.module.framework.systemnotice.schedule.StopSystemNoticeJob; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * @Title: SystemNoticeServiceImpl + * @Package: neatlogic.framework.systemnotice.service + * @Description: + * @Author: laiwt + * @Date: 2021/1/20 11:02 + **/ +@Service +public class SystemNoticeServiceImpl implements SystemNoticeService, ISystemNoticeCrossoverService { + + @Autowired + private SystemNoticeMapper systemNoticeMapper; + + @Autowired + private RoleMapper roleMapper; + + @Autowired + private TeamMapper teamMapper; + + @Resource + private UserSessionMapper userSessionMapper; + + @Resource + private SchedulerManager schedulerManager; + + @Resource + private UserService userService; + + @Override + public void clearSystemNoticeUser() { + List recipientUuidList = getRecipientUuidList(); + Set noticeIdList = new HashSet<>(); + /** 获取system_notice_user中,因公告被删除而残留的记录 **/ + noticeIdList.addAll(systemNoticeMapper.getNotExistsNoticeIdListFromNoticeUserByUserUuid(UserContext.get().getUserUuid(true))); + /** 获取system_notice_user中,因更改公告通知对象而残留的记录 **/ + noticeIdList.addAll(systemNoticeMapper.getNotInNoticeScopeNoticeIdListByUserUuid(recipientUuidList, UserContext.get().getUserUuid(true))); + /** 清理掉上述两种记录 **/ + if (CollectionUtils.isNotEmpty(noticeIdList)) { + systemNoticeMapper.deleteSystemNoticeUserByUserUuid(UserContext.get().getUserUuid(true), noticeIdList); + } + } + + @Override + public void stopExpiredSystemNotice() { + List recipientUuidList = getRecipientUuidList(); + List expiredNoticeList = systemNoticeMapper.getExpiredNoticeListByRecipientUuidList(recipientUuidList); + if (CollectionUtils.isNotEmpty(expiredNoticeList)) { + for (SystemNoticeVo vo : expiredNoticeList) { + systemNoticeMapper.stopSystemNoticeById(vo); + } + } + } + + @Override + public void pullIssuedSystemNotice() { +// List recipientUuidList = getRecipientUuidList(); +// List issuedNoticeList = systemNoticeMapper.getIssuedNoticeIdListByRecipientUuidList(recipientUuidList); +// if (CollectionUtils.isNotEmpty(issuedNoticeList)) { +// List noticeUserVoList = new ArrayList<>(); +// for (Long id : issuedNoticeList) { +// noticeUserVoList.add(new SystemNoticeUserVo(id, UserContext.get().getUserUuid(true))); +// } +// systemNoticeMapper.batchInsertSystemNoticeUser(noticeUserVoList); +// } + String userUuid = UserContext.get().getUserUuid(); + List uuidList = UserContext.get().getUuidList(); + uuidList.add(userUuid); + uuidList.add(UserType.ALL.getValue()); + systemNoticeMapper.insertInsertSystemNoticeUser(userUuid, uuidList); + } + + @Override + public void pullActiveSystemNotice() { + List recipientUuidList = getRecipientUuidList(); + List hasBeenActiveNoticeList = systemNoticeMapper.getHasBeenActiveNoticeListByRecipientUuidList(recipientUuidList); + if (CollectionUtils.isNotEmpty(hasBeenActiveNoticeList)) { + List currentUserNoticeList = new ArrayList<>(); + /** 更改这些公告的状态为已下发 **/ + for (SystemNoticeVo vo : hasBeenActiveNoticeList) { + vo.setStatus(SystemNoticeVo.Status.ISSUED.getValue()); + vo.setIssueTime(vo.getStartTime()); + systemNoticeMapper.updateSystemNoticeStatus(vo); + currentUserNoticeList.add(new SystemNoticeUserVo(vo.getId(), UserContext.get().getUserUuid(true))); + /** 如果没有忽略已读,那么更改is_read为0 **/ + if (vo.getIgnoreRead() != null && vo.getIgnoreRead() == 0) { + systemNoticeMapper.updateSystemNoticeUserReadStatus(vo.getId(), UserContext.get().getUserUuid(true), 0); + } + } + /** 发送给当前用户 **/ + if (CollectionUtils.isNotEmpty(currentUserNoticeList)) { + systemNoticeMapper.batchInsertSystemNoticeUser(currentUserNoticeList); + } + } + } + + @Override + public boolean issueSystemNotice(SystemNoticeVo vo) { + Integer PAGE_SIZE = 100; + /* 如果没有忽略已读,则把system_notice_user中的is_read设为0 **/ + if (vo.getIgnoreRead() != null && vo.getIgnoreRead() == 0) { + /* 经测试,该语句update 53万条数据耗时约1.2s,故不单独开线程执行 **/ + systemNoticeMapper.updateReadStatusToNotReadByNoticeId(vo.getId()); + } else { + systemNoticeMapper.deleteSystemNoticeUserByNoticeId(vo.getId()); + } + + List recipientList = systemNoticeMapper.getRecipientListByNoticeId(vo.getId()); + if (CollectionUtils.isNotEmpty(recipientList)) { + long expireTime = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(Config.USER_EXPIRETIME()); + if (recipientList.stream().anyMatch(o -> UserType.ALL.getValue().equals(o.getUuid()))) { + /* 如果通知范围是所有人,那么找出当前所有的在线用户 **/ + int allOnlineUserCount = userSessionMapper.getAllOnlineUserCount(new Date(expireTime)); + if (allOnlineUserCount > 0) { + CachedThreadPool.execute(new NeatLogicThread("NOTICE-INSERTER") { + @Override + protected void execute() { + Date expireDate = new Date(expireTime); + BasePageVo pageVo = new BasePageVo(); + pageVo.setPageSize(PAGE_SIZE); + pageVo.setPageCount(PageUtil.getPageCount(allOnlineUserCount, pageVo.getPageSize())); + List noticeUserVoList = new ArrayList<>(); + for (int i = 1; i <= pageVo.getPageCount(); i++) { + pageVo.setCurrentPage(i); + List allOnlineUser = userSessionMapper.getAllOnlineUser(expireDate, pageVo.getStartNum(), pageVo.getPageSize()); + if (CollectionUtils.isNotEmpty(allOnlineUser)) { + allOnlineUser.forEach(o -> noticeUserVoList.add(new SystemNoticeUserVo(vo.getId(), o))); + systemNoticeMapper.batchInsertSystemNoticeUser(noticeUserVoList); + noticeUserVoList.clear(); + } + } + } + }); + } + } else { + List userUuidList = recipientList.stream() + .filter(o -> GroupSearch.USER.getValue().equals(o.getType())) + .map(SystemNoticeRecipientVo::getUuid) + .collect(Collectors.toList()); + List teamUuidList = recipientList.stream() + .filter(o -> GroupSearch.TEAM.getValue().equals(o.getType())) + .map(SystemNoticeRecipientVo::getUuid) + .collect(Collectors.toList()); + List roleUuidList = recipientList.stream() + .filter(o -> GroupSearch.ROLE.getValue().equals(o.getType())) + .map(SystemNoticeRecipientVo::getUuid) + .collect(Collectors.toList()); + List allUserUuidlist = userService.getUserUuidListByUserUuidListAndTeamUuidListAndRoleUuidList(userUuidList, teamUuidList, roleUuidList); + if (CollectionUtils.isNotEmpty(allUserUuidlist)) { + CachedThreadPool.execute(new NeatLogicThread("NOTICE-INSERTER") { + @Override + protected void execute() { + Date expireDate = new Date(expireTime); + List noticeUserVoList = new ArrayList<>(); + for (int fromIndex = 0; fromIndex < allUserUuidlist.size(); fromIndex += PAGE_SIZE) { + int toIndex = fromIndex + PAGE_SIZE; + if (toIndex > allUserUuidlist.size()) { + toIndex = allUserUuidlist.size(); + } + List list = allUserUuidlist.subList(fromIndex, toIndex); + List onlineUserList = userSessionMapper.getOnlineUserUuidListByUserUuidListAndGreaterThanSessionTime(list, expireDate); + if (CollectionUtils.isNotEmpty(onlineUserList)) { + onlineUserList.forEach(o -> noticeUserVoList.add(new SystemNoticeUserVo(vo.getId(), o))); + systemNoticeMapper.batchInsertSystemNoticeUser(noticeUserVoList); + noticeUserVoList.clear(); + } + } + } + }); + } + } + } + if (vo.getEndTime() != null) { + IJob jobHandler = SchedulerManager.getHandler(StopSystemNoticeJob.class.getName()); + if (jobHandler == null) { + throw new ScheduleHandlerNotFoundException(StopSystemNoticeJob.class.getName()); + } + String tenantUuid = TenantContext.get().getTenantUuid(); + JobObject jobObject = new JobObject.Builder(vo.getId().toString(), jobHandler.getGroupName(), jobHandler.getClassName(), tenantUuid) + .withBeginTime(vo.getEndTime()) + .withIntervalInSeconds(60 * 60) + .withRepeatCount(0) + .build(); + schedulerManager.loadJob(jobObject); + } + return true; + } + + private List getRecipientUuidList() { + List uuidList = new ArrayList<>(); + uuidList.add(UserContext.get().getUserUuid(true)); + uuidList.add(UserType.ALL.getValue()); + uuidList.addAll(teamMapper.getTeamUuidListByUserUuid(UserContext.get().getUserUuid(true))); + uuidList.addAll(roleMapper.getRoleUuidListByUserUuid(UserContext.get().getUserUuid(true))); + return uuidList; + } +}