Browse Source

bug处理

jwy_category
review512jwy@163.com 1 month ago
parent
commit
58fe68a9ab
  1. 101
      src/main/java/com/techsor/datacenter/sender/dao/DashboardAlertDao.java
  2. 4
      src/main/java/com/techsor/datacenter/sender/dto/DeviceAlertInfo.java
  3. 4
      src/main/java/com/techsor/datacenter/sender/dto/DeviceInfoVO.java

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

@ -1,5 +1,6 @@
package com.techsor.datacenter.sender.dao; package com.techsor.datacenter.sender.dao;
import com.techsor.datacenter.sender.config.DataSourceContextHolder;
import com.techsor.datacenter.sender.disruptor.AlertEvent; import com.techsor.datacenter.sender.disruptor.AlertEvent;
import com.techsor.datacenter.sender.entitiy.AlertHistoryDTO; import com.techsor.datacenter.sender.entitiy.AlertHistoryDTO;
import com.techsor.datacenter.sender.entitiy.DynamodbEntity; import com.techsor.datacenter.sender.entitiy.DynamodbEntity;
@ -9,6 +10,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -63,49 +66,61 @@ public class DashboardAlertDao {
} }
public void batchUpsertRawData(List<AlertEvent> list) { public void batchUpsertRawData(List<AlertEvent> list) {
jdbcTemplate.batchUpdate( // 按 companyId 分组
"INSERT INTO device_rawdata_realtime (" + Map<Long, List<AlertEvent>> grouped = list.stream()
"device_id, building_id, status, receive_ts, alert_title, alert_content, " + .filter(e -> e.getEntity() != null && e.getEntity().getCompanyId() != null)
"alert_cancel_title, alert_cancel_content, raw_data, upload_year, upload_month, upload_day) " + .collect(Collectors.groupingBy(e -> e.getEntity().getCompanyId()));
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE " + grouped.forEach((companyId, subList) -> {
"building_id = VALUES(building_id), " + String dsKey = "dataSourceForCompany_" + companyId;
"status = VALUES(status), " + try {
"receive_ts = VALUES(receive_ts), " + DataSourceContextHolder.setCurrentDataSourceKey(dsKey);
"alert_title = VALUES(alert_title), " +
"alert_content = VALUES(alert_content), " + // 批量执行 upsert
"alert_cancel_title = VALUES(alert_cancel_title), " + jdbcTemplate.batchUpdate(
"alert_cancel_content = VALUES(alert_cancel_content), " + "INSERT INTO device_rawdata_realtime (" +
"raw_data = VALUES(raw_data), " + "device_id, building_id, status, receive_ts, alert_title, alert_content, alert_cancel_title," +
"upload_year = VALUES(upload_year), " + "alert_cancel_content, raw_data, upload_year, upload_month, upload_day) " +
"upload_month = VALUES(upload_month), " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " +
"upload_day = VALUES(upload_day)", "ON DUPLICATE KEY UPDATE " +
new BatchPreparedStatementSetter() { "building_id = VALUES(building_id), " +
"status = VALUES(status), " +
@Override "receive_ts = VALUES(receive_ts)," +
public void setValues(PreparedStatement ps, int i) throws SQLException { "alert_title = VALUES(alert_title)," +
AlertEvent e = list.get(i); "alert_content = VALUES(alert_content)," +
"alert_cancel_title = VALUES(alert_cancel_title)," +
ps.setString(1, e.getEntity().getDeviceId()); "alert_cancel_content = VALUES(alert_cancel_content)," +
ps.setLong(2, e.getEntity().getDbBuildingId()); "raw_data = VALUES(raw_data)," +
ps.setString(3, e.getEntity().getStatus()); "upload_year = VALUES(upload_year)," +
ps.setLong(4, e.getEntity().getReceive_ts()); "upload_month = VALUES(upload_month)," +
ps.setString(5, e.getEntity().getAlertTitle()); "upload_day = VALUES(upload_day)",
ps.setString(6, e.getEntity().getAlertContent()); new BatchPreparedStatementSetter() {
ps.setString(7, e.getEntity().getAlertCancelTitle());
ps.setString(8, e.getEntity().getAlertCancelContent()); public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(9, e.getEntity().getRawData()); DynamodbEntity entity = subList.get(i).getEntity();
ps.setInt(10, Integer.parseInt(e.getEntity().getYearKey())); ps.setString(1, entity.getDeviceId());
ps.setInt(11, Integer.parseInt(e.getEntity().getMonthKey())); ps.setLong(2, entity.getDbBuildingId());
ps.setInt(12, Integer.parseInt(e.getEntity().getDayKey())); ps.setString(3, entity.getStatus());
} ps.setLong(4, entity.getReceive_ts());
ps.setString(5, entity.getAlertTitle());
@Override ps.setString(6, entity.getAlertContent());
public int getBatchSize() { ps.setString(7, entity.getAlertCancelTitle());
return list.size(); ps.setString(8, entity.getAlertCancelContent());
} ps.setString(9, entity.getRawData());
} ps.setInt(10, Integer.parseInt(entity.getYearKey()));
); ps.setInt(11, Integer.parseInt(entity.getMonthKey()));
ps.setInt(12, Integer.parseInt(entity.getDayKey()));
}
public int getBatchSize() {
return subList.size();
}
}
);
} finally {
DataSourceContextHolder.clearCurrentDataSourceKey();
}
});
} }
public void insertAlertHistory(DynamodbEntity entity) { public void insertAlertHistory(DynamodbEntity entity) {

4
src/main/java/com/techsor/datacenter/sender/dto/DeviceAlertInfo.java

@ -3,8 +3,10 @@ package com.techsor.datacenter.sender.dto;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
@Data @Data
public class DeviceAlertInfo { public class DeviceAlertInfo implements Serializable {
private Integer id; private Integer id;
private Long deviceConfigId; private Long deviceConfigId;

4
src/main/java/com/techsor/datacenter/sender/dto/DeviceInfoVO.java

@ -2,12 +2,14 @@ package com.techsor.datacenter.sender.dto;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
/** /**
* @author Mr.Jiang * @author Mr.Jiang
* @time 2022年7月21日 下午8:50:31 * @time 2022年7月21日 下午8:50:31
*/ */
@Data @Data
public class DeviceInfoVO{ public class DeviceInfoVO implements Serializable {
private String deviceId; private String deviceId;

Loading…
Cancel
Save