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 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 limit 1 ) AS WxText, ( SELECT 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 limit 1 ) AS Wx, ( SELECT w.get_value FROM api_weateher w WHERE w.weather_type = 'T' AND @DateNow < w.start_time limit 1 ) AS Temp, ( SELECT w.get_value FROM api_weateher w WHERE w.weather_type = 'RH' AND @DateNow < w.start_time limit 1 ) 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, v1.system_key mafull_name, v1.system_priority mapriority, v1.system_value, v2.system_key subfull_name, v2.system_priority subpriority from building_menu me left join building b on b.building_tag = me.building_tag left join variable v1 on me.main_system_tag = v1.system_value and v1.deleted = 0 and v1.system_type = 'device_system_category_layer2' left join variable v2 on me.sub_system_tag = v2.system_value and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3' inner join ( SELECT ap.building_tag, 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_tag = me.building_tag and shower.ShowView = v2.id order by b.priority, v1.system_priority, v2.system_priority, v2.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.full_name = a.floor_tag order by floor.priority;"); var common = await backendRepository.GetAllAsync($@"select ap.building_tag 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_tag).ToList(); List buildingMenus = new List(); foreach (var menu in building) { Building building1 = new Building() { 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(), 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_tag).Select(a => a.Value).ToList(); building1.common = commonlist; var mainsystem = menu.GroupBy(a => a.main_system_tag).ToList(); foreach (var ma in mainsystem) { Main_system main_System = new Main_system() { code = ma.Select(a => a.code).FirstOrDefault(), main_system_tag = ma.Select(a => a.main_system_tag).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_tag).ToList(); foreach (var sub in subsystem) { Sub_systemGuid sub_System = new Sub_systemGuid() { sub_system_tag = sub.Select(a => a.sub_system_tag).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.left_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 v.system_key subfull_name,me.* from building_menu me left join variable v on v.system_value = me.sub_system_tag and v.system_type = 'device_system_category_layer3' and v.deleted = 0 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 v.system_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_tag = '{get.main_system_tag}' and ssf.sub_system_tag = '{get.sub_system_tag}') a left join floor on floor.full_name = a.floor_tag and a.building_tag = floor.building_tag order by floor.priority"); Sub_system sub_System = new Sub_system() { sub_system_tag = sub_system.sub_system_tag, left_system_url = sub_system.left_system_url, left_drawing = sub_system.left_drawing, Floors = new List(), full_name = sub_system.subfull_name, left_icon_click = sub_system.left_icon_click, left_icon_click_url = sub_system.left_icon_click_url, left_planimetric_click = sub_system.left_planimetric_click, left_planimetric_floor_guid = sub_system.left_planimetric_floor_guid, left_riser_diagram_url = sub_system.left_riser_diagram_url, priority = sub_system.subpriority }; foreach (var floor in floorsql) { Floor floor1 = new Floor() { devices = null, floor_guid = floor.floor_guid, Floor_map_name = floor.Floor_map_name, 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, sub_system_tag FROM building_menu WHERE building_tag = @building_tag AND main_system_tag = @main_system_tag AND sub_system_tag = @sub_system_tag "; 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.left_icon_click, bm.left_icon_click_url, bm.left_icon_click_url_width, bm.left_icon_click_url_height, di.full_name as point_name, di.points, di.is_bool as points_is_bool, f.floor_guid 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 left join floor f on d.device_floor_tag = f.full_name and f.deleted = 0 {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); } [HttpPost] [Route("api/GetAllfloor")] public async Task>>> GetAllfloor(string building_tag) { ApiResult> apiResult = new ApiResult>(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { string sWhere = $@" SELECT floor_guid,full_name,building_tag FROM floor where building_tag = @building_tag and deleted = 0 order by priority"; var floor = await backendRepository.GetAllAsync(sWhere, new { building_tag = building_tag }); apiResult.Data = floor; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } return Ok(apiResult); } } }