ibms-dome/Backend/Controllers/BuildInfoController.cs

589 lines
25 KiB
C#
Raw Normal View History

2022-10-14 16:08:54 +08:00
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_guid, A.full_name, A.ip_address, A.ip_port, (SELECT COUNT(*) FROM floor f WHERE f.deleted = 0 AND f.building_guid = A.building_guid) 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_guid != @building_guid";
var buildInfos = await backendRepository.GetAllAsync<BuildInfo>("building", sWhere, new { ip_address = post.Ip_address, ip_port = post.Ip_port, building_guid = post.Building_guid });
if (buildInfos.Count == 0)
{
judgeIPAddressRepeat = false;
}
if (!judgeIPAddressRepeat)
{
//新增
if (post.Building_guid == "0")
{
//產生一組GUID
var guid = Guid.NewGuid(); //大樓GUID
//抓取當前的Priority
var current_priority = await backendRepository.GetCurrentPriority("building");
Dictionary<string, object> building = new Dictionary<string, object>();
building = new Dictionary<string, object>()
{
{ "@building_guid", guid},
{ "@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 //修改
{
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_guid='" + post.Building_guid + "'");
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 guid)
{
var apiResult = new ApiResult<string>();
try
{
string sWhere = @$"deleted = @Deleted AND building_guid = @Guid";
object param = new { Deleted = 0, Guid = guid };
var buildInfo = await backendRepository.GetOneAsync<BuildInfo>("building", sWhere, param);
if (buildInfo == null)
{
apiResult.Code = "9998";
apiResult.Msg = "查無該區域資料";
return apiResult;
}
//檢查是否有未刪除的區域選單
var sbuildMenuWhere = $@"building_guid = @Guid";
var buildMenus = await backendRepository.GetAllAsync<BuildMenu>("building_menu", sbuildMenuWhere, new { Guid = guid });
if (buildMenus.Count > 0)
{
apiResult.Code = "9997";
apiResult.Msg = "區域選單中尚有選單正在使用該棟別,故無法刪除";
return apiResult;
}
//檢查底下是否有未刪除的樓層
var sfloorWhere = $@"deleted = 0 AND building_guid = @Guid";
var floors = await backendRepository.GetAllAsync<BuildFloor>("floor", sfloorWhere, new { Guid = guid });
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(guid, "building", "building_guid");
apiResult.Code = "0000";
apiResult.Msg = "刪除成功";
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "building_guid=" + guid);
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_guid", building_priority.Building_guid},
{ "@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_guid = @building_guid";
await backendRepository.ExecuteSql(sql, building_priorities);
#region
await backendRepository.ManualInsertBackgroundServiceTask("", "", "building", "update_list", building_priorities);
#endregion
}
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 BuildGuid)
{
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_guid = @building_guid
ORDER BY A.priority ASC";
buildInfo = await backendRepository.GetAllAsync<BuildFloor>(sqlString, new { deleted = 0, building_guid = BuildGuid });
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_guid = '" + post.Building_guid + "'");
Dictionary<string, object> floor = new Dictionary<string, object>();
floor = new Dictionary<string, object>()
{
{ "@floor_guid", guid},
{ "@building_guid", post.Building_guid},
{ "@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);
}
}
#region
List<Repository.Models.FileInfo> fileInfos = new List<Repository.Models.FileInfo>();
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);
Repository.Models.FileInfo fileInfo = new Repository.Models.FileInfo();
fileInfo.Folder = "floor_map";
fileInfo.OriginalFileName = null;
fileInfo.FileName = fileName;
fileInfo.File = fullPath;
fileInfos.Add(fileInfo);
await backendRepository.ManualInsertFileBackgroundServiceTask("", post.Building_guid, "floor", fileInfos);
}
#endregion
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);
}
}
#region
List<Repository.Models.FileInfo> fileInfos = new List<Repository.Models.FileInfo>();
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);
Repository.Models.FileInfo fileInfo = new Repository.Models.FileInfo();
fileInfo.Folder = "floor_map";
fileInfo.OriginalFileName = buildFloor.Floor_map_name + ".svg";
fileInfo.FileName = fileName;
fileInfo.File = fullPath;
fileInfos.Add(fileInfo);
}
await backendRepository.ManualInsertFileBackgroundServiceTask("", post.Building_guid, "floor", fileInfos);
#endregion
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);
#region
await backendRepository.ManualInsertBackgroundServiceTask("", "", "floor", "update_list", floor_priorities);
#endregion
}
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, ' - ', ms.full_name, ' - ', ss.full_name)
FROM (
SELECT
ssf.building_guid,
ssf.main_system_guid,
ssf.sub_system_guid
FROM sub_system_floor ssf
WHERE ssf.deleted = 0 AND floor_guid = @Guid
) ssf
LEFT JOIN building b ON ssf.building_guid = b.building_guid AND b.deleted = 0
LEFT JOIN main_system ms ON ssf.main_system_guid = ms.main_system_guid AND ms.deleted = 0
LEFT JOIN sub_system ss ON ssf.sub_system_guid = ss.sub_system_guid AND ss.deleted = 0";
var sub_system_floors = await backendRepository.GetAllAsync<string>(sub_system_where, new { Guid = guid });
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;
}
}
}