ibms-dome/Backend/Controllers/EmergencyGroupingController.cs

313 lines
13 KiB
C#

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<EmergencyGroupingController>
{
private readonly IBackendRepository backendRepository;
public EmergencyGroupingController(IBackendRepository backendRepository)
{
this.backendRepository = backendRepository;
}
public IActionResult Index()
{
return View();
}
/// <summary>
/// 取得部門資訊
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<KeyValue>>> Getdepartlist()
{
ApiResult<List<KeyValue>> apiResult = new ApiResult<List<KeyValue>>();
List<KeyValue> Variable = new List<KeyValue>();
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<KeyValue>(sqlString);
apiResult.Code = "0000";
apiResult.Data = Variable;
}
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>> SaveGrouping(SaveGrouping post)
{
ApiResult<string> apiResult = new ApiResult<string>();
try
{
if(post.id == 0)
{
var value = await backendRepository.GetOneAsync<int>("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<string, object>()
{
{"@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<string, object>()
{
{"@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;
}
/// <summary>
/// 根據選擇的組別拿到人員清單
/// </summary>
/// <param name="grouping">組別id</param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult> Emergency_member_table(int grouping)
{
List<Emergency_member_table> Emergency_member_tables = new List<Emergency_member_table>();
ApiResult<List<Emergency_member_table>> apiResult = new ApiResult<List<Emergency_member_table>>();
try
{
Emergency_member_tables = await backendRepository.GetAllAsync<Emergency_member_table>($@"
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;
}
/// <summary>
/// 根據災害類別id拿到對應人員組別
/// </summary>
/// <param name="system_parent_id">災害類別id</param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<KeyValueID>>> GetGroupingList (int system_parent_id)
{
ApiResult<List<KeyValueID>> apiResult = new ApiResult<List<KeyValueID>>();
List<KeyValueID> keyvalue = new List<KeyValueID>();
try
{
keyvalue = await backendRepository.GetAllAsync<KeyValueID>($"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;
}
/// <summary>
/// 取得單一編組設定
/// </summary>
/// <param name="selectgroupid">組別id</param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<SaveGrouping>> GetOneGrouping(int selectgroupid)
{
ApiResult<SaveGrouping> apiResult = new ApiResult<SaveGrouping>();
try
{
var group = await backendRepository.GetOneAsync<SaveGrouping>($"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;
}
/// <summary>
/// variable的軟刪除
/// </summary>
/// <param name="selectgroupid">variable_id</param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<string>>DeleteOne(int selectgroupid)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 新增/編輯人員資訊
/// </summary>
/// <param name="post">人員資訊參數</param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<string>>SaveMember(Emergency_member post)
{
ApiResult<string> apiResult = new ApiResult<string>();
try
{
if (post.emergency_member_guid == null)
{
var dictionary = new Dictionary<string, object>()
{
{"@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<string, object>()
{
{"@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;
}
/// <summary>
/// 取得單一人員資訊
/// </summary>
/// <param name="guid">人員guid</param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<Emergency_member>> GetOneMember(string guid)
{
ApiResult<Emergency_member> apiResult = new ApiResult<Emergency_member>();
try
{
var member = await backendRepository.GetOneAsync<Emergency_member>("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;
}
/// <summary>
/// 軟刪除單一人員
/// </summary>
/// <param name="guid">人員guid</param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<string>> DeletedOneMember(string guid)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
}
}