Skip to content

Commit

Permalink
feat: [功能]网页通知持久化 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeswalker23 committed May 12, 2021
1 parent 40ea0d6 commit 2d8acad
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.walkers.planes.fundhelper.dao;

import io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
* Dao of {@link WebsiteNoticeModel}
*
* @author planeswalker23
*/
@Mapper
public interface WebsiteNoticeDao {

/**
* 插入 WebsiteNoticeModel 数据
*
* @param websiteNotice 待插入数据
*/
void insert(@Param("websiteNotice") WebsiteNoticeModel websiteNotice);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.walkers.planes.fundhelper.entity.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* 网页通知模型
*
* @author planeswalker23
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WebsiteNoticeModel implements Serializable {
/**
* 主键
*/
private Long id;
/**
* 用户ID
*/
private Long virtualUserId;
/**
* 通知标题
*/
private String title;
/**
* 通知内容
*/
private String content;
/**
* 阅读状态
* False-0-未读
* True-1-已读
*/
private Boolean readStatus;
/**
* 创建时间
*/
private java.util.Date createDate;
/**
* 更新时间
*/
private java.util.Date updateDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* 通知方式枚举类
*
* @author 范逸东
* @author planeswalker23
*/
public interface NoticeMethod {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.walkers.planes.fundhelper.service.notice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
* 通知选择器,业务方使用该类进行通知
Expand All @@ -15,31 +13,28 @@
*/
@Slf4j
@Service
public class NotificationSelector implements ApplicationContextAware {
public class NotificationSelector {

@Resource
private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private Map<String, Notification> notificationMap;

/**
* 模板方法,执行通知方法
* @param noticeMessage 通知消息体
* @return Boolean
*/
public Boolean doNotice(NoticeMessage noticeMessage) {
try {
Notification notification = applicationContext.getBean(noticeMessage.getNoticeMethod(), Notification.class);
if (notification.match(noticeMessage.getNoticeMethod())) {
return notification.notice(noticeMessage);
}
} catch (BeansException e) {
log.warn("通知方式[{}]非法,请检查", noticeMessage.getNoticeMethod());
log.error(e.getMessage());
// 防止依赖注入为空
if (notificationMap == null) {
return Boolean.FALSE;
}
Notification notification = notificationMap.get(noticeMessage.getNoticeMethod());
// 校验通知方式存在且匹配
if (notification != null && notification.match(noticeMessage.getNoticeMethod())) {
// 执行通知逻辑
return notification.notice(noticeMessage);
}
log.warn("通知方式[{}]非法,请检查通知方式实现类: [{}]", noticeMessage.getNoticeMethod(), notificationMap);
return Boolean.FALSE;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.walkers.planes.fundhelper.service.notice.impl;

import io.walkers.planes.fundhelper.dao.VirtualUserDao;
import io.walkers.planes.fundhelper.dao.WebsiteNoticeDao;
import io.walkers.planes.fundhelper.entity.model.VirtualUserModel;
import io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel;
import io.walkers.planes.fundhelper.service.notice.NoticeMessage;
import io.walkers.planes.fundhelper.service.notice.NoticeMethod;
import io.walkers.planes.fundhelper.service.notice.Notification;
Expand All @@ -8,6 +12,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* 网页通知,仅会被 {@link NotificationSelector} 使用
*
Expand All @@ -17,14 +23,27 @@
@Service(value = NoticeMethod.WEBSITE)
public class NotificationByWebsite implements Notification {

@Resource
private VirtualUserDao virtualUserDao;
@Resource
private WebsiteNoticeDao websiteNoticeDao;

@Override
public Boolean match(String noticeMethod) {
return NoticeMethod.WEBSITE.equals(noticeMethod);
}

@Override
public Boolean notice(NoticeMessage noticeMessage) {
// todo 网页通知实现
// todo 根据邮箱查询用户
VirtualUserModel userByMailbox = virtualUserDao.selectByAccount(noticeMessage.getReceiver());
WebsiteNoticeModel websiteNotice = WebsiteNoticeModel.builder()
.title(noticeMessage.getTitle())
.content(noticeMessage.getContent())
.virtualUserId(userByMailbox.getId())
.readStatus(Boolean.FALSE)
.build();
websiteNoticeDao.insert(websiteNotice);
log.info("网页通知成功 -> 内容: {}", JacksonUtil.toJson(noticeMessage));
return Boolean.TRUE;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/mapper/WebstieNoticeMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.walkers.planes.fundhelper.dao.WebsiteNoticeDao">
<insert id="insert" parameterType="io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel" useGeneratedKeys="true" keyProperty="id">
insert into website_notice (virtual_user_id, title, content, read_status, create_date, update_date)
values (#{websiteNotice.virtualUserId}, #{websiteNotice.title}, #{websiteNotice.content}, #{websiteNotice.readStatus}, now(), now())
</insert>
</mapper>

0 comments on commit 2d8acad

Please sign in to comment.