ibms-dome/FrontendWebApi/ApiControllers/BuildController.cs
2022-11-05 18:18:29 +08:00

690 lines
31 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<BuildController>
{
private readonly IBackendRepository backendRepository;
private readonly IFrontendRepository frontendRepository;
public BuildController
(
IBackendRepository backendRepository,
IFrontendRepository frontendRepository
)
{
this.backendRepository = backendRepository;
this.frontendRepository = frontendRepository;
}
/// <summary>
/// 取得天氣資訊
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/GetWeateher")]
public async Task<ActionResult<ApiResult<Weateher>>> GetWeateher()
{
ApiResult<Weateher> apiResult = new ApiResult<Weateher>(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<Weateher>(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);
}
/// <summary>
/// 區域基本資料列表
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<BuildInfo>>> BuildInfoList()
{
ApiResult<List<BuildInfo>> apiResult = new ApiResult<List<BuildInfo>>();
List<BuildInfo> buildInfo = new List<BuildInfo>();
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<BuildInfo>(sqlString);
apiResult.Code = "0000";
apiResult.Data = buildInfo;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
/// <summary>
/// 取得預設的棟別資訊
/// </summary>
/// <param name="defaultBuilding"></param>
/// <returns></returns>
[HttpPost]
[Route("api/GetDefaultBuilding")]
public async Task<ActionResult<ApiResult<Building>>> GetDefaultBuilding(DefaultBuilding defaultBuilding)
{
ApiResult<Building> apiResult = new ApiResult<Building>(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>("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);
}
/// <summary>
/// 取得L型選單資料
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
[HttpPost]
[Route("api/GetMenu")]
public async Task<ActionResult<ApiResult<List<Building>>>> GetMenu(string account)
{
ApiResult<List<Building>> apiResult = new ApiResult<List<Building>>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
var buildmenusql = await backendRepository.GetAllAsync<BuildMenuSql>(@$"
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<Floorsql>(@"
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<KeyValue>($@"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<Building> buildingMenus = new List<Building>();
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<Main_system>(),
common = new List<string>()
};
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<Sub_systemGuid>()
};
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<ActionResult<ApiResult<Sub_system>>> GetSubSystem(GetSubPost get)
{
ApiResult<Sub_system> apiResult = new ApiResult<Sub_system>(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<BuildMenuSql>(@$"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<Floor> Floors = new List<Floor>();
var floorsql = await backendRepository.GetAllAsync<Floorsql>($@"
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<Floor>(),
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<ActionResult<ApiResult<Floor>>> GetFloor(string guid)
{
ApiResult<Floor> apiResult = new ApiResult<Floor>(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>("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);
}
/// <summary>
/// 依據過濾條件取得該棟別與系統tag name
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
[Route("api/GetBuildingAndSystemTagName")]
public async Task<ActionResult<ApiResult<BuildingSystemTagName>>> GetBuildingAndSystemTagName(PostTagName post)
{
ApiResult<BuildingSystemTagName> apiResult = new ApiResult<BuildingSystemTagName>(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<BuildingSystemTagName>(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);
}
/// <summary>
/// 透過過濾條件,取得設備
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
[Route("api/GetDevice")]
public async Task<ActionResult<ApiResult<List<DeviceList>>>> GetDevice(GetSubPost post)
{
ApiResult<List<DeviceList>> apiResult = new ApiResult<List<DeviceList>>();
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<string>();
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<DeviceFloor>(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<DeviceNode>(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<DeviceItem>(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<DeviceList>();
foreach (var one in dev)
{
DeviceList afloor = new DeviceList()
{
floor_guid = one.Key,
device = new List<Device>()
};
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<ActionResult<ApiResult<List<string>>>> GetAllfloorSVG(string guid)
{
ApiResult<List<string>> apiResult = new ApiResult<List<string>>(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<string>(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);
}
}
}