Browse Source

Merge branch 'master' into zhczh_c

zhczh_c
zhczyx@163.com 7 days ago
parent
commit
120ab69fe9
  1. 21
      src/main/java/com/techsor/datacenter/sender/dao/DashboardAlertDao.java

21
src/main/java/com/techsor/datacenter/sender/dao/DashboardAlertDao.java

@ -74,12 +74,25 @@ public class DashboardAlertDao {
.filter(e -> e.getEntity() != null && e.getEntity().getCompanyId() != null) .filter(e -> e.getEntity() != null && e.getEntity().getCompanyId() != null)
.collect(Collectors.groupingBy(e -> e.getEntity().getCompanyId())); .collect(Collectors.groupingBy(e -> e.getEntity().getCompanyId()));
grouped.forEach((companyId, subList) -> { grouped.forEach((companyId, companyList) -> {
long topCompanyId = companyInfoDao.getTopCompanyId(String.valueOf(companyId)); long topCompanyId = companyInfoDao.getTopCompanyId(String.valueOf(companyId));
String dsKey = "dataSourceForCompany_" + topCompanyId; String dsKey = "dataSourceForCompany_" + topCompanyId;
try { try {
DataSourceContextHolder.setCurrentDataSourceKey(dsKey); DataSourceContextHolder.setCurrentDataSourceKey(dsKey);
// 再按 retainAlert 分组(关键)
Map<Integer, List<AlertEvent>> retainGroup =
companyList.stream()
.collect(Collectors.groupingBy(
e -> e.getEntity().getRetainAlert()
));
// 每个 retainAlert 执行一次 batch upsert
retainGroup.forEach((retainAlert, subList) -> {
if (subList.isEmpty()) {
return;
}
// 批量执行 upsert // 批量执行 upsert
StringBuilder sql = new StringBuilder( StringBuilder sql = new StringBuilder(
"INSERT INTO device_rawdata_realtime (" + "INSERT INTO device_rawdata_realtime (" +
@ -109,9 +122,8 @@ public class DashboardAlertDao {
params.add(Integer.parseInt(entity.getYearKey())); params.add(Integer.parseInt(entity.getYearKey()));
params.add(Integer.parseInt(entity.getMonthKey())); params.add(Integer.parseInt(entity.getMonthKey()));
params.add(Integer.parseInt(entity.getDayKey())); params.add(Integer.parseInt(entity.getDayKey()));
//给下面dashboard_status = IF(? = 1 里的?用
params.add(entity.getRetainAlert());
} }
// ON DUPLICATE KEY UPDATE(retainAlert 只绑定一次)
sql.append(" ON DUPLICATE KEY UPDATE ") sql.append(" ON DUPLICATE KEY UPDATE ")
.append("building_id = VALUES(building_id), ") .append("building_id = VALUES(building_id), ")
.append("status = VALUES(status), ") .append("status = VALUES(status), ")
@ -125,7 +137,10 @@ public class DashboardAlertDao {
.append("upload_month = VALUES(upload_month), ") .append("upload_month = VALUES(upload_month), ")
.append("upload_day = VALUES(upload_day), ") .append("upload_day = VALUES(upload_day), ")
.append("dashboard_status = IF(? = 1 AND VALUES(status) = 'alert_cancel' AND dashboard_status = 'alert', dashboard_status, VALUES(status))"); .append("dashboard_status = IF(? = 1 AND VALUES(status) = 'alert_cancel' AND dashboard_status = 'alert', dashboard_status, VALUES(status))");
//retainAlert 分组了,只加一次
params.add(retainAlert);
jdbcTemplate.update(sql.toString(), params.toArray()); jdbcTemplate.update(sql.toString(), params.toArray());
});
} finally { } finally {
DataSourceContextHolder.clearCurrentDataSourceKey(); DataSourceContextHolder.clearCurrentDataSourceKey();

Loading…
Cancel
Save