[BackendWorkerService]修改水電報表歸檔邏輯

This commit is contained in:
張家睿 2024-02-05 11:10:32 +08:00
parent 151e543564
commit 30457e621f

View File

@ -140,6 +140,7 @@ namespace BackendWorkerService.Quartz.Jobs
{ {
try try
{ {
await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Day", "水電表天任務開始"); await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Day", "水電表天任務開始");
var preDay = now.AddDays(-1); //取得前一天 var preDay = now.AddDays(-1); //取得前一天
var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0'); var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0');
@ -167,10 +168,10 @@ namespace BackendWorkerService.Quartz.Jobs
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
foreach (DateTime day in EachDay(startDay, endDay)) 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")); // 巨蛋用此時間抓到的實際是T00:15:00的資料 var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料
var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:00:00的資料 //var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:00:00的資料
var eDay = string.Format("{0}T00:00:00.000+08:00", day.AddDays(1).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"));
string historyQueryFilter = $@"<obj is='obix: HistoryFilter'> string historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
@ -251,10 +252,10 @@ namespace BackendWorkerService.Quartz.Jobs
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
foreach (DateTime day in EachDay(startDay, endDay)) 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")); // 巨蛋用此時間抓到的實際是T00:15:00的資料 var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料
var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:00:00的資料 //var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:00:00的資料
var eDay = string.Format("{0}T00:00:00.000+08:00", day.AddDays(1).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"));
string historyQueryFilter = $@"<obj is='obix: HistoryFilter'> string historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
@ -338,7 +339,11 @@ namespace BackendWorkerService.Quartz.Jobs
if (electericArchiveDayRawDatas.Count() > 0) if (electericArchiveDayRawDatas.Count() > 0)
{ {
var sql = $@"CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_day_{dbDateName}` ( foreach (var row in electericArchiveDayRawDatas)
{
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, `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,
@ -405,7 +410,7 @@ namespace BackendWorkerService.Quartz.Jobs
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}](
@ -493,15 +498,20 @@ namespace BackendWorkerService.Quartz.Jobs
END END
COMMIT TRANSACTION;"; COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, electericArchiveDayRawDatas); await backgroundServiceRepository.ExecuteSql(sql, row);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{ {
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electericArchiveDayRawDatas); await backgroundServiceMsSqlRepository.ExecuteSql(mySql, row);
}
} }
} }
if (waterArchiveDayRawDatas.Count() > 0) if (waterArchiveDayRawDatas.Count() > 0)
{ {
var sql = $@" foreach (var row in waterArchiveDayRawDatas)
{
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}` ( 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,
@ -569,7 +579,7 @@ namespace BackendWorkerService.Quartz.Jobs
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}](
@ -657,10 +667,11 @@ namespace BackendWorkerService.Quartz.Jobs
END END
COMMIT TRANSACTION;"; COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, waterArchiveDayRawDatas); await backgroundServiceRepository.ExecuteSql(sql, row);
if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1")
{ {
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveDayRawDatas); await backgroundServiceMsSqlRepository.ExecuteSql(mySql, row);
}
} }
} }
await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Day", "任務完成"); await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Day", "任務完成");
@ -1097,9 +1108,11 @@ namespace BackendWorkerService.Quartz.Jobs
var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).ToString("yyyy-MM-dd")); // 巨蛋要這樣抓數據才是對的 //var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).ToString("yyyy-MM-dd")); // 巨蛋要這樣抓數據才是對的
var startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.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}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd"));
var endTimestamp = string.Format("{0}T00:00:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome var endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'> var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
@ -1644,6 +1657,14 @@ namespace BackendWorkerService.Quartz.Jobs
for (var day = strtday.Date; day.Date <= endday.Date; day = day.AddDays(1)) for (var day = strtday.Date; day.Date <= endday.Date; day = day.AddDays(1))
yield return day; yield return day;
} }
public IEnumerable<DateTime> EachMonth(string from, string thru)
{
var strtday = DateTime.Parse(from);
var endday = DateTime.Parse(thru);
for (var _month = strtday.Date; _month.Month <= endday.Month; _month = _month.AddDays(1))
yield return _month;
}
private List<Dictionary<string, object>> ArrangeRawData(DeviceNumberPoint deviceNumberPoint, JObject jsonResult) private List<Dictionary<string, object>> ArrangeRawData(DeviceNumberPoint deviceNumberPoint, JObject jsonResult)
{ {
List<Dictionary<string, object>> arrangeRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> arrangeRawDatas = new List<Dictionary<string, object>>();