586 lines
24 KiB
C#
586 lines
24 KiB
C#
using Backend.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Repository.BackendRepository.Interface;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using System.Transactions;
|
|
|
|
namespace Backend.Controllers
|
|
{
|
|
public class BuildInfoController : MybaseController<BuildInfoController>
|
|
{
|
|
private readonly IBackendRepository backendRepository;
|
|
private string mapFileSaveAsPath = "";
|
|
|
|
public BuildInfoController(IBackendRepository backendRepository)
|
|
{
|
|
this.backendRepository = backendRepository;
|
|
|
|
mapFileSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "floor_map");
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <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
|
|
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="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> SaveBuildInfo(BuildInfo post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
try
|
|
{
|
|
//判斷監控主機IP是否重複
|
|
var judgeIPAddressRepeat = true;
|
|
|
|
var sWhere = $@"deleted = 0 AND ip_address = @ip_address AND ip_port = @ip_port AND building_tag != @building_tag";
|
|
var buildInfos = await backendRepository.GetAllAsync<BuildInfo>("building", sWhere, new { ip_address = post.Ip_address, ip_port = post.Ip_port, building_tag = post.building_tag });
|
|
if (buildInfos.Count == 0)
|
|
{
|
|
judgeIPAddressRepeat = false;
|
|
}
|
|
|
|
if (!judgeIPAddressRepeat)
|
|
{
|
|
//Check for duplicate building tag
|
|
sWhere = $@"deleted = 0 AND building_tag = @Building_tag";
|
|
buildInfos = await backendRepository.GetAllAsync<BuildInfo>("building", sWhere, new { building_tag = post.building_tag });
|
|
|
|
if (buildInfos.Count > 0)
|
|
{
|
|
apiResult.Code = "0002";
|
|
apiResult.Msg = "區域代號不可重複";
|
|
return apiResult;
|
|
}
|
|
|
|
//新增
|
|
//抓取當前的Priority
|
|
var current_priority = await backendRepository.GetCurrentPriority("building");
|
|
|
|
Dictionary<string, object> building = new Dictionary<string, object>();
|
|
building = new Dictionary<string, object>()
|
|
{
|
|
{ "@building_tag", post.building_tag},
|
|
{ "@full_name", post.Full_name},
|
|
{ "@ip_address", post.Ip_address},
|
|
{ "@ip_port", post.Ip_port},
|
|
{ "@priority", current_priority + 1},
|
|
{ "@created_by", myUserInfo.Userinfo_guid}
|
|
};
|
|
await backendRepository.AddOneByCustomTable(building, "building");
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "新增成功";
|
|
}
|
|
else
|
|
{
|
|
apiResult.Code = "0001";
|
|
apiResult.Msg = "監控主機IP不可重複";
|
|
}
|
|
|
|
}
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 / 修改 區域基本資料
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> EditBuildInfo(BuildInfo post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
try
|
|
{
|
|
//判斷監控主機IP是否重複
|
|
var judgeIPAddressRepeat = true;
|
|
|
|
var sWhere = $@"deleted = 0 AND ip_address = @ip_address AND ip_port = @ip_port AND building_tag != @building_tag";
|
|
var buildInfos = await backendRepository.GetAllAsync<BuildInfo>("building", sWhere, new { ip_address = post.Ip_address, ip_port = post.Ip_port, building_tag = post.building_tag });
|
|
if (buildInfos.Count == 0)
|
|
{
|
|
judgeIPAddressRepeat = false;
|
|
}
|
|
|
|
if (!judgeIPAddressRepeat)
|
|
{
|
|
Dictionary<string, object> building = new Dictionary<string, object>();
|
|
building = new Dictionary<string, object>()
|
|
{
|
|
{ "@full_name", post.Full_name},
|
|
{ "@ip_address", post.Ip_address},
|
|
{ "@ip_port", post.Ip_port},
|
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
|
|
};
|
|
await backendRepository.UpdateOneByCustomTable(building, "building", "building_tag='" + post.building_tag + "'");
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "修改成功";
|
|
}
|
|
else
|
|
{
|
|
apiResult.Code = "0001";
|
|
apiResult.Msg = "監控主機IP不可重複";
|
|
}
|
|
|
|
}
|
|
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<ApiResult<string>> DeleteOneBuild(string tag)
|
|
{
|
|
var apiResult = new ApiResult<string>();
|
|
try
|
|
{
|
|
string sWhere = @$"deleted = @Deleted AND building_tag = @Tag";
|
|
|
|
object param = new { Deleted = 0, Tag = tag };
|
|
|
|
var buildInfo = await backendRepository.GetOneAsync<BuildInfo>("building", sWhere, param);
|
|
|
|
if (buildInfo == null)
|
|
{
|
|
apiResult.Code = "9998";
|
|
apiResult.Msg = "查無該區域資料";
|
|
return apiResult;
|
|
}
|
|
|
|
//檢查是否有未刪除的區域選單
|
|
var sbuildMenuWhere = $@"building_tag = @Tag";
|
|
var buildMenus = await backendRepository.GetAllAsync<BuildMenu>("building_menu", sbuildMenuWhere, new { Tag = tag });
|
|
|
|
if (buildMenus.Count > 0)
|
|
{
|
|
apiResult.Code = "9997";
|
|
apiResult.Msg = "區域選單中尚有選單正在使用該棟別,故無法刪除";
|
|
return apiResult;
|
|
}
|
|
|
|
//檢查底下是否有未刪除的樓層
|
|
var sfloorWhere = $@"deleted = 0 AND building_tag = @tag";
|
|
var floors = await backendRepository.GetAllAsync<BuildFloor>("floor", sfloorWhere, new { Tag = tag });
|
|
|
|
if (floors.Count > 0)
|
|
{
|
|
apiResult.Code = "9997";
|
|
apiResult.Msg = "樓層設定中尚有以下樓層正在使用該棟別,故無法刪除";
|
|
apiResult.Data = string.Join("<br>", floors.Select(x => x.Full_name).ToList());
|
|
return apiResult;
|
|
}
|
|
|
|
await backendRepository.DeleteOne(tag, "building", "building_tag");
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "刪除成功";
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "building_tag=" + tag);
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
return apiResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改棟別區域排列順序
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<List<string>>> ChangeBuildInfoPriority(PostBuildInfoPriority post)
|
|
{
|
|
ApiResult<List<string>> apiResult = new ApiResult<List<string>>();
|
|
try
|
|
{
|
|
List<Dictionary<string, object>> building_priorities = new List<Dictionary<string, object>>();
|
|
if (post.BuildInfoPriorities != null)
|
|
{
|
|
foreach (var building_priority in post.BuildInfoPriorities)
|
|
{
|
|
Dictionary<string, object> building_priority_dic = new Dictionary<string, object>();
|
|
building_priority_dic = new Dictionary<string, object>()
|
|
{
|
|
{ "building_tag", building_priority.Building_tag},
|
|
{ "@priority", building_priority.Priority},
|
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
|
|
};
|
|
|
|
building_priorities.Add(building_priority_dic);
|
|
}
|
|
|
|
var sql = $@"UPDATE building SET priority = @priority, updated_by = @updated_by, updated_at=@updated_at WHERE building_tag = @building_tag";
|
|
|
|
await backendRepository.ExecuteSql(sql, building_priorities);
|
|
}
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "修改成功";
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
return apiResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 樓層列表
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<List<BuildFloor>>> BuildFloorList(string build_tag)
|
|
{
|
|
ApiResult<List<BuildFloor>> apiResult = new ApiResult<List<BuildFloor>>();
|
|
List<BuildFloor> buildInfo = new List<BuildFloor>();
|
|
|
|
try
|
|
{
|
|
var sqlString = @$"SELECT A.floor_guid, A.full_name, InitMapName + '.svg' AS 'initMapName', A.priority, A.created_at
|
|
FROM floor A
|
|
WHERE deleted = @deleted
|
|
AND A.building_tag = @building_tag
|
|
ORDER BY A.priority ASC";
|
|
buildInfo = await backendRepository.GetAllAsync<BuildFloor>(sqlString, new { deleted = 0, building_tag = build_tag });
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Data = buildInfo;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
return apiResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得單一樓層設定
|
|
/// </summary>
|
|
/// <param name="guid"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<BuildFloor>> GetOneBuildFloor(string guid)
|
|
{
|
|
ApiResult<BuildFloor> apiResult = new ApiResult<BuildFloor>();
|
|
|
|
try
|
|
{
|
|
string sWhere = @$"deleted = @Deleted AND floor_guid = @Guid";
|
|
|
|
object param = new { Deleted = 0, Guid = guid };
|
|
|
|
var buildFloor = await backendRepository.GetOneAsync<BuildFloor>("floor", sWhere, param);
|
|
|
|
if (!string.IsNullOrEmpty(buildFloor.InitMapName))
|
|
{
|
|
buildFloor.MapUrl = "/upload/floor_map/" + buildFloor.Floor_guid + ".svg";
|
|
}
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Data = buildFloor;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
return apiResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 / 修改 樓層設定
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> SaveBuildFloor([FromForm] BuildFloor post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
try
|
|
{
|
|
string sWhere = @$"deleted = @Deleted AND floor_guid = @Guid";
|
|
|
|
object param = new { Deleted = 0, Guid = post.Floor_guid };
|
|
|
|
var buildFloor = await backendRepository.GetOneAsync<BuildFloor>("floor", sWhere, param);
|
|
|
|
if (buildFloor == null)
|
|
{
|
|
//新增
|
|
//產生一組GUID
|
|
var guid = Guid.NewGuid();
|
|
var floor_map_guid = Guid.NewGuid();
|
|
|
|
//抓取當前的Priority
|
|
var current_priority = await backendRepository.GetCurrentPriority("floor", "deleted = 0 AND building_tag = '" + post.Building_tag + "'");
|
|
|
|
Dictionary<string, object> floor = new Dictionary<string, object>();
|
|
floor = new Dictionary<string, object>()
|
|
{
|
|
{ "@floor_guid", guid},
|
|
{ "@building_tag", post.Building_tag},
|
|
{ "@full_name", post.Full_name},
|
|
{ "@InitMapName", post.InitMapName},
|
|
{ "@floor_map_name", floor_map_guid},
|
|
{ "@priority", current_priority + 1},
|
|
{ "@created_by", myUserInfo.Userinfo_guid}
|
|
};
|
|
await backendRepository.AddOneByCustomTable(floor, "floor");
|
|
|
|
if (post.MapFile != null)
|
|
{
|
|
var split = post.MapFile.FileName.Split(".");
|
|
var fileName = floor_map_guid + "." + split[split.Length - 1];
|
|
|
|
var fullPath = Path.Combine(mapFileSaveAsPath, fileName);
|
|
|
|
using (var stream = new FileStream(fullPath, FileMode.Create))
|
|
{
|
|
post.MapFile.CopyTo(stream);
|
|
}
|
|
}
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "新增成功";
|
|
}
|
|
else //修改
|
|
{
|
|
Dictionary<string, object> floor = new Dictionary<string, object>();
|
|
floor = new Dictionary<string, object>()
|
|
{
|
|
{ "@full_name", post.Full_name},
|
|
{ "@InitMapName", post.InitMapName},
|
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
|
|
};
|
|
await backendRepository.UpdateOneByCustomTable(floor, "floor", "floor_guid='" + post.Floor_guid + "'");
|
|
|
|
var floor_map_guid = Guid.NewGuid();
|
|
if (post.MapFile != null)
|
|
{
|
|
//刪除原本檔案
|
|
FolderFunction folderFunction = new FolderFunction();
|
|
folderFunction.DeleteFile(Path.Combine(mapFileSaveAsPath, buildFloor.Floor_map_name + ".svg"));
|
|
|
|
Dictionary<string, object> floor_map_dic = new Dictionary<string, object>();
|
|
floor_map_dic = new Dictionary<string, object>()
|
|
{
|
|
{ "@floor_map_name", floor_map_guid},
|
|
};
|
|
await backendRepository.UpdateOneByCustomTable(floor_map_dic, "floor", "floor_guid='" + post.Floor_guid + "'");
|
|
|
|
var split = post.MapFile.FileName.Split(".");
|
|
var fileName = floor_map_guid + "." + split[split.Length - 1];
|
|
|
|
var fullPath = Path.Combine(mapFileSaveAsPath, fileName);
|
|
|
|
using (var stream = new FileStream(fullPath, FileMode.Create))
|
|
{
|
|
post.MapFile.CopyTo(stream);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改樓層排列順序
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<List<string>>> ChangeFloorPriority(PostFloorPriority post)
|
|
{
|
|
ApiResult<List<string>> apiResult = new ApiResult<List<string>>();
|
|
try
|
|
{
|
|
List<Dictionary<string, object>> floor_priorities = new List<Dictionary<string, object>>();
|
|
if (post.FloorPriorities != null)
|
|
{
|
|
foreach (var floor_priority in post.FloorPriorities)
|
|
{
|
|
Dictionary<string, object> floor_priority_dic = new Dictionary<string, object>();
|
|
floor_priority_dic = new Dictionary<string, object>()
|
|
{
|
|
{ "@floor_guid", floor_priority.Floor_guid},
|
|
{ "@priority", floor_priority.Priority},
|
|
{ "@updated_by", myUserInfo.Userinfo_guid},
|
|
{ "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
|
|
};
|
|
|
|
floor_priorities.Add(floor_priority_dic);
|
|
}
|
|
|
|
var sql = $@"UPDATE floor SET priority = @priority, updated_by = @updated_by, updated_at=@updated_at WHERE floor_guid = @floor_guid";
|
|
|
|
await backendRepository.ExecuteSql(sql, floor_priorities);
|
|
}
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "修改成功";
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
return apiResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刪除單一樓層設定
|
|
/// </summary>
|
|
/// <param name="guid"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> DeleteOneFloor(string guid)
|
|
{
|
|
var apiResult = new ApiResult<string>();
|
|
try
|
|
{
|
|
string sWhere = @$"deleted = @Deleted AND floor_guid = @Guid";
|
|
|
|
object param = new { Deleted = 0, Guid = guid };
|
|
|
|
var buildFloor = await backendRepository.GetOneAsync<BuildFloor>("floor", sWhere, param);
|
|
|
|
if (buildFloor == null)
|
|
{
|
|
apiResult.Code = "9998";
|
|
apiResult.Msg = "查無該樓層設定";
|
|
return apiResult;
|
|
}
|
|
|
|
//判斷區域選單是否還有使用該樓層
|
|
var sub_system_where = $@"SELECT
|
|
CONCAT(b.full_name, ' - ', mv.system_key, ' - ', sv.system_key)
|
|
FROM (
|
|
SELECT
|
|
ssf.building_tag,
|
|
ssf.main_system_tag,
|
|
ssf.sub_system_tag
|
|
FROM sub_system_floor ssf
|
|
WHERE ssf.deleted = 0 AND ssf.floor_tag = @floor_tag
|
|
) ssf
|
|
LEFT JOIN building b ON ssf.building_tag = b.building_tag AND b.deleted = 0
|
|
LEFT JOIN variable mv ON ssf.main_system_tag = mv.system_value AND mv.system_type = @main_system_type AND mv.deleted = 0
|
|
LEFT JOIN variable sv ON ssf.sub_system_tag = sv.system_value AND sv.system_type = @sub_system_type AND sv.deleted = 0";
|
|
|
|
var sub_system_floors = await backendRepository.GetAllAsync<string>(sub_system_where, new { floor_tag = buildFloor.Full_name, main_system_type = main_system_type, sub_system_type = sub_system_type });
|
|
if (sub_system_floors.Count > 0)
|
|
{
|
|
apiResult.Code = "9997";
|
|
apiResult.Msg = "區域選單中尚有以下選單正在使用該樓層,故無法刪除";
|
|
apiResult.Data = string.Join("<br>", sub_system_floors);
|
|
return apiResult;
|
|
}
|
|
|
|
await backendRepository.DeleteOne(guid, "floor", "floor_guid");
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "刪除成功";
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "floor_guid=" + guid);
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
return apiResult;
|
|
}
|
|
}
|
|
}
|