This commit is contained in:
dev02 2022-10-20 23:27:07 +08:00
commit 9e583a5b9c
2 changed files with 208 additions and 132 deletions

View File

@ -26,43 +26,34 @@ namespace tpDomeWinAPP.Service
private readonly IBackendRepository backendRepository;
public bool InsertNiagaraTagList(List<device_value2> dt, string building )
public bool ToInsertNiagaraTagList(List<device_value2> dt, string building )
{
bool result = false;
//tag
InsertNiagaraTagList(dt, building);
insertItemFromNiagara(dt, building);
deviceComparison();
deviceItemComparison();
insertBuildingMenu();
insertSubSystemFloor();
return result;
}
protected void InsertNiagaraTagList(List<device_value2> dt, string building)
{
var ds2 = dt.GroupBy(x => new
{
tag_name2 = x.tag_name
}).Select( x => new device_value2 {
}).Select(x => new device_value2
{
tag_name = x.Key.tag_name2
});
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
#region Create table
//string ss = @"IF (not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'import_niagara_tag'))
// BEGIN
// CREATE TABLE [dbo].[import_niagara_tag](
// [id] [int] IDENTITY(1,1) NOT NULL,
// [db_tags] [varchar](50) NULL,
// [niagara_tags] [varchar](50) NULL,
// [building] [varchar](10) NULL,
// [system_code1] [varchar](10) NULL,
// [floor] [varchar](10) NULL,
// [system_code2] [varchar](10) NULL,
// [device_serial] [varchar](10) NULL,
// [atDateTime] [smalldatetime] NULL,
// CONSTRAINT [PK_import_niagara_tag] PRIMARY KEY CLUSTERED
// (
// [id] ASC
// )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
// ) ON [PRIMARY];
// END" ;
string ss = @" CREATE TABLE IF NOT EXISTS `import_niagara_tag` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`db_tags` varchar(50) DEFAULT NULL,
@ -81,7 +72,6 @@ namespace tpDomeWinAPP.Service
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
conn.Execute(ss.ToString());
ss = "delete from import_niagara_tag where device_building_tag = '" + building + "'";
//string ss = "delete from device where device_building_tag = '" + building + "'";
conn.Execute(ss.ToString());
#endregion
@ -104,19 +94,6 @@ namespace tpDomeWinAPP.Service
arrTag[6] + "', '" +
arrTag[7] + "', " +
"now());");
//sb.Append($@" insert device(device_number, device_guid, deleted, status, priority, device_system_category_layer3, device_area_tag, device_building_tag, device_system_tag,
// device_name_tag, device_floor_tag, device_master, device_last_name, device_serial_tag, created_at, updated_at) values('" +
// row.tag_name + "', uuid(), 0, 1, 0, '" +
// arrTag[0] + "', '" +
// arrTag[1] + "', '" +
// arrTag[2] + "', '" +
// arrTag[3] + "', '" +
// arrTag[4] + "', '" +
// arrTag[5] + "', '" +
// arrTag[6] + "', '" +
// arrTag[7] + "', " +
// "now(), now());");
}
try
{
@ -130,16 +107,14 @@ namespace tpDomeWinAPP.Service
{
throw ex;
}
insertItemFromNiagara(dt, conn, building);
deviceComparison();
deviceItemComparison();
}
}
return result;
}
protected void insertItemFromNiagara(List<device_value2> dt, MySqlConnection conn, string building)
protected void insertItemFromNiagara(List<device_value2> dt, string building)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
#region create table
string ss = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
@ -207,18 +182,6 @@ namespace tpDomeWinAPP.Service
row2.device_name_tag + "', '" +
row2.point_name + "'" +
");");
//var isControll = 0;
//var isBool = 0;
//if(row2.point_name == "ER" || row2.point_name == "AL" || row2.point_name == "ST")
//{
// isControll = 1;
//}
//if(row2.point_name == "ER" || row2.point_name == "ST")
//{
// isBool = 1;
//}
//sb.Append($@" insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, created_at, updated_at)
// values('0, " + row2.point_name + "', 1, 0," + isControll + "," + isBool + ", now(), now();");
}
try
{
@ -233,7 +196,11 @@ namespace tpDomeWinAPP.Service
throw ex;
}
}
}
/// <summary>
/// 比對device和import_Niagara_tag並更新至device(資料表)
/// </summary>
public void deviceComparison()
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
@ -258,8 +225,8 @@ namespace tpDomeWinAPP.Service
sb.Append($@" insert device(device_guid, deleted, status, priority, is_link, device_area_tag,
device_building_tag, device_system_tag, device_name_tag, device_floor_tag, device_master,
device_last_name, device_serial_tag, device_number, device_system_category_layer3, created_at, updated_at)
values(uuid(), 0, 1, 0, 1, '" + //device_guid
data.device_area_tag + "', '" + //
values(uuid(), 0, 1, 0, 1, '" +
data.device_area_tag + "', '" +
data.device_building_tag + "', '" +
data.device_system_tag + "', '" +
data.device_name_tag + "', '" +
@ -268,7 +235,7 @@ namespace tpDomeWinAPP.Service
data.device_master_tag + "', '" +
data.device_last_name_tag + "', '" +
data.device_serial_tag + "', '" +
data.niagara_tags + "', '" + //device_number
data.niagara_tags + "', '" +
data.device_system_tag + "', now(), now() );");
}
if(sb.Length > 0)
@ -288,6 +255,9 @@ namespace tpDomeWinAPP.Service
}
}
/// <summary>
/// 比對device_item和import_Niagara_item並更新至device_item(資料表)
/// </summary>
public void deviceItemComparison()
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
@ -299,8 +269,7 @@ namespace tpDomeWinAPP.Service
sb.Append($@" SELECT m.*
FROM import_niagara_item m
LEFT JOIN device_item d
ON m.device_area_tag = d.device_area_tag and m.device_building_tag = d.device_building_tag
and m.device_system_tag = d.device_system_tag and m.device_name_tag = d.device_name_tag and m.device_point_name = d.points
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 = conn.Query<NiagaraTagsForItem>(sb.ToString()).ToList<NiagaraTagsForItem>();
sb.Clear();
@ -319,14 +288,12 @@ namespace tpDomeWinAPP.Service
{
isBool = 1;
}
sb.Append($@"insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, is_link, device_area_tag,
device_building_tag, device_system_tag, device_name_tag, created_at, updated_at)
sb.Append($@"insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, is_link,
device_system_tag, device_name_tag, created_at, updated_at)
VALUES (0, '" +
data.device_point_name + "', 1, 0, " +
isControll + "," +
isBool + ", 1, '" +
data.device_area_tag + "', '" +
data.device_building_tag + "', '" +
data.device_system_tag + "', '" +
data.device_name_tag + "', " +
"now(), now());");
@ -340,11 +307,120 @@ namespace tpDomeWinAPP.Service
//device有niagara沒有is_link 更新成 0
sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device_item d LEFT JOIN import_niagara_item m
ON d.device_area_tag = m.device_area_tag and d.device_building_tag = m.device_building_tag
and d.device_system_tag = m.device_system_tag and d.device_name_tag = m.device_name_tag and d.points = m.device_point_name
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
WHERE m.device_point_name IS NULL");
conn.Execute(sb.ToString());
}
}
public void insertBuildingMenu()
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
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 = conn.Query<NiagaraTags>(sb.ToString()).ToList<NiagaraTags>();
#endregion
sb.Clear();
if (result.Count > 0)
{
foreach (var data in result) {
#region insert building_menu
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() );");
#endregion
}
if (sb.Length > 0)
{
conn.Execute(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");
conn.Execute(sb.ToString());
}
}
public void insertSubSystemFloor()
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
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 = conn.Query<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)
{
conn.Execute(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");
conn.Execute(sb.ToString());
}
}

View File

@ -141,7 +141,7 @@ namespace tpDomeWinAPP
if (ds != null && ds.Count > 0)
{
svcCompare.InsertNiagaraTagList(ds, building);
svcCompare.ToInsertNiagaraTagList(ds, building);
lbMsg2.Text = "Done!" + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else