[後台] 後端修改排程流程, 修改niagara 同步building_menu 問題

This commit is contained in:
dev02 2023-05-29 16:21:41 +08:00
parent 7a6b2068bd
commit 8aebc49e40
3 changed files with 149 additions and 20 deletions

View File

@ -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<string>("select system_value from variable where system_type = 'save_to_ms_db' and deleted = 0");
var variableArchive = await backgroundServiceRepository.GetAllAsync<KeyValue>(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,8 +406,11 @@ namespace BackendWorkerService.Quartz.Jobs
COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, electericArchiveDayRawDatas);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electericArchiveDayRawDatas);
}
}
if (waterArchiveDayRawDatas.Count() > 0)
{
var sql = $@"
@ -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,8 +542,11 @@ namespace BackendWorkerService.Quartz.Jobs
COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, waterArchiveDayRawDatas);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveDayRawDatas);
}
}
await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Day", "任務完成");
}
catch (Exception exception)
@ -691,7 +790,10 @@ namespace BackendWorkerService.Quartz.Jobs
COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, 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);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveWeekRawDatas);
}
}
await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Week", "任務完成");
}
@ -1035,8 +1139,11 @@ namespace BackendWorkerService.Quartz.Jobs
COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, electricArchiveMonthRawDatas);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveMonthRawDatas);
}
}
if (waterArchiveMonthRawDatas.Count() > 0)
{
var sql = $@"
@ -1130,8 +1237,11 @@ namespace BackendWorkerService.Quartz.Jobs
COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, waterArchiveMonthRawDatas);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveMonthRawDatas);
}
}
await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Month", "任務完成");
}
catch (Exception exception)

View File

@ -52,6 +52,7 @@ namespace BackendWorkerService.Services.Implement
var variableArchive = await backgroundServiceRepository.GetAllAsync<KeyValue>(sqlArchive);
repeatTimes = Convert.ToInt32(variableArchive.Where(x => x.Name == "RepeatTimes").Select(x => x.Value).FirstOrDefault());
var saveToMSDB = await backgroundServiceRepository.GetOneAsync<string>("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);
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);
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);
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);
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);
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);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{
await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveMonthRawDatas);
}
await backgroundServiceRepository.ExecuteSql(MYsql_update_format, waterArchiveMonthRawDatas);
}
}

View File

@ -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");