using Backend.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Repository.BackendRepository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; namespace Backend.Controllers { public class BuildMenuController : MybaseController { private readonly IBackendRepository backendRepository; public BuildMenuController(IBackendRepository backendRepository) { this.backendRepository = backendRepository; } public IActionResult Index() { return View(); } [HttpPost] public async Task>> BuildInfoList() { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { var sqlString = @$"select building_guid as Value, full_name as Name from building a where a.deleted = 0 and a.status = 0 ORDER BY a.priority ASC, a.created_at DESC"; KeyValue = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task>> MainListBybuild(string build) { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { var sqlString = @$"select ms.main_system_guid value, ms.full_name name from (select main_system_guid from building_menu bm where bm.building_guid = '{build}' group by bm.main_system_guid ) bm left join main_system ms on ms.main_system_guid = bm.main_system_guid ORDER BY ms.priority ASC"; KeyValue = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task>> MainList() { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { var sqlString = @$"select ms.full_name Name,ms.main_system_guid Value from main_system ms where ms.deleted = 0 and ms.status = 0 ORDER BY ms.priority ASC"; KeyValue = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task>> SubListNotAdd(SubListIn post) { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { var sqlString = @$"select ss.sub_system_guid value,ss.full_name name from sub_system ss left join ( select * from building_menu bm where bm.building_guid = '{post.build}') bm on ss.sub_system_guid = bm.sub_system_guid where bm.sub_system_guid is null and ss.deleted = 0 and ss.status = 0 and ss.main_system_guid = @guid ORDER BY ss.priority ASC, ss.created_at DESC"; KeyValue = await backendRepository.GetAllAsync(sqlString, new { guid = post.main }); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> SavebuildMenuModal(BuildMenu buildMenu) { ApiResult apiResult = new ApiResult(); try { var get = await backendRepository.GetOneAsync("building_menu", $"building_guid = '{buildMenu.building_guid}' and main_system_guid = '{buildMenu.main_system_guid}' and sub_system_guid = '{buildMenu.sub_system_guid}'"); if (get == null) { var dictionary = new Dictionary() { {"@building_guid", buildMenu.building_guid}, {"@main_system_guid",buildMenu.main_system_guid }, {"@sub_system_guid", buildMenu.sub_system_guid}, {"@drawing",buildMenu.drawing }, {"@created_by",myUserInfo.Userinfo_guid }, {"@planimetric_click",buildMenu.planimetric_click} }; if (buildMenu.drawing == 2) { if (buildMenu.system_url != null && buildMenu.system_url.CompareTo("http://") < 0 && buildMenu.system_url.CompareTo("https://") < 0) { //未包含http || https 抓該棟ip + port var building_where = @"deleted = 0 AND building_guid = @Building_guid"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_guid = buildMenu.building_guid }); buildMenu.system_url = string.Format("http://{0}:{1}{2}", building.Ip_address, building.Ip_port, buildMenu.system_url); } dictionary.Add("@system_url", buildMenu.system_url); } else if (buildMenu.drawing == 4) { if (buildMenu.system_url != null && buildMenu.system_url.CompareTo("http://") < 0 && buildMenu.system_url.CompareTo("https://") < 0) { //未包含http || https 抓該棟ip + port var building_where = @"deleted = 0 AND building_guid = @Building_guid"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_guid = buildMenu.building_guid }); buildMenu.system_url = string.Format("http://{0}:{1}{2}", building.Ip_address, building.Ip_port, buildMenu.system_url); } dictionary.Add("@riser_diagram_url", buildMenu.riser_diagram_url); dictionary.Add("@icon_click", buildMenu.icon_click); dictionary.Add("@icon_click_url", buildMenu.icon_click_url); dictionary.Add("@icon_click_url_width", buildMenu.icon_click_url_width); dictionary.Add("@icon_click_url_height", buildMenu.icon_click_url_height); } else if (buildMenu.drawing == 1) { dictionary.Add("@planimetric_floor_guid", buildMenu.planimetric_floor_guid); } await backendRepository.AddOneByCustomTable(dictionary, "building_menu"); var max = await backendRepository.GetOneAsync("select Max(CONVERT(int,SUBSTRING(AuthCode,2,5))) AuthCode from auth_page ap where ap.AuthCode like 'F%'"); var page = await backendRepository.GetOneAsync($"select ss.full_name SubName,ms.full_name MainName from sub_system ss left join main_system ms on ms.main_system_guid = ss.main_system_guid where ss.sub_system_guid = '{buildMenu.sub_system_guid}' and ms.main_system_guid = '{buildMenu.main_system_guid}'"); var pagedictionary = new Dictionary() { {"@AuthCode", "F" +(max+1).ToString() }, {"@AuthType", 1 }, {"@MainName", page.MainName}, {"@SubName",page.SubName}, {"@building_guid",buildMenu.building_guid}, {"@ShowView",buildMenu.sub_system_guid} }; await backendRepository.AddOneByCustomTable(pagedictionary, "auth_page"); await backendRepository.ExecuteSql(@"DELETE FROM auth_page WHERE auth_page.AuthCode like 'F%'; INSERT INTO auth_page (AuthCode,AuthType,MainName,SubName,building_guid,ShowView) SELECT 'F' + CONVERT(varchar,ROW_NUMBER() OVER(ORDER BY bm.building_guid ASC)) AuthCode,'1' AuthType,ms.full_name MainName,ss.full_name SubName,bm.building_guid,bm.sub_system_guid ShowView FROM building_menu bm left join main_system ms on ms.main_system_guid = bm.main_system_guid left join sub_system ss on ss.sub_system_guid = bm.sub_system_guid"); await backendRepository.ExecuteSql(@"delete a from role_auth a join role b on a.role_guid = b.role_guid where b.layer = 0; INSERT INTO role_auth (role_guid,AuthCode,created_by) SELECT r.role_guid,ap.AuthCode,'0' created_by FROM auth_page ap,role r WHERE r.layer = 0;"); #region 新增至派送資料表 var sql = $@"SELECT ra.* FROM role_auth ra join role r on ra.role_guid = r.role_guid where r.layer = 0"; var role_auths = await backendRepository.GetAllAsync(sql); List> role_auth_dicts = new List>(); foreach (var role_auth in role_auths) { Dictionary role_auth_dict = new Dictionary() { { "role_guid", role_auth.Role_guid}, { "@AuthCode", role_auth.AuthCode}, }; role_auth_dicts.Add(role_auth_dict); } await backendRepository.ManualInsertBackgroundServiceTask("", "", "role_auth", "purge_specify_insert", role_auth_dicts); #endregion apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } else { var dictionary = new Dictionary() { {"@drawing",buildMenu.drawing }, {"@updated_by",myUserInfo.Userinfo_guid }, {"@updated_at",DateTime.Now }, {"@planimetric_click",buildMenu.planimetric_click} }; if (buildMenu.drawing == 2) { if (buildMenu.system_url != null && buildMenu.system_url.CompareTo("http://") < 0 && buildMenu.system_url.CompareTo("https://") < 0) { //未包含http || https 抓該棟ip + port var building_where = @"deleted = 0 AND building_guid = @Building_guid"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_guid = buildMenu.building_guid }); buildMenu.system_url = string.Format("http://{0}:{1}{2}", building.Ip_address, building.Ip_port, buildMenu.system_url); } dictionary.Add("@system_url", buildMenu.system_url); } else if (buildMenu.drawing == 4) { if (buildMenu.system_url != null && buildMenu.system_url.CompareTo("http://") < 0 && buildMenu.system_url.CompareTo("https://") < 0) { //未包含http || https 抓該棟ip + port var building_where = @"deleted = 0 AND building_guid = @Building_guid"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_guid = buildMenu.building_guid }); buildMenu.system_url = string.Format("http://{0}:{1}{2}", building.Ip_address, building.Ip_port, buildMenu.system_url); } dictionary.Add("@riser_diagram_url", buildMenu.riser_diagram_url); dictionary.Add("@icon_click", buildMenu.icon_click); dictionary.Add("@icon_click_url", buildMenu.icon_click_url); dictionary.Add("@icon_click_url_width", buildMenu.icon_click_url_width); dictionary.Add("@icon_click_url_height", buildMenu.icon_click_url_height); } else if (buildMenu.drawing == 1) { dictionary.Add("@planimetric_floor_guid", buildMenu.planimetric_floor_guid); } await backendRepository.UpdateOneByCustomTable(dictionary, "building_menu", $"building_guid = '{buildMenu.building_guid}' and main_system_guid = '{buildMenu.main_system_guid}' and sub_system_guid = '{buildMenu.sub_system_guid}'"); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(buildMenu); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task BuildMenuTable(BuildMenuTablePost post) { List buildMenuTables = new List(); ApiResult> apiResult = new ApiResult>(); try { buildMenuTables = await backendRepository.GetAllAsync($@"select bm.*, case drawing when 1 then '樓層平面圖' when 2 then '系統圖' when 4 then '昇位圖' end drawing_name, case icon_click when 1 then '開' when 0 then '關' end icon_click_name, case planimetric_click when 1 then '開' when 0 then '關' end planimetric_click_name, ms.full_name main_system_guid_name, ss.full_name sub_system_guid_name,ff.full_name floor_guid_name from building_menu bm left join main_system ms on ms.main_system_guid = bm.main_system_guid left join sub_system ss on ss.sub_system_guid = bm.sub_system_guid left join floor ff on ff.floor_guid = bm.planimetric_floor_guid where bm.building_guid = '{post.build}' and bm.main_system_guid in @MainList ORDER BY ms.priority ASC, ss.priority ASC, ss.created_at DESC ", new { MainList = post.MainList }); apiResult.Code = "0000"; apiResult.Data = buildMenuTables; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } [HttpPost] public async Task> GetBuildMenu(MenuIn post) { ApiResult apiResult = new ApiResult(); try { var BuildMenu = await backendRepository.GetOneAsync( $@"select *,ss.full_name sub_system_guid_name from building_menu bm left join sub_system ss on bm.sub_system_guid = ss.sub_system_guid where bm.building_guid = @bg and bm.main_system_guid = @msg and bm.sub_system_guid = @ssg", new { bg = post.build, msg = post.main, ssg = post.sub }); apiResult.Code = "0000"; apiResult.Data = BuildMenu; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> DeleteBuildMenu(MenuIn post) { ApiResult apiResult = new ApiResult(); try { await backendRepository.PurgeOneByGuidWithCustomDBNameAndTable("building_menu", $"building_guid = '{post.build}' and main_system_guid = '{post.main}' and sub_system_guid = '{post.sub}'"); var authcode = await backendRepository.GetOneAsync(@$"select AuthCode from auth_page where building_guid = '{post.build}' and ShowView = '{post.sub}'"); if (authcode != null) { await backendRepository.PurgeOneByGuidWithCustomDBNameAndTable("role_auth", $" AuthCode = '{authcode}'"); await backendRepository.PurgeOneByGuidWithCustomDBNameAndTable("auth_page", $" AuthCode = '{authcode}'"); await backendRepository.ExecuteSql(@"delete a from role_auth a join role b on a.role_guid = b.role_guid where b.layer = 0; INSERT INTO role_auth (role_guid,AuthCode,created_by) SELECT r.role_guid,ap.AuthCode,'0' created_by FROM auth_page ap,role r WHERE r.layer = 0;"); #region 新增至派送資料表 var sql = $@"SELECT ra.* FROM role_auth ra join role r on ra.role_guid = r.role_guid where r.layer = 0"; var role_auths = await backendRepository.GetAllAsync(sql); List> role_auth_dicts = new List>(); foreach (var role_auth in role_auths) { Dictionary role_auth_dict = new Dictionary() { { "role_guid", role_auth.Role_guid}, { "@AuthCode", role_auth.AuthCode}, }; role_auth_dicts.Add(role_auth_dict); } await backendRepository.ManualInsertBackgroundServiceTask("", "", "role_auth", "purge_specify_insert", role_auth_dicts); #endregion } apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task BuildMenuFloorTable(MenuIn post) { List buildMenuFloorTables = new List(); ApiResult> apiResult = new ApiResult>(); try { buildMenuFloorTables = await backendRepository.GetAllAsync($@" select f.full_name floor_guid_name,sf.*,ms.full_name main_system_guid_name,ss.full_name sub_system_guid_name from (select * from sub_system_floor ssf where ssf.building_guid = '{post.build}' and ssf.main_system_guid = '{post.main}' and ssf.sub_system_guid = '{post.sub}' and deleted = 0 and status = 0) sf left join floor f on sf.floor_guid = f.floor_guid left join main_system ms on ms.main_system_guid = sf.main_system_guid left join sub_system ss on ss.sub_system_guid = sf.sub_system_guid ORDER BY ms.priority, ss.priority, f.priority"); apiResult.Code = "0000"; apiResult.Data = buildMenuFloorTables; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } [HttpPost] public async Task>> GetNotUsefloor(MenuIn post) { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { var sqlString = @$"select fg.floor_guid value, fg.full_name name from (select * from floor fg where fg.building_guid = '{post.build}' and fg.deleted = 0 and fg.status = 0 ) fg left join (select * from sub_system_floor where building_guid = '{post.build}' and main_system_guid = '{post.main}' and sub_system_guid = '{post.sub}' and deleted = 0 and status = 0) ssf on ssf.floor_guid = fg.floor_guid where ssf.floor_guid is null ORDER BY fg.priority"; KeyValue = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> SaveAddsubfloor(MenuInfloor menuInfloor) { ApiResult apiResult = new ApiResult(); try { var listdictionary = new List>(); foreach (var a in menuInfloor.floorlist) { var dictionary = new Dictionary() { {"@sub_system_floor_guid", Guid.NewGuid()}, {"@building_guid",menuInfloor.build}, {"@main_system_guid", menuInfloor.main}, {"@sub_system_guid",menuInfloor.sub}, {"@floor_guid",a}, {"@created_by", myUserInfo.Userinfo_guid} }; listdictionary.Add(dictionary); } await backendRepository.AddMutiByCustomTable(listdictionary, "sub_system_floor"); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(menuInfloor); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> DeleteBuildFloorMenu(string subfloorguid) { ApiResult apiResult = new ApiResult(); try { //檢查該樓層底下是否有設備 var sql = $@" SELECT CONCAT(b.full_name, ' - ', ms.full_name, ' - ', ss.full_name , ' - ', f.full_name) FROM device d LEFT JOIN ( SELECT * FROM sub_system_floor ssf WHERE ssf.deleted = 0 AND ssf.sub_system_floor_guid = @Guid) ssf ON d.deleted = 0 AND d.building_guid = ssf.building_guid AND d.main_system_guid = ssf.main_system_guid AND d.sub_system_guid = ssf.sub_system_guid AND d.floor_guid = ssf.floor_guid LEFT JOIN building b ON b.deleted = 0 AND d.building_guid = b.building_guid LEFT JOIN main_system ms ON ms.deleted = 0 AND d.main_system_guid = ms.main_system_guid LEFT JOIN sub_system ss ON ss.deleted = 0 AND d.sub_system_guid = ss.sub_system_guid LEFT JOIN floor f ON f.deleted = 0 AND d.floor_guid = f.floor_guid WHERE ssf.sub_system_floor_guid = @Guid"; var sub_system_floors = await backendRepository.GetAllAsync(sql, new { Guid = subfloorguid }); if (sub_system_floors.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "設備管理中尚有設備正在使用該選單樓層,故無法刪除"; return apiResult; } await backendRepository.DeleteOne(subfloorguid, "sub_system_floor", "sub_system_floor_guid"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(subfloorguid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task>> GetFloorInSubSystem(MenuIn post) { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { var sqlString = @$"select floor.floor_guid Value,floor.full_name Name from sub_system_floor sf left join floor on sf.floor_guid = floor.floor_guid where sf.deleted = 0 and sf.status = 0 and sf.building_guid = '{post.build}' and sf.main_system_guid = '{post.main}' and sf.sub_system_guid = '{post.sub}' ORDER BY floor.priority, floor.created_at"; KeyValue = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }