From abf63105ef75f83c9748fb204503ecd76c16d719 Mon Sep 17 00:00:00 2001 From: dev02 Date: Thu, 20 Oct 2022 23:27:00 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E5=8F=B0]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8D=80=E5=9F=9F=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Controllers/BuildInfoController.cs | 229 +++++++++--------- Backend/Models/BuildModel.cs | 2 + Backend/Views/BuildInfo/Index.cshtml | 44 +++- Backend/Views/BuildInfo/_BuildInfo.cshtml | 4 + .../be95566e-860e-48cb-af54-9f98218663b9.svg | 5 + 5 files changed, 167 insertions(+), 117 deletions(-) create mode 100644 Backend/wwwroot/upload/floor_map/be95566e-860e-48cb-af54-9f98218663b9.svg diff --git a/Backend/Controllers/BuildInfoController.cs b/Backend/Controllers/BuildInfoController.cs index ac19472..f4e4265 100644 --- a/Backend/Controllers/BuildInfoController.cs +++ b/Backend/Controllers/BuildInfoController.cs @@ -19,6 +19,8 @@ namespace Backend.Controllers { private readonly IBackendRepository backendRepository; private string mapFileSaveAsPath = ""; + private string main_system_name = "device_system_category_layer2"; + private string sub_system_name = "device_system_category_layer1"; public BuildInfoController(IBackendRepository backendRepository) { @@ -44,7 +46,7 @@ namespace Backend.Controllers 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 + 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"; @@ -78,8 +80,8 @@ namespace Backend.Controllers //判斷監控主機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 }); + var sWhere = $@"deleted = 0 AND ip_address = @ip_address AND ip_port = @ip_port AND building_tag != @building_tag"; + var buildInfos = await backendRepository.GetAllAsync("building", sWhere, new { ip_address = post.Ip_address, ip_port = post.Ip_port, building_tag = post.building_tag }); if (buildInfos.Count == 0) { judgeIPAddressRepeat = false; @@ -87,46 +89,92 @@ namespace Backend.Controllers if (!judgeIPAddressRepeat) { + //Check for duplicate building tag + sWhere = $@"deleted = 0 AND building_tag = @Building_tag"; + buildInfos = await backendRepository.GetAllAsync("building", sWhere, new { building_tag = post.building_tag }); + + if (buildInfos.Count > 0) + { + apiResult.Code = "0002"; + apiResult.Msg = "區域代號不可重複"; + return apiResult; + } + //新增 - if (post.Building_guid == "0") + //抓取當前的Priority + var current_priority = await backendRepository.GetCurrentPriority("building"); + + Dictionary building = new Dictionary(); + building = new Dictionary() { - //產生一組GUID - var guid = Guid.NewGuid(); //大樓GUID + { "@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"); - //抓取當前的Priority - var current_priority = await backendRepository.GetCurrentPriority("building"); + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + else + { + apiResult.Code = "0001"; + apiResult.Msg = "監控主機IP不可重複"; + } - 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"); + } + 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); + } - apiResult.Code = "0000"; - apiResult.Msg = "新增成功"; - } - else //修改 + return apiResult; + } + + /// + /// 新增 / 修改 區域基本資料 + /// + /// + /// + [HttpPost] + public async Task> EditBuildInfo(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_tag != @building_tag"; + var buildInfos = await backendRepository.GetAllAsync("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 building = new Dictionary(); + building = new Dictionary() { - 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 + "'"); + { "@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 = "修改成功"; - } + apiResult.Code = "0000"; + apiResult.Msg = "修改成功"; } else { @@ -148,14 +196,14 @@ namespace Backend.Controllers } [HttpPost] - public async Task> DeleteOneBuild(string guid) + public async Task> DeleteOneBuild(string tag) { var apiResult = new ApiResult(); try { - string sWhere = @$"deleted = @Deleted AND building_guid = @Guid"; + string sWhere = @$"deleted = @Deleted AND building_tag = @Tag"; - object param = new { Deleted = 0, Guid = guid }; + object param = new { Deleted = 0, Tag = tag }; var buildInfo = await backendRepository.GetOneAsync("building", sWhere, param); @@ -167,8 +215,8 @@ namespace Backend.Controllers } //檢查是否有未刪除的區域選單 - var sbuildMenuWhere = $@"building_guid = @Guid"; - var buildMenus = await backendRepository.GetAllAsync("building_menu", sbuildMenuWhere, new { Guid = guid }); + var sbuildMenuWhere = $@"building_tag = @Tag"; + var buildMenus = await backendRepository.GetAllAsync("building_menu", sbuildMenuWhere, new { Tag = tag }); if (buildMenus.Count > 0) { @@ -178,8 +226,8 @@ namespace Backend.Controllers } //檢查底下是否有未刪除的樓層 - var sfloorWhere = $@"deleted = 0 AND building_guid = @Guid"; - var floors = await backendRepository.GetAllAsync("floor", sfloorWhere, new { Guid = guid }); + var sfloorWhere = $@"deleted = 0 AND building_tag = @tag"; + var floors = await backendRepository.GetAllAsync("floor", sfloorWhere, new { Tag = tag }); if (floors.Count > 0) { @@ -189,7 +237,7 @@ namespace Backend.Controllers return apiResult; } - await backendRepository.DeleteOne(guid, "building", "building_guid"); + await backendRepository.DeleteOne(tag, "building", "building_tag"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; @@ -199,7 +247,7 @@ namespace Backend.Controllers { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; - Logger.LogError("【" + controllerName + "/" + actionName + "】" + "building_guid=" + guid); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "building_tag=" + tag); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; @@ -223,7 +271,7 @@ namespace Backend.Controllers Dictionary building_priority_dic = new Dictionary(); building_priority_dic = new Dictionary() { - { "building_guid", building_priority.Building_guid}, + { "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")} @@ -232,14 +280,9 @@ namespace Backend.Controllers 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"; + 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); - - #region 新增至派送資料表 - await backendRepository.ManualInsertBackgroundServiceTask("", "", "building", "update_list", building_priorities); - #endregion - } apiResult.Code = "0000"; @@ -261,7 +304,7 @@ namespace Backend.Controllers /// /// [HttpPost] - public async Task>> BuildFloorList(string BuildGuid) + public async Task>> BuildFloorList(string build_tag) { ApiResult> apiResult = new ApiResult>(); List buildInfo = new List(); @@ -271,9 +314,9 @@ namespace Backend.Controllers 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 + AND A.building_tag = @building_tag ORDER BY A.priority ASC"; - buildInfo = await backendRepository.GetAllAsync(sqlString, new { deleted = 0, building_guid = BuildGuid }); + buildInfo = await backendRepository.GetAllAsync(sqlString, new { deleted = 0, building_tag = build_tag }); apiResult.Code = "0000"; apiResult.Data = buildInfo; @@ -348,13 +391,13 @@ namespace Backend.Controllers var floor_map_guid = Guid.NewGuid(); //抓取當前的Priority - var current_priority = await backendRepository.GetCurrentPriority("floor", "deleted = 0 AND building_guid = '" + post.Building_guid + "'"); + var current_priority = await backendRepository.GetCurrentPriority("floor", "deleted = 0 AND building_tag = '" + post.Building_tag + "'"); Dictionary floor = new Dictionary(); floor = new Dictionary() { { "@floor_guid", guid}, - { "@building_guid", post.Building_guid}, + { "@building_tag", post.Building_tag}, { "@full_name", post.Full_name}, { "@InitMapName", post.InitMapName}, { "@floor_map_name", floor_map_guid}, @@ -374,27 +417,7 @@ namespace Backend.Controllers { 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 = "新增成功"; @@ -436,26 +459,6 @@ namespace Backend.Controllers } } - #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 = "修改成功"; } @@ -502,10 +505,6 @@ namespace Backend.Controllers 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"; @@ -547,20 +546,20 @@ namespace Backend.Controllers //判斷區域選單是否還有使用該樓層 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"; + CONCAT(b.full_name, ' - ', v1.system_key, ' - ', v2.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 v1 ON ssf.main_system_tag = v1.system_value AND v1.system_type = @main_system_type AND v1.deleted = 0 + LEFT JOIN variable v2 ON ssf.sub_system_tag = v2.system_value AND v2.system_type = @sub_system_type AND v2.deleted = 0"; - var sub_system_floors = await backendRepository.GetAllAsync(sub_system_where, new { Guid = guid }); + var sub_system_floors = await backendRepository.GetAllAsync(sub_system_where, new { floor_tag = buildFloor.Full_name, main_system_type = main_system_name, sub_system_type = sub_system_name }); if (sub_system_floors.Count > 0) { apiResult.Code = "9997"; diff --git a/Backend/Models/BuildModel.cs b/Backend/Models/BuildModel.cs index 25e47bd..ab59ef2 100644 --- a/Backend/Models/BuildModel.cs +++ b/Backend/Models/BuildModel.cs @@ -23,6 +23,7 @@ namespace Backend.Models { public string Floor_guid { get; set; } //樓層GUID public string Building_guid { get; set; } //區域GUID + public string Building_tag { get; set; } //區域GUID public string Full_name { get; set; } //建築名稱 public string InitMapName { get; set; } //使用者命名平面圖檔檔名 public string MapUrl { get; set; } //使用者命名平面圖檔檔名 @@ -43,6 +44,7 @@ namespace Backend.Models public class BuildInfoPriority { public string Building_guid { get; set; } //區域GUID + public string Building_tag { get; set; } //區域TAG public int Priority { get; set; } } diff --git a/Backend/Views/BuildInfo/Index.cshtml b/Backend/Views/BuildInfo/Index.cshtml index 956fd36..9e49dfb 100644 --- a/Backend/Views/BuildInfo/Index.cshtml +++ b/Backend/Views/BuildInfo/Index.cshtml @@ -163,6 +163,9 @@ selected_build_guid = $(this).parents('tr').attr('data-guid'); $('#build-modal .modal-title').html("區域基本資料 - 修改"); + $('#build_name_tag').val(selected_build_guid); + $('#build_name_tag').prop('disabled', true); + $("#save-building-btn").attr("onClick", "EditBuild()"); $('#build_name_modal').val($(this).parents('tr')[0].children[1].innerText); $('#ip_address_modal').val($(this).parents('tr')[0].children[2].innerText); $('#ip_port_modal').val($(this).parents('tr')[0].children[3].innerText); @@ -187,7 +190,7 @@ if (result.value) { var url = "/BuildInfo/DeleteOneBuild/"; var send_data = { - guid: selected_build_guid + tag: selected_build_guid } $.post(url, send_data, function (rel) { if (rel.code == "9999") { @@ -218,7 +221,9 @@ function AddBuild() { selected_build_guid = "0"; BuildInfoValidate.resetForm(); + $("#save-building-btn").attr("onClick", "SaveBuild()"); $("#build-modal .modal-title").html("區域基本資料 - 新增"); + $('#build_name_tag').prop('disabled', false); $("#build-form").trigger("reset"); $("#build-modal").modal(); @@ -252,6 +257,41 @@ if ($("#build-form").valid()) { $("#save-building-btn").html('').attr("disabled", true); var url = "/BuildInfo/SaveBuildInfo"; + var send_data = { + building_tag: $('#build_name_tag').val(), + Full_name: $('#build_name_modal').val(), + Ip_address: $('#ip_address_modal').val(), + Ip_port: $('#ip_port_modal').val() + } + + $.post(url, send_data, function (rel) { + $("#save-building-btn").html('確定').attr("disabled", false); + if (rel.code != "0000") { + if (rel.code == "9999") { + toast_error(rel.msg); + } + else { + toast_warning(rel.msg); + } + return; + } + else { + toast_ok(rel.msg); + buildInfoTable.ajax.reload(null, false); + $('#build-modal').modal('hide'); + return; + } + }, 'json') + .fail(function (xhr, status, error) { + $("#save-building-btn").html('確定').attr("disabled", false); + }); + } + } + + function EditBuild() { + if ($("#build-form").valid()) { + $("#save-building-btn").html('').attr("disabled", true); + var url = "/BuildInfo/EditBuildInfo"; var send_data = { building_tag: selected_build_guid, Full_name: $('#build_name_modal').val(), @@ -342,7 +382,7 @@ "url": "/BuildInfo/BuildFloorList", "type": "POST", "data": function (d) { - d.BuildGuid = selected_build_guid_top; + d.build_tag = selected_build_guid_top; }, "dataSrc": function (rel) { if (rel.code == "9999") { diff --git a/Backend/Views/BuildInfo/_BuildInfo.cshtml b/Backend/Views/BuildInfo/_BuildInfo.cshtml index c0d2dc3..fe932af 100644 --- a/Backend/Views/BuildInfo/_BuildInfo.cshtml +++ b/Backend/Views/BuildInfo/_BuildInfo.cshtml @@ -40,6 +40,10 @@