Browse Source

优化邮件发送功能,新增关注设备在离线监控

master
bgy 2 months ago
parent
commit
7f29160d44
7 changed files with 149 additions and 29 deletions
  1. +7
    -0
      src/main/java/com/topsail/scheduletask/mapper/TopsailTransmitLogDao.java
  2. +40
    -0
      src/main/java/com/topsail/scheduletask/pojo/FocusDeviceOnline.java
  3. +19
    -20
      src/main/java/com/topsail/scheduletask/pojo/InformLogVo.java
  4. +54
    -4
      src/main/java/com/topsail/scheduletask/task/CheckRabbitMqScheduleTask.java
  5. +1
    -1
      src/main/resources/application.properties
  6. +7
    -4
      src/main/resources/com/topsail/scheduletask/mapper/InformLogMapper.xml
  7. +21
    -0
      src/main/resources/com/topsail/scheduletask/mapper/TopsailTransmitLogMapper.xml

+ 7
- 0
src/main/java/com/topsail/scheduletask/mapper/TopsailTransmitLogDao.java View File

@ -1,9 +1,12 @@
package com.topsail.scheduletask.mapper; package com.topsail.scheduletask.mapper;
import com.topsail.scheduletask.pojo.FocusDeviceOnline;
import com.topsail.scheduletask.pojo.TopsailTransmitLog; import com.topsail.scheduletask.pojo.TopsailTransmitLog;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
/** /**
* 信息推送日志表Dao * 信息推送日志表Dao
* *
@ -37,4 +40,8 @@ public interface TopsailTransmitLogDao {
* @return 影响行数 * @return 影响行数
*/ */
int updateTopsailTransmitLogByImei(TopsailTransmitLog topsailTransmitLog); int updateTopsailTransmitLogByImei(TopsailTransmitLog topsailTransmitLog);
List<FocusDeviceOnline> getFocusDevice(@Param("isFocus") Integer isFocus);
long getOfflineDeviceCount(@Param("imeiList") List<String> imeiList, @Param("projectId") Integer projectId);
} }

+ 40
- 0
src/main/java/com/topsail/scheduletask/pojo/FocusDeviceOnline.java View File

@ -0,0 +1,40 @@
package com.topsail.scheduletask.pojo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 通用明文数据转发对象
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class FocusDeviceOnline {
//1.平台信息
/**
* 用户ID
*/
private Integer userId;
/**
* 用户名称
*/
private String userName;
//2.设备基础信息
/**
* 项目ID
*/
private Integer projectId;
/**
* 关注的设备号
*/
private String focusImeis;
/**
* 是否关注1关注0不关注
*/
private Integer isFocus;
}

+ 19
- 20
src/main/java/com/topsail/scheduletask/pojo/InformLogVo.java View File

@ -8,12 +8,9 @@ import java.util.Date;
/** /**
*
* 信息推送日志表实体 * 信息推送日志表实体
* *
* @version
*
<pre>
* @version <pre>
* Author Version Date Changes * Author Version Date Changes
* Administrator 1.0 2021年11月24日 Created * Administrator 1.0 2021年11月24日 Created
* *
@ -25,38 +22,40 @@ public class InformLogVo implements Serializable {
private static final long serialVersionUID = 6509367370157091L; private static final long serialVersionUID = 6509367370157091L;
/** /**
*id
*/
* id
*/
private Integer id; private Integer id;
/** /**
*推送方式
*/
* 推送方式
*/
private String informType; private String informType;
/** /**
*推送地址
*/
* 推送地址
*/
private String informAddress; private String informAddress;
/** /**
*推送内容
*/
* 推送内容
*/
private String informData; private String informData;
/** /**
*推送结果0失败1成功
*/
* 推送结果0失败1成功
*/
private Integer state; private Integer state;
/** /**
*推送返回值记录
*/
* 推送返回值记录
*/
private String resultMsg; private String resultMsg;
/** /**
*推送场景
*/
* 推送场景
*/
private String sendScenarios; private String sendScenarios;
/** /**
*创建时间
*/
* 创建时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; private Date createTime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date belongTimeHour;
/** /**
* 开始时间 * 开始时间


+ 54
- 4
src/main/java/com/topsail/scheduletask/task/CheckRabbitMqScheduleTask.java View File

@ -1,6 +1,9 @@
package com.topsail.scheduletask.task; package com.topsail.scheduletask.task;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.topsail.scheduletask.mapper.InformLogDao;
import com.topsail.scheduletask.mapper.TopsailTransmitLogDao;
import com.topsail.scheduletask.pojo.FocusDeviceOnline;
import com.topsail.scheduletask.service.AmqpService; import com.topsail.scheduletask.service.AmqpService;
import com.topsail.scheduletask.util.MaiSenderlUtil; import com.topsail.scheduletask.util.MaiSenderlUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -11,9 +14,8 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;
@Component @Component
@Configuration //1.主要用于标记配置类兼备Component的效果 @Configuration //1.主要用于标记配置类兼备Component的效果
@ -27,6 +29,10 @@ public class CheckRabbitMqScheduleTask {
AmqpService amqpService; AmqpService amqpService;
@Value("${ignore.mq.queue}") @Value("${ignore.mq.queue}")
String ignoreQueue; String ignoreQueue;
@Autowired
TopsailTransmitLogDao topsailTransmitLogDao;
@Autowired
InformLogDao informLogDao;
//1.每个小时定时检查rabbitmq //1.每个小时定时检查rabbitmq
// @Scheduled(cron = "0 0 0/1 * * ?") // @Scheduled(cron = "0 0 0/1 * * ?")
@ -55,7 +61,51 @@ public class CheckRabbitMqScheduleTask {
amqpService.SendMessage("mailNotice", json); amqpService.SendMessage("mailNotice", json);
} }
} }
// @Scheduled(cron = "1/60 * * * * ?")
/**
* 每分钟检查设备是否在线
*/
@Scheduled(cron = "0 * * * * ?")
// @Scheduled(cron = "1/60 * * * * ?")
//1.每分钟检查设备是否在线
//或直接指定时间间隔例如5秒
// @Scheduled(fixedRate=5000)
private void checkDeviceOnline() {
try {
//1.查询需要监控的设备
List<FocusDeviceOnline> focusDeviceOnlineList = topsailTransmitLogDao.getFocusDevice(1);
if (focusDeviceOnlineList != null && focusDeviceOnlineList.size() > 0) {
for (FocusDeviceOnline focusDeviceOnline : focusDeviceOnlineList) {
String imeis = focusDeviceOnline.getFocusImeis();
List<String> imeiList = new ArrayList<>();
if (imeis != null && imeis.length() > 0) {
String[] imeisArray = imeis.split(",");
for (String imei : imeisArray) {
imeiList.add(imei);
}
}
//2.查询设备离线数量
long count = topsailTransmitLogDao.getOfflineDeviceCount(imeiList, focusDeviceOnline.getProjectId());
if (count > 0) {
String subject = "紧急--设备离线通知"+"-" + focusDeviceOnline.getUserName();
String today = new SimpleDateFormat("yyyy-MM-dd HH:00:00").format(new Date());
Integer logCount = informLogDao.getNewInformLogCount(subject, today);
if (logCount == null || (logCount != null && logCount < 1)) {
//3.发送邮件
String content = "客户:" + focusDeviceOnline.getUserName() + ",设备离线数量:" + count + "个,请进入系统查看原因";
maiSenderlUtil.sendMail("1129801211@qq.com", subject, content, true, null, subject);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送邮件失败");
System.out.println(e.getMessage());
}
}
// @Scheduled(cron = "1/60 * * * * ?")
// //或直接指定时间间隔例如5秒 // //或直接指定时间间隔例如5秒
//// @Scheduled(fixedRate=5000) //// @Scheduled(fixedRate=5000)
// private void checkMail() { // private void checkMail() {


+ 1
- 1
src/main/resources/application.properties View File

@ -21,7 +21,7 @@ spring.datasource.username=yunmei
spring.datasource.password=yunmei1234 spring.datasource.password=yunmei1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 启用 MyBatis 日志输出 # 启用 MyBatis 日志输出
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.type-aliases-package=com.topsail.scheduletask.pojo mybatis.type-aliases-package=com.topsail.scheduletask.pojo
mybatis.mapper-locations=classpath:com/topsail/scheduletask/mapper/*.xml mybatis.mapper-locations=classpath:com/topsail/scheduletask/mapper/*.xml
compute.schedule=0 compute.schedule=0


+ 7
- 4
src/main/resources/com/topsail/scheduletask/mapper/InformLogMapper.xml View File

@ -12,6 +12,7 @@
<result column="result_msg" jdbcType="VARCHAR" property="resultMsg" /> <result column="result_msg" jdbcType="VARCHAR" property="resultMsg" />
<result column="send_scenarios" jdbcType="VARCHAR" property="sendScenarios" /> <result column="send_scenarios" jdbcType="VARCHAR" property="sendScenarios" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="belong_time_hour" jdbcType="TIMESTAMP" property="belongTimeHour" />
</resultMap> </resultMap>
<!-- 基本列 --> <!-- 基本列 -->
@ -44,7 +45,8 @@
state, state,
result_msg, result_msg,
send_scenarios, send_scenarios,
create_time
create_time,
belong_time_hour
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
#{informLog.informType,jdbcType=VARCHAR}, #{informLog.informType,jdbcType=VARCHAR},
@ -53,7 +55,8 @@
#{informLog.state,jdbcType=INTEGER}, #{informLog.state,jdbcType=INTEGER},
#{informLog.resultMsg,jdbcType=VARCHAR}, #{informLog.resultMsg,jdbcType=VARCHAR},
#{informLog.sendScenarios,jdbcType=VARCHAR}, #{informLog.sendScenarios,jdbcType=VARCHAR},
#{informLog.createTime,jdbcType=TIMESTAMP}
#{informLog.createTime,jdbcType=TIMESTAMP},
#{informLog.belongTimeHour,jdbcType=TIMESTAMP}
</trim> </trim>
</insert> </insert>
@ -177,10 +180,10 @@
</select> </select>
<select id="getNewInformLogCount" resultType="java.lang.Integer"> <select id="getNewInformLogCount" resultType="java.lang.Integer">
SELECT SELECT
count(*)
count(1)
FROM inform_log as il FROM inform_log as il
where il.send_scenarios like concat('%',#{subject,jdbcType=VARCHAR},'%') where il.send_scenarios like concat('%',#{subject,jdbcType=VARCHAR},'%')
and il.state=1 and il.state=1
and DATE(il.create_time) = #{today,jdbcType=VARCHAR}
and il.belong_time_hour = #{today,jdbcType=TIMESTAMP}
</select> </select>
</mapper> </mapper>

+ 21
- 0
src/main/resources/com/topsail/scheduletask/mapper/TopsailTransmitLogMapper.xml View File

@ -54,6 +54,27 @@
FROM topsail_transmit_log FROM topsail_transmit_log
WHERE imei = #{imei} LIMIT 1 WHERE imei = #{imei} LIMIT 1
</select> </select>
<select id="getFocusDevice" resultType="com.topsail.scheduletask.pojo.FocusDeviceOnline">
SELECT user_id userId,
user_name userName,
project_id projectId,
focus_imeis focusImeis,
is_focus isFocus
FROM focus_device_online
WHERE is_focus = #{isFocus}
</select>
<select id="getOfflineDeviceCount" resultType="java.lang.Long">
SELECT COUNT(1)
FROM device_user
WHERE project_id = #{projectId}
AND status = '离线'
<if test="imeiList != null and imeiList.size() > 0">
AND imei IN
<foreach collection="imeiList" item="imei" open="(" separator="," close=")">
#{imei}
</foreach>
</if>
</select>
<!-- 新增记录 --> <!-- 新增记录 -->
<insert id="insertTopsailTransmitLog" parameterType="com.topsail.scheduletask.pojo.TopsailTransmitLog"> <insert id="insertTopsailTransmitLog" parameterType="com.topsail.scheduletask.pojo.TopsailTransmitLog">


Loading…
Cancel
Save