|
|
@ -11,6 +11,7 @@ import java.math.BigDecimal; |
|
|
import java.sql.PreparedStatement; |
|
|
import java.sql.PreparedStatement; |
|
|
import java.sql.SQLException; |
|
|
import java.sql.SQLException; |
|
|
import java.sql.Types; |
|
|
import java.sql.Types; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -85,69 +86,67 @@ public class DashboardStatisticsDao { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void measureBatchInsert(List<MeasureEvent> list) { |
|
|
public void measureBatchInsert(List<MeasureEvent> list) { |
|
|
|
|
|
if (list.isEmpty()) return; |
|
|
|
|
|
|
|
|
// 批量 insert
|
|
|
StringBuilder insertSql = new StringBuilder( |
|
|
auroraJdbcTemplate.batchUpdate( |
|
|
|
|
|
"INSERT INTO dashboard_record_measure (" + |
|
|
"INSERT INTO dashboard_record_measure (" + |
|
|
"device_id, date_year, date_month, date_day, date_hour, date_minute, date_second, upload_value, upload_at, attr_code) " + |
|
|
"device_id, date_year, date_month, date_day, date_hour, date_minute, date_second, " + |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
|
|
"upload_value, upload_at, attr_code) VALUES " |
|
|
new BatchPreparedStatementSetter() { |
|
|
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
|
|
|
MeasureEvent e = list.get(i); |
|
|
|
|
|
StatisticsMeasureInfo info = e.getInfo(); |
|
|
|
|
|
ps.setString(1, e.getDeviceId()); |
|
|
|
|
|
ps.setInt(2, info.getYearKey()); |
|
|
|
|
|
ps.setInt(3, info.getMonthKey()); |
|
|
|
|
|
ps.setInt(4, info.getDayKey()); |
|
|
|
|
|
ps.setInt(5, info.getHourKey()); |
|
|
|
|
|
ps.setInt(6, info.getMinuteKey()); |
|
|
|
|
|
ps.setInt(7, info.getSecondKey()); |
|
|
|
|
|
ps.setString(8, e.getUploadValue()); |
|
|
|
|
|
ps.setLong(9, info.getUploadAt()); |
|
|
|
|
|
ps.setString(10, e.getAttrCode()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getBatchSize() { |
|
|
|
|
|
return list.size(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
); |
|
|
|
|
|
List<Object> insertParams = new ArrayList<>(list.size() * 10); |
|
|
// 批量 upsert
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
auroraJdbcTemplate.batchUpdate( |
|
|
MeasureEvent e = list.get(i); |
|
|
|
|
|
StatisticsMeasureInfo info = e.getInfo(); |
|
|
|
|
|
|
|
|
|
|
|
insertSql.append("(?,?,?,?,?,?,?,?,?,?)"); |
|
|
|
|
|
if (i < list.size() - 1) insertSql.append(","); |
|
|
|
|
|
|
|
|
|
|
|
insertParams.add(e.getDeviceId()); |
|
|
|
|
|
insertParams.add(info.getYearKey()); |
|
|
|
|
|
insertParams.add(info.getMonthKey()); |
|
|
|
|
|
insertParams.add(info.getDayKey()); |
|
|
|
|
|
insertParams.add(info.getHourKey()); |
|
|
|
|
|
insertParams.add(info.getMinuteKey()); |
|
|
|
|
|
insertParams.add(info.getSecondKey()); |
|
|
|
|
|
insertParams.add(e.getUploadValue()); |
|
|
|
|
|
insertParams.add(info.getUploadAt()); |
|
|
|
|
|
insertParams.add(e.getAttrCode()); |
|
|
|
|
|
} |
|
|
|
|
|
auroraJdbcTemplate.update(insertSql.toString(), insertParams.toArray()); |
|
|
|
|
|
|
|
|
|
|
|
// 实时表
|
|
|
|
|
|
StringBuilder upsertSql = new StringBuilder( |
|
|
"INSERT INTO dashboard_realtime_measure (" + |
|
|
"INSERT INTO dashboard_realtime_measure (" + |
|
|
"device_id, date_year, date_month, date_day, date_hour, date_minute, date_second," + |
|
|
"device_id, date_year, date_month, date_day, date_hour, date_minute, date_second, " + |
|
|
"upload_value, min_value, max_value, upload_at, attr_code) " + |
|
|
"upload_value, min_value, max_value, upload_at, attr_code) VALUES " |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " + |
|
|
|
|
|
"ON DUPLICATE KEY UPDATE " + |
|
|
|
|
|
"upload_value = VALUES(upload_value), " + |
|
|
|
|
|
"min_value = VALUES(min_value)," + |
|
|
|
|
|
"max_value = VALUES(max_value)," + |
|
|
|
|
|
"upload_at = VALUES(upload_at)", |
|
|
|
|
|
new BatchPreparedStatementSetter() { |
|
|
|
|
|
|
|
|
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
|
|
|
MeasureEvent e = list.get(i); |
|
|
|
|
|
StatisticsMeasureInfo info = e.getInfo(); |
|
|
|
|
|
ps.setString(1, e.getDeviceId()); |
|
|
|
|
|
ps.setInt(2, info.getYearKey()); |
|
|
|
|
|
ps.setInt(3, info.getMonthKey()); |
|
|
|
|
|
ps.setInt(4, info.getDayKey()); |
|
|
|
|
|
ps.setInt(5, info.getHourKey()); |
|
|
|
|
|
ps.setInt(6, info.getMinuteKey()); |
|
|
|
|
|
ps.setInt(7, info.getSecondKey()); |
|
|
|
|
|
ps.setString(8, e.getUploadValue()); |
|
|
|
|
|
ps.setString(9, e.getMinValue().toString()); |
|
|
|
|
|
ps.setString(10, e.getMaxValue().toString()); |
|
|
|
|
|
ps.setLong(11, info.getUploadAt()); |
|
|
|
|
|
ps.setString(12, e.getAttrCode()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getBatchSize() { |
|
|
|
|
|
return list.size(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
); |
|
|
|
|
|
List<Object> upsertParams = new ArrayList<>(list.size() * 12); |
|
|
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
|
|
MeasureEvent e = list.get(i); |
|
|
|
|
|
StatisticsMeasureInfo info = e.getInfo(); |
|
|
|
|
|
|
|
|
|
|
|
upsertSql.append("(?,?,?,?,?,?,?,?,?,?,?,?)"); |
|
|
|
|
|
if (i < list.size() - 1) upsertSql.append(","); |
|
|
|
|
|
|
|
|
|
|
|
upsertParams.add(e.getDeviceId()); |
|
|
|
|
|
upsertParams.add(info.getYearKey()); |
|
|
|
|
|
upsertParams.add(info.getMonthKey()); |
|
|
|
|
|
upsertParams.add(info.getDayKey()); |
|
|
|
|
|
upsertParams.add(info.getHourKey()); |
|
|
|
|
|
upsertParams.add(info.getMinuteKey()); |
|
|
|
|
|
upsertParams.add(info.getSecondKey()); |
|
|
|
|
|
upsertParams.add(e.getUploadValue()); |
|
|
|
|
|
upsertParams.add(e.getMinValue() != null ? e.getMinValue().toString() : null); |
|
|
|
|
|
upsertParams.add(e.getMaxValue() != null ? e.getMaxValue().toString() : null); |
|
|
|
|
|
upsertParams.add(info.getUploadAt()); |
|
|
|
|
|
upsertParams.add(e.getAttrCode()); |
|
|
|
|
|
} |
|
|
|
|
|
upsertSql.append(" ON DUPLICATE KEY UPDATE ") |
|
|
|
|
|
.append("upload_value = VALUES(upload_value), ") |
|
|
|
|
|
.append("min_value = VALUES(min_value), ") |
|
|
|
|
|
.append("max_value = VALUES(max_value), ") |
|
|
|
|
|
.append("upload_at = VALUES(upload_at)"); |
|
|
|
|
|
auroraJdbcTemplate.update(upsertSql.toString(), upsertParams.toArray()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void insertDeviceAccumulateInfo(String uploadValue, String deviceId, Double incrementToday, |
|
|
public void insertDeviceAccumulateInfo(String uploadValue, String deviceId, Double incrementToday, |
|
|
@ -195,70 +194,65 @@ public class DashboardStatisticsDao { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void accumulateBatchInsert(List<AccumulateEvent> list) { |
|
|
public void accumulateBatchInsert(List<AccumulateEvent> list) { |
|
|
auroraJdbcTemplate.batchUpdate( |
|
|
if (list.isEmpty()) return; |
|
|
"INSERT INTO dashboard_record_accumulate (" + |
|
|
|
|
|
"device_id, date_year, date_month, date_day, date_hour, date_minute, date_second, " + |
|
|
|
|
|
"upload_value, increment_today, increment_minute, upload_at, attr_code) " + |
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
|
|
|
|
|
new BatchPreparedStatementSetter() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
|
|
|
AccumulateEvent e = list.get(i); |
|
|
|
|
|
StatisticsAccumulateInfo info = e.getInfo(); |
|
|
|
|
|
|
|
|
|
|
|
ps.setString(1, e.getDeviceId()); |
|
|
|
|
|
ps.setInt(2, info.getYearKey()); |
|
|
|
|
|
ps.setInt(3, info.getMonthKey()); |
|
|
|
|
|
ps.setInt(4, info.getDayKey()); |
|
|
|
|
|
ps.setInt(5, info.getHourKey()); |
|
|
|
|
|
ps.setInt(6, info.getMinuteKey()); |
|
|
|
|
|
ps.setInt(7, info.getSecondKey()); |
|
|
|
|
|
ps.setString(8, e.getUploadValue()); |
|
|
|
|
|
ps.setDouble(9, e.getIncrementToday() != null ? e.getIncrementToday() : 0.0); |
|
|
|
|
|
ps.setDouble(10, e.getIncrementMinute() != null ? e.getIncrementMinute() : 0.0); |
|
|
|
|
|
ps.setLong(11, info.getUploadAt()); |
|
|
|
|
|
ps.setString(12, e.getAttrCode()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public int getBatchSize() { |
|
|
|
|
|
return list.size(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
auroraJdbcTemplate.batchUpdate( |
|
|
StringBuilder insertSql = new StringBuilder( |
|
|
"INSERT INTO dashboard_realtime_accumulate_day (" + |
|
|
"INSERT INTO dashboard_record_accumulate " + |
|
|
"device_id, date_year, date_month, date_day, upload_value, increment_today, upload_at, attr_code) " + |
|
|
"(device_id, date_year, date_month, date_day, date_hour, date_minute, date_second, " + |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?) " + |
|
|
"upload_value, increment_today, increment_minute, upload_at, attr_code) VALUES " |
|
|
"ON DUPLICATE KEY UPDATE " + |
|
|
); |
|
|
"upload_value = VALUES(upload_value), " + |
|
|
List<Object> insertParams = new ArrayList<>(); |
|
|
"increment_today = VALUES(increment_today), " + |
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
"upload_at = VALUES(upload_at)", |
|
|
AccumulateEvent e = list.get(i); |
|
|
new BatchPreparedStatementSetter() { |
|
|
StatisticsAccumulateInfo info = e.getInfo(); |
|
|
|
|
|
|
|
|
@Override |
|
|
insertSql.append("(?,?,?,?,?,?,?,?,?,?,?,?)"); |
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
if (i < list.size() - 1) { |
|
|
AccumulateEvent e = list.get(i); |
|
|
insertSql.append(","); |
|
|
StatisticsAccumulateInfo info = e.getInfo(); |
|
|
} |
|
|
|
|
|
|
|
|
ps.setString(1, e.getDeviceId()); |
|
|
insertParams.add(e.getDeviceId()); |
|
|
ps.setInt(2, info.getYearKey()); |
|
|
insertParams.add(info.getYearKey()); |
|
|
ps.setInt(3, info.getMonthKey()); |
|
|
insertParams.add(info.getMonthKey()); |
|
|
ps.setInt(4, info.getDayKey()); |
|
|
insertParams.add(info.getDayKey()); |
|
|
ps.setString(5, e.getUploadValue()); |
|
|
insertParams.add(info.getHourKey()); |
|
|
ps.setDouble(6, e.getIncrementToday()); |
|
|
insertParams.add(info.getMinuteKey()); |
|
|
ps.setLong(7, info.getUploadAt()); |
|
|
insertParams.add(info.getSecondKey()); |
|
|
ps.setString(8, e.getAttrCode()); |
|
|
insertParams.add(e.getUploadValue()); |
|
|
} |
|
|
insertParams.add(e.getIncrementToday() != null ? e.getIncrementToday() : 0D); |
|
|
|
|
|
insertParams.add(e.getIncrementMinute() != null ? e.getIncrementMinute() : 0D); |
|
|
@Override |
|
|
insertParams.add(info.getUploadAt()); |
|
|
public int getBatchSize() { |
|
|
insertParams.add(e.getAttrCode()); |
|
|
return list.size(); |
|
|
} |
|
|
} |
|
|
auroraJdbcTemplate.update(insertSql.toString(), insertParams.toArray()); |
|
|
} |
|
|
|
|
|
|
|
|
//实时表
|
|
|
|
|
|
StringBuilder updateSql = new StringBuilder( |
|
|
|
|
|
"INSERT INTO dashboard_realtime_accumulate_day " + |
|
|
|
|
|
"(device_id, date_year, date_month, date_day, upload_value, increment_today, upload_at, attr_code) VALUES " |
|
|
); |
|
|
); |
|
|
|
|
|
List<Object> updateParams = new ArrayList<>(); |
|
|
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
|
|
AccumulateEvent e = list.get(i); |
|
|
|
|
|
StatisticsAccumulateInfo info = e.getInfo(); |
|
|
|
|
|
|
|
|
|
|
|
updateSql.append("(?,?,?,?,?,?,?,?)"); |
|
|
|
|
|
if (i < list.size() - 1) updateSql.append(","); |
|
|
|
|
|
|
|
|
|
|
|
updateParams.add(e.getDeviceId()); |
|
|
|
|
|
updateParams.add(info.getYearKey()); |
|
|
|
|
|
updateParams.add(info.getMonthKey()); |
|
|
|
|
|
updateParams.add(info.getDayKey()); |
|
|
|
|
|
updateParams.add(e.getUploadValue()); |
|
|
|
|
|
updateParams.add(e.getIncrementToday()); |
|
|
|
|
|
updateParams.add(info.getUploadAt()); |
|
|
|
|
|
updateParams.add(e.getAttrCode()); |
|
|
|
|
|
} |
|
|
|
|
|
updateSql.append(" ON DUPLICATE KEY UPDATE ") |
|
|
|
|
|
.append("upload_value = VALUES(upload_value), ") |
|
|
|
|
|
.append("increment_today = VALUES(increment_today), ") |
|
|
|
|
|
.append("upload_at = VALUES(upload_at)"); |
|
|
|
|
|
auroraJdbcTemplate.update(updateSql.toString(), updateParams.toArray()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|