Java全栈开发---Java ERP系统开发:商业ERP(十一)库存预警,JavaMail发送预警邮件

一、库存预警报表

1、需求以及实现思路

(1)需求

统计每种商品的库存与代发货数量,如果库存数量小于待发货数量侧要进行库存预警
在这里插入图片描述

(2)实现思路

将整个查结果拆分成2个子查询,再合并起来
在这里插入图片描述

(3)SQL语句编写

这种带子查询的语句相对复杂,我们分步来实现
1)先实现当前商品库存统计,查询出每种商品的库存数量
在这里插入图片描述
这种结果只能查询存在库存的商品数量,而库存中不存在的商品侧不会显示出来。我们可以通过左外链接的方式进行查询。使用nvi方法把null转换0
在这里插入图片描述
2)实现销售订单中商品的统计,统计每种待发货的商品数量
在这里插入图片描述
3)通过商品编号将上面这2种查询组合起来
在这里插入图片描述

(4)视图创建

我们上边的语句查询的结果是我们经常要用到的,每次都写的很繁琐,所以我们可以把常用的复杂的查询直接建立视图,把它当成hibernate中的一个表来处理,这样可以极简化我们的开发,便于日常维护。

创建视图SQL语句
在这里插入图片描述

2、代码编写

(1)创建实体类Storealert
package com.itzheng.erp.entity;

public class Storealert {
    
    
	private Long uuid;
	private String name;
	private Long storenum;
	private Long outnum;
	public Long getUuid() {
    
    
		return uuid;
	}
	public void setUuid(Long uuid) {
    
    
		this.uuid = uuid;
	}
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public Long getStorenum() {
    
    
		return storenum;
	}
	public void setStorenum(Long storenum) {
    
    
		this.storenum = storenum;
	}
	public Long getOutnum() {
    
    
		return outnum;
	}
	public void setOutnum(Long outnum) {
    
    
		this.outnum = outnum;
	}
	@Override
	public String toString() {
    
    
		return "Storealert [uuid=" + uuid + ", name=" + name + ", storenum=" + storenum + ", outnum=" + outnum + "]";
	}
}
(2)创建映射文件

映射文件:storealert.hbm.xml
在这里插入图片描述

(3)数据访问层实现

在IStoredetailDao和StoredetailDao中加入方法与实践
1)IStoredetailDao
在这里插入图片描述
2)IStoredetailDao
在这里插入图片描述

(4)业务逻辑层实现

在IStoredetailBiz和StoredetailBiz中添加方法与实现
在这里插入图片描述
在这里插入图片描述

(5)Action层的实现

在StoredetailAction中加入方法:
在这里插入图片描述

(6)页面代码

1)添加js/storealert.js
在这里插入图片描述
2)添加storealert.html文件
在这里插入图片描述
3)运行结果
http://localhost:8080/erp/storealert.html
在这里插入图片描述

二、JavaMail发送预警邮件(扩展)

1、需求分析

实现发送邮件预警功能,根据相关人员进行采购,点击“发送警报邮件”按钮即可发送库存报警
如果存在报警商品并成功发送 ,提示发送成功
如果不存在报警商品,提示没有库存报警商品
在这里插入图片描述

2、JavaMail介绍

JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。JavaMail API是Sun公司为方便Java开发人员在应用程序中实现邮件发送和接收功能而提供的一套标准开发包,它支持一些常用的邮件协议,如:SMTP、POP3、IMAP。开发人员使用JavaMail API编写邮件处理软件时,无须考虑邮件协议底层的实现细节,只要调用JavaMail开发包中相应的API类就可以了。
使用 JavaMail,我们可以实现类似 OutLook、FoxMail 的软件
Spring的javaMailSender对JavaMail进行封装,简化了开发,告别繁琐的API

3、ERP中实现发送预警邮件

(1)添加依赖

在父工程的pom.xml中添加java mail的依赖
在这里插入图片描述

(2)编写发送邮件的工具类

在业务逻辑层当中创建MailUtil
在这里插入图片描述

(3)编写spring配置文件

创建applicationContext_mail.xml
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
	<!-- 邮件发送器 -->
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" >
		<!-- smtp:发送邮件的协议 -->
		<property name="protocol" value="smtp" ></property>
		<!-- 邮件服务器地址 -->
		<property name="host" value="smtp.qq.com" ></property>
		<!-- 端口 -->
		<property name="port" value="465"></property>
		<!-- 邮箱用户名 -->
		<property name="username" value="[email protected]"></property>
		<!-- 邮箱密码 -->
		<property name="password" value="bqpfdguzaymvdcfa" ></property>
		<property name="javaMailProperties">
			<props>
				<!-- 开启认证,会检验发件人是否为邮件的用户 -->
				<prop key="mail.smtp.auth">true</prop>
				<!-- 采用ssl安全方式 -->
				<prop key="mail.smtp.ssl.enable">true</prop>
			</props>
		</property>
	</bean>
	<bean id="mailUtil" class="com.itzheng.erp.util.MailUtil" >
		 <property name="sender" ref="javaMailSender"></property>
        <property name="from" value="[email protected]"></property>
	</bean>
</beans>
(4)编写业务逻辑类

给IStoredetailBiz和StoredetailBiz添加sendStoreAlertMail方法
IStoredetailBiz
在这里插入图片描述
StoredetailBiz
在这里插入图片描述

private MailUtil mailUtil;
	public void setMailUtil(MailUtil mailUtil) {
    
    
		this.mailUtil = mailUtil;
	}
	private String toAddress;// 收件箱
	private String subject;// 主题
	private String text;// 正文
	public void setToAddress(String toAddress) {
    
    
		this.toAddress = toAddress;
	}
	public void setSubject(String subject) {
    
    
		this.subject = subject;
	}
	public void setText(String text) {
    
    
		this.text = text;
	}
	/**
	 * 发送库存预警邮件
	 * 
	 * @throws MessagingException
	 */
	public void sendStoreAlertMail() throws MessagingException {
    
    
		// 得到库存预警列表
		List<Storealert> storeAlertList = storedetailDao.getStorealertList();
		if (storeAlertList.size() > 0) {
    
    
			mailUtil.sendMail(toAddress,
					subject.replace("[time]", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())),
					text.replace("[count]", String.valueOf(storeAlertList.size())));
		} else {
    
    
			throw new ErpException("没有库存预警信息");
		}
	}
(5)修改applicationContext_biz.xml

storedeatilBiz节点,添加
在这里插入图片描述

(6)编写action代码

修改StoredetailAction,添加代码
在这里插入图片描述

(7)编写js代码

修改storealert.js
在这里插入图片描述

4、Quartz定时发送预警邮件

(1)需求以及实现思路

定时查询库存预警信息,一旦存在库存预警的商品,侧发送邮件通知相关工作人员

(2)Quart框架

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中。
Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。
它提供了巨大的灵活性而不牺牲简单性。
你能够用它来为执行一个作业而创建简单的或复杂的调度。

1)Job
表示一个任务(工作),要执行的具体内容。

2)JobDetail JobDetail
表示一个具体的可执行的调度程序,Job 是这个可执行程调度程序所要 执行的内容,另外
JobDetail 还包含了这个任务调度的方案和策略。
告诉调度容器,将来执行哪个类(job)的哪个方法

3)Trigger
是一个类,代表一个调度参数的配置,描述触发Job执行的时间触发规则。
一个Job可以对应多个Trigger,但一个Trigger只能对应一个Job

4)Scheduler 代表一个调度容器,一个调度容器中可以注册多个 JobDetail 和Trigger。
Scheduler可以将Trigger绑定到某一JobDetail中,这样当Trigger触发时,对应的Job就被执行。
Note: 当JobDetail和Trigger在scheduler容器上注册后,形成了装配好的作业(JobDetail和Trigger所组成的一对儿),就可以伴随容器启动而调度执行了。

5、代码实现

(1)添加quartz依赖

打开父工程的pom.xml,添加quartz依赖:
在这里插入图片描述

(2)创建任务类

在业务逻辑层中新建MailJob类,包名为com.itzheng.erp.job
在这里插入图片描述

(3)创建applicationContext_job.xml注入

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd
		">
	<!-- 发送邮件任务类 -->
	<bean id="mailJob" class="com.itzheng.erp.job.MailJob">
		<property name="storedetailBiz" ref="storedetailBiz"></property>
	</bean>
	<!-- 发送库存报警邮件任务 -->
	<bean id="sendStoreAlertMailJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="mailJob"></property>
		<property name="targetMethod" value="sendStoreAlertMail"></property>
	</bean>
	<!-- 触发器 -->
	<bean id="mailTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="sendStoreAlertMailJobDetail"></property>
		<!-- cron 表达式 (时间点的描述) 每天的 上午 12:20 和下午 16:20 的时间 执行 -->
		<property name="cronExpression" value="0 17 12,16 * * ?"></property>
	</bean>
	<!-- 总管理容器 -->
	<bean id="startQuartz"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="mailTrigger" />
			</list>
		</property>
	</bean>
</beans>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44757034/article/details/111320079