using Backend.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Repository.BackendRepository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Backend.Controllers { public class SystemCategoryController : MybaseController { private readonly IBackendRepository backendRepository; public SystemCategoryController(IBackendRepository backendRepository) { this.backendRepository = backendRepository; } public IActionResult Index() { return View(); } /// /// 取得系統大類清單 /// /// [HttpPost] public async Task>> SystemMainList() { ApiResult> apiResult = new ApiResult>(); try { var sWhere = "deleted = 0"; var systemMainList = await backendRepository.GetAllAsync("main_system", sWhere, null, "priority ASC, created_at DESC"); apiResult.Code = "0000"; apiResult.Data = systemMainList; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 取得單一系統大類 /// /// /// [HttpPost] public async Task> GetOneSystemMain(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND main_system_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var systemMain = await backendRepository.GetOneAsync("main_system", sWhere, param); apiResult.Code = "0000"; apiResult.Data = systemMain; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 新增 / 修改 系統大類資料 /// /// /// [HttpPost] public async Task> SaveSystemMain(SystemMain post) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND main_system_guid = @Guid"; object param = new { Deleted = 0, Guid = post.Main_system_guid }; var systemMain = await backendRepository.GetOneAsync("main_system", sWhere, param); if (systemMain == null) { //新增 //產生一組GUID var guid = Guid.NewGuid(); //系統大類GUID Dictionary systemMainDic = new Dictionary() { { "@main_system_guid", guid}, { "@full_name", post.Full_name}, { "@code", post.Code}, { "@created_by", myUserInfo.Userinfo_guid} }; await backendRepository.AddOneByCustomTable(systemMainDic, "main_system"); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { Dictionary systemMainDic = new Dictionary() { { "@full_name", post.Full_name}, { "@code", post.Code}, { "@updated_by", myUserInfo.Userinfo_guid}, { "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, }; await backendRepository.UpdateOneByCustomTable(systemMainDic, "main_system", "main_system_guid='" + systemMain.Main_system_guid + "'"); var AuthCodes = await backendRepository.GetAllAsync( @$"select AuthCode from auth_page ap join sub_system ss on ss.sub_system_guid = ap.ShowView join main_system ms on ms.main_system_guid = ss.main_system_guid where ms.main_system_guid = '{systemMain.Main_system_guid}'"); if(AuthCodes.Count > 0) { await backendRepository.ExecuteSql($@"UPDATE auth_page SET MainName = '{post.Full_name}' WHERE AuthCode IN @authCode;",new { authCode = AuthCodes }); } #region 新增至派送資料表 var auth_Pages = await backendRepository.GetAllAsync("auth_page", ""); List> authPagesDics = new List>(); foreach (var auth_page in auth_Pages) { Dictionary authPagesDic = new Dictionary() { { "@AuthCode", auth_page.AuthCode}, { "@AuthType", auth_page.AuthType}, { "@MainName", auth_page.MainName}, { "@SubName", auth_page.SubName}, { "@building_guid", auth_page.building_guid}, { "@ShowView", auth_page.ShowView}, { "@created_at", auth_page.created_at}, }; authPagesDics.Add(authPagesDic); } await backendRepository.ManualInsertBackgroundServiceTask("", "", "auth_page", "purge_all_insert", authPagesDics); #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> DeleteOneSystemMain(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND main_system_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var systemMain = await backendRepository.GetOneAsync("main_system", sWhere, param); if (systemMain == null) { apiResult.Code = "9998"; apiResult.Msg = "查無該系統大類"; return apiResult; } //檢查是否有未刪除的區域選單 var sbuildMenu = $@"SELECT b.full_name FROM building_menu bm LEFT JOIN building b ON bm.building_guid = b.building_guid AND b.deleted = 0 WHERE bm.main_system_guid = @Guid GROUP BY b.full_name"; var buildMenus = await backendRepository.GetAllAsync(sbuildMenu, new { Guid = guid }); if (buildMenus.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "區域選單中尚有棟別正在使用該系統大類,故無法刪除"; apiResult.Data = string.Join("
", buildMenus); return apiResult; } //檢查底下是否有未刪除的系統小類 string sSubWhere = @$"deleted = @Deleted AND main_system_guid = @Guid"; object sub_param = new { Deleted = 0, Guid = systemMain.Main_system_guid }; var systemSubs = await backendRepository.GetAllAsync("sub_system", sWhere, param); if (systemSubs.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "系統小類中尚有小類正在使用系統大類,故無法刪除"; apiResult.Data = string.Join("
", systemSubs.Select(x => x.Full_name).ToList()); return apiResult; } await backendRepository.DeleteOne(guid, "main_system", "main_system_guid"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "main_system_guid=" + guid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 取得系統小類清單 /// /// /// [HttpPost] public async Task>> SystemSubList(string main_system_guid) { ApiResult> apiResult = new ApiResult>(); try { var sWhere = @"deleted = @Deleted AND main_system_guid = @Main_system_guid"; object param = new { Deleted = 0, Main_system_guid = main_system_guid }; var systemSubs = await backendRepository.GetAllAsync("sub_system", sWhere, param, "priority ASC, created_at DESC"); apiResult.Code = "0000"; apiResult.Data = systemSubs; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 取得單一系統小類 /// /// /// [HttpPost] public async Task> GetOneSystemSub(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND sub_system_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var systemSub = await backendRepository.GetOneAsync("sub_system", sWhere, param); apiResult.Code = "0000"; apiResult.Data = systemSub; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 新增 / 修改 系統小類資料 /// /// /// [HttpPost] public async Task> SaveSystemSub(SystemSub post) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND sub_system_guid = @Guid"; object param = new { Deleted = 0, Guid = post.Sub_system_guid }; var systemSub = await backendRepository.GetOneAsync("sub_system", sWhere, param); if (systemSub == null) { //新增 //產生一組GUID var guid = Guid.NewGuid(); //GUID Dictionary systemSubDic = new Dictionary() { { "@sub_system_guid", guid}, { "@main_system_guid", post.Main_system_guid}, { "@full_name", post.Full_name}, { "@created_by", myUserInfo.Userinfo_guid} }; await backendRepository.AddOneByCustomTable(systemSubDic, "sub_system"); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { Dictionary systemSubDic = new Dictionary() { { "@full_name", post.Full_name}, { "@updated_by", myUserInfo.Userinfo_guid}, { "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, }; await backendRepository.UpdateOneByCustomTable(systemSubDic, "sub_system", "sub_system_guid='" + systemSub.Sub_system_guid + "'"); var AuthCodes = await backendRepository.GetAllAsync( @$"select AuthCode from auth_page ap where ap.ShowView = '{systemSub.Sub_system_guid}'"); if (AuthCodes.Count > 0) { await backendRepository.ExecuteSql($@"UPDATE auth_page SET SubName = '{post.Full_name}' WHERE AuthCode IN @authCode;", new { authCode = AuthCodes }); } #region 新增至派送資料表 var auth_Pages = await backendRepository.GetAllAsync("auth_page", ""); List> authPagesDics = new List>(); foreach (var auth_page in auth_Pages) { Dictionary authPagesDic = new Dictionary() { { "@AuthCode", auth_page.AuthCode}, { "@AuthType", auth_page.AuthType}, { "@MainName", auth_page.MainName}, { "@SubName", auth_page.SubName}, { "@building_guid", auth_page.building_guid}, { "@ShowView", auth_page.ShowView}, { "@created_at", auth_page.created_at}, }; authPagesDics.Add(authPagesDic); } await backendRepository.ManualInsertBackgroundServiceTask("", "", "auth_page", "purge_all_insert", authPagesDics); #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> DeleteOneSystemSub(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND sub_system_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var systemSub = await backendRepository.GetOneAsync("sub_system", sWhere, param); if (systemSub == null) { apiResult.Code = "9998"; apiResult.Msg = "查無該系統小類"; return apiResult; } //檢查是否有未刪除的區域選單 var sbuildMenu = $@"SELECT CONCAT(b.full_name, ' - ', ms.full_name) FROM building_menu bm LEFT JOIN building b ON bm.building_guid = b.building_guid AND b.deleted = 0 LEFT JOIN main_system ms ON bm.main_system_guid = ms.main_system_guid AND ms.deleted = 0 WHERE bm.sub_system_guid = @Guid"; var buildMenus = await backendRepository.GetAllAsync(sbuildMenu, new { Guid = guid }); if (buildMenus.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "區域選單中尚有選單正在使用該系統小類,故無法刪除"; apiResult.Data = string.Join("
", buildMenus); return apiResult; } //檢查是否有未刪除的系統小類樓層 var ssubSystemFloor = $@"SELECT CONCAT(b.full_name, ' - ', ms.full_name, ' - ', ss.full_name, ' - ', f.full_name) FROM sub_system_floor 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 LEFT JOIN floor f ON ssf.floor_guid = f.floor_guid AND f.deleted = 0 WHERE ssf.sub_system_guid = @Guid AND ssf.deleted = 0"; var subSystemFloor = await backendRepository.GetAllAsync(sbuildMenu, new { Guid = guid }); if (subSystemFloor.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "區域選單中尚有樓層正在使用該系統小類,故無法刪除"; apiResult.Data = string.Join("
", subSystemFloor); return apiResult; } //檢查是否有未刪除的設備項目 var sdeviceItem = $@"SELECT di.full_name FROM device_item di WHERE di.deleted = 0 AND di.sub_system_guid = @Guid"; var deviceItems = await backendRepository.GetAllAsync(sdeviceItem, new { Guid = guid }); if (deviceItems.Count > 0) { apiResult.Code = "9997"; apiResult.Msg = "設備項目中尚有項目正在使用該系統小類,故無法刪除"; apiResult.Data = string.Join("
", deviceItems); return apiResult; } await backendRepository.DeleteOne(guid, "sub_system", "sub_system_guid"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "sub_system_guid=" + guid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> Savedevice_item(Device_item device_Item) { ApiResult apiResult = new ApiResult(); try { //檢查是否有未刪除的區域選單 if(device_Item.is_show_riserDiagram == 1) { var sql_show_riserDiagram = $@"SELECT * FROM device_item di WHERE di.sub_system_guid = @SubSystemGuid AND di.deleted = 0 AND is_show_riserDiagram = 1"; var is_show_riserDiagram = await backendRepository.GetAllAsync(sql_show_riserDiagram, new { SubSystemGuid = device_Item.sub_system_guid }); if (is_show_riserDiagram.Count() > 0) { apiResult.Code = "9998"; apiResult.Msg = "請先取消已選擇顯示於昇位圖點位。"; return apiResult; } } if (device_Item.device_item_guid == null) { //新增 //產生一組GUID var guid = Guid.NewGuid(); //GUID Dictionary Device_itemDic = new Dictionary() { { "@device_item_guid", guid}, { "@sub_system_guid", device_Item.sub_system_guid}, { "@full_name", device_Item.full_name}, { "@points", device_Item.points}, { "@unit", device_Item.unit}, { "@is_show", device_Item.is_show}, { "@is_show_riserDiagram", device_Item.is_show_riserDiagram}, { "@is_controll", device_Item.is_controll}, { "@is_bool", device_Item.is_bool}, { "@created_by", myUserInfo.Userinfo_guid}, }; await backendRepository.AddOneByCustomTable(Device_itemDic, "device_item"); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { Dictionary Device_itemDic = new Dictionary() { { "@sub_system_guid", device_Item.sub_system_guid}, { "@full_name", device_Item.full_name}, { "@points", device_Item.points}, { "@unit", device_Item.unit}, { "@is_show", device_Item.is_show}, { "@is_show_riserDiagram", device_Item.is_show_riserDiagram}, { "@is_controll", device_Item.is_controll}, { "@is_bool", device_Item.is_bool}, { "@updated_by", myUserInfo.Userinfo_guid}, { "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, }; await backendRepository.UpdateOneByCustomTable(Device_itemDic, "device_item", "device_item_guid='" + device_Item.device_item_guid + "'"); apiResult.Code = "0000"; apiResult.Msg = "修改成功"; } } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(device_Item); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task>> DeviceItemTable(string sub_system_guid) { ApiResult> apiResult = new ApiResult>(); try { var sWhere = @"deleted = @Deleted AND sub_system_guid = @Sub_system_guid"; object param = new { Deleted = 0, Sub_system_guid = sub_system_guid }; var systemSubs = await backendRepository.GetAllAsync("device_item", sWhere, param, "created_at DESC"); apiResult.Code = "0000"; apiResult.Data = systemSubs; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> GetOneDeviceItem(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND device_item_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var Deviceitem = await backendRepository.GetOneAsync("device_item", sWhere, param); apiResult.Code = "0000"; apiResult.Data = Deviceitem; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> DeleteOneSystemSubDeviceItem(string guid) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = @Deleted AND device_item_guid = @Guid"; object param = new { Deleted = 0, Guid = guid }; var device_Item = await backendRepository.GetOneAsync("device_item", sWhere, param); if (device_Item == null) { apiResult.Code = "9998"; apiResult.Msg = "查無該設備項目"; return apiResult; } await backendRepository.DeleteOne(guid, "device_item", "device_item_guid"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "device_item_guid=" + guid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } public async Task> HaveSamePoints(Checksame post) { ApiResult apiResult = new ApiResult(); try { var point = await backendRepository.GetOneAsync("device_item", $" sub_system_guid = '{post.sub_system_guid}' and points = '{post.points}' and device_item_guid != '{post.device_item_guid}' and deleted = 0"); if (point != null) { apiResult.Data = true; } else { apiResult.Data = false; } apiResult.Code = "0000"; } 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; } public async Task> CheckCanDelete(guidandsubguid post) { ApiResult apiResult = new ApiResult(); try { var tags = await backendRepository.GetAllAsync( @$"select * from (select dk.device_building_tag ,dk.device_name_tag,dk.device_system_tag from device_kind dk where dk.device_normal_point_guid = '{post.guid}') dkn union(select dk.device_building_tag, dk.device_name_tag, dk.device_system_tag from device_kind dk where dk.device_close_point_guid = '{post.guid}') union(select dk.device_building_tag, dk.device_name_tag, dk.device_system_tag from device_kind dk where dk.device_error_point_guid = '{post.guid}')"); if (tags.Count == 0) { Deletebool deletebool = new Deletebool() { Delete = false, Reason = "" }; apiResult.Data = deletebool; } else { Deletebool deletebool = new Deletebool() { Delete = true, Reason = "" }; var unionsql = ""; var last = tags.Last(); foreach (var tag in tags) { unionsql += $@"select d.building_guid,d.main_system_guid,d.sub_system_guid,d.device_name_tag from device d where d.sub_system_guid = '{post.subguid}' and d.device_building_tag = '{tag.device_building_tag}' and d.device_system_tag = '{tag.device_system_tag}' and d.device_name_tag = '{tag.device_name_tag}' group by d.building_guid,d.main_system_guid,d.sub_system_guid,d.device_name_tag"; if (!last.Equals(tag)) { unionsql += " union "; } } var sql = @$"select ms.full_name msname,b.full_name bname,s.full_name subname,de.device_name_tag from ({unionsql}) de left join main_system ms on ms.main_system_guid = de.main_system_guid left join building b on b.building_guid = de.building_guid left join sub_system s on s.sub_system_guid = de.sub_system_guid"; var names = await backendRepository.GetAllAsync(sql); var count = 0; foreach (var name in names) { count++; deletebool.Reason += count.ToString() + "." + name.bname + "-" + name.msname + "-" + name.subname + "-" + name.device_name_tag + "
"; } apiResult.Data = deletebool; } apiResult.Code = "0000"; } 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; } public async Task> CheckCanSubDelete(string guid) { ApiResult apiResult = new ApiResult(); try { var text = ""; var item = await backendRepository.GetAllAsync(@$"select device_item_guid from device_item where deleted = 0 and sub_system_guid = '{guid}'"); if(item.Count > 0) { text += "項目還有尚未刪除
"; } var menu = await backendRepository.GetAllAsync($"select sub_system_guid from building_menu where sub_system_guid = '{guid}'"); if (menu.Count > 0) { text += "區域選單還有尚未刪除
"; } Deletebool deletebool = new Deletebool() { Delete = false, Reason = "" }; if (text != "") { deletebool.Delete = true; deletebool.Reason = text; } else { deletebool.Delete = false; } apiResult.Data = deletebool; apiResult.Code = "0000"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + guid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }