diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs
index cabd432..1e13bef 100644
--- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs
+++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs
@@ -90,11 +90,11 @@ namespace Repository.BackendRepository.Implement
{
tag_name = x.Key.tag_name2,
displayName = x.Key.displayName2
- });
-
+ });
+
// StringBuilder sb = new StringBuilder();
bool isDome = false; //是否為巨蛋案
- int count = 0;
+ int count = 0;
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach (var row in ds2)
@@ -125,7 +125,7 @@ namespace Repository.BackendRepository.Implement
{
if (arrTag.Length == 5)
{
- isDome= true;
+ 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_model_tag, device_full_name, atDateTime) values('" +
row.tag_name + "', '" + //niagara_tags
@@ -209,7 +209,7 @@ namespace Repository.BackendRepository.Implement
}
}
}
-
+
///
/// 獲取照明開關 是否在 device_node 層
///
@@ -220,9 +220,9 @@ namespace Repository.BackendRepository.Implement
stopwatch.Start();
using (IDbConnection conn = GetDbConnection())
{
- conn.Open();
+ conn.Open();
var sql = "select system_value from variable where system_type = 'module_light_switch' ";
- var module_light_switch = conn.QueryAsync(sql).Result.FirstOrDefault();
+ var module_light_switch = conn.QueryAsync(sql).Result.FirstOrDefault();
conn.Close();
stopwatch.Stop();
await KeepTimeLog("getLightSwitchLevel", stopwatch.ElapsedMilliseconds);
@@ -250,7 +250,7 @@ namespace Repository.BackendRepository.Implement
{
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
- foreach(var b in building)
+ foreach (var b in building)
{
string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
@@ -281,7 +281,8 @@ namespace Repository.BackendRepository.Implement
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
- ds = ds.GroupBy(x => new {
+ 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,
@@ -304,7 +305,7 @@ namespace Repository.BackendRepository.Implement
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('"+
+ values('" +
row.device_area_tag + "', '" +
row.device_building_tag + "', '" +
row.device_system_tag + "', '" +
@@ -446,7 +447,7 @@ namespace Repository.BackendRepository.Implement
{
conn.Close();
}
-
+
}
}
@@ -456,376 +457,404 @@ namespace Repository.BackendRepository.Implement
///
public async Task DeviceComparison(string LightSwitchLevel)
{
- stopwatch = new Stopwatch();
- stopwatch.Start();
+ Stopwatch stopwatch = Stopwatch.StartNew();
+
using (IDbConnection conn = GetDbConnection())
{
conn.Open();
+
try
{
- List result;
- StringBuilder sb = new StringBuilder();
- StringBuilder sb2 = new StringBuilder();
- stopwatchSection = new Stopwatch();
- stopwatchSection.Start();
- 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();
- stopwatchSection.Stop();
- await KeepTimeLog("filter device item from import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
+ // 1. 取得需要新增的 NiagaraTags
+ Stopwatch sw1 = Stopwatch.StartNew();
+ string sqlGetNiagaraTags = @"
+ SELECT m.*
+ FROM import_niagara_tag m
+ WHERE NOT EXISTS (
+ SELECT 1
+ FROM device d
+ WHERE m.niagara_tags = d.device_number
+ );";
+ List niagaraTagsToAdd = (await conn.QueryAsync(sqlGetNiagaraTags)).ToList();
+ sw1.Stop();
+ await KeepTimeLog("filter device item from import_niagara_tag", sw1.ElapsedMilliseconds);
- #region device, device_kind 新增
- stopwatchSection = new Stopwatch();
- stopwatchSection.Start();
- int count = 0;
- //新增至device, is_link = 1
- if (result.Count > 0)
+ // 2. 新增 device 和 device_kind
+ if (niagaraTagsToAdd.Count > 0)
{
- var result2 = result.GroupBy(x => new
+ // 2.1. 新增 device
+ Stopwatch sw2 = Stopwatch.StartNew();
+ List sqlInsertDeviceList = new List();
+ foreach (var data in niagaraTagsToAdd)
{
- 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
- });
-
- 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
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_model_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.device_model_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;
- }
- }
+
+ string sqlInsertDevice = @"
+ INSERT INTO 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_model_tag, device_number, device_system_category_layer3, visible, created_at, updated_at)
+ VALUES (uuid(), 0, 1, 0, 1, @device_area_tag, @device_building_tag, @device_system_tag, @device_name_tag,
+ @full_name, @device_floor_tag, @device_master_tag, @device_last_name_tag, @device_serial_tag,
+ @device_model_tag, @niagara_tags, @device_system_tag, 1, now(), now());";
+
+ sqlInsertDeviceList.Add(sqlInsertDevice); // 先暫存SQL語法,等等一次執行
}
- if (sb.Length > 0)
+ // 2.1.1 分批執行新增 device
+ int batchSize = 100;
+ for (int i = 0; i < sqlInsertDeviceList.Count; i += batchSize)
{
+ //建立參數
+ var batch = niagaraTagsToAdd.Skip(i).Take(batchSize).ToList();
+ //建立參數化查詢要用的物件
+ List