using FrontendWebApi.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Repository.BackendRepository.Interface; using Repository.FrontendRepository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace FrontendWebApi.ApiControllers { public class BuildController : MyBaseApiController { private readonly IBackendRepository backendRepository; private readonly IFrontendRepository frontendRepository; public BuildController ( IBackendRepository backendRepository, IFrontendRepository frontendRepository ) { this.backendRepository = backendRepository; this.frontendRepository = frontendRepository; } /// /// 取得天氣資訊 /// /// [HttpPost] [Route("api/GetWeateher")] public async Task>> GetWeateher() { ApiResult apiResult = new ApiResult(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { var sql = $@" SELECT ( SELECT TOP(1) w.get_value FROM api_weateher w WHERE w.weather_type = 'Wx' AND @DateNow BETWEEN w.start_time AND w.end_time ORDER BY created_at DESC ) AS WxText, ( SELECT TOP(1) wd.WeatherKey FROM api_weateher w LEFT JOIN weather_description wd ON w.get_value = wd.WeatherValue WHERE w.weather_type = 'WxV' AND @DateNow BETWEEN w.start_time AND w.end_time ORDER BY created_at DESC ) AS Wx, ( SELECT TOP(1) w.get_value FROM api_weateher w WHERE w.weather_type = 'T' AND @DateNow < w.start_time ) AS Temp, ( SELECT TOP(1) w.get_value FROM api_weateher w WHERE w.weather_type = 'RH' AND @DateNow < w.start_time ) AS RH "; var dateNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:00"); var weateher = await backendRepository.GetOneAsync(sql, new { DateNow = dateNow }); apiResult.Data = weateher; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } /// /// 區域基本資料列表 /// /// [HttpPost] public async Task>> BuildInfoList() { ApiResult> apiResult = new ApiResult>(); List buildInfo = new List(); try { var sqlString = @$"SELECT A.priority, A.building_tag, A.full_name, A.ip_address, A.ip_port, (SELECT COUNT(*) FROM floor f WHERE f.deleted = 0 AND f.building_tag = A.building_tag) AS 'floorNum', A.created_at, A.orgName_3D, A.extName_3D FROM building A WHERE A.deleted = 0 ORDER BY A.priority ASC, A.created_at DESC"; buildInfo = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = buildInfo; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 取得預設的棟別資訊 /// /// /// [HttpPost] [Route("api/GetDefaultBuilding")] public async Task>> GetDefaultBuilding(DefaultBuilding defaultBuilding) { ApiResult apiResult = new ApiResult(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { var sWhere = "deleted = 0 AND ip_address = @ip_address AND ip_port = @ip_port"; var building = await frontendRepository.GetOneAsync("building", sWhere, new { ip_address = defaultBuilding.ip_address, ip_port = defaultBuilding.ip_port }); apiResult.Data = building; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } /// /// 取得L型選單資料 /// /// /// [HttpPost] [Route("api/GetMenu")] public async Task>>> GetMenu(string account) { ApiResult> apiResult = new ApiResult>(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { 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 inner join ( SELECT ap.building_guid, ap.ShowView FROM ( SELECT * FROM role_auth ra WHERE ra.role_guid = (SELECT ui.role_guid from userinfo ui where account = @Account) ) ra left join auth_page ap on ra.AuthCode = ap.AuthCode where ap.AuthType = 1 ) shower on shower.building_guid = me.building_guid and shower.ShowView = me.sub_system_guid order by b.priority, ma.priority,sub.priority, sub.created_at DESC", new { Account = account }); 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 order by floor.priority"); var common = await backendRepository.GetAllAsync($@"select ap.building_guid Name,ap.ShowView Value from auth_page ap left join role_auth ra on ra.AuthCode = ap.AuthCode right join userinfo ui on ui.role_guid = ra.role_guid where SUBSTRING(ap.AuthCode,1,1) = 'C' and ui.account = '{account}' "); var building = buildmenusql.GroupBy(a => a.building_guid).ToList(); List buildingMenus = new List(); foreach (var menu in building) { 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(), device_building_tag = menu.Select(a => a.device_building_tag).FirstOrDefault(), main_system = new List(), common = new List() }; var commonlist = common.Where(a => a.Name == building1.building_guid).Select(a => a.Value).ToList(); building1.common = commonlist; 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(), device_system_tag = sub.Select(a => a.device_system_tag).FirstOrDefault(), OpenTab = sub.Select(a => a.OpenTab).FirstOrDefault(), system_url = sub.Select(a => a.system_url).FirstOrDefault() }; main_System.Sub_system.Add(sub_System); } building1.main_system.Add(main_System); } buildingMenus.Add(building1); } apiResult.Data = buildingMenus; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } [HttpPost] [Route("api/GetSubSystem")] public async Task>> GetSubSystem(GetSubPost get) { ApiResult apiResult = new ApiResult(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } if (get.building_tag == null || get.main_system_tag == null || get.sub_system_tag == null) { apiResult.Code = "9997"; return Ok(apiResult); } try { var sub_system = await backendRepository.GetOneAsync(@$"select ss.full_name subfull_name,me.* from building_menu me left join sub_system ss on ss.sub_system_tag = me.sub_system_tag where me.building_tag = '{get.building_tag}' and me.main_system_tag = '{get.main_system_tag}' and me.sub_system_tag = '{get.sub_system_tag}' order by ss.priority"); List Floors = new List(); var floorsql = await backendRepository.GetAllAsync($@" select * from (select * from sub_system_floor ssf where ssf.deleted = 0 and ssf.status = 0 and ssf.building_tag = '{get.building_tag}' and ssf.main_system_guid = '{get.main_system_tag}' and ssf.sub_system_tag = '{get.sub_system_tag}') a left join floor on floor.floor_guid = a.floor_guid order by floor.priority"); Sub_system sub_System = new Sub_system() { sub_system_guid = sub_system.sub_system_guid, system_url = sub_system.system_url, drawing = sub_system.drawing, Floors = new List(), full_name = sub_system.subfull_name, icon_click = sub_system.icon_click, icon_click_url = sub_system.icon_click_url, planimetric_click = sub_system.planimetric_click, planimetric_floor_guid = sub_system.planimetric_floor_guid, riser_diagram_url = sub_system.riser_diagram_url, priority = sub_system.subpriority }; foreach (var floor in floorsql) { Floor floor1 = new Floor() { devices = null, floor_guid = floor.floor_guid, full_name = floor.full_name, InitMapName = floor.InitMapName, priority = floor.priority }; Floors.Add(floor1); } sub_System.Floors = Floors; apiResult.Data = sub_System; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } [HttpPost] [Route("api/GetFloor")] public async Task>> GetFloor(string guid) { ApiResult apiResult = new ApiResult(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { string sWhere = $@"deleted = 0 AND floor_guid = @Guid"; var floor = await backendRepository.GetOneAsync("floor", sWhere, new { Guid = guid }); apiResult.Data = floor; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } /// /// 依據過濾條件,取得該棟別與系統tag name /// /// /// [HttpPost] [Route("api/GetBuildingAndSystemTagName")] public async Task>> GetBuildingAndSystemTagName(PostTagName post) { ApiResult apiResult = new ApiResult(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { string sql = $@" SELECT device_building_tag, device_system_tag FROM building_menu WHERE building_guid = @building_guid AND main_system_guid = @main_system_guid AND sub_system_guid = @sub_system_guid "; var tagName = await backendRepository.GetOneAsync(sql, post); apiResult.Data = tagName; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } /// /// 透過過濾條件,取得設備 /// /// /// [HttpPost] [Route("api/GetDevice")] public async Task>>> GetDevice(GetSubPost post) { ApiResult> apiResult = new ApiResult>(); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } if (post.building_tag == null) { apiResult.Code = "9997"; return Ok(apiResult); } try { var main_system_guidsql = ""; var sub_system_guidsql = ""; var disasterjoinsql = ""; var disastersql = ""; var layer3sql = ""; var sWhere = ""; if (!String.IsNullOrEmpty(post.main_system_tag)) { main_system_guidsql = $" and d.device_system_tag = '{post.main_system_tag}'"; } if (!String.IsNullOrEmpty(post.sub_system_tag)) { sub_system_guidsql = $" and d.device_name_tag = '{post.sub_system_tag}'"; } if (!String.IsNullOrEmpty(post.device_system_category_layer3)) { layer3sql = $" and d.device_system_category_layer3 = '{post.device_system_category_layer3}'"; } if (!String.IsNullOrEmpty(post.device_system_value)) { disasterjoinsql = " left join device_disaster dd on dd.device_guid = d.device_guid"; disastersql = $" and dd.device_system_value = '{post.device_system_value}'"; } if (!String.IsNullOrEmpty(post.show_cctv) && post.show_cctv == "1") { var str_arr = new List(); if (!String.IsNullOrEmpty(post.main_system_tag)) { str_arr.Add($@"d.device_system_tag = '{post.main_system_tag}'"); } if (!String.IsNullOrEmpty(post.sub_system_tag)) { str_arr.Add($@"d.device_name_tag = '{post.sub_system_tag}'"); } if (!String.IsNullOrEmpty(post.device_system_category_layer3)) { str_arr.Add($@"d.device_system_category_layer3 = '{post.device_system_category_layer3}'"); } if (!String.IsNullOrEmpty(post.device_system_value)) { str_arr.Add($@"dd.device_system_value = '{post.device_system_value}'"); } if (str_arr.Count > 0) { sWhere = $@" AND ( ( {string.Join(" AND ", str_arr)} ) OR d.device_system_category_layer3 = 'C' )"; } else { sWhere = $@" AND d.device_system_category_layer3 = 'C'"; } } else { sWhere = $@"{main_system_guidsql} {sub_system_guidsql} {disastersql} {layer3sql}"; } var sql = $@"select d.*, dk.device_image, dk.device_normal_text, dk.device_normal_point_name, dk.device_normal_point_col, dk.device_normal_point_value, dk.device_normal_color, dk.device_normal_flashing, dk.device_close_text, dk.device_close_point_name, dk.device_close_point_col, dk.device_close_point_value, dk.device_close_color, dk.device_close_flashing, dk.device_error_text, dk.device_error_point_name, dk.device_error_point_col, dk.device_error_point_value, dk.device_error_color, dk.device_error_flashing, dk.device_error_independent, CASE WHEN dm.device_master_number IS NOT NULL THEN dm.device_master_number ELSE CONCAT(d.device_building_tag, '_', d.device_name_tag) END AS device_master_number, dm.device_master_full_name, dm.device_master_icon, bm.icon_click, bm.icon_click_url, bm.icon_click_url_width, bm.icon_click_url_height, di.full_name as point_name, di.points, di.is_bool as points_is_bool from device d left join device_kind dk on d.device_building_tag = dk.device_building_tag and d.device_system_tag = dk.device_system_tag -- and d.device_floor_tag = dk.device_floor_tag and d.device_name_tag = dk.device_name_tag left join device_master dm ON d.device_building_tag = dm.device_building_tag AND d.device_name_tag = dm.device_name_tag left join device_item di ON d.device_name_tag = di.device_name_tag AND di.deleted = 0 AND di.is_show_riserDiagram = 1 {disasterjoinsql} LEFT JOIN building_menu bm ON d.device_building_tag = bm.building_tag AND d.device_system_tag = bm.main_system_tag AND d.device_name_tag = bm.sub_system_tag where d.deleted = 0 and d.device_building_tag = '{post.building_tag}' {sWhere} order by d.priority ASC, d.device_number ASC"; var devicelist = await backendRepository.GetAllAsync(sql); //抓出該設備底下的子節點 var sql_node = $@"SELECT dn.device_node_guid, dn.device_guid, dn.full_name AS Device_node_full_name, dn.device_node_coordinate, dn.priority FROM device_node dn WHERE dn.deleted = 0 AND dn.device_guid = @device_guid ORDER BY dn.priority ASC"; foreach (var device in devicelist) { device.Device_nodes = await backendRepository.GetAllAsync(sql_node, new { device_guid = device.device_guid }); } //抓出該設備所需要的顯示的即時資料(平面圖) var sql_tips = $@"SELECT di.* FROM device_item di WHERE di.deleted = 0 AND is_show = 1 AND device_name_tag = @sub_system_tag"; var device_item_floormap = await backendRepository.GetAllAsync(sql_tips, new { sub_system_tag = post.sub_system_tag }); foreach (var device in devicelist) { device.deviceItems = device_item_floormap; } var dev = devicelist.GroupBy(a => a.floor_guid).ToList(); apiResult.Data = new List(); foreach (var one in dev) { DeviceList afloor = new DeviceList() { floor_guid = one.Key, device = new List() }; foreach (var de in one) { var device_url = string.Empty; if (((de.icon_click & 1) > 0) && !string.IsNullOrEmpty(de.icon_click_url)) { device_url = de.icon_click_url; //點擊顯示系統圖 if (de.icon_click_url.Contains("[device_building_tag]")) { device_url = device_url.Replace("[device_building_tag]", de.device_building_tag); } if (de.icon_click_url.Contains("[device_system_tag]")) { device_url = device_url.Replace("[device_system_tag]", de.device_system_tag); } if (de.icon_click_url.Contains("[device_floor_tag]")) { device_url = device_url.Replace("[device_floor_tag]", de.device_floor_tag); } if (de.icon_click_url.Contains("[device_name_tag]")) { device_url = device_url.Replace("[device_name_tag]", de.device_name_tag); } if (de.icon_click_url.Contains("[device_serial_tag]")) { device_url = device_url.Replace("[device_serial_tag]", de.device_serial_tag); } if (de.icon_click_url.Contains("[tag_name]")) { device_url = device_url.Replace("[tag_name]", de.device_number); } de.icon_click_url = null; de.DeviceURL = device_url; } afloor.device.Add(de); //Device device = new Device() //{ // device_building_tag = de.device_building_tag, // device_close_color =de.device_close_color, // device_coordinate = de.device_coordinate, // device_error_color = de.device_error_color, // device_flashing = de.device_flashing, // device_floor_tag = de.device_floor_tag, // device_guid = de.device_guid , // device_image = de.device_image, // device_model = de.device_model, // device_name_tag = de.device_name_tag, // device_normal_color = de.device_normal_color, // device_number = de.device_number, // device_serial_tag= de.device_serial_tag, // device_system_tag= de.device_system_tag, // full_name = de.full_name, // priority = de.priority, // status = de.status, // DeviceURL = device_url, // icon_click = de.icon_click //}; //afloor.device.Add(device); } apiResult.Data.Add(afloor); } apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } [HttpPost] [Route("api/GetAllfloorSVG")] public async Task>>> GetAllfloorSVG(string guid) { ApiResult> apiResult = new ApiResult>(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { string sWhere = $@" SELECT floor_map_name FROM floor where building_guid = @Guid and deleted = 0"; var floor = await backendRepository.GetAllAsync(sWhere, new { Guid = guid }); apiResult.Data = floor; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } } }