diff --git a/Backend/appsettings.Development.json b/Backend/appsettings.Development.json index 0d3c1f3..12cfb8b 100644 --- a/Backend/appsettings.Development.json +++ b/Backend/appsettings.Development.json @@ -22,8 +22,9 @@ //"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel //"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office //"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //mcut - "Database": "+5RAiFLJVU+LRyDxF1K/pcLZaoZa4k/thZqF6xKoCag=", //dome_online_0821 + //"Database": "+5RAiFLJVU+LRyDxF1K/pcLZaoZa4k/thZqF6xKoCag=", //dome_online_0821 //"Database": "2U+9jYGy0dCbMzLaguBXow==", //tpe_dome_mall + "Database": "FbHCCU4CapG9UryANXC1W+iMe403Bd82RnA7bCNVbL0=", //ibms_dome_cinema, "Root": "SzdxEgaJJ7tcTCrUl2zKsA==", "Password": "FVAPxztxpY4gJJKQ/se4bQ==" }, diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 98942f8..fe49b52 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -35,135 +35,147 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + StringBuilder sb = new StringBuilder(); + try { - StringBuilder sb = new StringBuilder(); - try + foreach(var b in building) { - foreach(var b in building) + string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_tag` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `db_tags` varchar(50) DEFAULT NULL, + `niagara_tags` varchar(50) DEFAULT NULL, + `device_area_tag` varchar(50) DEFAULT NULL, + `device_building_tag` varchar(50) DEFAULT NULL, + `device_system_tag` varchar(50) DEFAULT NULL, + `device_name_tag` varchar(50) DEFAULT NULL, + `device_floor_tag` varchar(50) DEFAULT NULL, + `device_master_tag` varchar(50) DEFAULT NULL, + `device_last_name_tag` varchar(50) DEFAULT NULL, + `device_serial_tag` varchar(50) DEFAULT NULL, + `atDateTime` datetime(1) DEFAULT NULL, + `device_full_name` varchar(100) DEFAULT NULL, + `is_used` smallint(1) DEFAULT 0, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_tag` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `db_tags` varchar(50) DEFAULT NULL, - `niagara_tags` varchar(50) DEFAULT NULL, - `device_area_tag` varchar(50) DEFAULT NULL, - `device_building_tag` varchar(50) DEFAULT NULL, - `device_system_tag` varchar(50) DEFAULT NULL, - `device_name_tag` varchar(50) DEFAULT NULL, - `device_floor_tag` varchar(50) DEFAULT NULL, - `device_master_tag` varchar(50) DEFAULT NULL, - `device_last_name_tag` varchar(50) DEFAULT NULL, - `device_serial_tag` varchar(50) DEFAULT NULL, - `atDateTime` datetime(1) DEFAULT NULL, - `device_full_name` varchar(100) DEFAULT NULL, - `is_used` smallint(1) DEFAULT 0, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; - await conn.ExecuteAsync(sql); - sql = "delete from import_niagara_tag where device_building_tag = '" + b + "' limit 10000"; await conn.ExecuteAsync(sql); } + sql = "delete from import_niagara_tag where device_building_tag = '" + b + "' limit 10000"; + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + await conn.ExecuteAsync(sql); + } + } - //N4資料groupBy後放入import_niagara_tag資料表 - var ds2 = ds.GroupBy(x => new - { - tag_name2 = x.tag_name, - displayName2 = x.displayName - }).Select(x => new Device_value - { - tag_name = x.Key.tag_name2, - displayName = x.Key.displayName2 - }); + //N4資料groupBy後放入import_niagara_tag資料表 + var ds2 = ds.GroupBy(x => new + { + tag_name2 = x.tag_name, + displayName2 = x.displayName + }).Select(x => new Device_value + { + tag_name = x.Key.tag_name2, + displayName = x.Key.displayName2 + }); - // StringBuilder sb = new StringBuilder(); - bool isDome = false; //是否為巨蛋案 - int count = 0; - foreach (var row in ds2) - { - if (string.IsNullOrEmpty(row.tag_name)) continue; - string[] arrTag = row.tag_name.Split('_'); + // StringBuilder sb = new StringBuilder(); + bool isDome = false; //是否為巨蛋案 + int count = 0; + foreach (var row in ds2) + { + if (string.IsNullOrEmpty(row.tag_name)) continue; + string[] arrTag = row.tag_name.Split('_'); - if (int.Parse(tag_quantity) == 8) + if (int.Parse(tag_quantity) == 8) + { + if (arrTag.Length == 8) { - if (arrTag.Length == 8) - { - sb.Append($@" insert into import_niagara_tag(niagara_tags, device_area_tag, device_building_tag, device_system_tag, - device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, device_full_name, atDateTime) values('" + - row.tag_name + "', '" + //niagara_tags - arrTag[0] + "', '" + //device_area_tag - arrTag[1] + "', '" + //device_building_tag - arrTag[2] + "', '" + //device_system_tag - arrTag[3] + "', '" + //device_name_tag - arrTag[4] + "', '" + //device_floor_tag - arrTag[5] + "', '" + //device_master_tag - arrTag[6] + "', '" + //device_last_name_tag - arrTag[7] + "', '" + //device_serial_tag - row.displayName + "', " + //device_full_name - "now());"); //atDateTime - } + sb.Append($@" insert into import_niagara_tag(niagara_tags, device_area_tag, device_building_tag, device_system_tag, + device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, device_full_name, atDateTime) values('" + + row.tag_name + "', '" + //niagara_tags + arrTag[0] + "', '" + //device_area_tag + arrTag[1] + "', '" + //device_building_tag + arrTag[2] + "', '" + //device_system_tag + arrTag[3] + "', '" + //device_name_tag + arrTag[4] + "', '" + //device_floor_tag + arrTag[5] + "', '" + //device_master_tag + arrTag[6] + "', '" + //device_last_name_tag + arrTag[7] + "', '" + //device_serial_tag + row.displayName + "', " + //device_full_name + "now());"); //atDateTime } - else + } + else + { + if (arrTag.Length == 5) { - if (arrTag.Length == 5) + isDome= true; + sb.Append($@" insert into import_niagara_tag(niagara_tags, device_area_tag, device_building_tag, device_system_tag, + device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, device_full_name, atDateTime) values('" + + row.tag_name + "', '" + //niagara_tags + "', '" + //device_area_tag + arrTag[0] + "', '" + //device_building_tag + arrTag[1] + "', '" + //device_system_tag 巨蛋沒有大類,後面再用關聯的方式補上 + arrTag[3] + "', '" + //device_name_tag + arrTag[2] + "', '" + //device_floor_tag + "', '" + //device_master_tag + "', '" + //device_last_name_tag + arrTag[4] + "', '" + //device_serial_tag + row.displayName + "', " + //device_full_name + "now());"); //atDateTime + //await conn.ExecuteAsync(sb.ToString()); + //sb.Clear(); + count += 1; + if (count >= 100) { - isDome= true; - sb.Append($@" insert into import_niagara_tag(niagara_tags, device_area_tag, device_building_tag, device_system_tag, - device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, device_full_name, atDateTime) values('" + - row.tag_name + "', '" + //niagara_tags - "', '" + //device_area_tag - arrTag[0] + "', '" + //device_building_tag - arrTag[1] + "', '" + //device_system_tag 巨蛋沒有大類,後面再用關聯的方式補上 - arrTag[3] + "', '" + //device_name_tag - arrTag[2] + "', '" + //device_floor_tag - "', '" + //device_master_tag - "', '" + //device_last_name_tag - arrTag[4] + "', '" + //device_serial_tag - row.displayName + "', " + //device_full_name - "now());"); //atDateTime - //await conn.ExecuteAsync(sb.ToString()); - //sb.Clear(); - count += 1; - if (count >= 100) + count = 0; + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - count = 0; await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); } + sb.Clear(); } } } - if (sb.Length > 0) + } + if (sb.Length > 0) + { + var temp = sb.ToString(); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - var temp = sb.ToString(); await conn.ExecuteAsync(temp); - sb.Clear(); + } + sb.Clear(); - //巨蛋特殊處理 - if (isDome) + //巨蛋特殊處理 + if (isDome) + { + sb.Append($@" -- tag 第二碼為小類(除了安全系統之外) + update import_niagara_tag set device_name_tag = device_system_tag + where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and + (device_system_tag <> 'FM' or device_system_tag <> 'APS' or device_system_tag <> 'VICL')); + + update import_niagara_tag a join dic_system b + on a.device_name_tag COLLATE utf8mb4_general_ci = b.s2_code + set device_system_tag = b.s1_code -- tag 第二碼為小類(除了安全系統之外) + where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and + (device_system_tag <> 'FM' or device_system_tag <> 'APS' or device_system_tag <> 'VICL')); -- 安全系統 第二段大類 第四段小類"); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - sb.Append($@" -- tag 第二碼為小類(除了安全系統之外) - update import_niagara_tag set device_name_tag = device_system_tag - where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and - (device_system_tag <> 'FM' or device_system_tag <> 'APS' or device_system_tag <> 'VICL')); - - update import_niagara_tag a join dic_system b - on a.device_name_tag COLLATE utf8mb4_general_ci = b.s2_code - set device_system_tag = b.s1_code -- tag 第二碼為小類(除了安全系統之外) - where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and - (device_system_tag <> 'FM' or device_system_tag <> 'APS' or device_system_tag <> 'VICL')); -- 安全系統 第二段大類 第四段小類"); await conn.ExecuteAsync(sb.ToString()); } } } - catch (Exception exception) - { - Console.WriteLine(sb.ToString()); - throw exception; - } - finally - { - conn.Close(); - } + } + catch (Exception exception) + { + Console.WriteLine(sb.ToString()); + throw exception; + } + finally + { + conn.Close(); } } } @@ -196,95 +208,102 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + try { - try + #region 刪除 import_niagara_item資料表中選取的棟別 + foreach(var b in building) { - #region 刪除 import_niagara_item資料表中選取的棟別 - foreach(var b in building) + string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `device_area_tag` varchar(50) DEFAULT NULL, + `device_building_tag` varchar(50) DEFAULT NULL, + `device_system_tag` varchar(50) DEFAULT NULL, + `device_name_tag` varchar(50) DEFAULT NULL, + `device_point_name` varchar(50) DEFAULT NULL, + `check_status` varchar(50) DEFAULT NULL, + `parent_path` varchar(50) DEFAULT NULL, + `full_name` varchar(50) DEFAULT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `device_area_tag` varchar(50) DEFAULT NULL, - `device_building_tag` varchar(50) DEFAULT NULL, - `device_system_tag` varchar(50) DEFAULT NULL, - `device_name_tag` varchar(50) DEFAULT NULL, - `device_point_name` varchar(50) DEFAULT NULL, - `check_status` varchar(50) DEFAULT NULL, - `parent_path` varchar(50) DEFAULT NULL, - `full_name` varchar(50) DEFAULT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; - await conn.ExecuteAsync(sql); - sql = "delete from import_niagara_item where device_building_tag = '" + b + "' limit 10000;"; await conn.ExecuteAsync(sql); } - #endregion - ds = ds.GroupBy(x => new { - device_area_tag = x.device_area_tag, - device_building_tag = x.device_building_tag, - device_system_tag = x.device_system_tag, - device_name_tag = x.device_name_tag, - device_point_name = x.device_point_name, - full_name = x.full_name, - parent_path = x.parent_path, - isHistory = x.isHistory - }) - .Select(x => new ImpNiaItem + sql = "delete from import_niagara_item where device_building_tag = '" + b + "' limit 10000;"; + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - device_area_tag = x.Key.device_area_tag, - device_building_tag = x.Key.device_building_tag, - device_system_tag = x.Key.device_system_tag, - device_name_tag = x.Key.device_name_tag, - device_point_name = x.Key.device_point_name, - full_name = x.Key.full_name, - parent_path = x.Key.parent_path, - isHistory = x.Key.isHistory - }).ToList(); + await conn.ExecuteAsync(sql); + } + } + #endregion + ds = ds.GroupBy(x => new { + device_area_tag = x.device_area_tag, + device_building_tag = x.device_building_tag, + device_system_tag = x.device_system_tag, + device_name_tag = x.device_name_tag, + device_point_name = x.device_point_name, + full_name = x.full_name, + parent_path = x.parent_path, + isHistory = x.isHistory + }) + .Select(x => new ImpNiaItem + { + device_area_tag = x.Key.device_area_tag, + device_building_tag = x.Key.device_building_tag, + device_system_tag = x.Key.device_system_tag, + device_name_tag = x.Key.device_name_tag, + device_point_name = x.Key.device_point_name, + full_name = x.Key.full_name, + parent_path = x.Key.parent_path, + isHistory = x.Key.isHistory + }).ToList(); - StringBuilder sb = new StringBuilder(); - #region 放入import_niagara_item資料表 - foreach (var row in ds) + StringBuilder sb = new StringBuilder(); + #region 放入import_niagara_item資料表 + foreach (var row in ds) + { + sb.Append($@" insert import_niagara_item(device_area_tag, device_building_tag, device_system_tag, device_name_tag, device_point_name, parent_path, full_name, is_history) + values('"+ + row.device_area_tag + "', '" + + row.device_building_tag + "', '" + + row.device_system_tag + "', '" + + row.device_name_tag + "', '" + + row.device_point_name + "', '" + + row.parent_path + "', '" + + row.full_name + "'," + + row.isHistory + + ");"); + } + + var isDome = await GetOneAsync("select system_value from variable where deleted = 0 and system_type = 'project_name';"); + if (sb.Length > 0) + { + if (isDome.Contains("ibms_dome")) { - sb.Append($@" insert import_niagara_item(device_area_tag, device_building_tag, device_system_tag, device_name_tag, device_point_name, parent_path, full_name, is_history) - values('"+ - row.device_area_tag + "', '" + - row.device_building_tag + "', '" + - row.device_system_tag + "', '" + - row.device_name_tag + "', '" + - row.device_point_name + "', '" + - row.parent_path + "', '" + - row.full_name + "'," + - row.isHistory + - ");"); + sb.Append($@" update import_niagara_item a join dic_system b + on a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.s2_code COLLATE utf8mb4_0900_ai_ci + set device_system_tag = b.s1_code + where (device_system_tag <> 'S' and device_system_tag <> 'CWB');"); } - var isDome = await GetOneAsync("select system_value from variable where deleted = 0 and system_type = 'project_name';"); - if (sb.Length > 0) + sb.Append($@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;"); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - if (isDome.Contains("ibms_dome")) - { - sb.Append($@" update import_niagara_item a join dic_system b - on a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.s2_code COLLATE utf8mb4_0900_ai_ci - set device_system_tag = b.s1_code - where (device_system_tag <> 'S' and device_system_tag <> 'CWB');"); - } - - sb.Append($@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;"); await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); } - #endregion - } - catch (Exception exception) - { - throw exception; - } - finally - { - conn.Close(); + sb.Clear(); } + #endregion } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } } /// @@ -297,7 +316,6 @@ namespace Repository.BackendRepository.Implement { conn.Open(); using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) - { try { List result; @@ -489,6 +507,7 @@ namespace Repository.BackendRepository.Implement { conn.Close(); } + { } } } @@ -502,112 +521,118 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + try { - try - { - List result; - StringBuilder sb = new StringBuilder(); - sb.Append($@" SELECT m.* - FROM import_niagara_item m - LEFT JOIN device_item d - ON m.device_system_tag = d.device_system_tag and m.device_name_tag = d.device_name_tag and m.device_point_name = d.points and m.device_building_tag collate utf8mb4_0900_ai_ci = d.device_building_tag - WHERE d.points IS NULL;"); - result = (await conn.QueryAsync(sb.ToString())).ToList(); + List result; + StringBuilder sb = new StringBuilder(); + sb.Append($@" SELECT m.* + FROM import_niagara_item m + LEFT JOIN device_item d + ON m.device_system_tag = d.device_system_tag and m.device_name_tag = d.device_name_tag and m.device_point_name = d.points and m.device_building_tag collate utf8mb4_0900_ai_ci = d.device_building_tag + WHERE d.points IS NULL;"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); - sb.Clear(); - # region 新增至device, is_link = 1 - if (result.Count > 0) + sb.Clear(); + # region 新增至device, is_link = 1 + if (result.Count > 0) + { + foreach (var data in result) { - foreach (var data in result) + var isControll = 0; + var isBool = 0; + if (data.device_point_name == "ER" || data.device_point_name == "AL" || data.device_point_name == "ST") { - var isControll = 0; - var isBool = 0; - if (data.device_point_name == "ER" || data.device_point_name == "AL" || data.device_point_name == "ST") - { - isControll = 1; - } - if (data.device_point_name == "ER" || data.device_point_name == "ST") - { - isBool = 1; - } - sb.Append($@"insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, is_show_history, is_link, - device_system_tag, device_name_tag, device_building_tag, full_name, parent_path, created_at, updated_at) - VALUES (0, '" + - data.device_point_name + "', 1, 0, " + - isControll + "," + - isBool + "," + - data.is_history + - ", 1, '" + - data.device_system_tag + "', '" + - data.device_name_tag + "', '" + - data.device_building_tag + "', '" + - data.full_name + "', '" + - data.parent_path + "', " + - "now(), now());"); + isControll = 1; } - if (sb.Length > 0) + if (data.device_point_name == "ER" || data.device_point_name == "ST") + { + isBool = 1; + } + sb.Append($@"insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, is_show_history, is_link, + device_system_tag, device_name_tag, device_building_tag, full_name, parent_path, created_at, updated_at) + VALUES (0, '" + + data.device_point_name + "', 1, 0, " + + isControll + "," + + isBool + "," + + data.is_history + + ", 1, '" + + data.device_system_tag + "', '" + + data.device_name_tag + "', '" + + data.device_building_tag + "', '" + + data.full_name + "', '" + + data.parent_path + "', " + + "now(), now());"); + } + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); } + sb.Clear(); } - #endregion + } + #endregion - //device有,niagara沒有,is_link 更新成 0 - //sb.Append($@" SET SQL_SAFE_UPDATES = 0; - // UPDATE device_item d LEFT JOIN import_niagara_item m - // ON d.device_system_tag = m.device_system_tag and d.device_name_tag = m.device_name_tag and d.points = m.device_point_name - // SET d.is_link = 0, d.is_show_history = 0 - // WHERE m.device_point_name IS NULL"); + //device有,niagara沒有,is_link 更新成 0 + //sb.Append($@" SET SQL_SAFE_UPDATES = 0; + // UPDATE device_item d LEFT JOIN import_niagara_item m + // ON d.device_system_tag = m.device_system_tag and d.device_name_tag = m.device_name_tag and d.points = m.device_point_name + // SET d.is_link = 0, d.is_show_history = 0 + // WHERE m.device_point_name IS NULL"); - //本次有匯入的 isLink 改為 1, 沒有的 isLink = 0 - sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE device_item d - LEFT JOIN ( - SELECT device_system_tag, device_name_tag, device_point_name, full_name - FROM import_niagara_item - LIMIT 100000 -- Specify your desired limit here - ) m - ON d.device_system_tag = m.device_system_tag - AND d.device_name_tag = m.device_name_tag - AND d.points = m.device_point_name - AND d.full_name = m.full_name - SET d.is_link = CASE WHEN m.device_point_name IS NULL THEN 0 ELSE 1 END; + //本次有匯入的 isLink 改為 1, 沒有的 isLink = 0 + sb.Append($@" SET SQL_SAFE_UPDATES = 0; + UPDATE device_item d + LEFT JOIN ( + SELECT device_system_tag, device_name_tag, device_point_name, full_name + FROM import_niagara_item + LIMIT 100000 -- Specify your desired limit here + ) m + ON d.device_system_tag = m.device_system_tag + AND d.device_name_tag = m.device_name_tag + AND d.points = m.device_point_name + AND d.full_name = m.full_name + SET d.is_link = CASE WHEN m.device_point_name IS NULL THEN 0 ELSE 1 END; - "); - - await conn.ExecuteAsync(sb.ToString()); - - sb.Clear(); - sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE device_item d - LEFT JOIN ( - SELECT m.device_system_tag, m.device_name_tag, m.device_point_name, m.full_name - FROM import_niagara_item m - WHERE (m.is_history = 1) - LIMIT 100000 -- Specify your desired limit here - ) AS subquery - ON d.device_system_tag = subquery.device_system_tag - AND d.device_name_tag = subquery.device_name_tag - AND d.points = subquery.device_point_name - AND d.full_name = subquery.full_name - SET d.is_show_history = CASE WHEN subquery.device_point_name IS NULL THEN 0 ELSE 1 END - WHERE (d.is_show_history);"); + "); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { await conn.ExecuteAsync(sb.ToString()); } - catch (Exception exception) + + sb.Clear(); + sb.Append($@" SET SQL_SAFE_UPDATES = 0; + UPDATE device_item d + LEFT JOIN ( + SELECT m.device_system_tag, m.device_name_tag, m.device_point_name, m.full_name + FROM import_niagara_item m + WHERE (m.is_history = 1) + LIMIT 100000 -- Specify your desired limit here + ) AS subquery + ON d.device_system_tag = subquery.device_system_tag + AND d.device_name_tag = subquery.device_name_tag + AND d.points = subquery.device_point_name + AND d.full_name = subquery.full_name + SET d.is_show_history = CASE WHEN subquery.device_point_name IS NULL THEN 0 ELSE 1 END + WHERE (d.is_show_history);"); + + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { - throw exception; - } - finally - { - conn.Close(); + await conn.ExecuteAsync(sb.ToString()); } } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } } } @@ -620,73 +645,76 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + try { - try + List result; + StringBuilder sb = new StringBuilder(); + #region comparison building_menu and import_niagara_tag + sb.Append($@" select a.* from ( + 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 + LEFT JOIN building_menu b + ON a.device_building_tag COLLATE utf8mb4_0900_ai_ci = b.device_building_tag COLLATE utf8mb4_0900_ai_ci and + a.device_system_tag COLLATE utf8mb4_0900_ai_ci = b.device_system_tag COLLATE utf8mb4_0900_ai_ci and + a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.sub_system_tag COLLATE utf8mb4_0900_ai_ci + WHERE b.device_building_tag IS NULL"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); + #endregion + sb.Clear(); + + if (result.Count > 0) { - List result; - StringBuilder sb = new StringBuilder(); - #region comparison building_menu and import_niagara_tag - sb.Append($@" select a.* from ( - 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 - LEFT JOIN building_menu b - ON a.device_building_tag COLLATE utf8mb4_0900_ai_ci = b.device_building_tag COLLATE utf8mb4_0900_ai_ci and - a.device_system_tag COLLATE utf8mb4_0900_ai_ci = b.device_system_tag COLLATE utf8mb4_0900_ai_ci and - a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.sub_system_tag COLLATE utf8mb4_0900_ai_ci - WHERE b.device_building_tag IS NULL"); - result = (await conn.QueryAsync(sb.ToString())).ToList(); - #endregion - sb.Clear(); - - if (result.Count > 0) + foreach (var data in result) { - foreach (var data in result) - { - StringBuilder sqlString = new StringBuilder(); - sqlString.Append(@"select * from building_menu where building_tag = '" + data.device_building_tag + "' and main_system_tag = '" + data.device_system_tag + "' and sub_system_tag = '" + data.device_name_tag + "'"); - var bm = (await conn.QueryAsync(sqlString.ToString())).ToList(); + StringBuilder sqlString = new StringBuilder(); + sqlString.Append(@"select * from building_menu where building_tag = '" + data.device_building_tag + "' and main_system_tag = '" + data.device_system_tag + "' and sub_system_tag = '" + data.device_name_tag + "'"); + var bm = (await conn.QueryAsync(sqlString.ToString())).ToList(); - #region insert building_menu - if (bm.Count == 0) - { - sb.Append(@$"insert building_menu(building_tag, main_system_tag, sub_system_tag, device_building_tag, device_system_tag, - left_drawing, left_icon_click, left_icon_click_url_width, left_icon_click_url_height, left_planimetric_click, - is_link, created_by, created_at, updated_by, updated_at) - values('{data.device_building_tag}', '{data.device_system_tag}', '{data.device_name_tag}', '{data.device_building_tag}', '{data.device_system_tag}', - 4, 1, 0, 0, 1, - 1, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); - } - #endregion + #region insert building_menu + if (bm.Count == 0) + { + sb.Append(@$"insert building_menu(building_tag, main_system_tag, sub_system_tag, device_building_tag, device_system_tag, + left_drawing, left_icon_click, left_icon_click_url_width, left_icon_click_url_height, left_planimetric_click, + is_link, created_by, created_at, updated_by, updated_at) + values('{data.device_building_tag}', '{data.device_system_tag}', '{data.device_name_tag}', '{data.device_building_tag}', '{data.device_system_tag}', + 4, 1, 0, 0, 1, + 1, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); } - if (sb.Length > 0) + #endregion + } + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); } + sb.Clear(); } - //building_menu有,import_niagara_tag沒有,is_link 更新成 0 - sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE building_menu b LEFT JOIN ( - 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.building_tag COLLATE utf8mb4_0900_ai_ci = a.device_building_tag COLLATE utf8mb4_0900_ai_ci - and a.device_system_tag COLLATE utf8mb4_0900_ai_ci = b.main_system_tag COLLATE utf8mb4_0900_ai_ci and a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.sub_system_tag COLLATE utf8mb4_0900_ai_ci - SET b.is_link = 0 - WHERE a.device_building_tag IS NULL"); + } + //building_menu有,import_niagara_tag沒有,is_link 更新成 0 + sb.Append($@" SET SQL_SAFE_UPDATES = 0; + UPDATE building_menu b LEFT JOIN ( + 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.building_tag COLLATE utf8mb4_0900_ai_ci = a.device_building_tag COLLATE utf8mb4_0900_ai_ci + and a.device_system_tag COLLATE utf8mb4_0900_ai_ci = b.main_system_tag COLLATE utf8mb4_0900_ai_ci and a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.sub_system_tag COLLATE utf8mb4_0900_ai_ci + SET b.is_link = 0 + WHERE a.device_building_tag IS NULL"); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { await conn.ExecuteAsync(sb.ToString()); } - catch (Exception exception) - { - throw exception; - } - finally - { - conn.Close(); - } + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); } } } @@ -700,71 +728,75 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + try { - try - { - List result; - StringBuilder sb = new StringBuilder(); - #region comparison sub_system_floor and import_niagara_tag - sb.Append($@" select a.* from ( - select device_building_tag, device_system_tag, device_name_tag, device_floor_tag - from import_niagara_tag - group by device_building_tag, device_system_tag, device_name_tag, device_floor_tag - ) AS a - LEFT JOIN sub_system_floor b - ON a.device_building_tag COLLATE utf8mb4_0900_ai_ci = b.building_tag COLLATE utf8mb4_0900_ai_ci and - a.device_system_tag COLLATE utf8mb4_0900_ai_ci = b.main_system_tag COLLATE utf8mb4_0900_ai_ci and - a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.sub_system_tag COLLATE utf8mb4_0900_ai_ci and - a.device_floor_tag COLLATE utf8mb4_0900_ai_ci = b.floor_tag COLLATE utf8mb4_0900_ai_ci - WHERE b.building_tag IS NULL"); - result = (await conn.QueryAsync(sb.ToString())).ToList(); - #endregion + List result; + StringBuilder sb = new StringBuilder(); + #region comparison sub_system_floor and import_niagara_tag + sb.Append($@" select a.* from ( + select device_building_tag, device_system_tag, device_name_tag, device_floor_tag + from import_niagara_tag + group by device_building_tag, device_system_tag, device_name_tag, device_floor_tag + ) AS a + LEFT JOIN sub_system_floor b + ON a.device_building_tag COLLATE utf8mb4_0900_ai_ci = b.building_tag COLLATE utf8mb4_0900_ai_ci and + a.device_system_tag COLLATE utf8mb4_0900_ai_ci = b.main_system_tag COLLATE utf8mb4_0900_ai_ci and + a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.sub_system_tag COLLATE utf8mb4_0900_ai_ci and + a.device_floor_tag COLLATE utf8mb4_0900_ai_ci = b.floor_tag COLLATE utf8mb4_0900_ai_ci + WHERE b.building_tag IS NULL"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); + #endregion - sb.Clear(); - if (result.Count > 0) + sb.Clear(); + if (result.Count > 0) + { + foreach (var data in result) { - foreach (var data in result) - { - #region insert building_menu - sb.Append(@"insert sub_system_floor(building_tag, main_system_tag, sub_system_tag, floor_tag, - is_link, created_by, created_at, updated_by, updated_at) - VALUES ('" + - data.device_building_tag + "', '" + - data.device_system_tag + "', '" + - data.device_name_tag + "', '" + - data.device_floor_tag + "', " + - "1, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); - #endregion - } - if (sb.Length > 0) + #region insert building_menu + sb.Append(@"insert sub_system_floor(building_tag, main_system_tag, sub_system_tag, floor_tag, + is_link, created_by, created_at, updated_by, updated_at) + VALUES ('" + + data.device_building_tag + "', '" + + data.device_system_tag + "', '" + + data.device_name_tag + "', '" + + data.device_floor_tag + "', " + + "1, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); + #endregion + } + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); } + sb.Clear(); } - //building_menu有,import_niagara_tag沒有,is_link 更新成 0 - sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE sub_system_floor b LEFT JOIN ( - select device_building_tag, device_system_tag, device_name_tag, device_floor_tag - from import_niagara_tag - group by device_building_tag, device_system_tag, device_name_tag, device_floor_tag - ) AS a ON b.building_tag COLLATE utf8mb4_0900_ai_ci = a.device_building_tag COLLATE utf8mb4_0900_ai_ci - and b.main_system_tag COLLATE utf8mb4_0900_ai_ci = a.device_system_tag COLLATE utf8mb4_0900_ai_ci - and b.sub_system_tag COLLATE utf8mb4_0900_ai_ci = a.device_name_tag COLLATE utf8mb4_0900_ai_ci - and b.floor_tag COLLATE utf8mb4_0900_ai_ci = a.device_floor_tag COLLATE utf8mb4_0900_ai_ci - SET b.is_link = 0 - WHERE b.building_tag IS NULL"); + } + //building_menu有,import_niagara_tag沒有,is_link 更新成 0 + sb.Append($@" SET SQL_SAFE_UPDATES = 0; + UPDATE sub_system_floor b LEFT JOIN ( + select device_building_tag, device_system_tag, device_name_tag, device_floor_tag + from import_niagara_tag + group by device_building_tag, device_system_tag, device_name_tag, device_floor_tag + ) AS a ON b.building_tag COLLATE utf8mb4_0900_ai_ci = a.device_building_tag COLLATE utf8mb4_0900_ai_ci + and b.main_system_tag COLLATE utf8mb4_0900_ai_ci = a.device_system_tag COLLATE utf8mb4_0900_ai_ci + and b.sub_system_tag COLLATE utf8mb4_0900_ai_ci = a.device_name_tag COLLATE utf8mb4_0900_ai_ci + and b.floor_tag COLLATE utf8mb4_0900_ai_ci = a.device_floor_tag COLLATE utf8mb4_0900_ai_ci + SET b.is_link = 0 + WHERE b.building_tag IS NULL"); + + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { await conn.ExecuteAsync(sb.ToString()); } - catch (Exception exception) - { - throw exception; - } - finally - { - conn.Close(); - } + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); } } } @@ -1123,12 +1155,10 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + try { - try - { - //改成每次都新增 - string sql = @"SET NAMES utf8mb4; + //改成每次都新增 + string sql = @"SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- @@ -1136,74 +1166,79 @@ SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- DROP TABLE IF EXISTS `import_niagara_item_history`; CREATE TABLE `import_niagara_item_history` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `device_area_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_building_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_system_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_floor_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_master_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_last_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_serial_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `device_point_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `parent_path` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `is_history` bit(1) NULL DEFAULT b'0', - `full_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `check_status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, - `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) USING BTREE +`id` int(11) NOT NULL AUTO_INCREMENT, +`device_area_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_building_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_system_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_floor_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_master_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_last_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_serial_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`device_point_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`parent_path` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`is_history` bit(1) NULL DEFAULT b'0', +`full_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`check_status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, +`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 271 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;"; + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { await conn.ExecuteAsync(sql); + } - #region 刪除 import_niagara_item資料表中選取的棟別 - //foreach (var b in building) - //{ + #region 刪除 import_niagara_item資料表中選取的棟別 + //foreach (var b in building) + //{ - // sql = "delete from import_niagara_item_history where device_building_tag = '" + b + "'"; - // await conn.ExecuteAsync(sql); - //} - #endregion + // sql = "delete from import_niagara_item_history where device_building_tag = '" + b + "'"; + // await conn.ExecuteAsync(sql); + //} + #endregion - StringBuilder sb = new StringBuilder(); - #region 放入import_niagara_item資料表 - foreach (var row in ds) - { - string ss = $@" insert import_niagara_item_history(device_area_tag, device_building_tag, device_system_tag, device_name_tag, + StringBuilder sb = new StringBuilder(); + #region 放入import_niagara_item資料表 + foreach (var row in ds) + { + string ss = $@" insert import_niagara_item_history(device_area_tag, device_building_tag, device_system_tag, device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, device_point_name, parent_path, full_name, is_history, created_at) - values('" + - row.device_area_tag + "', '" + - row.device_building_tag + "', '" + - row.device_system_tag + "', '" + - row.device_name_tag + "', '" + - row.device_floor_tag + "', '" + - row.device_master_tag + "', '" + - row.device_last_name_tag + "', '" + - row.device_serial_tag + "', '" + - row.device_point_name + "', '" + - row.parent_path + "', '" + - row.full_name + "'," + - row.isHistory + ", now() " + - ");"; - sb.Append(ss); - } - if (sb.Length > 0) + values('" + + row.device_area_tag + "', '" + + row.device_building_tag + "', '" + + row.device_system_tag + "', '" + + row.device_name_tag + "', '" + + row.device_floor_tag + "', '" + + row.device_master_tag + "', '" + + row.device_last_name_tag + "', '" + + row.device_serial_tag + "', '" + + row.device_point_name + "', '" + + row.parent_path + "', '" + + row.full_name + "'," + + row.isHistory + ", now() " + + ");"; + sb.Append(ss); + } + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); } - #endregion - } - catch (Exception exception) - { - throw exception; - } - finally - { - conn.Close(); + sb.Clear(); } + #endregion + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); } } }