From e9189fe047a7fc4500ca422e82ff03e575008d94 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Fri, 3 May 2024 16:33:43 +0800 Subject: [PATCH] =?UTF-8?q?[Backend]=E5=BE=8C=E8=87=BA=E6=AC=8A=E9=99=90?= =?UTF-8?q?=E8=A6=8F=E5=89=87=E4=BF=AE=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Controllers/BuildMenuController.cs | 8 +- .../NiagaraDataSynchronizeController.cs | 6 + .../Controllers/SystemCategoryController.cs | 23 ++-- .../ApiControllers/LightScheduleController.cs | 2 +- .../NiagaraDataSynchronizeRepository.cs | 125 ++++++++++++++++++ .../INiagaraDataSynchronizeRepository.cs | 5 + Repository/Models/BackgroundServiceTask.cs | 12 +- 7 files changed, 164 insertions(+), 17 deletions(-) diff --git a/Backend/Controllers/BuildMenuController.cs b/Backend/Controllers/BuildMenuController.cs index d635af6..8d407cc 100644 --- a/Backend/Controllers/BuildMenuController.cs +++ b/Backend/Controllers/BuildMenuController.cs @@ -54,7 +54,7 @@ namespace Backend.Controllers { var sqlString = @$"select v.system_value value, v.system_key name from ( - select main_system_tag from building_menu bm where bm.building_tag = @building_tag and is_link = 1 group by bm.building_tag,bm.main_system_tag + select main_system_tag from building_menu bm where bm.building_tag = @building_tag group by bm.building_tag,bm.main_system_tag ) bm join variable v on v.system_value = bm.main_system_tag AND v.system_type = @main_system_type and v.deleted = 0 ORDER BY v.system_priority ASC"; @@ -113,7 +113,7 @@ namespace Backend.Controllers sv.system_value value, sv.system_key name from variable sv left join ( - select * from building_menu bm where bm.building_tag = @building_tag and is_link = 1 + select * from building_menu bm where bm.building_tag = @building_tag ) bm on sv.system_value = bm.sub_system_tag where bm.sub_system_tag is null and sv.deleted = 0 and sv.system_parent_id in @mainList ORDER BY sv.system_priority ASC, sv.created_at DESC"; @@ -139,7 +139,7 @@ namespace Backend.Controllers ApiResult apiResult = new ApiResult(); try { - var get = await backendRepository.GetOneAsync("building_menu", $"building_tag = '{buildMenu.building_tag}' and main_system_tag = '{buildMenu.main_system_tag}' and sub_system_tag = '{buildMenu.sub_system_tag}' and is_link = 1"); + var get = await backendRepository.GetOneAsync("building_menu", $"building_tag = '{buildMenu.building_tag}' and main_system_tag = '{buildMenu.main_system_tag}' and sub_system_tag = '{buildMenu.sub_system_tag}'"); if (get == null) { var subV = await backendRepository.GetOneAsync("variable", "system_value = @sub_tag and system_type = @sub_system_type", new { sub_tag = buildMenu.sub_system_tag, sub_system_type = sub_system_type }); @@ -394,7 +394,7 @@ namespace Backend.Controllers join variable sv on bm.sub_system_tag = sv.system_value and sv.system_type = @sub_system_type and sv.deleted = 0 left join floor lf on lf.floor_guid = bm.left_planimetric_floor_guid left join floor rf on rf.floor_guid = bm.right_planimetric_floor_guid - where bm.building_tag = @building_tag and bm.main_system_tag in @MainList and bm.is_link > 0 + where bm.building_tag = @building_tag and bm.main_system_tag in @MainList ORDER BY bm.priority, mv.system_priority ASC, sv.system_priority ASC, sv.created_at DESC ", new { building_tag = post.building_tag, MainList = post.MainList, main_system_type = main_system_type, sub_system_type = sub_system_type }); diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index 6fb01fb..2607c85 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -172,6 +172,12 @@ namespace Backend.Controllers stopwatchSection.Stop(); await niagaraDataSynchronizeRepository.KeepTimeLog("InsertBuildingMenu", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + await niagaraDataSynchronizeRepository.UpdateAuthPageByBuildingMenu(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("UpdateAuthPageByBuildingMenu", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); diff --git a/Backend/Controllers/SystemCategoryController.cs b/Backend/Controllers/SystemCategoryController.cs index b4bf6fb..d634ee9 100644 --- a/Backend/Controllers/SystemCategoryController.cs +++ b/Backend/Controllers/SystemCategoryController.cs @@ -355,18 +355,19 @@ namespace Backend.Controllers }; await backendRepository.AddOneByCustomTable(systemSubDic, "variable"); - var systemId = await backendRepository.GetOneAsync($@"select id from variable where system_value = '{post.system_value}' and deleted = 0 and system_parent_id = '{post.system_parent_id}' and system_type = '{sub_system_type}'"); - var buildings = await backendRepository.GetAllAsync($@"select building_tag from building where deleted = 0"); - var authCode = await backendRepository.GetOneAsync($@"select max(cast(SUBSTRING(authcode, 2) AS UNSIGNED)) from auth_page where authcode like 'F%'"); - var parentSystemName = await backendRepository.GetOneAsync($@"select system_key from variable where id = {post.system_parent_id}"); - StringBuilder sb = new StringBuilder(); - foreach (var b in buildings) - { - sb.Append($@" insert into auth_page(AuthCode, AuthType, MainName, SubName, building_tag, ShowView, created_at) - values ('F{++authCode}', '1', '{parentSystemName}', '{post.System_key}', '{b}', '{systemId}', Now());"); - } + // 加入權限的功能統一由區域選單管理新增,故先註解 + //var systemId = await backendRepository.GetOneAsync($@"select id from variable where system_value = '{post.system_value}' and deleted = 0 and system_parent_id = '{post.system_parent_id}' and system_type = '{sub_system_type}'"); + //var buildings = await backendRepository.GetAllAsync($@"select building_tag from building where deleted = 0"); + //var authCode = await backendRepository.GetOneAsync($@"select max(cast(SUBSTRING(authcode, 2) AS UNSIGNED)) from auth_page where authcode like 'F%'"); + //var parentSystemName = await backendRepository.GetOneAsync($@"select system_key from variable where id = {post.system_parent_id}"); + //StringBuilder sb = new StringBuilder(); + //foreach (var b in buildings) + //{ + // sb.Append($@" insert into auth_page(AuthCode, AuthType, MainName, SubName, building_tag, ShowView, created_at) + // values ('F{++authCode}', '1', '{parentSystemName}', '{post.System_key}', '{b}', '{systemId}', Now());"); + //} - await backendRepository.ExecuteSql(sb.ToString()); + //await backendRepository.ExecuteSql(sb.ToString()); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; diff --git a/FrontendWebApi/ApiControllers/LightScheduleController.cs b/FrontendWebApi/ApiControllers/LightScheduleController.cs index 58b60fd..a43657a 100644 --- a/FrontendWebApi/ApiControllers/LightScheduleController.cs +++ b/FrontendWebApi/ApiControllers/LightScheduleController.cs @@ -361,7 +361,7 @@ namespace FrontendWebApi.ApiControllers strResponse = sr.ReadToEnd(); } } - + apiResult.Msg = "執行成功"; apiResult.Code = "0000"; } catch (Exception ex) diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index ba53b85..cc98cd7 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -19,6 +19,7 @@ using Org.BouncyCastle.Asn1.Mozilla; using System.IO; using System.Diagnostics; using System.Runtime.CompilerServices; +using Microsoft.Extensions.Hosting; namespace Repository.BackendRepository.Implement { @@ -1095,6 +1096,130 @@ namespace Repository.BackendRepository.Implement } } + /// + /// 根據Building_menu 更新 AuthPage + /// + /// + public async Task UpdateAuthPageByBuildingMenu() + { + stopwatch = new Stopwatch(); + stopwatch.Start(); + using (IDbConnection conn = GetDbConnection()) + { + conn.Open(); + try + { + List result; + StringBuilder sb = new StringBuilder(); + //building_menu有,auth_page沒有,且variable有對應的大小類,新增這些權限 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + sb.Append($@"SELECT vm.system_key MainName,vs.system_key SubName,bm.building_tag,vs.id ShowView from building_menu bm + left join(SELECT vm.system_value main_system_tag,vs.system_value sub_system_tag,a.building_tag FROM `auth_page` a + join variable vs on a.ShowView = vs.id and vs.deleted = 0 + join variable vm on vs.system_parent_id = vm.id and vm.deleted = 0 + GROUP BY main_system_tag, sub_system_tag, building_tag) a on a.main_system_tag = bm.main_system_tag + and a.sub_system_tag = bm.sub_system_tag and a.building_tag = bm.building_tag + join variable vs on bm.sub_system_tag = vs.system_value and vs.system_type = 'device_system_category_layer3' and vs.deleted = 0 + join variable vm on bm.main_system_tag = vm.system_value and vm.system_type = 'device_system_category_layer2'and vm.deleted = 0 + WHERE bm.is_link = 1 and a.building_tag is null + ORDER BY vs.id"); + result = (await conn.QueryAsync(sb.ToString())).ToList(); + + sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("filter auth_page from building_menu", stopwatchSection.ElapsedMilliseconds); + var max = (await conn.QueryAsync("select ifnull((select Max(CONVERT(SUBSTRING(AuthCode,2,5), SIGNED)) AuthCode from auth_page ap where ap.AuthCode like 'F%'),'0') AuthCode")).First(); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + if (result.Count > 0) + { + foreach (var data in result) + { + StringBuilder sqlString = new StringBuilder(); + + sb.Append(@$"insert into auth_page(AuthCode, AuthType, MainName, SubName, building_tag, ShowView, created_at) + values ('F{++max}', '1', '{data.MainName}', '{data.SubName}', '{data.building_tag}', '{data.ShowView}', Now());"); + } + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + await conn.ExecuteAsync(sb.ToString()); + } + sb.Clear(); + } + } + stopwatchSection.Stop(); + await KeepTimeLog("insert into auth_page", stopwatchSection.ElapsedMilliseconds); + + //auth_page有,building_menu沒有,把auth_code對應的role_auth也一併刪除 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + sb.Append($@"DELETE FROM role_auth WHERE AuthCode in + (SELECT ap.AuthCode + FROM (SELECT vm.system_value main_system_tag,vs.system_value sub_system_tag,a.* FROM `auth_page` a + join variable vs on a.ShowView = vs.id and vs.deleted = 0 + join variable vm on vs.system_parent_id = vm.id and vm.deleted = 0 + GROUP BY main_system_tag, sub_system_tag, building_tag) ap + LEFT JOIN ( + SELECT bm.main_system_tag, bm.sub_system_tag, bm.building_tag + FROM building_menu bm + WHERE bm.is_link = 1 + ) bm ON ap.building_tag = bm.building_tag + AND bm.main_system_tag = ap.main_system_tag + AND bm.sub_system_tag = ap.sub_system_tag + WHERE bm.building_tag IS NULL);"); + + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + await conn.ExecuteAsync(sb.ToString()); + } + stopwatchSection.Stop(); + await KeepTimeLog("auth_page有,building_menu沒有,把auth_code對應的role_auth也一併刪除", stopwatchSection.ElapsedMilliseconds); + + //auth_page有,building_menu沒有,刪除這些權限 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + + sb.Append($@"DELETE FROM auth_page WHERE AuthCode in + (SELECT ap.AuthCode + FROM (SELECT vm.system_value main_system_tag,vs.system_value sub_system_tag,a.* FROM `auth_page` a + join variable vs on a.ShowView = vs.id and vs.deleted = 0 + join variable vm on vs.system_parent_id = vm.id and vm.deleted = 0 + GROUP BY main_system_tag, sub_system_tag, building_tag) ap + LEFT JOIN ( + SELECT bm.main_system_tag, bm.sub_system_tag, bm.building_tag + FROM building_menu bm + WHERE bm.is_link = 1 + ) bm ON ap.building_tag = bm.building_tag + AND bm.main_system_tag = ap.main_system_tag + AND bm.sub_system_tag = ap.sub_system_tag + WHERE bm.building_tag IS NULL);"); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + await conn.ExecuteAsync(sb.ToString()); + } + sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("auth_page有,building_menu沒有,刪除這些權限", stopwatchSection.ElapsedMilliseconds); + + + + stopwatch.Stop(); + await KeepTimeLog("UpdateAuthPageByBuildingMenu", stopwatch.ElapsedMilliseconds); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } /// /// 新增資料至 subSystemFloor /// diff --git a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs index 05d9abc..9a23779 100644 --- a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs @@ -42,6 +42,11 @@ namespace Repository.BackendRepository.Interface Task CheckDiffFullNameAndCover(); Task CheckItemIsShowHistory(); + /// + ///根據Building_menu 更新 AuthPage + /// + /// + Task UpdateAuthPageByBuildingMenu(); Task InsertItemFromNiagara_History(List ds, List building); /// diff --git a/Repository/Models/BackgroundServiceTask.cs b/Repository/Models/BackgroundServiceTask.cs index c8e0920..b321f67 100644 --- a/Repository/Models/BackgroundServiceTask.cs +++ b/Repository/Models/BackgroundServiceTask.cs @@ -105,7 +105,17 @@ namespace Repository.Models public string device_system_tag { get; set; } public string device_name_tag { get; set; } } - + public class AuthPage + { + public string AuthCode { get; set; } + public byte AuthType { get; set; } + public string MainName { get; set; } + public string SubName { get; set; } + public string building_guid { get; set; } + public string building_tag { get; set; } + public string ShowView { get; set; } + public string created_at { get; set; } + } public class device_kind { public string device_kind_guid { get; set; }