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 EmergencyGroupingController : MybaseController { private readonly IBackendRepository backendRepository; public EmergencyGroupingController(IBackendRepository backendRepository) { this.backendRepository = backendRepository; } public IActionResult Index() { return View(); } /// /// 取得部門資訊 /// /// [HttpPost] public async Task>> Getdepartlist() { ApiResult> apiResult = new ApiResult>(); List Variable = new List(); try { var sqlString = @$"select system_value as Value, system_key as Name from variable a where a.system_type = 'department' and a.deleted = 0"; Variable = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = Variable; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 新增/編輯人員租別 /// /// 人員編組參數 /// [HttpPost] public async Task> SaveGrouping(SaveGrouping post) { ApiResult apiResult = new ApiResult(); try { if(post.id == 0) { var value = await backendRepository.GetOneAsync("select td.system_value from variable td where td.system_type = 'grouping' order by cast(td.system_value as SIGNED) desc"); var dictionary = new Dictionary() { {"@system_type", "grouping"}, {"@system_key",post.name}, {"@system_value", value + 1}, {"@system_remark","急救編組"}, {"@system_priority",post.priority}, {"@system_parent_id", post.disaster} }; await backendRepository.AddOneByCustomTable(dictionary, "variable"); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { var dictionary = new Dictionary() { {"@system_key",post.name}, {"@system_priority",post.priority} }; await backendRepository.UpdateOneByCustomTable(dictionary, "variable", $" id = {post.id}"); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } } catch(Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } /// /// 根據選擇的組別拿到人員清單 /// /// 組別id /// [HttpPost] public async Task Emergency_member_table(int grouping) { List Emergency_member_tables = new List(); ApiResult> apiResult = new ApiResult>(); try { Emergency_member_tables = await backendRepository.GetAllAsync($@" select v.system_key grouping_name,va.system_key department_name,em.* from emergency_member em left join variable v on em.grouping = v.id left join (select * from variable vs where vs.system_type = 'department' and vs.deleted = 0) va on va.system_value = em.department where em.grouping = {grouping} and em.deleted = 0"); apiResult.Code = "0000"; apiResult.Data = Emergency_member_tables; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } /// /// 根據災害類別id拿到對應人員組別 /// /// 災害類別id /// [HttpPost] public async Task>> GetGroupingList (int system_parent_id) { ApiResult> apiResult = new ApiResult>(); List keyvalue = new List(); try { keyvalue = await backendRepository.GetAllAsync($"select id, system_key as name,system_value as value from variable where deleted = 0 and system_parent_id = {system_parent_id} and system_type = 'grouping' order by system_priority"); apiResult.Data = keyvalue; apiResult.Code = "0000"; } catch(Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + system_parent_id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } /// /// 取得單一編組設定 /// /// 組別id /// [HttpPost] public async Task> GetOneGrouping(int selectgroupid) { ApiResult apiResult = new ApiResult(); try { var group = await backendRepository.GetOneAsync($"select v.system_priority as priority,v.system_key as name,id from variable v where id = {selectgroupid} and deleted = 0"); apiResult.Data = group; apiResult.Code = "0000"; } catch(Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + selectgroupid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } /// /// variable的軟刪除 /// /// variable_id /// [HttpPost] public async Task>DeleteOne(int selectgroupid) { ApiResult apiResult = new ApiResult(); try { await backendRepository.DeleteOne(selectgroupid.ToString(), "variable", "id"); apiResult.Msg = "刪除成功"; apiResult.Code = "0000"; } catch (Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + selectgroupid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } /// /// 新增/編輯人員資訊 /// /// 人員資訊參數 /// [HttpPost] public async Task>SaveMember(Emergency_member post) { ApiResult apiResult = new ApiResult(); try { if (post.emergency_member_guid == null) { var dictionary = new Dictionary() { {"@emergency_member_guid", Guid.NewGuid()}, {"@grouping",post.grouping}, {"@full_name", post.full_name}, {"@department",post.department}, {"@phone",post.phone}, {"@lineid", post.lineid}, {"@email", post.email}, {"@created_by",myUserInfo.Userinfo_guid } }; await backendRepository.AddOneByCustomTable(dictionary, "emergency_member"); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { var dictionary = new Dictionary() { {"@full_name", post.full_name}, {"@department",post.department}, {"@phone",post.phone}, {"@lineid", post.lineid}, {"@email", post.email}, {"@updated_by",myUserInfo.Userinfo_guid }, {"@updated_at",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") } }; await backendRepository.UpdateOneByCustomTable(dictionary, "emergency_member", $"emergency_member_guid = '{post.emergency_member_guid}'"); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } } catch (Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } /// /// 取得單一人員資訊 /// /// 人員guid /// [HttpPost] public async Task> GetOneMember(string guid) { ApiResult apiResult = new ApiResult(); try { var member = await backendRepository.GetOneAsync("emergency_member", $"emergency_member_guid = '{guid}'"); apiResult.Data = member; apiResult.Code = "0000"; } catch (Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + guid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } /// /// 軟刪除單一人員 /// /// 人員guid /// [HttpPost] public async Task> DeletedOneMember(string guid) { ApiResult apiResult = new ApiResult(); try { await backendRepository.DeleteOne(guid, "emergency_member", "emergency_member_guid"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception ex) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + guid); Logger.LogError("【" + controllerName + "/" + actionName + "】" + ex.Message); } return apiResult; } } }