Browse Source

Merge branch 'master' into zhczh_c

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

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

@ -74,58 +74,73 @@ 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);
// 批量执行 upsert // 再按 retainAlert 分组(关键)
StringBuilder sql = new StringBuilder( Map<Integer, List<AlertEvent>> retainGroup =
"INSERT INTO device_rawdata_realtime (" + companyList.stream()
"device_id, building_id, status, dashboard_status, receive_ts, " + .collect(Collectors.groupingBy(
"alert_title, alert_content, alert_cancel_title, alert_cancel_content, " + e -> e.getEntity().getRetainAlert()
"raw_data, upload_year, upload_month, upload_day) VALUES " ));
);
List<Object> params = new ArrayList<>(); // 每个 retainAlert 执行一次 batch upsert
for (int i = 0; i < subList.size(); i++) { retainGroup.forEach((retainAlert, subList) -> {
DynamodbEntity entity = subList.get(i).getEntity(); if (subList.isEmpty()) {
return;
sql.append("(?,?,?,?,?,?,?,?,?,?,?,?,?)");
if (i < subList.size() - 1) {
sql.append(",");
} }
params.add(entity.getDeviceId()); // 批量执行 upsert
params.add(entity.getDbBuildingId()); StringBuilder sql = new StringBuilder(
params.add(entity.getStatus()); "INSERT INTO device_rawdata_realtime (" +
params.add(entity.getStatus()); "device_id, building_id, status, dashboard_status, receive_ts, " +
params.add(entity.getReceive_ts()); "alert_title, alert_content, alert_cancel_title, alert_cancel_content, " +
params.add(entity.getAlertTitle()); "raw_data, upload_year, upload_month, upload_day) VALUES "
params.add(entity.getAlertContent()); );
params.add(entity.getAlertCancelTitle()); List<Object> params = new ArrayList<>();
params.add(entity.getAlertCancelContent()); for (int i = 0; i < subList.size(); i++) {
params.add(entity.getRawData()); DynamodbEntity entity = subList.get(i).getEntity();
params.add(Integer.parseInt(entity.getYearKey()));
params.add(Integer.parseInt(entity.getMonthKey())); sql.append("(?,?,?,?,?,?,?,?,?,?,?,?,?)");
params.add(Integer.parseInt(entity.getDayKey())); if (i < subList.size() - 1) {
//给下面dashboard_status = IF(? = 1 里的?用 sql.append(",");
params.add(entity.getRetainAlert()); }
}
sql.append(" ON DUPLICATE KEY UPDATE ") params.add(entity.getDeviceId());
.append("building_id = VALUES(building_id), ") params.add(entity.getDbBuildingId());
.append("status = VALUES(status), ") params.add(entity.getStatus());
.append("receive_ts = VALUES(receive_ts), ") params.add(entity.getStatus());
.append("alert_title = VALUES(alert_title), ") params.add(entity.getReceive_ts());
.append("alert_content = VALUES(alert_content), ") params.add(entity.getAlertTitle());
.append("alert_cancel_title = VALUES(alert_cancel_title), ") params.add(entity.getAlertContent());
.append("alert_cancel_content = VALUES(alert_cancel_content), ") params.add(entity.getAlertCancelTitle());
.append("raw_data = VALUES(raw_data), ") params.add(entity.getAlertCancelContent());
.append("upload_year = VALUES(upload_year), ") params.add(entity.getRawData());
.append("upload_month = VALUES(upload_month), ") params.add(Integer.parseInt(entity.getYearKey()));
.append("upload_day = VALUES(upload_day), ") params.add(Integer.parseInt(entity.getMonthKey()));
.append("dashboard_status = IF(? = 1 AND VALUES(status) = 'alert_cancel' AND dashboard_status = 'alert', dashboard_status, VALUES(status))"); params.add(Integer.parseInt(entity.getDayKey()));
jdbcTemplate.update(sql.toString(), params.toArray()); }
// ON DUPLICATE KEY UPDATE(retainAlert 只绑定一次)
sql.append(" ON DUPLICATE KEY UPDATE ")
.append("building_id = VALUES(building_id), ")
.append("status = VALUES(status), ")
.append("receive_ts = VALUES(receive_ts), ")
.append("alert_title = VALUES(alert_title), ")
.append("alert_content = VALUES(alert_content), ")
.append("alert_cancel_title = VALUES(alert_cancel_title), ")
.append("alert_cancel_content = VALUES(alert_cancel_content), ")
.append("raw_data = VALUES(raw_data), ")
.append("upload_year = VALUES(upload_year), ")
.append("upload_month = VALUES(upload_month), ")
.append("upload_day = VALUES(upload_day), ")
.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());
});
} finally { } finally {
DataSourceContextHolder.clearCurrentDataSourceKey(); DataSourceContextHolder.clearCurrentDataSourceKey();

Loading…
Cancel
Save