diff --git a/src/main/java/com/cm/tenlion/pollutantdata/PollutantDataApplication.java b/src/main/java/com/cm/tenlion/pollutantdata/PollutantDataApplication.java index 0f0b58b..a23d22a 100644 --- a/src/main/java/com/cm/tenlion/pollutantdata/PollutantDataApplication.java +++ b/src/main/java/com/cm/tenlion/pollutantdata/PollutantDataApplication.java @@ -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) -> { diff --git a/src/main/java/com/cm/tenlion/pollutantdata/dao/alarmlog/IAlarmLogDao.java b/src/main/java/com/cm/tenlion/pollutantdata/dao/alarmlog/IAlarmLogDao.java index 94d263a..164673c 100644 --- a/src/main/java/com/cm/tenlion/pollutantdata/dao/alarmlog/IAlarmLogDao.java +++ b/src/main/java/com/cm/tenlion/pollutantdata/dao/alarmlog/IAlarmLogDao.java @@ -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 params) throws SearchException; + + /** + * 统计企业报警次数 + * + * @param params + * @return + * @throws SearchException + */ + List listEnterpriseCount(Map params) throws SearchException; + + /** + * 保存通知 + * + * @param params + * @throws SaveException + */ + void saveNotice(Map params) throws SaveException; + + /** + * 通知企业ID列表 + * + * @param params + * @return + * @throws SearchException + */ + List listNoticeEnterpriseId(Map params) throws SearchException; } diff --git a/src/main/java/com/cm/tenlion/pollutantdata/pojo/bos/alarmlog/AlarmLogCountBO.java b/src/main/java/com/cm/tenlion/pollutantdata/pojo/bos/alarmlog/AlarmLogCountBO.java new file mode 100644 index 0000000..75e2cfe --- /dev/null +++ b/src/main/java/com/cm/tenlion/pollutantdata/pojo/bos/alarmlog/AlarmLogCountBO.java @@ -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; + +} diff --git a/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/IAlarmLogService.java b/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/IAlarmLogService.java index 947781b..74f1ee3 100644 --- a/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/IAlarmLogService.java +++ b/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/IAlarmLogService.java @@ -64,4 +64,9 @@ public interface IAlarmLogService { * @throws SearchException */ Integer count(String day) throws SearchException; + + /** + * 通知超时 + */ + void alarmEnterpriseNotice(); } diff --git a/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/impl/AlarmLogServiceImpl.java b/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/impl/AlarmLogServiceImpl.java index 5f47566..2eddfcf 100644 --- a/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/impl/AlarmLogServiceImpl.java +++ b/src/main/java/com/cm/tenlion/pollutantdata/service/alarmlog/impl/AlarmLogServiceImpl.java @@ -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 params = getHashMap(2); + params.put("day", day); + List alarmLogCountBOs = alarmLogDao.listEnterpriseCount(params); + if (alarmLogCountBOs.isEmpty()) { + return; + } + List enterpriseIds = listNoticeEnterpriseId(alarmLogCountBOs, params); + if (enterpriseIds.isEmpty()) { + return; + } + // 发送通知 + sendNoticeTask(enterpriseIds); + // 保存通知记录 + saveAlarmNotice(enterpriseIds); + } + + /** + * 需要通知的企业ID + * + * @param alarmLogCountBOs + * @param params + * @return + */ + private List listNoticeEnterpriseId(List alarmLogCountBOs, Map params) { + // 得到当日超过预警次数的企业 + List enterpriseIds = new ArrayList<>(); + for (AlarmLogCountBO alarmLogCountBO : alarmLogCountBOs) { + if (alarmLogCountBO.getCount() <= alarmNoticeLimit) { + continue; + } + enterpriseIds.add(alarmLogCountBO.getEnterpriseId()); + } + // 已经通知的企业 + List 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 enterpriseIds) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + JSONObject body = new JSONObject(); + body.put("enterpriseIds", enterpriseIds); + HttpEntity 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 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 enterpriseIds) { + Map params = getHashMap(6); + for (String enterpriseId : enterpriseIds) { + params.put("alarmNoticeId", UUIDUtil.getUUID()); + params.put("enterpriseId", enterpriseId); + params.put("gmtCreate", DateUtil.getTime()); + alarmLogDao.saveNotice(params); + } + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index cd9b632..8dff3f4 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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 diff --git a/src/main/resources/mybatis/mapper/alarmlog/alarm-log-mapper.xml b/src/main/resources/mybatis/mapper/alarmlog/alarm-log-mapper.xml index 5585224..d3c09ed 100644 --- a/src/main/resources/mybatis/mapper/alarmlog/alarm-log-mapper.xml +++ b/src/main/resources/mybatis/mapper/alarmlog/alarm-log-mapper.xml @@ -17,6 +17,11 @@ + + + + + INSERT INTO pollute_alarm_log ( @@ -89,4 +94,48 @@ + + + + + + INSERT INTO pollute_alarm_notice( + alarm_notice_id, + enterprise_id, + gmt_create + ) VALUES( + #{alarmNoticeId}, + #{enterpriseId}, + #{gmtCreate} + ) + + + + + \ No newline at end of file diff --git a/src/main/resources/static/route/bigdata/style1/index.html b/src/main/resources/static/route/bigdata/style1/index.html index f1a2faf..7c89056 100755 --- a/src/main/resources/static/route/bigdata/style1/index.html +++ b/src/main/resources/static/route/bigdata/style1/index.html @@ -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; }