ibms-dome/z01_WinAPP/Service/procCompare.cs
2022-10-14 16:08:54 +08:00

312 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using tpDomeWinAPP.Models;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using Repository.Helper;
using Repository.BackendRepository.Interface;
using Dapper;
using System.Linq;
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;
var ds2 = dt.GroupBy(x => new
{
tag_name2 = x.tag_name
}).Select( x => new device_value2 {
tag_name = x.Key.tag_name2
});
using (SqlConnection conn = new SqlConnection(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 = 'importTag'))
BEGIN
CREATE TABLE [dbo].[importTag](
[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_importTag] 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";
conn.Execute(ss.ToString());
ss = "delete from importTag where building = '" + building + "'";
conn.Execute(ss.ToString());
#endregion
StringBuilder sb = new StringBuilder();
int i = 0;
foreach (var row in ds2)
{
if (string.IsNullOrEmpty(row.tag_name)) continue;
sb.Append($@" insert importTag(niagara_tags, building, atDateTime) values('" +
row.tag_name + "', '" +
building + "', getDate());");
i++;
try
{
if (i >= 100)
{
conn.Execute(sb.ToString());
sb.Clear();
i = 0;
}
}
catch (Exception ex)
{
throw ex;
}
}
if (sb.Length > 0)
{
conn.Execute(sb.ToString());
}
#region update 4 tag
sb.Clear();
sb.Append($@"update importTag set importTag.system_code1 = b.tagName
from (
select niagara_tags, building, serial, tagName
from dbo.importTag a CROSS APPLY [Func_StringSplit](a.niagara_tags, '_')
where serial = 2 and building = '{building}'
) b
where importTag.niagara_tags = b.niagara_tags and importTag.building = '{building}';
update importTag set importTag.floor = b.tagName
from (
select niagara_tags, building, serial, tagName
from dbo.importTag a CROSS APPLY [Func_StringSplit](a.niagara_tags, '_')
where serial = 3 and building = '{building}'
) b
where importTag.niagara_tags = b.niagara_tags and importTag.building = '{building}';
update importTag set importTag.system_code2 = b.tagName
from (
select niagara_tags, building, serial, tagName
from dbo.importTag a CROSS APPLY [Func_StringSplit](a.niagara_tags, '_')
where serial = 4 and building = '{building}'
) b
where importTag.niagara_tags = b.niagara_tags and importTag.building = '{building}';
update importTag set importTag.device_serial = b.tagName
from (
select niagara_tags, building, serial, tagName
from dbo.importTag a CROSS APPLY [Func_StringSplit](a.niagara_tags, '_')
where serial = 5 and building = '{building}'
) b
where importTag.niagara_tags = b.niagara_tags and importTag.building = '{building}';
update importTag set db_tags = b.device_number
from dbo.device b
where niagara_tags = b.device_number and importTag.building = '{building}'
");
conn.Execute(sb.ToString());
#endregion
}
return result;
}
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
});
using (SqlConnection conn = new SqlConnection(Connection1))
{
//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());
//ss = "delete from importTag where building = '" + building + "'";
//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
from [dbo].[importTag] b
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;
using (SqlConnection conn = new SqlConnection(Connection1))
{
//CONCAT('{baseURL}', '{deviceKindFilePath}', dk.device_image) AS device_image_url,
conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append($@" select * from importTag where building = '{building}'");
result = conn.Query<NiagaraTags>(sb.ToString()).ToList<NiagaraTags>();
}
return result;
}
public List<NiagaraTags> SearchNiagaraTags(string sql)
{
List<NiagaraTags> result;
using (SqlConnection conn = new SqlConnection(Connection1))
{
//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;
using (SqlConnection conn = new SqlConnection(Connection1))
{
//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;
using (SqlConnection conn = new SqlConnection(Connection1))
{
//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;
using (SqlConnection conn = new SqlConnection(Connection1))
{
//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
from [dbo].[importTag] b
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;
}
}
}