From 7ab418c7b1839b454abbe6134b5deafe390e4e71 Mon Sep 17 00:00:00 2001 From: dev02 Date: Thu, 14 Sep 2023 17:30:26 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9device=20item=20=E7=8D=B2?= =?UTF-8?q?=E5=8F=96=E8=B3=87=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Implement/webRequestService.cs | 25 +- .../NiagaraDataSynchronizeRepository.cs | 322 ++++++++++-------- 2 files changed, 183 insertions(+), 164 deletions(-) diff --git a/Backend/Services/Implement/webRequestService.cs b/Backend/Services/Implement/webRequestService.cs index 9e1996f..2386ec1 100644 --- a/Backend/Services/Implement/webRequestService.cs +++ b/Backend/Services/Implement/webRequestService.cs @@ -235,24 +235,23 @@ namespace Backend.Services.Implement result.Add(row); } - else if (tagName.Split('_').Length == 6) + else if (tagName.Split('_').Length == 7) { //巨蛋 tag 5 段版本 ImpNiaItem row = new ImpNiaItem(); - if (tagName.Split('_')[1] == "S") //for security system + if (tagName.Split('_')[2] == "S" || tagName.Split('_')[2] == "B" || tagName.Split('_')[2] == "CWB") //for security system and parking system { - row.device_building_tag = tagName.Split('_')[0]; - row.device_system_tag = tagName.Split('_')[1]; - row.device_floor_tag = tagName.Split('_')[2]; - row.device_name_tag = tagName.Split('_')[3]; - row.device_point_name = tagName.Split('_')[5]; + row.device_building_tag = tagName.Split('_')[1]; + row.device_system_tag = tagName.Split('_')[2]; + row.device_floor_tag = tagName.Split('_')[3]; + row.device_name_tag = tagName.Split('_')[4]; + row.device_point_name = tagName.Split('_')[6]; } else //for normal { - row.device_building_tag = tagName.Split('_')[0]; - //row.device_system_tag = tagName.Split('_')[1]; - row.device_floor_tag = tagName.Split('_')[2]; - row.device_name_tag = tagName.Split('_')[1]; - row.device_point_name = tagName.Split('_')[5]; + row.device_building_tag = tagName.Split('_')[1]; + row.device_floor_tag = tagName.Split('_')[3]; + row.device_name_tag = tagName.Split('_')[2]; + row.device_point_name = tagName.Split('_')[6]; } row.parent_path = stationName; @@ -288,7 +287,7 @@ namespace Backend.Services.Implement } else if (n.name.Split('/').Length == 7) // Arena/D2/CWB/L110/CAP/D2_CWB_L110_CAP_MET1/Summery { - if (n.name.Split('/')[3] == "S" || n.name.Split('/')[3] == "CWB") //for security system and CWB system + if (n.name.Split('/')[2] == "S" || n.name.Split('/')[2] == "B" || n.name.Split('/')[2] == "CWB") //for security system and CWB system { result.Add(new ImpNiaItem { device_building_tag = n.name.Split('/')[1], diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index e554cc8..b30d49e 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -283,7 +283,7 @@ namespace Repository.BackendRepository.Implement 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');"); + where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B');"); } sb.Append($@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;"); @@ -315,200 +315,220 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) - try + try + { + List result; + StringBuilder sb = new StringBuilder(); + StringBuilder sb2 = new StringBuilder(); + sb.Append($@"SELECT m.* + FROM import_niagara_tag m + WHERE NOT EXISTS ( + SELECT 1 + FROM device d + WHERE m.niagara_tags = d.device_number + );"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); + + sb.Clear(); + #region device, device_kind 新增 + //新增至device, is_link = 1 + if (result.Count > 0) { - List result; - StringBuilder sb = new StringBuilder(); - StringBuilder sb2 = new StringBuilder(); - sb.Append($@"SELECT m.* - FROM import_niagara_tag m - WHERE NOT EXISTS ( - SELECT 1 - FROM device d - WHERE m.niagara_tags = d.device_number - );"); - result = (await conn.QueryAsync(sb.ToString())).ToList(); - - sb.Clear(); - #region device, device_kind 新增 - //新增至device, is_link = 1 - if (result.Count > 0) + var result2 = result.GroupBy(x => new { - var result2 = result.GroupBy(x => new - { - device_building_tag2 = x.device_building_tag, - device_system_tag2 = x.device_system_tag, - device_name_tag2 = x.device_name_tag - }).Select(x => new Device_item8 - { - device_building_tag = x.Key.device_building_tag2, - device_system_tag = x.Key.device_system_tag2, - device_name_tag = x.Key.device_name_tag2 - }); + device_building_tag2 = x.device_building_tag, + device_system_tag2 = x.device_system_tag, + device_name_tag2 = x.device_name_tag + }).Select(x => new Device_item8 + { + device_building_tag = x.Key.device_building_tag2, + device_system_tag = x.Key.device_system_tag2, + device_name_tag = x.Key.device_name_tag2 + }); - int count = 0; - foreach (var data in result) + int count = 0; + foreach (var data in result) + { + //開關控制在個別燈具(device_node層) and 小類為二線式照明系統 and tag第八段開頭不是 G + if (LightSwitchLevel == "node" && data.device_name_tag == "L1" && data.device_serial_tag.Substring(0, 1) != "G") { - //開關控制在個別燈具(device_node層) and 小類為二線式照明系統 and tag第八段開頭不是 G - if (LightSwitchLevel == "node" && data.device_name_tag == "L1" && data.device_serial_tag.Substring(0, 1) != "G") + // 燈具新增 device_node + continue; + } + else { + sb.Append($@" insert device(device_guid, deleted, status, priority, is_link, device_area_tag, + device_building_tag, device_system_tag, device_name_tag, full_name, device_floor_tag, device_master, + device_last_name, device_serial_tag, device_number, device_system_category_layer3, visible, created_at, updated_at) + values(uuid(), 0, 1, 0, 1, '" + + data.device_area_tag + "', '" + + data.device_building_tag + "', '" + + data.device_system_tag + "', '" + + data.device_name_tag + "', '" + + data.device_full_name + "', '" + + data.device_floor_tag + "', '" + + data.device_master_tag + "', '" + + data.device_last_name_tag + "', '" + + data.device_serial_tag + "', '" + + data.niagara_tags + "', '" + + data.device_system_tag + "', 1, now(), now() );"); + count += 1; + if (count >= 100) { - // 燈具新增 device_node - continue; - } - else { - sb.Append($@" insert device(device_guid, deleted, status, priority, is_link, device_area_tag, - device_building_tag, device_system_tag, device_name_tag, full_name, device_floor_tag, device_master, - device_last_name, device_serial_tag, device_number, device_system_category_layer3, visible, created_at, updated_at) - values(uuid(), 0, 1, 0, 1, '" + - data.device_area_tag + "', '" + - data.device_building_tag + "', '" + - data.device_system_tag + "', '" + - data.device_name_tag + "', '" + - data.device_full_name + "', '" + - data.device_floor_tag + "', '" + - data.device_master_tag + "', '" + - data.device_last_name_tag + "', '" + - data.device_serial_tag + "', '" + - data.niagara_tags + "', '" + - data.device_system_tag + "', 1, now(), now() );"); - count += 1; - if (count >= 100) + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); - count = 0; } - } - } + sb.Clear(); + count = 0; + } + } + } - count = 0; - foreach (var data in result2) + count = 0; + foreach (var data in result2) + { + var sqlString = new StringBuilder(); + sqlString.Append("select * from device_kind where device_building_tag = '" + data.device_building_tag + "' and device_system_tag = '" + data.device_system_tag + "' and device_name_tag = '" + data.device_name_tag + "'"); + var dk = (await conn.QueryAsync(sqlString.ToString())).ToList(); + + if (dk.Count == 0) { - var sqlString = new StringBuilder(); - sqlString.Append("select * from device_kind where device_building_tag = '" + data.device_building_tag + "' and device_system_tag = '" + data.device_system_tag + "' and device_name_tag = '" + data.device_name_tag + "'"); - var dk = (await conn.QueryAsync(sqlString.ToString())).ToList(); - - if (dk.Count == 0) + sb2.Append($@"INSERT device_kind (device_kind_guid, device_building_tag, device_system_tag, device_name_tag, + device_normal_flashing, device_close_flashing, device_error_flashing, device_error_independent, + created_by, created_at) + VALUES (uuid(), '" + data.device_building_tag + "', '" + data.device_system_tag + "', '" + data.device_name_tag + + "', 0, 0, 1, 0, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); + count += 1; + if (count >= 100) { - sb2.Append($@"INSERT device_kind (device_kind_guid, device_building_tag, device_system_tag, device_name_tag, - device_normal_flashing, device_close_flashing, device_error_flashing, device_error_independent, - created_by, created_at) - VALUES (uuid(), '" + data.device_building_tag + "', '" + data.device_system_tag + "', '" + data.device_name_tag + - "', 0, 0, 1, 0, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); - count += 1; - if (count >= 100) + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb2.ToString()); - sb2.Clear(); - count = 0; } + sb2.Clear(); + count = 0; } } + } - if (sb.Length > 0) + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb2.ToString()); - - sb.Clear(); - sb2.Clear(); } + + sb.Clear(); + sb2.Clear(); } - #endregion + } + #endregion - #region device_node 新增 - sb.Append($@" SELECT m.* - FROM import_niagara_tag m - WHERE NOT EXISTS ( - SELECT 1 - FROM device_node d - WHERE m.niagara_tags COLLATE utf8mb4_0900_ai_ci = d.device_number COLLATE utf8mb4_0900_ai_ci - );"); - result = (await conn.QueryAsync(sb.ToString())).ToList(); - sb.Clear(); + #region device_node 新增 + sb.Append($@" SELECT m.* + FROM import_niagara_tag m + WHERE NOT EXISTS ( + SELECT 1 + FROM device_node d + WHERE m.niagara_tags COLLATE utf8mb4_0900_ai_ci = d.device_number COLLATE utf8mb4_0900_ai_ci + );"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); + sb.Clear(); - if (result.Count > 0) + if (result.Count > 0) + { + var result2 = result.GroupBy(x => new { - var result2 = result.GroupBy(x => new - { - device_building_tag2 = x.device_building_tag, - device_system_tag2 = x.device_system_tag, - device_name_tag2 = x.device_name_tag - }).Select(x => new Device_item8 - { - device_building_tag = x.Key.device_building_tag2, - device_system_tag = x.Key.device_system_tag2, - device_name_tag = x.Key.device_name_tag2 - }); + device_building_tag2 = x.device_building_tag, + device_system_tag2 = x.device_system_tag, + device_name_tag2 = x.device_name_tag + }).Select(x => new Device_item8 + { + device_building_tag = x.Key.device_building_tag2, + device_system_tag = x.Key.device_system_tag2, + device_name_tag = x.Key.device_name_tag2 + }); - int count = 0; - foreach (var data in result) + int count = 0; + foreach (var data in result) + { + //開關控制在個別燈具(device_node層) and 小類為二線式照明系統 and tag第八段開頭不是 G + if (LightSwitchLevel == "node" && data.device_name_tag == "L1" && data.device_serial_tag.Substring(0, 1) != "G") { - //開關控制在個別燈具(device_node層) and 小類為二線式照明系統 and tag第八段開頭不是 G - if (LightSwitchLevel == "node" && data.device_name_tag == "L1" && data.device_serial_tag.Substring(0, 1) != "G") + // 燈具新增 device_node + sb.Append($@"INSERT INTO device_node(device_node_guid, deleted, device_guid, device_number, full_name, created_by, created_at, updated_at) + VALUES (uuid(), 0, '' " + //device_guid 父層需要 forge 那邊提供 + ",'" + data.niagara_tags + // device_number + "', '" + data.device_full_name + //full_name + "','B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), now());"); + count += 1; + if (count >= 100) { - // 燈具新增 device_node - sb.Append($@"INSERT INTO device_node(device_node_guid, deleted, device_guid, device_number, full_name, created_by, created_at, updated_at) - VALUES (uuid(), 0, '' " + //device_guid 父層需要 forge 那邊提供 - ",'" + data.niagara_tags + // device_number - "', '" + data.device_full_name + //full_name - "','B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), now());"); - count += 1; - if (count >= 100) + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); - sb.Clear(); - count = 0; } + sb.Clear(); + count = 0; } } + } - if (sb.Length > 0) + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)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 d + //device有,niagara沒有,is_link 更新成 0 + sb.Append($@" SET SQL_SAFE_UPDATES = 0; + UPDATE device d + left JOIN ( + SELECT niagara_tags + FROM import_niagara_tag + LIMIT 100000 + ) i ON d.device_number = i.niagara_tags + SET d.is_link = 0 + WHERE d.is_link = 1 and i.niagara_tags is null;"); + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) + { + await conn.ExecuteAsync(sb.ToString()); + } + + sb.Clear(); + + // device_node 有, niagara沒有, is_link 更新成 0 + sb.Append($@" SET SQL_SAFE_UPDATES = 0; + UPDATE device_node dn left JOIN ( - SELECT niagara_tags - FROM import_niagara_tag - LIMIT 100000 - ) i ON d.device_number = i.niagara_tags - SET d.is_link = 0 - WHERE d.is_link = 1 and i.niagara_tags is null;"); - await conn.ExecuteAsync(sb.ToString()); + SELECT niagara_tags + FROM import_niagara_tag + LIMIT 100000 + ) i ON dn.device_number COLLATE utf8mb4_0900_ai_ci = i.niagara_tags COLLATE utf8mb4_0900_ai_ci + SET dn.is_link = 0 + WHERE dn.is_link = 1 and i.niagara_tags is null;"); - sb.Clear(); - - // device_node 有, niagara沒有, is_link 更新成 0 - sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE device_node dn - left JOIN ( - SELECT niagara_tags - FROM import_niagara_tag - LIMIT 100000 - ) i ON dn.device_number COLLATE utf8mb4_0900_ai_ci = i.niagara_tags COLLATE utf8mb4_0900_ai_ci - SET dn.is_link = 0 - WHERE dn.is_link = 1 and i.niagara_tags is null;"); + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) + { await conn.ExecuteAsync(sb.ToString()); } - catch (Exception exception) - { - //throw exception; - throw; - } - finally - { - conn.Close(); - } - { } + catch (Exception exception) + { + //throw exception; + throw; + } + finally + { + conn.Close(); + } + } }