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 { 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(); } /// /// 區域基本資料列表 /// /// [HttpPost] public async Task>> BuildInfoList() { ApiResult> apiResult = new ApiResult>(); List buildInfo = new List(); 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(sqlString); apiResult.Code = "0000"; apiResult.Data = buildInfo; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 新增 / 修改 區域基本資料 /// /// /// [HttpPost] public async Task> SaveBuildInfo(BuildInfo post) { ApiResult apiResult = new ApiResult(); 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("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 building = new Dictionary(); building = new Dictionary() { { "@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 building = new Dictionary(); building = new Dictionary() { { "@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> DeleteOneBuild(string guid) { var apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND building_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var buildInfo = await backendRepository.GetOneAsync("building", sWhere, param); if (buildInfo == null) { apiResult.Code = "9998"; apiResult.Msg = "查無該區域資料"; return apiResult; } //檢查是否有未刪除的區域選單 var sbuildMenuWhere = $@"building_guid = @Guid"; var buildMenus = await backendRepository.GetAllAsync("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("floor", sfloorWhere, new { Guid = guid }); if (floors.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "樓層設定中尚有以下樓層正在使用該棟別,故無法刪除"; apiResult.Data = string.Join("
", 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; } /// /// 修改棟別區域排列順序 /// /// [HttpPost] public async Task>> ChangeBuildInfoPriority(PostBuildInfoPriority post) { ApiResult> apiResult = new ApiResult>(); try { List> building_priorities = new List>(); if (post.BuildInfoPriorities != null) { foreach (var building_priority in post.BuildInfoPriorities) { Dictionary building_priority_dic = new Dictionary(); building_priority_dic = new Dictionary() { { "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; } /// /// 樓層列表 /// /// /// [HttpPost] public async Task>> BuildFloorList(string BuildGuid) { ApiResult> apiResult = new ApiResult>(); List buildInfo = new List(); 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(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; } /// /// 取得單一樓層設定 /// /// /// [HttpPost] public async Task> GetOneBuildFloor(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND floor_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var buildFloor = await backendRepository.GetOneAsync("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; } /// /// 新增 / 修改 樓層設定 /// /// /// [HttpPost] public async Task> SaveBuildFloor([FromForm] BuildFloor post) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND floor_guid = @Guid"; object param = new { Deleted = 0, Guid = post.Floor_guid }; var buildFloor = await backendRepository.GetOneAsync("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 floor = new Dictionary(); floor = new Dictionary() { { "@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 fileInfos = new List(); 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 floor = new Dictionary(); floor = new Dictionary() { { "@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 floor_map_dic = new Dictionary(); floor_map_dic = new Dictionary() { { "@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 fileInfos = new List(); 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; } /// /// 修改樓層排列順序 /// /// [HttpPost] public async Task>> ChangeFloorPriority(PostFloorPriority post) { ApiResult> apiResult = new ApiResult>(); try { List> floor_priorities = new List>(); if (post.FloorPriorities != null) { foreach (var floor_priority in post.FloorPriorities) { Dictionary floor_priority_dic = new Dictionary(); floor_priority_dic = new Dictionary() { { "@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; } /// /// 刪除單一樓層設定 /// /// /// [HttpPost] public async Task> DeleteOneFloor(string guid) { var apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND floor_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var buildFloor = await backendRepository.GetOneAsync("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(sub_system_where, new { Guid = guid }); if (sub_system_floors.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "區域選單中尚有以下選單正在使用該樓層,故無法刪除"; apiResult.Data = string.Join("
", 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; } } }