Search in sources :

Example 1 with Announcement

use of com.hccake.ballcat.notify.model.entity.Announcement in project ballcat by ballcat-projects.

the class AnnouncementServiceImpl method updateAnnouncement.

/**
 * 更新公告信息
 * @param announcementDTO announcementDTO
 * @return boolean
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateAnnouncement(AnnouncementDTO announcementDTO) {
    Announcement oldAnnouncement = baseMapper.selectById(announcementDTO.getId());
    if (oldAnnouncement.getStatus() != AnnouncementStatusEnum.UNPUBLISHED.getValue()) {
        throw new BusinessException(SystemResultCode.BAD_REQUEST.getCode(), "不允许修改已经发布过的公告!");
    }
    Announcement announcement = AnnouncementConverter.INSTANCE.dtoToPo(announcementDTO);
    // 不允许修改为《发布中》以外的状态
    boolean isPublishStatus = announcement.getStatus() == AnnouncementStatusEnum.ENABLED.getValue();
    if (!isPublishStatus) {
        announcement.setStatus(null);
    }
    // 保证当前状态未被修改过
    boolean isUpdated = baseMapper.updateIfUnpublished(announcement);
    // 公告发布事件
    if (isUpdated && isPublishStatus) {
        this.onAnnouncementPublish(announcement);
    }
    return isUpdated;
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) Announcement(com.hccake.ballcat.notify.model.entity.Announcement) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Announcement

use of com.hccake.ballcat.notify.model.entity.Announcement in project ballcat by ballcat-projects.

the class AnnouncementServiceImpl method addAnnouncement.

/**
 * 创建公告
 * @param announcementDTO 公告信息
 * @return boolean
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean addAnnouncement(AnnouncementDTO announcementDTO) {
    Announcement announcement = AnnouncementConverter.INSTANCE.dtoToPo(announcementDTO);
    announcement.setId(null);
    int flag = baseMapper.insert(announcement);
    boolean inserted = SqlHelper.retBool(flag);
    // 公告发布事件
    boolean isPublishStatus = announcement.getStatus() == AnnouncementStatusEnum.ENABLED.getValue();
    if (inserted && isPublishStatus) {
        this.onAnnouncementPublish(announcement);
    }
    return inserted;
}
Also used : Announcement(com.hccake.ballcat.notify.model.entity.Announcement) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Announcement

use of com.hccake.ballcat.notify.model.entity.Announcement in project ballcat by ballcat-projects.

the class AnnouncementServiceImpl method publish.

/**
 * 发布公告信息
 * @param announcementId 公告ID
 * @return boolean
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean publish(Long announcementId) {
    Announcement announcement = baseMapper.selectById(announcementId);
    if (announcement.getStatus() != AnnouncementStatusEnum.UNPUBLISHED.getValue()) {
        throw new BusinessException(SystemResultCode.BAD_REQUEST.getCode(), "不允许修改已经发布过的公告!");
    }
    if (BooleanEnum.TRUE.getValue() != announcement.getImmortal() && LocalDateTime.now().isAfter(announcement.getDeadline())) {
        throw new BusinessException(SystemResultCode.BAD_REQUEST.getCode(), "公告失效时间必须迟于当前时间!");
    }
    // 更新公共至发布状态
    Announcement entity = new Announcement();
    entity.setId(announcementId);
    entity.setStatus(AnnouncementStatusEnum.ENABLED.getValue());
    boolean isUpdated = baseMapper.updateIfUnpublished(entity);
    if (isUpdated) {
        announcement.setStatus(AnnouncementStatusEnum.ENABLED.getValue());
        this.onAnnouncementPublish(announcement);
    }
    return isUpdated;
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) Announcement(com.hccake.ballcat.notify.model.entity.Announcement) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Announcement

use of com.hccake.ballcat.notify.model.entity.Announcement in project ballcat by ballcat-projects.

the class AnnouncementLoginEventListener method onAuthenticationSuccessEvent.

/**
 * 登陆成功时间监听 用户未读公告生成
 * @param event 登陆成功 event
 */
@EventListener(AuthenticationSuccessEvent.class)
public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) throws InterruptedException {
    AbstractAuthenticationToken source = (AbstractAuthenticationToken) event.getSource();
    Object details = source.getDetails();
    if (!(details instanceof HashMap)) {
        return;
    }
    // https://github.com/spring-projects-experimental/spring-authorization-server
    if ("password".equals(((HashMap) details).get("grant_type"))) {
        User user = (User) source.getPrincipal();
        SysUser sysUser = getSysUser(user);
        // 获取当前用户未拉取过的公告信息
        Integer userId = sysUser.getUserId();
        List<Announcement> announcements = announcementService.listUnPulled(userId);
        // 获取当前用户的各个过滤属性
        Map<Integer, Object> filterAttrs = recipientHandler.getFilterAttrs(sysUser);
        // 获取符合当前用户条件的,且接收类型包含站内的公告,保存其关联关系
        List<UserAnnouncement> userAnnouncements = announcements.stream().filter(x -> x.getReceiveMode().contains(NotifyChannelEnum.STATION.getValue())).filter(x -> filterMatched(x, filterAttrs)).map(Announcement::getId).map(id -> userAnnouncementService.prodUserAnnouncement(userId, id)).collect(Collectors.toList());
        try {
            userAnnouncementService.saveBatch(userAnnouncements);
        } catch (Exception exception) {
            log.error("用户公告保存失败:[{}]", userAnnouncements, exception);
        }
    }
}
Also used : Announcement(com.hccake.ballcat.notify.model.entity.Announcement) RequiredArgsConstructor(lombok.RequiredArgsConstructor) EventListener(org.springframework.context.event.EventListener) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) AuthenticationSuccessEvent(org.springframework.security.authentication.event.AuthenticationSuccessEvent) NotifyChannelEnum(com.hccake.ballcat.notify.enums.NotifyChannelEnum) User(com.hccake.ballcat.common.security.userdetails.User) AnnouncementService(com.hccake.ballcat.notify.service.AnnouncementService) UserAnnouncement(com.hccake.ballcat.notify.model.entity.UserAnnouncement) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) RecipientHandler(com.hccake.ballcat.notify.recipient.RecipientHandler) SysUser(com.hccake.ballcat.system.model.entity.SysUser) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Map(java.util.Map) UserAnnouncementService(com.hccake.ballcat.notify.service.UserAnnouncementService) User(com.hccake.ballcat.common.security.userdetails.User) SysUser(com.hccake.ballcat.system.model.entity.SysUser) SysUser(com.hccake.ballcat.system.model.entity.SysUser) Announcement(com.hccake.ballcat.notify.model.entity.Announcement) UserAnnouncement(com.hccake.ballcat.notify.model.entity.UserAnnouncement) HashMap(java.util.HashMap) UserAnnouncement(com.hccake.ballcat.notify.model.entity.UserAnnouncement) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) EventListener(org.springframework.context.event.EventListener)

Example 5 with Announcement

use of com.hccake.ballcat.notify.model.entity.Announcement in project ballcat by ballcat-projects.

the class AnnouncementServiceImpl method close.

/**
 * 关闭公告信息
 * @param announcementId 公告ID
 * @return boolean
 */
@Override
public boolean close(Long announcementId) {
    Announcement announcement = new Announcement();
    announcement.setId(announcementId);
    announcement.setStatus(AnnouncementStatusEnum.DISABLED.getValue());
    int flag = baseMapper.updateById(announcement);
    boolean isUpdated = SqlHelper.retBool(flag);
    if (isUpdated) {
        publisher.publishEvent(new AnnouncementCloseEvent(announcementId));
    }
    return isUpdated;
}
Also used : Announcement(com.hccake.ballcat.notify.model.entity.Announcement) AnnouncementCloseEvent(com.hccake.ballcat.notify.event.AnnouncementCloseEvent)

Aggregations

Announcement (com.hccake.ballcat.notify.model.entity.Announcement)5 Transactional (org.springframework.transaction.annotation.Transactional)3 BusinessException (com.hccake.ballcat.common.core.exception.BusinessException)2 User (com.hccake.ballcat.common.security.userdetails.User)1 NotifyChannelEnum (com.hccake.ballcat.notify.enums.NotifyChannelEnum)1 AnnouncementCloseEvent (com.hccake.ballcat.notify.event.AnnouncementCloseEvent)1 UserAnnouncement (com.hccake.ballcat.notify.model.entity.UserAnnouncement)1 RecipientHandler (com.hccake.ballcat.notify.recipient.RecipientHandler)1 AnnouncementService (com.hccake.ballcat.notify.service.AnnouncementService)1 UserAnnouncementService (com.hccake.ballcat.notify.service.UserAnnouncementService)1 SysUser (com.hccake.ballcat.system.model.entity.SysUser)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 EventListener (org.springframework.context.event.EventListener)1 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)1 AuthenticationSuccessEvent (org.springframework.security.authentication.event.AuthenticationSuccessEvent)1