[後端] 修改transaction scope使用地方

This commit is contained in:
dev02 2023-09-13 14:41:08 +08:00
parent 0fe9c76acb
commit bf1133311d
2 changed files with 473 additions and 437 deletions

View File

@ -22,8 +22,9 @@
//"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel //"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel
//"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office //"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office
//"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //mcut //"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": "2U+9jYGy0dCbMzLaguBXow==", //tpe_dome_mall
"Database": "FbHCCU4CapG9UryANXC1W+iMe403Bd82RnA7bCNVbL0=", //ibms_dome_cinema,
"Root": "SzdxEgaJJ7tcTCrUl2zKsA==", "Root": "SzdxEgaJJ7tcTCrUl2zKsA==",
"Password": "FVAPxztxpY4gJJKQ/se4bQ==" "Password": "FVAPxztxpY4gJJKQ/se4bQ=="
}, },

View File

@ -35,8 +35,6 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try try
{ {
@ -59,10 +57,16 @@ namespace Repository.BackendRepository.Implement
`is_used` smallint(1) DEFAULT 0, `is_used` smallint(1) DEFAULT 0,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
}
sql = "delete from import_niagara_tag where device_building_tag = '" + b + "' limit 10000"; sql = "delete from import_niagara_tag where device_building_tag = '" + b + "' limit 10000";
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
}
//N4資料groupBy後放入import_niagara_tag資料表 //N4資料groupBy後放入import_niagara_tag資料表
var ds2 = ds.GroupBy(x => new var ds2 = ds.GroupBy(x => new
@ -126,7 +130,10 @@ namespace Repository.BackendRepository.Implement
if (count >= 100) if (count >= 100)
{ {
count = 0; count = 0;
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
} }
} }
@ -135,7 +142,10 @@ namespace Repository.BackendRepository.Implement
if (sb.Length > 0) if (sb.Length > 0)
{ {
var temp = sb.ToString(); var temp = sb.ToString();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(temp); await conn.ExecuteAsync(temp);
}
sb.Clear(); sb.Clear();
//巨蛋特殊處理 //巨蛋特殊處理
@ -151,10 +161,13 @@ namespace Repository.BackendRepository.Implement
set device_system_tag = b.s1_code -- tag () set device_system_tag = b.s1_code -- tag ()
where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and 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')); -- "); (device_system_tag <> 'FM' or device_system_tag <> 'APS' or device_system_tag <> 'VICL')); -- ");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
} }
} }
}
catch (Exception exception) catch (Exception exception)
{ {
Console.WriteLine(sb.ToString()); Console.WriteLine(sb.ToString());
@ -166,7 +179,6 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
}
/// <summary> /// <summary>
/// 獲取照明開關 是否在 device_node 層 /// 獲取照明開關 是否在 device_node 層
@ -196,8 +208,6 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try try
{ {
#region import_niagara_item資料表中選取的棟別 #region import_niagara_item資料表中選取的棟別
@ -215,10 +225,16 @@ namespace Repository.BackendRepository.Implement
`full_name` varchar(50) DEFAULT NULL, `full_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
}
sql = "delete from import_niagara_item where device_building_tag = '" + b + "' limit 10000;"; sql = "delete from import_niagara_item where device_building_tag = '" + b + "' limit 10000;";
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
}
#endregion #endregion
ds = ds.GroupBy(x => new { ds = ds.GroupBy(x => new {
device_area_tag = x.device_area_tag, device_area_tag = x.device_area_tag,
@ -271,7 +287,10 @@ namespace Repository.BackendRepository.Implement
} }
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;");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
} }
#endregion #endregion
@ -284,7 +303,7 @@ namespace Repository.BackendRepository.Implement
{ {
conn.Close(); conn.Close();
} }
}
} }
} }
/// <summary> /// <summary>
@ -297,7 +316,6 @@ namespace Repository.BackendRepository.Implement
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
{
try try
{ {
List<NiagaraTags> result; List<NiagaraTags> result;
@ -489,6 +507,7 @@ namespace Repository.BackendRepository.Implement
{ {
conn.Close(); conn.Close();
} }
{
} }
} }
} }
@ -502,8 +521,6 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try try
{ {
List<NiagaraTagsForItem> result; List<NiagaraTagsForItem> result;
@ -547,8 +564,11 @@ namespace Repository.BackendRepository.Implement
"now(), now());"); "now(), now());");
} }
if (sb.Length > 0) if (sb.Length > 0)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
} }
} }
@ -579,7 +599,10 @@ namespace Repository.BackendRepository.Implement
"); ");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
@ -597,8 +620,11 @@ namespace Repository.BackendRepository.Implement
SET d.is_show_history = CASE WHEN subquery.device_point_name IS NULL THEN 0 ELSE 1 END SET d.is_show_history = CASE WHEN subquery.device_point_name IS NULL THEN 0 ELSE 1 END
WHERE (d.is_show_history);"); WHERE (d.is_show_history);");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
}
catch (Exception exception) catch (Exception exception)
{ {
throw exception; throw exception;
@ -609,7 +635,6 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
}
/// <summary> /// <summary>
/// 新增資料至 buildingMenu /// 新增資料至 buildingMenu
@ -620,8 +645,6 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try try
{ {
List<NiagaraTags> result; List<NiagaraTags> result;
@ -662,8 +685,11 @@ namespace Repository.BackendRepository.Implement
#endregion #endregion
} }
if (sb.Length > 0) if (sb.Length > 0)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
} }
} }
@ -677,8 +703,11 @@ namespace Repository.BackendRepository.Implement
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_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 SET b.is_link = 0
WHERE a.device_building_tag IS NULL"); WHERE a.device_building_tag IS NULL");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
}
catch (Exception exception) catch (Exception exception)
{ {
throw exception; throw exception;
@ -689,7 +718,6 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
}
/// <summary> /// <summary>
/// 新增資料至 subSystemFloor /// 新增資料至 subSystemFloor
@ -700,8 +728,6 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try try
{ {
List<NiagaraTags> result; List<NiagaraTags> result;
@ -738,8 +764,11 @@ namespace Repository.BackendRepository.Implement
#endregion #endregion
} }
if (sb.Length > 0) if (sb.Length > 0)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
} }
} }
@ -755,8 +784,12 @@ namespace Repository.BackendRepository.Implement
and b.floor_tag COLLATE utf8mb4_0900_ai_ci = a.device_floor_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 SET b.is_link = 0
WHERE b.building_tag IS NULL"); WHERE b.building_tag IS NULL");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
}
catch (Exception exception) catch (Exception exception)
{ {
throw exception; throw exception;
@ -767,7 +800,6 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
}
/// <summary> /// <summary>
/// 檢查device內FullName為空的值以device_Name取代 /// 檢查device內FullName為空的值以device_Name取代
/// </summary> /// </summary>
@ -1123,8 +1155,6 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try try
{ {
//改成每次都新增 //改成每次都新增
@ -1136,26 +1166,29 @@ SET FOREIGN_KEY_CHECKS = 0;
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `import_niagara_item_history`; DROP TABLE IF EXISTS `import_niagara_item_history`;
CREATE TABLE `import_niagara_item_history` ( CREATE TABLE `import_niagara_item_history` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`device_area_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `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_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_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_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_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_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_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_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, `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, `parent_path` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
`is_history` bit(1) NULL DEFAULT b'0', `is_history` bit(1) NULL DEFAULT b'0',
`full_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `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, `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, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 271 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; ) ENGINE = InnoDB AUTO_INCREMENT = 271 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;"; SET FOREIGN_KEY_CHECKS = 1;";
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
}
#region import_niagara_item資料表中選取的棟別 #region import_niagara_item資料表中選取的棟別
//foreach (var b in building) //foreach (var b in building)
@ -1190,8 +1223,11 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
sb.Append(ss); sb.Append(ss);
} }
if (sb.Length > 0) if (sb.Length > 0)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
}
sb.Clear(); sb.Clear();
} }
#endregion #endregion
@ -1206,7 +1242,6 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
} }
} }
} }
}
/// <summary> /// <summary>
/// Recover device_system_tag /// Recover device_system_tag