ibms-dome/z01_WinAPP/Service/procCompare.cs

380 lines
17 KiB
C#
Raw Normal View History

2022-10-14 16:08:54 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using tpDomeWinAPP.Models;
using System.Data;
2022-10-19 13:24:45 +08:00
//using System.Data.SqlClient;
2022-10-14 16:08:54 +08:00
using MySql.Data.MySqlClient;
using Repository.Helper;
using Repository.BackendRepository.Interface;
using Dapper;
using System.Linq;
2022-10-19 13:24:45 +08:00
using NPOI.SS.Formula.Functions;
using Microsoft.Extensions.Primitives;
using MySqlX.XDevAPI.Relational;
2022-10-14 16:08:54 +08:00
namespace tpDomeWinAPP.Service
{
public class procCompare
{
string Connection1 = ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;
protected readonly IDatabaseHelper _databaseHelper;
protected string UseDB;
protected IDbConnection con;
private readonly IBackendRepository backendRepository;
public bool InsertNiagaraTagList(List<device_value2> dt, string building )
{
bool result = false;
2022-10-19 13:24:45 +08:00
//tag
2022-10-14 16:08:54 +08:00
var ds2 = dt.GroupBy(x => new
{
tag_name2 = x.tag_name
}).Select( x => new device_value2 {
tag_name = x.Key.tag_name2
});
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
#region Create table
2022-10-19 13:24:45 +08:00
//string ss = @"IF (not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'import_niagara_tag'))
// BEGIN
2022-10-14 16:08:54 +08:00
2022-10-19 13:24:45 +08:00
// 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,
`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_0900_ai_ci;";
conn.Execute(ss.ToString());
ss = "delete from import_niagara_tag where device_building_tag = '" + building + "'";
conn.Execute(ss.ToString());
2022-10-14 16:08:54 +08:00
#endregion
StringBuilder sb = new StringBuilder();
2022-10-19 13:24:45 +08:00
2022-10-14 16:08:54 +08:00
foreach (var row in ds2)
{
if (string.IsNullOrEmpty(row.tag_name)) continue;
2022-10-19 13:24:45 +08:00
string[] arrTag = row.tag_name.Split('_');
sb.Append($@" insert 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, atDateTime) values('" +
2022-10-14 16:08:54 +08:00
row.tag_name + "', '" +
2022-10-19 13:24:45 +08:00
arrTag[0] + "', '" +
arrTag[1] + "', '" +
arrTag[2] + "', '" +
arrTag[3] + "', '" +
arrTag[4] + "', '" +
arrTag[5] + "', '" +
arrTag[6] + "', '" +
arrTag[7] + "', " +
"now());");
}
try
{
if (sb.Length > 0)
2022-10-14 16:08:54 +08:00
{
2022-10-19 13:24:45 +08:00
conn.Execute(sb.ToString());
sb.Clear();
2022-10-14 16:08:54 +08:00
}
}
2022-10-19 13:24:45 +08:00
catch (Exception ex)
2022-10-14 16:08:54 +08:00
{
2022-10-19 13:24:45 +08:00
throw ex;
2022-10-14 16:08:54 +08:00
}
2022-10-19 13:24:45 +08:00
insertItemFromNiagara(dt, conn, building);
}
2022-10-14 16:08:54 +08:00
2022-10-19 13:24:45 +08:00
return result;
}
2022-10-14 16:08:54 +08:00
2022-10-19 13:24:45 +08:00
protected void insertItemFromNiagara(List<device_value2> dt, MySqlConnection conn, string building)
{
#region create table
string ss = @"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,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
conn.Execute(ss.ToString());
ss = "delete from import_niagara_item where device_building_tag = '" + building + "'";
conn.Execute(ss.ToString());
#endregion
2022-10-14 16:08:54 +08:00
2022-10-19 13:24:45 +08:00
List<device_item8> dt_item = new List<device_item8>();
2022-10-14 16:08:54 +08:00
2022-10-19 13:24:45 +08:00
foreach (var row in dt)
{
if (string.IsNullOrEmpty(row.tag_name)) continue;
string[] arrTag = row.tag_name.Split('_');
2022-10-14 16:08:54 +08:00
2022-10-19 13:24:45 +08:00
#region for item
device_item8 row_item = new device_item8();
row_item.tag_name = row.tag_name;
row_item.device_area_tag = arrTag[0];
row_item.device_building_tag = arrTag[1];
row_item.device_system_tag = arrTag[2];
row_item.device_name_tag = arrTag[3];
row_item.device_floor_tag = arrTag[4];
row_item.device_master_tag = arrTag[5];
row_item.device_last_name_tag = arrTag[6];
row_item.device_serial_tag = arrTag[7];
row_item.point_name = row.point_name;
dt_item.Add(row_item);
2022-10-14 16:08:54 +08:00
#endregion
}
2022-10-19 13:24:45 +08:00
//device_item
var ds2_item = dt_item.GroupBy(x => new
{
device_area_tag2 = x.device_area_tag,
device_building_tag2 = x.device_building_tag,
device_system_tag2 = x.device_system_tag,
device_name_tag2 = x.device_name_tag,
point_name2 = x.point_name
}).Select(x => new device_item8
{
device_area_tag = x.Key.device_area_tag2,
device_building_tag = x.Key.device_building_tag2,
device_system_tag = x.Key.device_system_tag2,
device_name_tag = x.Key.device_name_tag2,
point_name = x.Key.point_name2
});
StringBuilder sb = new StringBuilder();
foreach (var row2 in ds2_item)
{
sb.Append($@" insert import_niagara_item(device_area_tag, device_building_tag, device_system_tag, device_name_tag, device_point_name)
values('" +
row2.device_area_tag + "', '" +
row2.device_building_tag + "', '" +
row2.device_system_tag + "', '" +
row2.device_name_tag + "', '" +
row2.point_name + "'" +
");");
}
try
{
if (sb.Length > 0)
{
conn.Execute(sb.ToString());
sb.Clear();
}
}
catch (Exception ex)
{
throw ex;
}
2022-10-14 16:08:54 +08:00
}
public bool InsertDbTagList(List<device_value2> dt, string building)
{
bool result = false;
var ds2 = dt.GroupBy(x => new
{
tag_name2 = x.tag_name
}).Select(x => new device_value2
{
tag_name = x.Key.tag_name2
});
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
#region table
string ss = @"IF (not EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'import_dbTag_niagaraTag'))
BEGIN
CREATE TABLE [dbo].[import_dbTag_niagaraTag](
[device_guid] [varchar](36) NOT NULL,
[full_name] [varchar](50) NOT NULL,
[device_number] [varchar](50) NOT NULL,
[niagaraTag] [varchar](50) NOT NULL,
[device_coordinate] [varchar](50) NULL,
[device_number_old] [varchar](50) NULL,
[device_building_tag] [varchar](50) NOT NULL,
[device_system_tag] [varchar](50) NOT NULL,
[device_system_tag_old] [varchar](50) NULL,
[device_floor_tag] [varchar](50) NOT NULL,
[device_name_tag] [varchar](50) NOT NULL,
[device_serial_tag] [varchar](50) NOT NULL,
[atDateTime] [smalldatetime] NULL,
CONSTRAINT [PK_import_dbTag_niagaraTag] PRIMARY KEY CLUSTERED
(
[device_guid] 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";
conn.Execute(ss.ToString());
2022-10-19 13:24:45 +08:00
//ss = "delete from import_niagara_tag where building = '" + building + "'";
2022-10-14 16:08:54 +08:00
//conn.Execute(ss.ToString());
#endregion
StringBuilder sb = new StringBuilder();
int i = 0;
sb.Append($@" insert import_dbTag_niagaraTag(device_guid, full_name, device_number, niagaraTag, device_coordinate, device_number_old, device_building_tag, device_system_tag, device_system_tag_old, device_floor_tag, device_name_tag, device_serial_tag, atDateTime)
select device_guid, full_name, device_number, '', device_coordinate, device_number_old, device_building_tag, device_system_tag, device_system_tag_old, device_floor_tag, device_name_tag, device_serial_tag, getDate()
from device where deleted = 0;");
conn.Execute(sb.ToString());
sb.Clear();
#region update niagara_tags
sb.Clear();
sb.Append($@"update import_dbTag_niagaraTag set niagaraTag = b.niagara_tags
2022-10-19 13:24:45 +08:00
from [dbo].[import_niagara_tag] b
2022-10-14 16:08:54 +08:00
where import_dbTag_niagaraTag.device_number = b.niagara_tags ");
conn.Execute(sb.ToString());
#endregion
}
return result;
}
public List<NiagaraTags> GetNiagaraTags(string building)
{
List<NiagaraTags> result;
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
StringBuilder sb = new StringBuilder();
2022-10-19 13:24:45 +08:00
sb.Append($@" select * from import_niagara_tag where device_building_tag = '{building}'");
2022-10-14 16:08:54 +08:00
result = conn.Query<NiagaraTags>(sb.ToString()).ToList<NiagaraTags>();
}
return result;
}
public List<NiagaraTags> SearchNiagaraTags(string sql)
{
List<NiagaraTags> result;
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append(sql);
result = conn.Query<NiagaraTags>(sb.ToString()).ToList<NiagaraTags>();
}
return result;
}
public List<dbDevice> SearchDBTags(string building)
{
List<dbDevice> result;
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
StringBuilder sb = new StringBuilder();
if (string.IsNullOrEmpty(building))
{
sb.Append("select * from import_dbTag_niagaraTag order by device_number");
}
else {
sb.Append($@"select * from import_dbTag_niagaraTag where device_building_tag = '{building}' order by device_number");
}
result = conn.Query<dbDevice>(sb.ToString()).ToList<dbDevice>();
}
return result;
}
public List<dbDevice> filterDBTags(string sql)
{
List<dbDevice> result;
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append(sql);
result = conn.Query<dbDevice>(sb.ToString()).ToList<dbDevice>();
}
return result;
}
/// <summary>
/// 更新 DbTag 中的 Niagara tag 欄位
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int updateNiagaraTag_forDBTags(string bilding)
{
int i = -1;
2022-10-19 13:24:45 +08:00
using (MySqlConnection conn = new MySqlConnection(Connection1))
2022-10-14 16:08:54 +08:00
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
StringBuilder sb = new StringBuilder();
try
{
#region DB data
sb.Append($@"delete import_dbTag_niagaraTag where device_building_tag = '{bilding}'");
conn.Execute(sb.ToString());
sb.Clear();
sb.Append($@" insert import_dbTag_niagaraTag(device_guid, full_name, device_number, niagaraTag, device_coordinate, device_number_old, device_building_tag, device_system_tag, device_system_tag_old, device_floor_tag, device_name_tag, device_serial_tag, atDateTime)
select device_guid, full_name, device_number, '', device_coordinate, device_number_old, device_building_tag, device_system_tag, device_system_tag_old, device_floor_tag, device_name_tag, device_serial_tag, getDate()
from device where deleted = 0 and device_building_tag = '{bilding}';");
conn.Execute(sb.ToString());
sb.Clear();
#endregion
sb.Append($@"update import_dbTag_niagaraTag set niagaraTag = b.niagara_tags
2022-10-19 13:24:45 +08:00
from [dbo].[import_niagara_tag] b
2022-10-14 16:08:54 +08:00
where import_dbTag_niagaraTag.device_number = b.niagara_tags
and import_dbTag_niagaraTag.device_building_tag = '{bilding}'");
i = conn.Execute(sb.ToString());
sb.Clear();
}
catch (Exception ex)
{
throw ex;
}
}
return i;
}
}
}