完成任务联动

This commit is contained in:
wanggeng888 2021-03-27 00:04:05 +08:00
parent cd6377eae5
commit 805cc08607
8 changed files with 224 additions and 2 deletions

View File

@ -41,11 +41,22 @@ public class PollutantDataApplication {
app.run(args);
}
/**
*
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void clearOfflineCollector() {
CollectorManager.getInstance().clearTimeoutCollectors();
}
/**
* 预警通知网格员
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void alarmEnterpriseNotice() {
alarmLogService.alarmEnterpriseNotice();
}
@Bean
public ApplicationRunner applicationRunner() {
return (args) -> {

View File

@ -2,6 +2,7 @@ package com.cm.tenlion.pollutantdata.dao.alarmlog;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.tenlion.pollutantdata.pojo.bos.alarmlog.AlarmLogCountBO;
import com.cm.tenlion.pollutantdata.pojo.dtos.alarmlog.AlarmLogDTO;
import org.springframework.stereotype.Repository;
@ -46,4 +47,30 @@ public interface IAlarmLogDao {
* @throws SearchException
*/
Integer count(Map<String, Object> params) throws SearchException;
/**
* 统计企业报警次数
*
* @param params
* @return
* @throws SearchException
*/
List<AlarmLogCountBO> listEnterpriseCount(Map<String, Object> params) throws SearchException;
/**
* 保存通知
*
* @param params
* @throws SaveException
*/
void saveNotice(Map<String, Object> params) throws SaveException;
/**
* 通知企业ID列表
*
* @param params
* @return
* @throws SearchException
*/
List<String> listNoticeEnterpriseId(Map<String, Object> params) throws SearchException;
}

View File

@ -0,0 +1,23 @@
package com.cm.tenlion.pollutantdata.pojo.bos.alarmlog;
import io.swagger.annotations.ApiModel;
import lombok.Data;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: AlarmLogCountBO
* @Description: 报警日志统计
* @Author: wanggeng
* @Date: 2021/3/26 5:12 下午
* @Version: 1.0
*/
@Data
@ApiModel
public class AlarmLogCountBO {
private String enterpriseId;
private Integer count;
}

View File

@ -64,4 +64,9 @@ public interface IAlarmLogService {
* @throws SearchException
*/
Integer count(String day) throws SearchException;
/**
* 通知超时
*/
void alarmEnterpriseNotice();
}

View File

@ -1,21 +1,30 @@
package com.cm.tenlion.pollutantdata.service.alarmlog.impl;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.DateUtil;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.tenlion.pollutantdata.dao.alarmlog.IAlarmLogDao;
import com.cm.tenlion.pollutantdata.pojo.bos.alarmlog.AlarmLogCountBO;
import com.cm.tenlion.pollutantdata.pojo.dtos.alarmlog.AlarmLogDTO;
import com.cm.tenlion.pollutantdata.pojo.vos.alarmlog.AlarmLogVO;
import com.cm.tenlion.pollutantdata.service.alarmlog.IAlarmLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -32,6 +41,10 @@ import java.util.Map;
@Service
public class AlarmLogServiceImpl extends AbstractService implements IAlarmLogService {
@Value("${api-path.inspection}")
private String inspectionUrl;
@Value("${system.alarm-notice-limit:5}")
private Integer alarmNoticeLimit;
@Autowired
private IAlarmLogDao alarmLogDao;
@ -69,5 +82,96 @@ public class AlarmLogServiceImpl extends AbstractService implements IAlarmLogSer
return count(params);
}
@Override
public void alarmEnterpriseNotice() {
String day = DateUtil.getDay();
Map<String, Object> params = getHashMap(2);
params.put("day", day);
List<AlarmLogCountBO> alarmLogCountBOs = alarmLogDao.listEnterpriseCount(params);
if (alarmLogCountBOs.isEmpty()) {
return;
}
List<String> enterpriseIds = listNoticeEnterpriseId(alarmLogCountBOs, params);
if (enterpriseIds.isEmpty()) {
return;
}
// 发送通知
sendNoticeTask(enterpriseIds);
// 保存通知记录
saveAlarmNotice(enterpriseIds);
}
/**
* 需要通知的企业ID
*
* @param alarmLogCountBOs
* @param params
* @return
*/
private List<String> listNoticeEnterpriseId(List<AlarmLogCountBO> alarmLogCountBOs, Map<String, Object> params) {
// 得到当日超过预警次数的企业
List<String> enterpriseIds = new ArrayList<>();
for (AlarmLogCountBO alarmLogCountBO : alarmLogCountBOs) {
if (alarmLogCountBO.getCount() <= alarmNoticeLimit) {
continue;
}
enterpriseIds.add(alarmLogCountBO.getEnterpriseId());
}
// 已经通知的企业
List<String> noticeEnterpriseIds = alarmLogDao.listNoticeEnterpriseId(params);
// 去除已经通知过的
for (int i = 0; i < enterpriseIds.size(); i++) {
for (String noticeEnterpriseId : noticeEnterpriseIds) {
if (StringUtils.equals(enterpriseIds.get(i), noticeEnterpriseId)) {
enterpriseIds.remove(i);
i--;
break;
}
}
}
return enterpriseIds;
}
/**
* 发送任务通知
*
* @param enterpriseIds
*/
private void sendNoticeTask(List<String> enterpriseIds) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
JSONObject body = new JSONObject();
body.put("enterpriseIds", enterpriseIds);
HttpEntity<String> httpEntity = new HttpEntity<>(body.toJSONString(), httpHeaders);
String token = ClientTokenManager.getInstance().getClientToken().getAccessToken();
RestTemplate restTemplate = new RestTemplate();
String url = String.format("%s/resource/taskcheck/v2/save-pollutant-task?access_token=%s",
inspectionUrl, token);
try {
ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
if (response.getStatusCode().value() == HttpStatus.OK.value()) {
LOG.debug("下发任务成功");
}
} catch (HttpClientErrorException e) {
LOG.error(e.getMessage(), e);
}
}
/**
* 保存通知记录
*
* @param enterpriseIds
*/
private void saveAlarmNotice(List<String> enterpriseIds) {
Map<String, Object> params = getHashMap(6);
for (String enterpriseId : enterpriseIds) {
params.put("alarmNoticeId", UUIDUtil.getUUID());
params.put("enterpriseId", enterpriseId);
params.put("gmtCreate", DateUtil.getTime());
alarmLogDao.saveNotice(params);
}
}
}

View File

@ -58,11 +58,14 @@ spring:
api-path:
user-center: ${security.oauth2.oauth-server}
inspection: http://192.168.0.106:7006/inspection
system:
# 预警通知上限
alarm-notice-limit: 5
# 安全
security:
oauth2:
oauth-server: http://192.168.0.103:7001/usercenter
oauth-server: http://192.168.0.106:7001/usercenter
oauth-logout: ${security.oauth2.oauth-server}/logout?redirect_uri=${server.url}
client:
client-id: 74e4b55ad48840f9b4de86ce5da58b53

View File

@ -17,6 +17,11 @@
<result column="gmt_create" property="gmtCreate"/>
</resultMap>
<resultMap id="alarmLogCountBO" type="com.cm.tenlion.pollutantdata.pojo.bos.alarmlog.AlarmLogCountBO">
<result column="enterprise_id" property="enterpriseId"/>
<result column="count" property="count"/>
</resultMap>
<!-- 新增 -->
<insert id="save" parameterType="map">
INSERT INTO pollute_alarm_log (
@ -89,4 +94,48 @@
</if>
</select>
<!-- 统计企业报警次数 -->
<select id="listEnterpriseCount" parameterType="map" resultMap="alarmLogCountBO">
SELECT
enterprise_id,
COUNT(enterprise_id) count
FROM
pollute_alarm_log
WHERE
1 = 1
<if test="day != null and day != ''">
AND
LEFT(gmt_create, 10) = #{day}
</if>
GROUP BY
enterprise_id
</select>
<!-- 保存通知 -->
<insert id="saveNotice" parameterType="map">
INSERT INTO pollute_alarm_notice(
alarm_notice_id,
enterprise_id,
gmt_create
) VALUES(
#{alarmNoticeId},
#{enterpriseId},
#{gmtCreate}
)
</insert>
<!-- 通知企业ID列表 -->
<select id="listNoticeEnterpriseId" parameterType="map" resultType="java.lang.String">
SELECT
enterprise_id
FROM
pollute_alarm_notice
WHERE
1 = 1
<if test="day != null and day != ''">
AND
LEFT(gmt_create, 10) = #{day}
</if>
</select>
</mapper>

View File

@ -228,7 +228,7 @@
enterpriseId: self.enterpriseId
}, null, function(code, data) {
self.enterprisePollIds = data;
if(self.enterprisePollIds.size == 0) {
if(self.enterprisePollIds.length == 0) {
return;
}