2022-11-02 17:26:18 +08:00
using Dapper ;
using Repository.BackendRepository.Interface ;
using Repository.Helper ;
using System ;
using System.Text ;
using System.Collections.Generic ;
using System.Data ;
using System.Linq ;
using System.Threading.Tasks ;
using System.Transactions ;
using Repository.Models ;
2023-01-04 18:38:36 +08:00
using System.Data.SqlTypes ;
2023-02-07 11:55:52 +08:00
using Microsoft.Extensions.Primitives ;
2023-04-11 10:28:33 +08:00
using System.Web ;
2022-11-02 17:26:18 +08:00
namespace Repository.BackendRepository.Implement
{
public class NiagaraDataSynchronizeRepository : BackendRepository , INiagaraDataSynchronizeRepository
{
public NiagaraDataSynchronizeRepository ( IDatabaseHelper databaseHelper ) : base ( databaseHelper )
{
}
/// <summary>
/// 更新import_niagara_tag資料表
/// </summary>
/// <param name="ds"></param>
2023-03-28 13:48:30 +08:00
/// <param name="building"></param>
2022-11-02 17:26:18 +08:00
/// <returns></returns>
2023-05-10 15:14:46 +08:00
public async Task InsertNiagaraTagList ( List < Device_value > ds , List < string > building , string tag_quantity )
2022-11-02 17:26:18 +08:00
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
2023-05-10 15:14:46 +08:00
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 ,
` is_used ` smallint ( 1 ) DEFAULT 0 ,
PRIMARY KEY ( ` id ` )
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci ; ";
await conn . ExecuteAsync ( sql ) ;
sql = "delete from import_niagara_tag where device_building_tag = '" + b + "'" ;
await conn . ExecuteAsync ( sql ) ;
}
2022-11-02 17:26:18 +08:00
//N4資料groupBy後放入import_niagara_tag資料表
var ds2 = ds . GroupBy ( x = > new
{
2023-01-30 16:04:29 +08:00
tag_name2 = x . tag_name ,
displayName2 = x . displayName
2022-11-02 17:26:18 +08:00
} ) . Select ( x = > new Device_value
{
2023-01-30 16:04:29 +08:00
tag_name = x . Key . tag_name2 ,
displayName = x . Key . displayName2
2023-03-28 13:48:30 +08:00
} ) ;
2022-11-02 17:26:18 +08:00
StringBuilder sb = new StringBuilder ( ) ;
2023-04-21 02:06:32 +08:00
bool isDome = false ; //是否為巨蛋案
2022-11-02 17:26:18 +08:00
foreach ( var row in ds2 )
{
if ( string . IsNullOrEmpty ( row . tag_name ) ) continue ;
2023-04-11 10:28:33 +08:00
string [ ] arrTag = row . tag_name . Split ( '_' ) ;
2023-03-28 13:48:30 +08:00
2023-04-11 10:28:33 +08:00
if ( int . Parse ( tag_quantity ) = = 8 )
2023-01-06 14:12:47 +08:00
{
2023-04-11 10:28:33 +08:00
if ( arrTag . Length = = 8 )
{
sb . Append ( $ @ " insert into import_niagara_tag(niagara_tags, device_area_tag, device_building_tag, device_system_tag,
2023-01-13 17:28:23 +08:00
device_name_tag , device_floor_tag , device_master_tag , device_last_name_tag , device_serial_tag , device_full_name , atDateTime ) values ( ' " +
2023-04-11 10:28:33 +08:00
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
{
if ( arrTag . Length = = 5 )
{
2023-04-21 02:06:32 +08:00
isDome = true ;
2023-04-11 10:28:33 +08:00
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
2023-04-21 02:06:32 +08:00
arrTag [ 1 ] + "', '" + //device_system_tag 巨蛋沒有大類,後面再用關聯的方式補上
2023-04-11 10:28:33 +08:00
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
}
2023-01-06 14:12:47 +08:00
}
2023-04-11 10:28:33 +08:00
2022-11-02 17:26:18 +08:00
}
if ( sb . Length > 0 )
{
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
sb . Clear ( ) ;
2023-04-21 02:06:32 +08:00
//巨蛋特殊處理
if ( isDome )
{
sb . Append ( $ @ " -- tag 第二碼為小類(除了安全系統之外)
update import_niagara_tag set device_name_tag = device_system_tag
2023-04-24 09:49:37 +08:00
where ( device_system_tag < > 'S' and device_system_tag < > ' CWB ' ) ;
2023-04-21 02:06:32 +08:00
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 第 二 碼 為 小 類 ( 除 了 安 全 系 統 之 外 )
2023-04-24 09:49:37 +08:00
where ( device_system_tag < > 'S' and device_system_tag < > ' CWB ' ) ; - - 安 全 系 統 第 二 段 大 類 第 四 段 小 類 ");
2023-04-21 02:06:32 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
2022-11-02 17:26:18 +08:00
}
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
2023-03-28 13:48:30 +08:00
/// <summary>
/// 獲取照明開關 是否在 device_node 層
/// </summary>
/// <returns></returns>
public async Task < string > getLightSwitchLevel ( )
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
var sql = "select system_value from variable where system_type = 'module_light_switch' " ;
var module_light_switch = conn . QueryAsync < string > ( sql ) . Result . FirstOrDefault ( ) ;
conn . Close ( ) ;
return module_light_switch ;
}
}
2022-11-02 17:26:18 +08:00
/// <summary>
/// 更新import_niagara_tag資料表
/// </summary>
/// <param name="ds"></param>
/// <param name="building"></param>
2023-03-28 13:48:30 +08:00
2022-11-02 17:26:18 +08:00
/// <returns></returns>
2023-05-10 15:14:46 +08:00
public async Task InsertItemFromNiagara ( List < ImpNiaItem > ds , List < string > building )
2022-11-02 17:26:18 +08:00
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
#region 刪 除 import_niagara_item資料表中選取的棟別
2023-05-10 15:14:46 +08:00
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_unicode_ci ; ";
await conn . ExecuteAsync ( sql ) ;
sql = "delete from import_niagara_item where device_building_tag = '" + b + "'" ;
await conn . ExecuteAsync ( sql ) ;
}
2022-11-02 17:26:18 +08:00
#endregion
2023-04-27 14:43:31 +08:00
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 ( ) ;
2022-11-02 17:26:18 +08:00
StringBuilder sb = new StringBuilder ( ) ;
2023-01-13 17:28:23 +08:00
#region 放 入 import_niagara_item資料表
foreach ( var row in ds )
2022-11-02 17:26:18 +08:00
{
2023-04-26 17:43:55 +08:00
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)
2023-01-13 17:28:23 +08:00
values ( ' "+
row . device_area_tag + "', '" +
row . device_building_tag + "', '" +
row . device_system_tag + "', '" +
row . device_name_tag + "', '" +
row . device_point_name + "', '" +
row . parent_path + "', '" +
2023-04-26 17:43:55 +08:00
row . full_name + "'," +
row . isHistory +
2023-01-13 17:28:23 +08:00
");" ) ;
2022-11-02 17:26:18 +08:00
}
2023-04-21 17:40:18 +08:00
var isDome = await GetOneAsync < string > ( "select system_value from variable where deleted = 0 and system_type = 'project_name';" ) ;
2022-11-02 17:26:18 +08:00
if ( sb . Length > 0 )
{
2023-04-21 17:40:18 +08:00
if ( isDome = = "ibms_dome_dome/D2" )
{
sb . Append ( $ @ " update import_niagara_item a join dic_system b
on a . device_name_tag COLLATE utf8mb4_general_ci = b . s2_code
set device_system_tag = b . s1_code
2023-05-16 14:12:39 +08:00
where ( device_system_tag < > 'S' and device_system_tag < > ' CWB ' ) ; ");
2023-04-21 17:40:18 +08:00
}
2023-05-16 14:12:39 +08:00
sb . Append ( $@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;" ) ;
2022-11-02 17:26:18 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
sb . Clear ( ) ;
}
#endregion
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
/// <summary>
/// 比對 device
/// </summary>
/// <returns></returns>
2023-03-28 13:48:30 +08:00
public async Task DeviceComparison ( string LightSwitchLevel )
2022-11-02 17:26:18 +08:00
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
2023-05-10 15:14:46 +08:00
using ( TransactionScope scope = new TransactionScope ( ( TransactionScopeOption ) TransactionScopeAsyncFlowOption . Enabled , new TimeSpan ( 0 , 0 , 200 ) ) )
2022-11-02 17:26:18 +08:00
{
try
{
List < NiagaraTags > result ;
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb2 = new StringBuilder ( ) ;
sb . Append ( $ @ " SELECT m.*
FROM import_niagara_tag m
LEFT JOIN device d
ON m . niagara_tags = d . device_number
WHERE d . device_number IS NULL ");
result = ( await conn . QueryAsync < NiagaraTags > ( sb . ToString ( ) ) ) . ToList < NiagaraTags > ( ) ;
sb . Clear ( ) ;
2023-03-28 13:48:30 +08:00
#region device , device_kind 新 增
2022-11-02 17:26:18 +08:00
//新增至device, is_link = 1
if ( result . Count > 0 )
{
2022-12-31 16:59:19 +08:00
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
} ) ;
2022-11-02 17:26:18 +08:00
foreach ( var data in result )
{
2023-03-28 13:48:30 +08:00
//開關控制在個別燈具(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() );" ) ;
}
2022-12-31 16:59:19 +08:00
}
2022-12-31 17:03:17 +08:00
foreach ( var data in result2 )
2022-12-31 16:59:19 +08:00
{
2023-01-04 18:38:36 +08:00
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 )
{
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());" ) ;
}
2022-11-02 17:26:18 +08:00
}
2022-12-31 17:03:17 +08:00
2022-11-02 17:26:18 +08:00
if ( sb . Length > 0 )
{
2023-03-28 13:48:30 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
await conn . ExecuteAsync ( sb2 . ToString ( ) ) ;
2022-11-02 17:26:18 +08:00
sb . Clear ( ) ;
sb2 . Clear ( ) ;
}
}
2023-03-28 13:48:30 +08:00
#endregion
2022-11-02 17:26:18 +08:00
2023-03-28 13:48:30 +08:00
#region device_node 新 增
sb . Append ( $ @ " SELECT m.* FROM import_niagara_tag m
LEFT JOIN device_node d ON m . niagara_tags = d . device_number
WHERE d . device_number IS NULL ");
result = ( await conn . QueryAsync < NiagaraTags > ( sb . ToString ( ) ) ) . ToList < NiagaraTags > ( ) ;
sb . Clear ( ) ;
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_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
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());" ) ;
}
}
if ( sb . Length > 0 )
{
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
sb . Clear ( ) ;
}
}
#endregion
2023-05-10 15:14:46 +08:00
//device有, niagara沒有, is_link 更新成 0
sb . Append ( $ @ " SET SQL_SAFE_UPDATES = 0;
UPDATE device d
SET d . is_link = 0
WHERE d . is_link = 1 and not exists ( select niagara_tags from import_niagara_tag where niagara_tags = d . device_number ) ; ");
await conn . ExecuteAsync ( sb . ToString ( ) , commandTimeout : 100 ) ;
sb . Clear ( ) ;
2022-11-02 17:26:18 +08:00
2023-03-28 13:48:30 +08:00
// device_node 有, niagara沒有, is_link 更新成 0
sb . Append ( $ @ " SET SQL_SAFE_UPDATES = 0;
2023-05-10 15:14:46 +08:00
UPDATE device_node d
2023-03-28 13:48:30 +08:00
SET d . is_link = 0
2023-05-10 15:14:46 +08:00
WHERE d . is_link = 1 and not exists ( select niagara_tags from import_niagara_tag where niagara_tags = d . device_number ) ; ");
await conn . ExecuteAsync ( sb . ToString ( ) , commandTimeout : 100 ) ;
2022-11-02 17:26:18 +08:00
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
/// <summary>
/// 比對 device_item
/// </summary>
/// <returns></returns>
public async Task DeviceItemComparison ( )
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
List < NiagaraTagsForItem > 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
WHERE d . points IS NULL ");
result = ( await conn . QueryAsync < NiagaraTagsForItem > ( sb . ToString ( ) ) ) . ToList < NiagaraTagsForItem > ( ) ;
2023-04-27 14:43:31 +08:00
2022-11-02 17:26:18 +08:00
sb . Clear ( ) ;
2023-04-27 14:43:31 +08:00
# region 新 增 至 device , is_link = 1
2022-11-02 17:26:18 +08:00
if ( result . Count > 0 )
{
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" )
{
isControll = 1 ;
}
if ( data . device_point_name = = "ER" | | data . device_point_name = = "ST" )
{
isBool = 1 ;
}
2023-04-26 17:43:55 +08:00
sb . Append ( $ @ "insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, is_show_history, is_link,
2023-05-12 12:20:44 +08:00
device_system_tag , device_name_tag , device_building_tag , full_name , parent_path , created_at , updated_at )
2022-11-02 17:26:18 +08:00
VALUES ( 0 , ' " +
data . device_point_name + "', 1, 0, " +
isControll + "," +
2023-04-26 17:43:55 +08:00
isBool + "," +
data . is_history +
", 1, '" +
2022-11-02 17:26:18 +08:00
data . device_system_tag + "', '" +
2023-04-21 17:40:18 +08:00
data . device_name_tag + "', '" +
2023-05-12 12:20:44 +08:00
data . device_building_tag + "', '" +
2023-04-21 17:40:18 +08:00
data . full_name + "', '" +
2023-01-13 17:28:23 +08:00
data . parent_path + "', " +
2022-11-02 17:26:18 +08:00
"now(), now());" ) ;
}
if ( sb . Length > 0 )
{
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
sb . Clear ( ) ;
}
}
2023-03-14 13:37:48 +08:00
#endregion
2022-11-02 17:26:18 +08:00
//device有, niagara沒有, is_link 更新成 0
2023-03-14 13:37:48 +08:00
//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
2022-11-02 17:26:18 +08:00
sb . Append ( $ @ " SET SQL_SAFE_UPDATES = 0;
UPDATE device_item d LEFT JOIN import_niagara_item m
2023-04-27 14:43:31 +08:00
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 and d . is_show_history = m . is_history
2023-03-14 13:37:48 +08:00
SET d . is_link = case when device_point_name is null then 0 else 1 end ,
2023-04-27 14:43:31 +08:00
d . is_show_history = case when device_point_name is null then 0 else 1 end
where m . is_history = 1
");
2023-03-14 13:37:48 +08:00
2022-11-02 17:26:18 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
/// <summary>
/// 新增資料至 buildingMenu
/// </summary>
/// <returns></returns>
public async Task InsertBuildingMenu ( )
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
List < NiagaraTags > 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 = b . device_building_tag and a . device_system_tag = b . device_system_tag and a . device_name_tag = b . sub_system_tag
WHERE b . device_building_tag IS NULL ");
result = ( await conn . QueryAsync < NiagaraTags > ( sb . ToString ( ) ) ) . ToList < NiagaraTags > ( ) ;
#endregion
sb . Clear ( ) ;
if ( result . Count > 0 )
{
foreach ( var data in result )
{
2022-12-31 16:49:47 +08:00
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 < building_menu > ( sqlString . ToString ( ) ) ) . ToList < building_menu > ( ) ;
2022-11-02 17:26:18 +08:00
#region insert building_menu
2022-12-31 16:49:47 +08:00
if ( bm . Count = = 0 )
{
sb . Append ( @ "insert building_menu(building_tag, main_system_tag, sub_system_tag, device_building_tag, device_system_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_building_tag + "', '" +
data . device_system_tag + "', " +
"1, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now(), 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now() );" ) ;
}
2022-11-02 17:26:18 +08:00
#endregion
}
if ( sb . Length > 0 )
{
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
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 . device_building_tag = a . device_building_tag
and a . device_system_tag = b . device_system_tag and a . device_name_tag = b . sub_system_tag
SET b . is_link = 0
WHERE b . device_building_tag IS NULL ");
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
/// <summary>
/// 新增資料至 subSystemFloor
/// </summary>
/// <returns></returns>
public async Task InsertSubSystemFloor ( )
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
List < NiagaraTags > 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 = b . building_tag and a . device_system_tag = b . main_system_tag and a . device_name_tag = b . sub_system_tag and a . device_floor_tag = b . floor_tag
WHERE b . building_tag IS NULL ");
result = ( await conn . QueryAsync < NiagaraTags > ( sb . ToString ( ) ) ) . ToList < NiagaraTags > ( ) ;
#endregion
sb . Clear ( ) ;
if ( result . Count > 0 )
{
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 )
{
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
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 = a . device_building_tag
and b . main_system_tag = a . device_system_tag and b . sub_system_tag = a . device_name_tag and b . floor_tag = a . device_floor_tag
SET b . is_link = 0
WHERE b . building_tag IS NULL ");
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
2023-02-07 17:55:29 +08:00
{
2022-11-02 17:26:18 +08:00
conn . Close ( ) ;
}
}
}
}
2023-02-07 11:55:52 +08:00
/// <summary>
/// 檢查device內FullName為空的值, 以device_Name取代
/// </summary>
/// <returns></returns>
public async Task CheckFullNameEmptyReplaceByDeviceName ( )
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
StringBuilder sb = new StringBuilder ( ) ;
2023-02-07 17:55:29 +08:00
sb . Append ( "update device set full_name=device_number where full_Name='';" ) ;
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
/// <summary>
/// 檢查device_item內FullName為空的值, 以points取代
/// </summary>
/// <returns></returns>
public async Task ItemCheckFullNameEmptyReplaceByDeviceName ( )
{
2023-02-08 15:17:03 +08:00
//檢查是否有空值
2023-02-07 17:55:29 +08:00
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
StringBuilder sb = new StringBuilder ( ) ;
sb . Append ( "update device_item set full_name = points where full_Name = '''''';" ) ;
2023-02-07 11:55:52 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
2022-11-02 17:26:18 +08:00
2023-02-07 11:55:52 +08:00
}
}
2023-02-08 15:17:03 +08:00
/// <summary>
/// 檢查device的full_name與niagara device_full_name不同者 覆蓋
/// </summary>
/// <returns></returns>
public async Task CheckDiffFullNameAndCover ( )
{
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
2023-05-10 15:14:46 +08:00
using ( TransactionScope scope = new TransactionScope ( ( TransactionScopeOption ) TransactionScopeAsyncFlowOption . Enabled , new TimeSpan ( 0 , 0 , 200 ) ) )
2023-02-08 15:17:03 +08:00
{
try
{
StringBuilder sb = new StringBuilder ( ) ;
sb . Append ( "update device d inner JOIN import_niagara_tag m ON m.niagara_tags = d.device_number " +
"set d.full_name=m.device_full_name " +
"where m.device_full_name<>d.full_name;" ) ;
2023-05-10 15:14:46 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) , commandTimeout : 200 ) ;
2023-02-08 15:17:03 +08:00
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
}
}
/// <summary>
/// 比對 device_item full_name 與 import_niagara_item full_name是否相同
/// 不同則覆蓋
/// </summary>
/// <returns></returns>
public async Task CheckItemDiffFullNameAndCover ( )
{
//檢查
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
StringBuilder sb = new StringBuilder ( ) ;
2023-03-28 13:48:30 +08:00
sb . Append ( $ @ "update device_item d
inner JOIN import_niagara_item m 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
set d . full_name = m . full_name
where d . full_name < > m . full_name ; ");
2023-02-08 15:17:03 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
2023-02-09 12:07:17 +08:00
}
}
2023-02-08 15:17:03 +08:00
2023-02-09 12:07:17 +08:00
/// <summary>
2023-03-28 13:48:30 +08:00
/// 先將device_item 的is_show_history全部關掉
2023-02-09 12:07:17 +08:00
/// 根據niagara輸入資料來決定要開哪些點
/// </summary>
/// <returns></returns>
public async Task CheckItemIsShowHistory ( )
{
//先將所有is_show_history關閉,niagara有的才開啟
using ( IDbConnection conn = GetDbConnection ( ) )
{
conn . Open ( ) ;
using ( TransactionScope scope = new TransactionScope ( TransactionScopeAsyncFlowOption . Enabled ) )
{
try
{
StringBuilder sb = new StringBuilder ( ) ;
//先全部關閉
2023-04-27 14:43:31 +08:00
//sb.Append("update device_item set is_show_history=0;");
sb . Append ( "update device_item set is_show_history = 0 " +
"WHERE(device_system_tag, device_name_tag, points, full_name, is_show_history) NOT IN " +
"(select distinct device_system_tag, device_name_tag, device_point_name, full_name, is_history From import_niagara_item where is_history = 1);" ) ;
2023-02-09 12:07:17 +08:00
await conn . ExecuteAsync ( sb . ToString ( ) ) ;
}
catch ( Exception exception )
{
throw exception ;
}
finally
{
conn . Close ( ) ;
}
}
2023-02-08 15:17:03 +08:00
}
}
2022-11-02 17:26:18 +08:00
}
}