[BackendWorkerService]修改Niagara撈回的資料有科學記號時,型別會轉換錯誤的問題

This commit is contained in:
張家睿 2024-02-07 15:52:11 +08:00
parent 5ab5319116
commit 8efb19af68

View File

@ -1,6 +1,7 @@
using Backend.Models; using Backend.Models;
using BackendWorkerService.Services.Implement; using BackendWorkerService.Services.Implement;
using Dapper; using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -345,160 +346,160 @@ namespace BackendWorkerService.Quartz.Jobs
row.TryGetValue("@start_timestamp", out var yyyymmData); row.TryGetValue("@start_timestamp", out var yyyymmData);
dbDateName = System.DateTime.Parse(yyyymmData.ToString()).ToString("yyyyMM"); dbDateName = System.DateTime.Parse(yyyymmData.ToString()).ToString("yyyyMM");
var sql = $@"CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_day_{dbDateName}` ( 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, `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, `point` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`start_timestamp` datetime(6) NOT NULL, `start_timestamp` datetime(6) NOT NULL,
`end_timestamp` datetime(6) NULL DEFAULT NULL, `end_timestamp` datetime(6) NULL DEFAULT NULL,
`count_rawdata` int(11) NULL DEFAULT NULL, `count_rawdata` int(11) NULL DEFAULT NULL,
`min_rawdata` decimal(15, 3) NULL DEFAULT NULL, `min_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`max_rawdata` decimal(15, 3) NULL DEFAULT NULL, `max_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sub_result` decimal(15, 3) NULL DEFAULT NULL, `sub_result` decimal(15, 3) NULL DEFAULT NULL,
`avg_rawdata` decimal(15, 3) NULL DEFAULT NULL, `avg_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sum_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:', `is_complete` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '0: 1:',
`repeat_times` int(11) NULL DEFAULT 0 COMMENT '', `repeat_times` int(11) NULL DEFAULT 0 COMMENT '',
`fail_reason` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '', `fail_reason` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime(6) NULL DEFAULT NULL, `updated_at` datetime(6) NULL DEFAULT NULL,
PRIMARY KEY (`device_number`, `point`, `start_timestamp`) USING BTREE PRIMARY KEY (`device_number`, `point`, `start_timestamp`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC; ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1; SET FOREIGN_KEY_CHECKS = 1;
UPDATE archive_electric_water_meter_day_{dbDateName} SET UPDATE archive_electric_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,
sub_result = @max_rawdata - @min_rawdata, sub_result = @max_rawdata - @min_rawdata,
avg_rawdata = @avg_rawdata, avg_rawdata = @avg_rawdata,
sum_rawdata = @sum_rawdata, sum_rawdata = @sum_rawdata,
is_complete = @is_complete, is_complete = @is_complete,
repeat_times = @repeat_times, repeat_times = @repeat_times,
fail_reason = @fail_reason, fail_reason = @fail_reason,
updated_at = @updated_at updated_at = @updated_at
WHERE device_number = @device_number WHERE device_number = @device_number
AND point = @point AND point = @point
AND start_timestamp = @start_timestamp; AND start_timestamp = @start_timestamp;
INSERT INTO archive_electric_water_meter_day_{dbDateName} ( INSERT INTO archive_electric_water_meter_day_{dbDateName} (
device_number, device_number,
point, point,
start_timestamp, start_timestamp,
end_timestamp, end_timestamp,
count_rawdata, count_rawdata,
min_rawdata, min_rawdata,
max_rawdata, max_rawdata,
sub_result, sub_result,
avg_rawdata, avg_rawdata,
sum_rawdata, sum_rawdata,
is_complete, is_complete,
repeat_times, repeat_times,
fail_reason) fail_reason)
SELECT SELECT
@device_number, @device_number,
@point, @point,
@start_timestamp, @start_timestamp,
@end_timestamp, @end_timestamp,
@count_rawdata, @count_rawdata,
@min_rawdata, @min_rawdata,
@max_rawdata, @max_rawdata,
@max_rawdata - @min_rawdata, @max_rawdata - @min_rawdata,
@avg_rawdata, @avg_rawdata,
@sum_rawdata, @sum_rawdata,
@is_complete, @is_complete,
@repeat_times, @repeat_times,
@fail_reason @fail_reason
WHERE ROW_COUNT() = 0; WHERE ROW_COUNT() = 0;
update device set archive_lastDate = @start_timestamp, archive_lastActionDate = @updated_at update device set archive_lastDate = @start_timestamp, archive_lastActionDate = @updated_at
where device_number = @device_number;"; //archive_lastDate 前次完成時間 archive_lastActionDate 本次作業時間 where device_number = @device_number;"; //archive_lastDate 前次完成時間 archive_lastActionDate 本次作業時間
var mySql = $@"BEGIN TRANSACTION; var mySql = $@"BEGIN TRANSACTION;
IF OBJECT_ID(N'dbo.archive_electric_water_meter_day_{dbDateName}', N'U') is null IF OBJECT_ID(N'dbo.archive_electric_water_meter_day_{dbDateName}', N'U') is null
BEGIN BEGIN
CREATE TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}]( CREATE TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}](
[device_number] [varchar](50) NOT NULL, [device_number] [varchar](50) NOT NULL,
[point] [varchar](20) NOT NULL, [point] [varchar](20) NOT NULL,
[start_timestamp] [datetime] NOT NULL, [start_timestamp] [datetime] NOT NULL,
[end_timestamp] [datetime] NULL, [end_timestamp] [datetime] NULL,
[count_rawdata] [int] NULL, [count_rawdata] [int] NULL,
[min_rawdata] [decimal](15, 3) NULL, [min_rawdata] [decimal](15, 3) NULL,
[max_rawdata] [decimal](15, 3) NULL, [max_rawdata] [decimal](15, 3) NULL,
[sub_result] [decimal](15, 3) NULL, [sub_result] [decimal](15, 3) NULL,
[avg_rawdata] [decimal](15, 3) NULL, [avg_rawdata] [decimal](15, 3) NULL,
[sum_rawdata] [decimal](15, 3) NULL, [sum_rawdata] [decimal](15, 3) NULL,
[is_complete] [tinyint] NULL, [is_complete] [tinyint] NULL,
[repeat_times] [int] NULL, [repeat_times] [int] NULL,
[fail_reason] [nvarchar](max) NULL, [fail_reason] [nvarchar](max) NULL,
[created_at] [datetime] NULL, [created_at] [datetime] NULL,
[updated_at] [datetime] NULL, [updated_at] [datetime] NULL,
CONSTRAINT [PK_archive_electric_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED CONSTRAINT [PK_archive_electric_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED
( (
[device_number] ASC, [device_number] ASC,
[point] ASC, [point] ASC,
[start_timestamp] 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] )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] ) 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' 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 END
UPDATE archive_electric_water_meter_day_{dbDateName} SET UPDATE archive_electric_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,
sub_result = @max_rawdata - @min_rawdata, sub_result = @max_rawdata - @min_rawdata,
avg_rawdata = @avg_rawdata, avg_rawdata = @avg_rawdata,
sum_rawdata = @sum_rawdata, sum_rawdata = @sum_rawdata,
is_complete = @is_complete, is_complete = @is_complete,
repeat_times = @repeat_times, repeat_times = @repeat_times,
fail_reason = @fail_reason, fail_reason = @fail_reason,
updated_at = @updated_at updated_at = @updated_at
WHERE device_number = @device_number WHERE device_number = @device_number
AND point = @point AND point = @point
AND start_timestamp = @start_timestamp; AND start_timestamp = @start_timestamp;
IF @@ROWCOUNT = 0 IF @@ROWCOUNT = 0
BEGIN BEGIN
INSERT INTO archive_electric_water_meter_day_{dbDateName} ( INSERT INTO archive_electric_water_meter_day_{dbDateName} (
device_number, device_number,
point, point,
start_timestamp, start_timestamp,
end_timestamp, end_timestamp,
count_rawdata, count_rawdata,
min_rawdata, min_rawdata,
max_rawdata, max_rawdata,
sub_result, sub_result,
avg_rawdata, avg_rawdata,
sum_rawdata, sum_rawdata,
is_complete, is_complete,
repeat_times, repeat_times,
fail_reason) fail_reason)
VALUES ( VALUES (
@device_number, @device_number,
@point, @point,
@start_timestamp, @start_timestamp,
@end_timestamp, @end_timestamp,
@count_rawdata, @count_rawdata,
@min_rawdata, @min_rawdata,
@max_rawdata, @max_rawdata,
@max_rawdata - @min_rawdata, @max_rawdata - @min_rawdata,
@avg_rawdata, @avg_rawdata,
@sum_rawdata, @sum_rawdata,
@is_complete, @is_complete,
@repeat_times, @repeat_times,
@fail_reason) @fail_reason)
END END
COMMIT TRANSACTION;"; COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, row); await backgroundServiceRepository.ExecuteSql(sql, row);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{ {
@ -1725,15 +1726,15 @@ namespace BackendWorkerService.Quartz.Jobs
switch (name) switch (name)
{ {
case "min": 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); arrangeRawData.Add("@min_rawdata", min);
break; break;
case "max": 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); arrangeRawData.Add("@max_rawdata", max);
break; break;
case "avg": 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); arrangeRawData.Add("@avg_rawdata", avg);
break; break;
case "sum": case "sum":
@ -1793,15 +1794,15 @@ namespace BackendWorkerService.Quartz.Jobs
switch (name) switch (name)
{ {
case "min": 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); arrangeRawData.Add("@min_rawdata", min);
break; break;
case "max": 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); arrangeRawData.Add("@max_rawdata", max);
break; break;
case "avg": 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); arrangeRawData.Add("@avg_rawdata", avg);
break; break;
case "sum": case "sum":