diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index 1388483..cad41e1 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -120,6 +120,7 @@ namespace Backend.Controllers await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName(); await niagaraDataSynchronizeRepository.InsertBuildingMenu(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); + await niagaraDataSynchronizeRepository.InsertFloor(); await this.DeviceDisasterAsync(); result = true; diff --git a/Backend/appsettings.Development.json b/Backend/appsettings.Development.json index c20498b..060577f 100644 --- a/Backend/appsettings.Development.json +++ b/Backend/appsettings.Development.json @@ -18,10 +18,10 @@ "Port": "js2LutKe+rdjzdxMPQUrvQ==", //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp - //"Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome + "Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome //"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel //"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office - "Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //mcut + //"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //mcut //"Database": "2U+9jYGy0dCbMzLaguBXow==", //tpe_dome_mall "Root": "SzdxEgaJJ7tcTCrUl2zKsA==", "Password": "FVAPxztxpY4gJJKQ/se4bQ==" diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 7fe8a27..65271e6 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -453,7 +453,7 @@ namespace Repository.BackendRepository.Implement 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 + 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 and m.device_building_tag collate utf8mb4_0900_ai_ci = d.device_building_tag WHERE d.points IS NULL"); result = (await conn.QueryAsync(sb.ToString())).ToList(); @@ -913,5 +913,101 @@ namespace Repository.BackendRepository.Implement } } } + + /// + /// InsertFloor + /// + /// + public async Task InsertFloor() + { + using (IDbConnection conn = GetDbConnection()) + { + conn.Open(); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + try + { + List result; + StringBuilder sb = new StringBuilder(); + #region comparison floor and sub_system_floor + sb.Append($@" select tag.* + from ( + select device_building_tag, device_floor_tag + from import_niagara_tag + group by device_building_tag, device_floor_tag + ) AS tag + LEFT JOIN floor f + ON tag.device_building_tag = f.building_tag and tag.device_floor_tag = f.full_name and f.deleted = 0 + WHERE f.building_tag IS NULL;"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); + #endregion + + sb.Clear(); + #region insert floor + if (result.Count > 0) + { + Dictionary floorPriority = new Dictionary(); + foreach (var data in result) + { + FloorPriority fp = (await conn.QueryAsync($@"select building_tag, max(priority) as priority from floor + where building_tag = '{data.device_building_tag}' and deleted = 0 group by building_tag;")).FirstOrDefault(); + int priority = 0; + + if (fp == null && floorPriority.GetValueOrDefault(data.device_building_tag) == 0) + floorPriority.Add(data.device_building_tag, 0); + else if (fp != null && floorPriority.GetValueOrDefault(fp.building_tag) == 0) + floorPriority.Add(fp.building_tag, fp.Priority); + + priority = ++floorPriority[data.device_building_tag]; + + sb.Append($@"insert into floor(floor_guid, building_tag, deleted, status, full_name, priority, created_by, created_at) + VALUES (uuid(), '{data.device_building_tag}', 0, 1, '{data.device_floor_tag}', {priority}, 'B43E3CA7-96DD-4FC7-B6E6-974ACC3B0878', now());"); + } + if (sb.Length > 0) + { + await conn.ExecuteAsync(sb.ToString()); + sb.Clear(); + } + } + #endregion + + #region update unuse floor with niagara data + var floor = (await conn.QueryAsync($@"select f1.* + from floor as f1 + left join ( + select f.floor_guid + from ( + select device_building_tag, device_floor_tag + from import_niagara_tag + group by device_building_tag, device_floor_tag + ) AS tag + LEFT JOIN floor f + ON tag.device_building_tag = f.building_tag and tag.device_floor_tag = f.full_name and f.deleted = 0 + ) as f2 on f1.floor_guid = f2.floor_guid + where f2.floor_guid is null and f1.deleted = 0;")).ToList(); + + if (floor.Count > 0) + { + sb.Clear(); + foreach (var f in floor) + { + sb.Append($"update floor set deleted = 1 where floor_guid = '{f.Floor_guid}';"); + } + + await conn.ExecuteAsync(sb.ToString()); + } + #endregion + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } + } } } diff --git a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs index a535158..c60fc1b 100644 --- a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs @@ -71,5 +71,10 @@ namespace Repository.BackendRepository.Interface /// Task InsertSubSystemFloor(); Task DeviceDisasterAysnc(List dv); + /// + /// InsertFloor + /// + /// + Task InsertFloor(); } } diff --git a/Repository/Models/BuildModel.cs b/Repository/Models/BuildModel.cs index 92f2a70..60a48c4 100644 --- a/Repository/Models/BuildModel.cs +++ b/Repository/Models/BuildModel.cs @@ -65,5 +65,6 @@ namespace Repository.Models { public string Floor_guid { get; set; } //樓層GUID public int Priority { get; set; } + public string building_tag { get; set; } } }