diff --git a/FrontendWebApi/ApiControllers/DeviceManageController.cs b/FrontendWebApi/ApiControllers/DeviceManageController.cs index b46c2cb..318182d 100644 --- a/FrontendWebApi/ApiControllers/DeviceManageController.cs +++ b/FrontendWebApi/ApiControllers/DeviceManageController.cs @@ -33,12 +33,16 @@ namespace FrontendWebApi.ApiControllers this.frontendRepository = frontendRepository; } - + /// + /// 系統監控列表 + /// + /// + /// [HttpPost] - [Route("api/Device/GetDeviceList")] - public async Task>>> GetDeviceList() + [Route("api/Device/GetMainSub")] + public async Task>> GetMainSub() { - ApiResult> apiResult = new ApiResult>(jwt_str); + ApiResult apiResult = new ApiResult(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; @@ -46,58 +50,145 @@ namespace FrontendWebApi.ApiControllers } try { - List buildingMenus = new List(); - var buildmenusql = await backendRepository.GetAllAsync(@$"select - me.* , b.full_name bfull_name,b.ip_address,b.priority bpriority, - ma.full_name mafull_name,ma.priority mapriority,ma.code, - sub.full_name subfull_name,sub.priority subpriority - from building_menu me - left join building b on b.building_guid = me.building_guid - left join main_system ma on ma.main_system_guid = me.main_system_guid - left join sub_system sub on sub.sub_system_guid = me.sub_system_guid"); - - var floorsql = await backendRepository.GetAllAsync(@" - select * from (select * from sub_system_floor ssf where ssf.deleted = 0 and ssf.status = 0) a - left join floor on floor.floor_guid = a.floor_guid"); - - var building = buildmenusql.GroupBy(a => a.building_guid).ToList(); - foreach (var menu in building) + var dbsub = await frontendRepository.GetAllAsync( + @$"select distinct v1.system_key main_name, v1.system_value main_system_tag, v2.system_key sub_name, v2.system_value sub_system_tag, v1.system_priority, v2.system_priority + from role_auth a + join auth_page b on a.AuthCode = b.AuthCode + join userinfo c on c.role_guid = a.role_guid + join variable v2 on b.ShowView = v2.id and v2.system_type = @sub_system_type + join variable v1 on v1.id = v2.system_parent_id and v1.system_type = @main_system_type + where c.account = @account + order by v1.system_priority, v2.system_priority", new { @account = myUser.account, @sub_system_type = sub_system_type, @main_system_type = main_system_type }); + var mains = dbsub.GroupBy(a => a.main_system_tag).ToList(); + apiResult.Data = new History_MainSubBuildFloor(); + apiResult.Data.history_Main_Systems = new List(); + foreach (var main in mains) { - Building building1 = new Building() - { - building_guid = menu.Select(a => a.building_guid).FirstOrDefault(), - full_name = menu.Select(a => a.bfull_name).FirstOrDefault(), - ip_address = menu.Select(a => a.ip_address).FirstOrDefault(), - priority = menu.Select(a => a.bpriority).FirstOrDefault(), - main_system = new List() - }; - var mainsystem = menu.GroupBy(a => a.main_system_guid).ToList(); - foreach (var ma in mainsystem) - { - Main_system main_System = new Main_system() - { - code = ma.Select(a => a.code).FirstOrDefault(), - main_system_guid = ma.Select(a => a.main_system_guid).FirstOrDefault(), - full_name = ma.Select(a => a.mafull_name).FirstOrDefault(), - priority = ma.Select(a => a.mapriority).FirstOrDefault(), - Sub_system = new List() - }; - var subsystem = ma.GroupBy(a => a.sub_system_guid).ToList(); - foreach (var sub in subsystem) - { - Sub_systemGuid sub_System = new Sub_systemGuid() - { - sub_system_guid = sub.Select(a => a.sub_system_guid).FirstOrDefault(), - full_name = sub.Select(a => a.subfull_name).FirstOrDefault(), - priority = sub.Select(a => a.subpriority).FirstOrDefault() - }; - main_System.Sub_system.Add(sub_System); - } - building1.main_system.Add(main_System); - } - buildingMenus.Add(building1); + History_Main_system history_Main_System = new History_Main_system(); + history_Main_System.main_system_tag = main.Select(a => a.main_system_tag).FirstOrDefault(); + history_Main_System.full_name = main.Select(a => a.main_name).FirstOrDefault(); + + apiResult.Data.history_Main_Systems.Add(history_Main_System); } - apiResult.Data = buildingMenus; + + apiResult.Code = "0000"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + return Ok(apiResult); + } + return Ok(apiResult); + } + + /// + /// 東別列表 + /// + /// + /// + [HttpPost] + [Route("api/Device/GetBuild")] + public async Task>>> GetBuild() + { + ApiResult> apiResult = new ApiResult>(); + + try + { + var sqlString = $@"select building_tag, full_name from building where deleted = 0"; + var bl = await backendRepository.GetAllAsync(sqlString); + + apiResult.Code = "0000"; + apiResult.Data = bl; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + return Ok(apiResult); + } + return Ok(apiResult); + } + + /// + /// 樓層列表 + /// + /// + /// + [HttpPost] + [Route("api/Device/GetFloor")] + public async Task>>> GetFloor([FromBody] FindDevice fd) + { + ApiResult> apiResult = new ApiResult>(); + + if (string.IsNullOrEmpty(fd.building_tag)) + { + apiResult.Code = "0002"; + apiResult.Msg = "必須選擇東別"; + return apiResult; + } + + try + { + var sqlString = $@"select floor_tag, full_name from floor where deleted = 0 and building_tag = @building_tag"; + var param = new { @building_tag = fd.building_tag }; + var fl = await backendRepository.GetAllAsync(sqlString, param); + + apiResult.Code = "0000"; + apiResult.Data = fl; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + return Ok(apiResult); + } + return Ok(apiResult); + } + + /// + /// 設備列表 + /// + /// floor_tag: null->總覽 + /// + [HttpPost] + [Route("api/Device/GetDeviceList")] + public async Task>>> GetDeviceList([FromBody] FindDevice fd) + { + ApiResult> apiResult = new ApiResult>(); + + if (string.IsNullOrEmpty(fd.main_system_tag)) + { + apiResult.Code = "0001"; + apiResult.Msg = "必須系統類別"; + return BadRequest(apiResult); + } + else if (string.IsNullOrEmpty(fd.building_tag)) + { + apiResult.Code = "0002"; + apiResult.Msg = "必須選擇東別"; + return BadRequest(apiResult); + } + + try + { + var sqlString = $@"select floor_tag, full_name from floor where deleted = 0 and building_tag = @building_tag and full_name = ifnull(@floor_tag, full_name)"; + var param = new { @building_tag = fd.building_tag, @floor_tag = fd.floor_tag }; + var fl = await backendRepository.GetAllAsync(sqlString, param); + + foreach (var f in fl) + { + List dl = new List(); + sqlString = $@"select device_guid, device_last_name, status, device_coordinate where deleted = 0 and device_system_tag = @main_system_tag and device_building_tag = @building_tag and device_floor_tag = ifnull(@floor_tag, device_floor_tag)"; + var dlParam = new { @main_system_tag = fd.main_system_tag, @building_tag = fd.building_tag, @floor_tag = fd.floor_tag }; + dl = await backendRepository.GetAllAsync(sqlString, dlParam); + f.device_list = dl; + } + + apiResult.Data = fl; apiResult.Code = "0000"; } catch (Exception exception) @@ -433,56 +524,5 @@ namespace FrontendWebApi.ApiControllers return Ok(apiResult); } - - /// - /// 即時趨勢條件過濾條件面板 - /// - /// - /// - [HttpPost] - [Route("api/Device/GetMainSub")] - public async Task>> GetMainSub() - { - ApiResult apiResult = new ApiResult(jwt_str); - if (!jwtlife) - { - apiResult.Code = "5000"; - return BadRequest(apiResult); - } - try - { - var dbsub = await frontendRepository.GetAllAsync( - @$"select distinct v1.system_key main_name, v1.system_value main_system_tag, v2.system_key sub_name, v2.system_value sub_system_tag, v1.system_priority, v2.system_priority - from role_auth a - join auth_page b on a.AuthCode = b.AuthCode - join userinfo c on c.role_guid = a.role_guid - join variable v2 on b.ShowView = v2.id and v2.system_type = @sub_system_type - join variable v1 on v1.id = v2.system_parent_id and v1.system_type = @main_system_type - where c.account = @account - order by v1.system_priority, v2.system_priority", new { @account = myUser.account, @sub_system_type = sub_system_type, @main_system_type = main_system_type }); - var mains = dbsub.GroupBy(a => a.main_system_tag).ToList(); - apiResult.Data = new History_MainSubBuildFloor(); - apiResult.Data.history_Main_Systems = new List(); - foreach (var main in mains) - { - History_Main_system history_Main_System = new History_Main_system(); - history_Main_System.main_system_tag = main.Select(a => a.main_system_tag).FirstOrDefault(); - history_Main_System.full_name = main.Select(a => a.main_name).FirstOrDefault(); - - apiResult.Data.history_Main_Systems.Add(history_Main_System); - } - - apiResult.Code = "0000"; - } - catch (Exception exception) - { - apiResult.Code = "9999"; - apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; - Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); - return Ok(apiResult); - } - return Ok(apiResult); - - } } } diff --git a/FrontendWebApi/Models/Device.cs b/FrontendWebApi/Models/Device.cs new file mode 100644 index 0000000..b7d2cea --- /dev/null +++ b/FrontendWebApi/Models/Device.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; + +namespace FrontendWebApi.Models +{ + public class FindDevice + { + public string main_system_tag { get; set; } + public string building_tag { get; set; } + public string floor_tag { get; set; } + } + + public class BuildList + { + public string full_name { get; set; } + public string building_tag { get; set; } + } + + public class FloorList + { + public string full_name { get; set; } + public string floor_tag { get; set; } + public List device_list {get; set;} + } + + public class DeviceLists + { + public string device_guid { get; set; } + public string full_name { get; set; } + public string device_coordinate { get; set; } + public string status { get; set; } + public string device_status + { + get + { + Dictionary name = new Dictionary() + { + { "0", "關閉"}, + { "1", "正常"}, + { "2", "異常"} + }; + return name[status]; + } + } + } +}