Java OA系统通知公告模块

### 使用Spring Boot实现OA通知公告模块

使用Spring Boot框架实现一个支持多种形式公告发布、设置发布时间和有效期,以及公告发布后推送通知的模块。

#### 项目结构

结构组织项目:

```
OA_Notification_Module/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── oamodule/
│   │   │               ├── controller/
│   │   │               │   └── AnnouncementController.java
│   │   │               ├── model/
│   │   │               │   └── Announcement.java
│   │   │               ├── repository/
│   │   │               │   └── AnnouncementRepository.java
│   │   │               ├── service/
│   │   │               │   ├── AnnouncementService.java
│   │   │               │   └── NotificationService.java
│   │   │               └── OaNotificationModuleApplication.java
│   │   └── resources/
│   │       ├── application.properties
│   └── test/
│       └── java/
└── pom.xml
```

#### 1. `Announcement` 模型类

```java
package com.example.oamodule.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.time.LocalDateTime;

@Entity
public class Announcement {
    @Id
    private String id;
    private String type; // TEXT, IMAGE, VIDEO
    private String content;
    private LocalDateTime publishTime;
    private LocalDateTime expiryTime;

    public Announcement() {}

    public Announcement(String id, String type, String content, LocalDateTime publishTime, LocalDateTime expiryTime) {
        this.id = id;
        this.type = type;
        this.content = content;
        this.publishTime = publishTime;
        this.expiryTime = expiryTime;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public LocalDateTime getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(LocalDateTime publishTime) {
        this.publishTime = publishTime;
    }

    public LocalDateTime getExpiryTime() {
        return expiryTime;
    }

    public void setExpiryTime(LocalDateTime expiryTime) {
        this.expiryTime = expiryTime;
    }

    public boolean isExpired() {
        return LocalDateTime.now().isAfter(expiryTime);
    }
}
```

#### 2. `AnnouncementRepository` 接口

```java
package com.example.oamodule.repository;

import com.example.oamodule.model.Announcement;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface AnnouncementRepository extends JpaRepository<Announcement, String> {
    List<Announcement> findByExpiryTimeAfter(LocalDateTime now);
}
```

#### 3. `AnnouncementService` 服务类

```java
package com.example.oamodule.service;

import com.example.oamodule.model.Announcement;
import com.example.oamodule.repository.AnnouncementRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AnnouncementService {
    @Autowired
    private AnnouncementRepository announcementRepository;

    public void createAnnouncement(String id, String type, String content, LocalDateTime publishTime, LocalDateTime expiryTime) {
        Announcement announcement = new Announcement(id, type, content, publishTime, expiryTime);
        announcementRepository.save(announcement);
        System.out.println("Announcement created: " + announcement.getId());
    }

    public List<Announcement> getAllAnnouncements() {
        return announcementRepository.findAll();
    }

    public List<Announcement> getActiveAnnouncements() {
        return announcementRepository.findByExpiryTimeAfter(LocalDateTime.now());
    }

    public void removeExpiredAnnouncements() {
        List<Announcement> announcements = announcementRepository.findAll();
        for (Announcement announcement : announcements) {
            if (announcement.isExpired()) {
                announcementRepository.delete(announcement);
            }
        }
    }
}
```

#### 4. `NotificationService` 服务类

```java
package com.example.oamodule.service;

import com.example.oamodule.model.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NotificationService {
    @Autowired
    private JavaMailSender mailSender;

    public void sendNotifications(List<Announcement> announcements) {
        for (Announcement announcement : announcements) {
            // 模拟推送到电子邮件
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo("employee@example.com");
            message.setSubject("New Announcement: " + announcement.getType());
            message.setText(announcement.getContent());
            mailSender.send(message);
            System.out.println("Sent notification for announcement: " + announcement.getId());
        }
    }
}
```

#### 5. `AnnouncementController` 控制器类

```java
package com.example.oamodule.controller;

import com.example.oamodule.model.Announcement;
import com.example.oamodule.service.AnnouncementService;
import com.example.oamodule.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/announcements")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private NotificationService notificationService;

    @PostMapping
    public void createAnnouncement(@RequestBody Announcement announcement) {
        announcementService.createAnnouncement(announcement.getId(), announcement.getType(), announcement.getContent(),
                announcement.getPublishTime(), announcement.getExpiryTime());
        notificationService.sendNotifications(List.of(announcement));
    }

    @GetMapping
    public List<Announcement> getAllAnnouncements() {
        return announcementService.getAllAnnouncements();
    }

    @GetMapping("/active")
    public List<Announcement> getActiveAnnouncements() {
        return announcementService.getActiveAnnouncements();
    }

    @DeleteMapping("/expired")
    public void removeExpiredAnnouncements() {
        announcementService.removeExpiredAnnouncements();
    }
}
```

#### 6. 应用主类

```java
package com.example.oamodule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OaNotificationModuleApplication {
    public static void main(String[] args) {
        SpringApplication.run(OaNotificationModuleApplication.class, args);
    }
}
```

#### 7. `application.properties` 配置文件

```properties
spring.datasource.url=jdbc:mysql://localhost:3306/oamodule
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=your-email@example.com
spring.mail.password=your-email-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
```

#### 8. 数据库初始化脚本

```sql
CREATE DATABASE oamodule;

USE oamodule;

CREATE TABLE announcements (
    id VARCHAR(50) PRIMARY KEY,
    type VARCHAR(10),
    content TEXT,
    publish_time TIMESTAMP,
    expiry_time TIMESTAMP
);
```

#### 9. `pom.xml` 示例

```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://www.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>oamodule</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
```

使用Spring Boot框架实现了一个支持多种形式公告发布、设置发布时间和有效期以及公告发布后推送功能的OA通知公告模块。这个模块结合了JPA进行数据库操作,并使用Spring Mail进行邮件推送。

运行环境 操作系统:Windows XP。 Java平台:JDK 1.5。 Web服务器:Tomcat v 5.5.23,下载地址:http://tomcat.apache.org/。 数据库服务器:MySQL v 5.0.45,下载地址:http://www.mysql.com/。 开发平台:Eclipse SDK v 3.2.2,下载地址:http://www.eclipse.org/download/index.jsp。 Eclipse插件TomcatPlugins v 3.2.1,下载地址:http://www.eclipse-plugins.info/eclipse/index.jsp。 Eclipse插件ResourceBundleEditor v 0.7.7,下载地址:http://resourcebundleeditor.com/。 Eclipse插件MyEclipse v 5.5.1,下载地址:http://www.myeclipseide.com/ Spring 采用 2.0 版本 Hibernate 采用3.0版本 ============================ 请注意:如出现中文乱码,检查如下配置是否正确。 (1)MySql 数据库是否是utf-8 格式(在安装时选择支持多语言),数据是否正常。 (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 ============================== 数据库使用的是MySQL,其版本为5.0.45 版本。 数据库的用户名及密码均为root。 使用的时候,请参考附件数据库导入一节。或将需要用到的某章的数据库目录复制到“mysql安装根目录\data”文件夹下就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值