From c35aeb2f2ef57d906e69f2c0ccf286e31fda7972 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Thu, 1 Feb 2024 15:06:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B7=A8=E8=9B=8B=E6=B0=B4?= =?UTF-8?q?=E9=9B=BB=E6=AD=B8=E6=AA=94=E9=82=8F=E8=BC=AF=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=9C=88=E6=AD=B8=E6=AA=94sql=E6=9C=83=E8=87=AA?= =?UTF-8?q?=E5=8B=95=E5=BB=BA=E7=AB=8Btable=E8=AA=9E=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackendWorkerService/Program.cs | 24 +-- .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 170 +++++++++++++++--- .../appsettings.Development.json | 2 +- 3 files changed, 160 insertions(+), 36 deletions(-) diff --git a/BackendWorkerService/Program.cs b/BackendWorkerService/Program.cs index 6fdac3c..7521b31 100644 --- a/BackendWorkerService/Program.cs +++ b/BackendWorkerService/Program.cs @@ -121,12 +121,12 @@ namespace BackendWorkerService //); //#endregion - #region 停車場管理(設定每 5秒 執行一次) - services.AddSingleton(); - services.AddSingleton( - new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue("BackgroundServiceCron:ParkingJob")) - ); - #endregion + //#region 停車場管理(設定每 5秒 執行一次) + //services.AddSingleton(); + //services.AddSingleton( + //new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue("BackgroundServiceCron:ParkingJob")) + //); + //#endregion //#region 電錶歸檔(設定每 小時 執行一次) //services.AddSingleton(); @@ -142,12 +142,12 @@ namespace BackendWorkerService ); #endregion - #region 定時取得氣象API - services.AddSingleton(); - services.AddSingleton( - new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue("BackgroundServiceCron:WeatherAPIJob")) - ); - #endregion + //#region 定時取得氣象API + //services.AddSingleton(); + //services.AddSingleton( + //new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue("BackgroundServiceCron:WeatherAPIJob")) + //); + //#endregion }).ConfigureLogging((hostContext, logFactory) => { IConfiguration configuration = hostContext.Configuration; diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index aeb8626..45ebe99 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -141,8 +141,8 @@ namespace BackendWorkerService.Quartz.Jobs try { 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 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")); @@ -153,17 +153,11 @@ namespace BackendWorkerService.Quartz.Jobs // // "; - #region 敺甈 交唳典予箸迫 - - var preDay = now.AddDays(-1); //敺銝憭 - - var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0'); - - #endregion - //Stopwatch stopWatch = new Stopwatch(); //stopWatch.Start(); + #region 敺甈 交唳典予箸迫 + //瘥閮剖鞈 List> electericArchiveDayRawDatas = new List>(); List> waterArchiveDayRawDatas = new List>(); @@ -173,8 +167,10 @@ namespace BackendWorkerService.Quartz.Jobs var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); foreach (DateTime day in EachDay(startDay, endDay)) { - var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); - var eDay = string.Format("{0}T00:15:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); + //var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 撌刻冽迨啁撖阡狼00:15:00鞈 + var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 撌刻冽迨啁撖阡狼00:00:00鞈 + + var eDay = string.Format("{0}T00:00:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); string historyQueryFilter = $@" @@ -252,11 +248,13 @@ namespace BackendWorkerService.Quartz.Jobs foreach (var deviceNumberPoint in waterDeviceNumberPoints) { var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd"); - var endDay = System.DateTime.Now.ToString("yyyy-MM-dd"); + var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); foreach (DateTime day in EachDay(startDay, endDay)) { - var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); - var eDay = string.Format("{0}T00:15:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); + //var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 撌刻冽迨啁撖阡狼00:15:00鞈 + var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 撌刻冽迨啁撖阡狼00:00:00鞈 + + var eDay = string.Format("{0}T00:00:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); string historyQueryFilter = $@" @@ -334,7 +332,7 @@ namespace BackendWorkerService.Quartz.Jobs } } - + #endregion //stopWatch.Stop(); //logger.LogInformation("ArchiveElectricMeterDayJob憭拇飛瑼賣炎撽[敺鞈梯祥]{0} 瘥怎", stopWatch.ElapsedMilliseconds); @@ -1090,22 +1088,24 @@ namespace BackendWorkerService.Quartz.Jobs try { await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Month", "瘞湧餉”隞餃憪"); - var FirstDay = now.AddDays(-now.Day + 1); - //var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); - var LastDay = FirstDay.AddMonths(1); + var preDay = now.AddDays(-1); //敺銝憭 + + var dayInMonth = DateTime.DaysInMonth(preDay.Year, preDay.Month); + var FirstDay = preDay.AddDays(- dayInMonth + 1); + //var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); // 舀啗府敺銝憭 + var LastDay = FirstDay.AddMonths(1); //雿撖阡閰脫蝚砌憭 - var dayInMonth = DateTime.DaysInMonth(now.Year, now.Month); - var startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd")); + var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).ToString("yyyy-MM-dd")); // 撌刻閬璅豢臬 //var endTimestamp = string.Format("{0}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd")); - var endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome + var endTimestamp = string.Format("{0}T00:00:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome var historyQueryFilter = $@" - + "; //Stopwatch stopWatch = new Stopwatch(); @@ -1258,7 +1258,29 @@ namespace BackendWorkerService.Quartz.Jobs if (electricArchiveMonthRawDatas.Count() > 0) { var sql = $@" + CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_month` ( + `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_month SET + start_timestamp = @start_timestamp, + end_timestamp = @end_timestamp, count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, @@ -1304,8 +1326,48 @@ namespace BackendWorkerService.Quartz.Jobs WHERE ROW_COUNT() = 0;"; var mySql = $@"BEGIN TRANSACTION; + IF OBJECT_ID(N'dbo.archive_electric_water_meter_month', N'U') is null + BEGIN + CREATE TABLE [dbo].[archive_electric_water_meter_month]( + [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_month] 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_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_repeat_times] DEFAULT ((0)) FOR [repeat_times] + + ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_created_at] DEFAULT (getdate()) FOR [created_at] + + ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_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_month', @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_month', @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_month', @level2type=N'COLUMN',@level2name=N'fail_reason' + END UPDATE archive_electric_water_meter_month SET + start_timestamp = @start_timestamp, + end_timestamp = @end_timestamp, count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, @@ -1362,7 +1424,29 @@ namespace BackendWorkerService.Quartz.Jobs if (waterArchiveMonthRawDatas.Count() > 0) { var sql = $@" + CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_month` ( + `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_month SET + start_timestamp = @start_timestamp, + end_timestamp = @end_timestamp, count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, @@ -1408,8 +1492,48 @@ namespace BackendWorkerService.Quartz.Jobs WHERE ROW_COUNT() = 0;"; var mySql = $@"BEGIN TRANSACTION; + IF OBJECT_ID(N'dbo.archive_electric_water_meter_month', N'U') is null + BEGIN + CREATE TABLE [dbo].[archive_electric_water_meter_month]( + [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_month] 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_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_repeat_times] DEFAULT ((0)) FOR [repeat_times] + + ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_created_at] DEFAULT (getdate()) FOR [created_at] + + ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_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_month', @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_month', @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_month', @level2type=N'COLUMN',@level2name=N'fail_reason' + END UPDATE archive_electric_water_meter_month SET + start_timestamp = @start_timestamp, + end_timestamp = @end_timestamp, count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, diff --git a/BackendWorkerService/appsettings.Development.json b/BackendWorkerService/appsettings.Development.json index 1a5d4fd..9dd0d11 100644 --- a/BackendWorkerService/appsettings.Development.json +++ b/BackendWorkerService/appsettings.Development.json @@ -19,7 +19,7 @@ }, "DBConfig": { "MySqlDBConfig": { - "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.202 + "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.132 "Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306 //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp