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_tag 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 building_tag) { ApiResult> apiResult = new ApiResult>(); List KeyValue = new List(); try { 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 group by bm.main_system_tag ) bm left 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"; var param = new { building_tag = building_tag, main_system_type = main_system_type }; KeyValue = await backendRepository.GetAllAsync(sqlString, param); 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 system_key Name, system_value Value from variable where deleted = 0 and system_type = @main_system_type ORDER BY system_priority ASC"; var param = new { main_system_type = main_system_type }; KeyValue = await backendRepository.GetAllAsync(sqlString, param); 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 mainList = await backendRepository.GetAllAsync("select id from variable where system_value = @main_system_tag and system_type = @main_system_type", new { main_system_tag = post.main_system_tag, main_system_type = main_system_type}); var sqlString = @$"select sv.system_value value, sv.system_key name from variable sv left join ( 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"; KeyValue = await backendRepository.GetAllAsync(sqlString, new { building_tag = post.building_tag, mainList = mainList}); 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_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 }); var dictionary = new Dictionary() { {"@building_tag", buildMenu.building_tag}, {"@main_system_tag",buildMenu.main_system_tag }, {"@sub_system_tag", buildMenu.sub_system_tag}, {"@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_tag = @Building_tag"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_tag = buildMenu.building_tag }); 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_tag = @Building_tag"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_tag = buildMenu.building_tag }); 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(SUBSTRING(AuthCode,2,5), SIGNED)) AuthCode from auth_page ap where ap.AuthCode like 'F%'"); var pageSql = $@"select sv.system_key SubName, mv.system_key MainName from variable sv left join variable mv on sv.system_parent_id = mv.id where (sv.system_value = @sub_system_tag and sv.system_type = @sub_system_type) and (mv.system_value = @main_system_tag and mv.system_type = @main_system_type)"; var pageParam = new { sub_system_tag = buildMenu.sub_system_tag, main_system_tag = buildMenu.main_system_tag, sub_system_type = sub_system_type, main_system_type = main_system_type}; var page = await backendRepository.GetOneAsync(pageSql, pageParam); var pagedictionary = new Dictionary() { {"@AuthCode", "F" +(max+1).ToString() }, {"@AuthType", 1 }, {"@MainName", page.MainName}, {"@SubName",page.SubName}, {"@building_tag",buildMenu.building_tag}, {"@ShowView",subV.id} }; await backendRepository.AddOneByCustomTable(pagedictionary, "auth_page"); var param = new { main_system_type = main_system_type, sub_system_type = sub_system_type }; await backendRepository.ExecuteSql(@"DELETE FROM auth_page WHERE auth_page.AuthCode like 'F%'; INSERT INTO auth_page (AuthCode,AuthType,MainName,SubName,building_tag,ShowView) SELECT concat('F', CONVERT(ROW_NUMBER() OVER(ORDER BY bm.building_tag ASC), nchar)) AuthCode, '1' as AuthType, mv.system_key MainName, sv.system_key SubName, bm.building_tag, sv.id ShowView FROM building_menu bm LEFT JOIN variable mv ON bm.main_system_tag = mv.system_value AND mv.system_type = @main_system_type LEFT JOIN variable sv ON bm.sub_system_tag = sv.system_value AND sv.system_type = @sub_system_type", param); 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;"); 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_tag = @Building_tag"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_tag = buildMenu.building_tag }); 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_tag = @Building_tag"; var building = await backendRepository.GetOneAsync("building", building_where, new { Building_tag = buildMenu.building_tag }); 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_tag = '{buildMenu.building_tag}' and main_system_tag = '{buildMenu.main_system_tag}' and sub_system_tag = '{buildMenu.sub_system_tag}'"); 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, mv.system_key main_system_guid_name, sv.system_key sub_system_guid_name,ff.full_name floor_guid_name from building_menu bm left join variable mv on bm.main_system_tag = mv.system_value and mv.system_type = @main_system_type and mv.deleted = 0 left 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 ff on ff.floor_guid = bm.planimetric_floor_guid where bm.building_tag = @building_tag and bm.main_system_tag in @MainList ORDER BY 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 }); 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 *, sv.system_key sub_system_guid_name from building_menu bm left join variable sv on bm.sub_system_tag = sv.system_value and sv.system_type = @sub_system_type where bm.building_tag = @building_tag and bm.main_system_tag = @main_system_tag and bm.sub_system_tag = @sub_system_tag" , new { building_tag = post.building_tag, main_system_tag = post.main_system_tag, sub_system_tag = post.sub_system_tag, sub_system_type = sub_system_type }); 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_tag = '{post.building_tag}' and main_system_tag = '{post.main_system_tag}' and sub_system_tag = '{post.sub_system_tag}'"); var subV = await backendRepository.GetOneAsync(@$"select * from variable where system_value = @sub_system_tag and system_type = sub_system_type and deleted = 0", new { sub_system_tag = post.sub_system_tag, sub_system_type = sub_system_type }); var authcode = await backendRepository.GetOneAsync(@$"select AuthCode from auth_page where building_tag = @building_tag and ShowView = @sub_id", new {post.building_tag, subV.id}); 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;"); } 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 { var param = new { building_tag = post.building_tag, main_system_tag = post.main_system_tag, sub_system_tag = post.sub_system_tag, main_system_type = main_system_type, sub_system_type = sub_system_type }; buildMenuFloorTables = await backendRepository.GetAllAsync($@" select sf.id sub_system_floor_id, f.full_name floor_guid_name, sf.*, mv.system_key main_system_guid_name, sv.system_key sub_system_guid_name from ( select * from sub_system_floor ssf where ssf.building_tag = @building_tag and ssf.main_system_tag = @main_system_tag and ssf.sub_system_tag = @sub_system_tag and deleted = 0 and status = 0 ) sf left join floor f on sf.floor_tag = f.full_name left join variable mv on sf.main_system_tag = mv.system_value and mv.system_type = @main_system_type and mv.deleted = 0 left join variable sv on sf.sub_system_tag = sv.system_value and sv.system_type = @sub_system_type and sv.deleted = 0 ORDER BY mv.system_priority, sv.system_priority, f.priority", param); 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 param = new { building_tag = post.building_tag, main_system_tag = post.main_system_tag, sub_system_tag = post.sub_system_tag }; var sqlString = @$"select f.floor_guid value, f.full_name name from ( select * from floor f where f.building_tag = @building_tag and f.deleted = 0 and f.status = 0 ) f left join ( select * from sub_system_floor where building_tag = @building_tag and main_system_tag = @main_system_tag and sub_system_tag = @sub_system_tag and deleted = 0 and status = 0 ) ssf on ssf.floor_tag = f.full_name where ssf.floor_tag is null ORDER BY f.priority"; KeyValue = await backendRepository.GetAllAsync(sqlString, param); 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() { {"@building_tag", menuInfloor.building_tag}, {"@main_system_tag", menuInfloor.main_system_tag}, {"@sub_system_tag",menuInfloor.sub_system_tag}, {"@floor_tag", a}, {"@is_link", 1}, {"@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 sub_system_floor_id) { ApiResult apiResult = new ApiResult(); try { //檢查該樓層底下是否有設備 var sql = $@"SELECT CONCAT(b.full_name, ' - ', mv.system_key, ' - ', sv.system_key, ' - ', f.full_name) FROM device d LEFT JOIN ( SELECT * FROM sub_system_floor ssf WHERE ssf.deleted = 0 AND ssf.id = @sub_system_floor_id ) ssf ON d.deleted = 0 AND d.device_building_tag = ssf.building_tag AND d.device_system_tag = ssf.main_system_tag AND d.device_name_tag = ssf.sub_system_tag AND d.device_floor_tag = ssf.floor_tag LEFT JOIN building b ON b.deleted = 0 AND d.device_building_tag = b.building_tag LEFT JOIN variable mv on mv.deleted = 0 AND d.device_system_tag = mv.system_value AND mv.system_type = @main_system_type and mv.deleted = 0 LEFT JOIN variable sv on sv.deleted = 0 AND d.device_name_tag = sv.system_value AND sv.system_type = @sub_system_type and sv.deleted = 0 LEFT JOIN floor f ON f.deleted = 0 AND d.full_name = f.full_name WHERE ssf.id = @sub_system_floor_id"; var sub_system_floors = await backendRepository.GetAllAsync(sql, new { sub_system_floor_id = sub_system_floor_id, main_system_type = main_system_type, sub_system_type = sub_system_type }); if (sub_system_floors.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "設備管理中尚有設備正在使用該選單樓層,故無法刪除"; return apiResult; } await backendRepository.DeleteOne(sub_system_floor_id, "sub_system_floor", "id"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(sub_system_floor_id); 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 f.floor_guid Value, f.full_name Name from sub_system_floor sf left join floor f on sf.floor_tag = f.full_name where sf.deleted = 0 and sf.status = 0 and sf.building_tag = @building_tag and sf.main_system_tag = @main_system_tag and sf.sub_system_tag = @sub_system_tag ORDER BY f.priority, f.created_at"; KeyValue = await backendRepository.GetAllAsync(sqlString, new { building_tag = post.building_tag, main_system_tag = post.main_system_tag, sub_system_type = post.sub_system_tag }); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }