[後端] 修改同步修改device item 獲取資料

This commit is contained in:
dev02 2023-09-14 17:30:26 +08:00
parent 5931e057b0
commit 7ab418c7b1
2 changed files with 183 additions and 164 deletions

View File

@ -235,24 +235,23 @@ namespace Backend.Services.Implement
result.Add(row); result.Add(row);
} }
else if (tagName.Split('_').Length == 6) else if (tagName.Split('_').Length == 7)
{ //巨蛋 tag 5 段版本 { //巨蛋 tag 5 段版本
ImpNiaItem row = new ImpNiaItem(); 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_building_tag = tagName.Split('_')[1];
row.device_system_tag = tagName.Split('_')[1]; row.device_system_tag = tagName.Split('_')[2];
row.device_floor_tag = tagName.Split('_')[2]; row.device_floor_tag = tagName.Split('_')[3];
row.device_name_tag = tagName.Split('_')[3]; row.device_name_tag = tagName.Split('_')[4];
row.device_point_name = tagName.Split('_')[5]; row.device_point_name = tagName.Split('_')[6];
} }
else //for normal else //for normal
{ {
row.device_building_tag = tagName.Split('_')[0]; row.device_building_tag = tagName.Split('_')[1];
//row.device_system_tag = tagName.Split('_')[1]; row.device_floor_tag = tagName.Split('_')[3];
row.device_floor_tag = tagName.Split('_')[2]; row.device_name_tag = tagName.Split('_')[2];
row.device_name_tag = tagName.Split('_')[1]; row.device_point_name = tagName.Split('_')[6];
row.device_point_name = tagName.Split('_')[5];
} }
row.parent_path = stationName; 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 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 { result.Add(new ImpNiaItem {
device_building_tag = n.name.Split('/')[1], device_building_tag = n.name.Split('/')[1],

View File

@ -283,7 +283,7 @@ namespace Repository.BackendRepository.Implement
sb.Append($@" update import_niagara_item a join dic_system b 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 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 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;"); 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()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) try
try {
List<NiagaraTags> 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<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear();
#region device, device_kind
//新增至device, is_link = 1
if (result.Count > 0)
{ {
List<NiagaraTags> result; var result2 = result.GroupBy(x => new
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<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear();
#region device, device_kind
//新增至device, is_link = 1
if (result.Count > 0)
{ {
var result2 = result.GroupBy(x => new device_building_tag2 = x.device_building_tag,
{ device_system_tag2 = x.device_system_tag,
device_building_tag2 = x.device_building_tag, device_name_tag2 = x.device_name_tag
device_system_tag2 = x.device_system_tag, }).Select(x => new Device_item8
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_building_tag = x.Key.device_building_tag2, device_name_tag = x.Key.device_name_tag2
device_system_tag = x.Key.device_system_tag2, });
device_name_tag = x.Key.device_name_tag2
});
int count = 0; int count = 0;
foreach (var data in result) 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 // 燈具新增 device_node
if (LightSwitchLevel == "node" && data.device_name_tag == "L1" && data.device_serial_tag.Substring(0, 1) != "G") 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 using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
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)
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
sb.Clear();
count = 0;
} }
sb.Clear();
count = 0;
} }
} }
}
count = 0; count = 0;
foreach (var data in result2) 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<device_kind>(sqlString.ToString())).ToList<device_kind>();
if (dk.Count == 0)
{ {
var sqlString = new StringBuilder(); sb2.Append($@"INSERT device_kind (device_kind_guid, device_building_tag, device_system_tag, device_name_tag,
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 + "'"); device_normal_flashing, device_close_flashing, device_error_flashing, device_error_independent,
var dk = (await conn.QueryAsync<device_kind>(sqlString.ToString())).ToList<device_kind>(); created_by, created_at)
VALUES (uuid(), '" + data.device_building_tag + "', '" + data.device_system_tag + "', '" + data.device_name_tag +
if (dk.Count == 0) "', 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, using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
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)
{ {
await conn.ExecuteAsync(sb2.ToString()); 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(sb.ToString());
await conn.ExecuteAsync(sb2.ToString()); await conn.ExecuteAsync(sb2.ToString());
sb.Clear();
sb2.Clear();
} }
sb.Clear();
sb2.Clear();
} }
#endregion }
#endregion
#region device_node #region device_node
sb.Append($@" SELECT m.* sb.Append($@" SELECT m.*
FROM import_niagara_tag m FROM import_niagara_tag m
WHERE NOT EXISTS ( WHERE NOT EXISTS (
SELECT 1 SELECT 1
FROM device_node d FROM device_node d
WHERE m.niagara_tags COLLATE utf8mb4_0900_ai_ci = d.device_number COLLATE utf8mb4_0900_ai_ci WHERE m.niagara_tags COLLATE utf8mb4_0900_ai_ci = d.device_number COLLATE utf8mb4_0900_ai_ci
);"); );");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear(); 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_building_tag2 = x.device_building_tag, device_name_tag2 = x.device_name_tag
device_system_tag2 = x.device_system_tag, }).Select(x => new Device_item8
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_building_tag = x.Key.device_building_tag2, device_name_tag = x.Key.device_name_tag2
device_system_tag = x.Key.device_system_tag2, });
device_name_tag = x.Key.device_name_tag2
});
int count = 0; int count = 0;
foreach (var data in result) 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 // 燈具新增 device_node
if (LightSwitchLevel == "node" && data.device_name_tag == "L1" && data.device_serial_tag.Substring(0, 1) != "G") 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 using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
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)
{ {
await conn.ExecuteAsync(sb.ToString()); 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()); await conn.ExecuteAsync(sb.ToString());
sb.Clear();
} }
sb.Clear();
} }
#endregion }
#endregion
//device有niagara沒有is_link 更新成 0 //device有niagara沒有is_link 更新成 0
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device d 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 ( left JOIN (
SELECT niagara_tags SELECT niagara_tags
FROM import_niagara_tag FROM import_niagara_tag
LIMIT 100000 LIMIT 100000
) i ON d.device_number = i.niagara_tags ) i ON dn.device_number COLLATE utf8mb4_0900_ai_ci = i.niagara_tags COLLATE utf8mb4_0900_ai_ci
SET d.is_link = 0 SET dn.is_link = 0
WHERE d.is_link = 1 and i.niagara_tags is null;"); WHERE dn.is_link = 1 and i.niagara_tags is null;");
await conn.ExecuteAsync(sb.ToString());
sb.Clear(); using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
{
// 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;");
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
catch (Exception exception)
{
//throw exception;
throw;
}
finally
{
conn.Close();
}
{
} }
catch (Exception exception)
{
//throw exception;
throw;
}
finally
{
conn.Close();
}
} }
} }