From 8aebc49e40a926c5d98b8bbdea836804b0f84cb1 Mon Sep 17 00:00:00 2001 From: dev02 Date: Mon, 29 May 2023 16:21:41 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E5=8F=B0]=20=E5=BE=8C=E7=AB=AF?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=92=E7=A8=8B=E6=B5=81=E7=A8=8B,=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9niagara=20=E5=90=8C=E6=AD=A5building=5Fmenu?= =?UTF-8?q?=20=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 136 ++++++++++++++++-- .../Implement/ProcEletricMeterService.cs | 31 +++- .../NiagaraDataSynchronizeRepository.cs | 2 +- 3 files changed, 149 insertions(+), 20 deletions(-) diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 34812ad..cf7fe03 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -56,6 +56,7 @@ namespace BackendWorkerService.Quartz.Jobs XmlDocument xmlDocument = new XmlDocument(); var sqlArchive = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'archiveConfig'"; + var saveToMSDB = await backgroundServiceRepository.GetOneAsync("select system_value from variable where system_type = 'save_to_ms_db' and deleted = 0"); var variableArchive = await backgroundServiceRepository.GetAllAsync(sqlArchive); var electricMeterGuid = variableArchive.Where(x => x.Name == "ElectricMeterGuid").Select(x => x.Value).FirstOrDefault(); @@ -126,6 +127,7 @@ namespace BackendWorkerService.Quartz.Jobs { await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Day", "任務開始"); var preDay = now.AddDays(-1); //取得前一天 + var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0'); var startTimestamp = string.Format("{0}T00:00:00.000+08:00", preDay.ToString("yyyy-MM-dd")); var endTimestamp = string.Format("{0}T23:59:59.000+08:00", preDay.ToString("yyyy-MM-dd")); @@ -258,8 +260,26 @@ namespace BackendWorkerService.Quartz.Jobs if (electericArchiveDayRawDatas.Count() > 0) { - var sql = $@" - UPDATE archive_electric_meter_day SET + var sql = $@"CREATE TABLE IF NOT EXISTS `archive_electric_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; + + SET FOREIGN_KEY_CHECKS = 1; + UPDATE archive_electric_meter_day_{dbDateName} SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, @@ -273,8 +293,7 @@ namespace BackendWorkerService.Quartz.Jobs AND point = @point AND start_timestamp = @start_timestamp; - - INSERT INTO archive_electric_meter_day ( + INSERT INTO archive_electric_meter_day_{dbDateName} ( device_number, point, start_timestamp, @@ -303,8 +322,45 @@ namespace BackendWorkerService.Quartz.Jobs WHERE ROW_COUNT() = 0;"; var mySql = $@"BEGIN TRANSACTION; + IF OBJECT_ID(N'dbo.archive_water_meter_day_{dbDateName}', N'U') is null + BEGIN + CREATE TABLE [dbo].[archive_electric_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, + [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_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] - UPDATE archive_electric_meter_day SET + ALTER TABLE [dbo].[archive_electric_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] + + ALTER TABLE [dbo].[archive_electric_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] + + ALTER TABLE [dbo].[archive_electric_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_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_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_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_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' + END + + UPDATE archive_electric_meter_day_{dbDateName} SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, @@ -320,7 +376,7 @@ namespace BackendWorkerService.Quartz.Jobs IF @@ROWCOUNT = 0 BEGIN - INSERT INTO archive_electric_meter_day ( + INSERT INTO archive_electric_meter_day_{dbDateName} ( device_number, point, start_timestamp, @@ -350,7 +406,10 @@ namespace BackendWorkerService.Quartz.Jobs COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, electericArchiveDayRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electericArchiveDayRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electericArchiveDayRawDatas); + } } if (waterArchiveDayRawDatas.Count() > 0) { @@ -399,6 +458,43 @@ namespace BackendWorkerService.Quartz.Jobs WHERE ROW_COUNT() = 0;"; var mySql = $@"BEGIN TRANSACTION; + IF OBJECT_ID(N'dbo.archive_water_meter_day_{dbDateName}', N'U') is null + BEGIN + CREATE TABLE [dbo].[archive_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, + [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_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_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_water_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] + + ALTER TABLE [dbo].[archive_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_water_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] + + ALTER TABLE [dbo].[archive_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_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_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_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_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' + END UPDATE archive_water_meter_day SET count_rawdata = @count_rawdata, @@ -446,7 +542,10 @@ namespace BackendWorkerService.Quartz.Jobs COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, waterArchiveDayRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveDayRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveDayRawDatas); + } } await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Day", "任務完成"); } @@ -691,7 +790,10 @@ namespace BackendWorkerService.Quartz.Jobs COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, electricArchiveWeekRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveWeekRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveWeekRawDatas); + } } if (waterArchiveWeekRawDatas.Count() > 0) @@ -790,8 +892,10 @@ namespace BackendWorkerService.Quartz.Jobs COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, waterArchiveWeekRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveWeekRawDatas); - + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveWeekRawDatas); + } } await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Week", "任務完成"); } @@ -1035,7 +1139,10 @@ namespace BackendWorkerService.Quartz.Jobs COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, electricArchiveMonthRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveMonthRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveMonthRawDatas); + } } if (waterArchiveMonthRawDatas.Count() > 0) { @@ -1130,7 +1237,10 @@ namespace BackendWorkerService.Quartz.Jobs COMMIT TRANSACTION;"; await backgroundServiceRepository.ExecuteSql(sql, waterArchiveMonthRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveMonthRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveMonthRawDatas); + } } await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Month", "任務完成"); } diff --git a/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs b/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs index 5750872..8478c30 100644 --- a/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs +++ b/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs @@ -52,6 +52,7 @@ namespace BackendWorkerService.Services.Implement var variableArchive = await backgroundServiceRepository.GetAllAsync(sqlArchive); repeatTimes = Convert.ToInt32(variableArchive.Where(x => x.Name == "RepeatTimes").Select(x => x.Value).FirstOrDefault()); + var saveToMSDB = await backgroundServiceRepository.GetOneAsync("select system_value from variable where system_type = 'save_to_ms_db' and deleted = 0"); #region 取得obix 設定 var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; @@ -324,7 +325,10 @@ namespace BackendWorkerService.Services.Implement { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveDayRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveDayRawDatas); + } await backgroundServiceRepository.ExecuteSql(Mysql_error_update, electricArchiveDayRawDatas); } } @@ -405,7 +409,10 @@ namespace BackendWorkerService.Services.Implement { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveDayRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveDayRawDatas); + } await backgroundServiceRepository.ExecuteSql(Mysql_error_update, waterArchiveDayRawDatas); } } @@ -489,7 +496,10 @@ namespace BackendWorkerService.Services.Implement { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveWeekRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveWeekRawDatas); + } await backgroundServiceRepository.ExecuteSql(Mysql_error_update, electricArchiveWeekRawDatas); } } @@ -570,7 +580,10 @@ namespace BackendWorkerService.Services.Implement { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveWeekRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveWeekRawDatas); + } await backgroundServiceRepository.ExecuteSql(Mysql_error_update, waterArchiveWeekRawDatas); } } @@ -657,7 +670,10 @@ namespace BackendWorkerService.Services.Implement { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveMonthRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveMonthRawDatas); + } await backgroundServiceRepository.ExecuteSql(MYsql_update_format, electricArchiveMonthRawDatas); } } @@ -741,7 +757,10 @@ namespace BackendWorkerService.Services.Implement { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveMonthRawDatas); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveMonthRawDatas); + } await backgroundServiceRepository.ExecuteSql(MYsql_update_format, waterArchiveMonthRawDatas); } } diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 522bd0c..d293e89 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -589,7 +589,7 @@ namespace Repository.BackendRepository.Implement select device_building_tag, device_system_tag, device_name_tag from import_niagara_tag group by device_building_tag, device_system_tag, device_name_tag - ) AS a ON b.device_building_tag = a.building_tag + ) AS a ON b.building_tag = a.device_building_tag and a.device_system_tag = b.main_system_tag and a.device_name_tag = b.sub_system_tag SET b.is_link = 0 WHERE b.building_tag IS NULL");