diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 39c05fc..619d5c3 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -1,6 +1,7 @@ using Backend.Models; using BackendWorkerService.Services.Implement; using Dapper; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -345,160 +346,160 @@ namespace BackendWorkerService.Quartz.Jobs row.TryGetValue("@start_timestamp", out var yyyymmData); dbDateName = System.DateTime.Parse(yyyymmData.ToString()).ToString("yyyyMM"); var sql = $@"CREATE TABLE IF NOT EXISTS `archive_electric_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, - `sub_result` 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; + `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, + `sub_result` 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; - SET FOREIGN_KEY_CHECKS = 1; - UPDATE archive_electric_water_meter_day_{dbDateName} SET - count_rawdata = @count_rawdata, - min_rawdata = @min_rawdata, - max_rawdata = @max_rawdata, - sub_result = @max_rawdata - @min_rawdata, - avg_rawdata = @avg_rawdata, - sum_rawdata = @sum_rawdata, - is_complete = @is_complete, - repeat_times = @repeat_times, - fail_reason = @fail_reason, - updated_at = @updated_at - WHERE device_number = @device_number - AND point = @point - AND start_timestamp = @start_timestamp; + SET FOREIGN_KEY_CHECKS = 1; + UPDATE archive_electric_water_meter_day_{dbDateName} SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_rawdata, + sub_result = @max_rawdata - @min_rawdata, + avg_rawdata = @avg_rawdata, + sum_rawdata = @sum_rawdata, + is_complete = @is_complete, + repeat_times = @repeat_times, + fail_reason = @fail_reason, + updated_at = @updated_at + WHERE device_number = @device_number + AND point = @point + AND start_timestamp = @start_timestamp; - INSERT INTO archive_electric_water_meter_day_{dbDateName} ( - device_number, - point, - start_timestamp, - end_timestamp, - count_rawdata, - min_rawdata, - max_rawdata, - sub_result, - avg_rawdata, - sum_rawdata, - is_complete, - repeat_times, - fail_reason) - SELECT - @device_number, - @point, - @start_timestamp, - @end_timestamp, - @count_rawdata, - @min_rawdata, - @max_rawdata, - @max_rawdata - @min_rawdata, - @avg_rawdata, - @sum_rawdata, - @is_complete, - @repeat_times, - @fail_reason - WHERE ROW_COUNT() = 0; - update device set archive_lastDate = @start_timestamp, archive_lastActionDate = @updated_at - where device_number = @device_number;"; //archive_lastDate 前次完成時間 ,archive_lastActionDate 本次作業時間 + INSERT INTO archive_electric_water_meter_day_{dbDateName} ( + device_number, + point, + start_timestamp, + end_timestamp, + count_rawdata, + min_rawdata, + max_rawdata, + sub_result, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + SELECT + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @max_rawdata - @min_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason + WHERE ROW_COUNT() = 0; + update device set archive_lastDate = @start_timestamp, archive_lastActionDate = @updated_at + where device_number = @device_number;"; //archive_lastDate 前次完成時間 ,archive_lastActionDate 本次作業時間 var mySql = $@"BEGIN TRANSACTION; - IF OBJECT_ID(N'dbo.archive_electric_water_meter_day_{dbDateName}', N'U') is null - BEGIN - CREATE TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}]( - [device_number] [varchar](50) NOT NULL, - [point] [varchar](20) NOT NULL, - [start_timestamp] [datetime] NOT NULL, - [end_timestamp] [datetime] NULL, - [count_rawdata] [int] NULL, - [min_rawdata] [decimal](15, 3) NULL, - [max_rawdata] [decimal](15, 3) NULL, - [sub_result] [decimal](15, 3) NULL, - [avg_rawdata] [decimal](15, 3) NULL, - [sum_rawdata] [decimal](15, 3) NULL, - [is_complete] [tinyint] NULL, - [repeat_times] [int] NULL, - [fail_reason] [nvarchar](max) NULL, - [created_at] [datetime] NULL, - [updated_at] [datetime] NULL, - CONSTRAINT [PK_archive_electric_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED - ( - [device_number] ASC, - [point] ASC, - [start_timestamp] ASC - )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] - ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] + IF OBJECT_ID(N'dbo.archive_electric_water_meter_day_{dbDateName}', N'U') is null + BEGIN + CREATE TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}]( + [device_number] [varchar](50) NOT NULL, + [point] [varchar](20) NOT NULL, + [start_timestamp] [datetime] NOT NULL, + [end_timestamp] [datetime] NULL, + [count_rawdata] [int] NULL, + [min_rawdata] [decimal](15, 3) NULL, + [max_rawdata] [decimal](15, 3) NULL, + [sub_result] [decimal](15, 3) NULL, + [avg_rawdata] [decimal](15, 3) NULL, + [sum_rawdata] [decimal](15, 3) NULL, + [is_complete] [tinyint] NULL, + [repeat_times] [int] NULL, + [fail_reason] [nvarchar](max) NULL, + [created_at] [datetime] NULL, + [updated_at] [datetime] NULL, + CONSTRAINT [PK_archive_electric_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED + ( + [device_number] ASC, + [point] ASC, + [start_timestamp] ASC + )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] + ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] - ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] - ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] - ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_updated_at] DEFAULT (NULL) FOR [updated_at] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_updated_at] DEFAULT (NULL) FOR [updated_at] - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否完成,0:未完成 1:完成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'is_complete' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否完成,0:未完成 1:完成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'is_complete' - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重複次數' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'repeat_times' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重複次數' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'repeat_times' - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'失敗原因' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' - END + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'失敗原因' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' + END - UPDATE archive_electric_water_meter_day_{dbDateName} SET - count_rawdata = @count_rawdata, - min_rawdata = @min_rawdata, - max_rawdata = @max_rawdata, - sub_result = @max_rawdata - @min_rawdata, - avg_rawdata = @avg_rawdata, - sum_rawdata = @sum_rawdata, - is_complete = @is_complete, - repeat_times = @repeat_times, - fail_reason = @fail_reason, - updated_at = @updated_at - WHERE device_number = @device_number - AND point = @point - AND start_timestamp = @start_timestamp; + UPDATE archive_electric_water_meter_day_{dbDateName} SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_rawdata, + sub_result = @max_rawdata - @min_rawdata, + avg_rawdata = @avg_rawdata, + sum_rawdata = @sum_rawdata, + is_complete = @is_complete, + repeat_times = @repeat_times, + fail_reason = @fail_reason, + updated_at = @updated_at + WHERE device_number = @device_number + AND point = @point + AND start_timestamp = @start_timestamp; - IF @@ROWCOUNT = 0 - BEGIN - INSERT INTO archive_electric_water_meter_day_{dbDateName} ( - device_number, - point, - start_timestamp, - end_timestamp, - count_rawdata, - min_rawdata, - max_rawdata, - sub_result, - avg_rawdata, - sum_rawdata, - is_complete, - repeat_times, - fail_reason) - VALUES ( - @device_number, - @point, - @start_timestamp, - @end_timestamp, - @count_rawdata, - @min_rawdata, - @max_rawdata, - @max_rawdata - @min_rawdata, - @avg_rawdata, - @sum_rawdata, - @is_complete, - @repeat_times, - @fail_reason) - END + IF @@ROWCOUNT = 0 + BEGIN + INSERT INTO archive_electric_water_meter_day_{dbDateName} ( + device_number, + point, + start_timestamp, + end_timestamp, + count_rawdata, + min_rawdata, + max_rawdata, + sub_result, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + VALUES ( + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @max_rawdata - @min_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason) + END - COMMIT TRANSACTION;"; + COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, row); if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") { @@ -1725,15 +1726,15 @@ namespace BackendWorkerService.Quartz.Jobs switch (name) { case "min": - var min = Convert.ToDecimal(real["@val"].ToString()); + var min = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); arrangeRawData.Add("@min_rawdata", min); break; case "max": - var max = Convert.ToDecimal(real["@val"].ToString()); + var max = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); arrangeRawData.Add("@max_rawdata", max); break; case "avg": - var avg = Convert.ToDecimal(real["@val"].ToString()); + var avg = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); arrangeRawData.Add("@avg_rawdata", avg); break; case "sum": @@ -1793,15 +1794,15 @@ namespace BackendWorkerService.Quartz.Jobs switch (name) { case "min": - var min = Convert.ToDecimal(real["@val"].ToString()); + var min = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); arrangeRawData.Add("@min_rawdata", min); break; case "max": - var max = Convert.ToDecimal(real["@val"].ToString()); + var max = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); arrangeRawData.Add("@max_rawdata", max); break; case "avg": - var avg = Convert.ToDecimal(real["@val"].ToString()); + var avg = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); arrangeRawData.Add("@avg_rawdata", avg); break; case "sum":