This commit is contained in:
dev01 2023-05-29 18:47:54 +08:00
commit ed35146547
2 changed files with 28 additions and 7 deletions

View File

@ -322,7 +322,7 @@ namespace BackendWorkerService.Quartz.Jobs
WHERE ROW_COUNT() = 0;"; WHERE ROW_COUNT() = 0;";
var mySql = $@"BEGIN TRANSACTION; var mySql = $@"BEGIN TRANSACTION;
IF OBJECT_ID(N'dbo.archive_water_meter_day_{dbDateName}', N'U') is null IF OBJECT_ID(N'dbo.archive_electric_meter_day_{dbDateName}', N'U') is null
BEGIN BEGIN
CREATE TABLE [dbo].[archive_electric_meter_day_{dbDateName}]( CREATE TABLE [dbo].[archive_electric_meter_day_{dbDateName}](
[device_number] [varchar](50) NOT NULL, [device_number] [varchar](50) NOT NULL,
@ -414,7 +414,25 @@ namespace BackendWorkerService.Quartz.Jobs
if (waterArchiveDayRawDatas.Count() > 0) if (waterArchiveDayRawDatas.Count() > 0)
{ {
var sql = $@" var sql = $@"
UPDATE archive_water_meter_day SET CREATE TABLE IF NOT EXISTS `archive_water_meter_day_{dbDateName}` (
`device_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`point` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`start_timestamp` datetime(6) NOT NULL,
`end_timestamp` datetime(6) NULL DEFAULT NULL,
`count_rawdata` int(11) NULL DEFAULT NULL,
`min_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`max_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`avg_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sum_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`is_complete` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '0: 1:',
`repeat_times` int(11) NULL DEFAULT 0 COMMENT '',
`fail_reason` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime(6) NULL DEFAULT NULL,
PRIMARY KEY (`device_number`, `point`, `start_timestamp`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
UPDATE archive_water_meter_day_{dbDateName} SET
count_rawdata = @count_rawdata, count_rawdata = @count_rawdata,
min_rawdata = @min_rawdata, min_rawdata = @min_rawdata,
max_rawdata = @max_rawdata, max_rawdata = @max_rawdata,
@ -429,7 +447,7 @@ namespace BackendWorkerService.Quartz.Jobs
AND start_timestamp = @start_timestamp; AND start_timestamp = @start_timestamp;
INSERT INTO archive_water_meter_day ( INSERT INTO archive_water_meter_day_{dbDateName} (
device_number, device_number,
point, point,
start_timestamp, start_timestamp,
@ -496,7 +514,7 @@ namespace BackendWorkerService.Quartz.Jobs
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason'
END END
UPDATE archive_water_meter_day SET UPDATE archive_water_meter_day_{dbDateName} SET
count_rawdata = @count_rawdata, count_rawdata = @count_rawdata,
min_rawdata = @min_rawdata, min_rawdata = @min_rawdata,
max_rawdata = @max_rawdata, max_rawdata = @max_rawdata,
@ -512,7 +530,7 @@ namespace BackendWorkerService.Quartz.Jobs
IF @@ROWCOUNT = 0 IF @@ROWCOUNT = 0
BEGIN BEGIN
INSERT INTO archive_water_meter_day ( INSERT INTO archive_water_meter_day_{dbDateName} (
device_number, device_number,
point, point,
start_timestamp, start_timestamp,

View File

@ -74,6 +74,8 @@ namespace FrontendWebApi.ApiControllers
string sqlWhere = ""; string sqlWhere = "";
string sqlGroup = ""; string sqlGroup = "";
string sqlAvgRawData = ""; string sqlAvgRawData = "";
string dbDateName = startTime.Split("-")[0].ToString().PadLeft(4, '0') + startTime.Split("-")[1].ToString().PadLeft(2, '0');
if (input.floor_tag.Count > 0) if (input.floor_tag.Count > 0)
sqlWhere = $@" and substring_index(substring_index(device_number, '_', 3), '_', -1) in @floor_tag "; sqlWhere = $@" and substring_index(substring_index(device_number, '_', 3), '_', -1) in @floor_tag ";
@ -85,7 +87,7 @@ namespace FrontendWebApi.ApiControllers
else else
sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp "; sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp ";
var table = input.tableType == "year" ? "archive_electric_meter_day" : "archive_electric_meter_" + input.tableType; var table = input.tableType == "year" ? "archive_electric_meter_month" : "archive_electric_meter_" + input.tableType + (input.tableType == "day" ? "_" + dbDateName : "");
var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null; var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null;
var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})"; var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})";
var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date"; var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date";
@ -198,6 +200,7 @@ namespace FrontendWebApi.ApiControllers
: input.tableType == "month" ? (Int32.Parse(input.startTime.Split("-")[0]) + 1) + "-01-01" : input.tableType == "month" ? (Int32.Parse(input.startTime.Split("-")[0]) + 1) + "-01-01"
: input.tableType == "year" ? (Int32.Parse(input.endTime) + 1).ToString() + "-01-01" : input.tableType == "year" ? (Int32.Parse(input.endTime) + 1).ToString() + "-01-01"
: null; : null;
string dbDateName = startTime.Split("-")[0].ToString().PadLeft(4, '0') + startTime.Split("-")[1].ToString().PadLeft(2, '0');
string sqlWhere = ""; string sqlWhere = "";
string sqlGroup = ""; string sqlGroup = "";
string sqlAvgRawData = ""; string sqlAvgRawData = "";
@ -212,7 +215,7 @@ namespace FrontendWebApi.ApiControllers
else else
sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp "; sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp ";
var table = input.tableType == "year" ? "archive_water_meter_day" : "archive_water_meter_" + input.tableType; var table = input.tableType == "year" ? "archive_electric_meter_month" : "archive_electric_meter_" + input.tableType + (input.tableType == "day" ? "_" + dbDateName : "");
var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null; var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null;
var sql = $@"set @i = -1; var sql = $@"set @i = -1;
select fd.device_number, aemm.avg_rawdata, DATE_FORMAT(fd.date, @dateFormat) as timestamp select fd.device_number, aemm.avg_rawdata, DATE_FORMAT(fd.date, @dateFormat) as timestamp