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 VariableController : MybaseController { private readonly IBackendRepository backendRepository; public VariableController(IBackendRepository backendRepository) { this.backendRepository = backendRepository; } public IActionResult Index() { return View(); } /// /// SystemType列表 /// /// [HttpPost] public async Task>> SystemTypeList() { ApiResult> apiResult = new ApiResult>(); try { var sqlString = @$"SELECT DISTINCT system_type as Name, system_type as Value FROM variable v WHERE v.deleted = 0 ORDER BY v.system_type"; var KeyValue = await backendRepository.GetAllAsync(sqlString); apiResult.Code = "0000"; apiResult.Data = KeyValue; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 系統變數資料列表 /// /// [HttpPost] public async Task>> VariableInfoList(PostVariableInfoFilter post) { ApiResult> apiResult = new ApiResult>(); try { var sWhere = "deleted = 0"; if (!string.IsNullOrEmpty(post.SelectedSystemType)) { sWhere += " AND system_type = @SystemType"; } var variableInfos = await backendRepository.GetAllAsync("variable", sWhere, new { SystemType = post.SelectedSystemType }, "system_type, system_priority"); apiResult.Code = "0000"; apiResult.Data = variableInfos; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 系統變數資料列表 /// /// [HttpPost] public async Task> GetOneVariable(int id) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = 0 AND id = @Id"; object param = new { Id = id }; var variableInfo = await backendRepository.GetOneAsync("variable", sWhere, param); apiResult.Code = "0000"; apiResult.Data = variableInfo; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 新增 / 修改 系統變數資料 /// /// /// [HttpPost] public async Task> SaveVariable(VariableInfo post) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = 0 AND id = @Id"; object param = new { Id = post.id }; var variableInfo = await backendRepository.GetOneAsync("variable", sWhere, param); if (variableInfo == null) { //新增 Dictionary variableInfoDic = new Dictionary() { { "@system_type", post.System_type}, { "@system_key", post.System_key}, { "@system_value", post.system_value}, { "@system_remark", post.system_remark}, { "@system_priority", post.system_priority}, { "@system_parent_id", post.system_parent_id}, { "@created_by", myUserInfo.Userinfo_guid}, }; await backendRepository.AddOneByCustomTable(variableInfoDic, "variable"); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { Dictionary variableInfoDic = new Dictionary() { { "@system_type", post.System_type}, { "@system_key", post.System_key}, { "@system_value", post.system_value}, { "@system_remark", post.system_remark}, { "@system_priority", post.system_priority}, { "@system_parent_id", post.system_parent_id}, { "@updated_by", myUserInfo.Userinfo_guid}, { "@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, }; await backendRepository.UpdateOneByCustomTable(variableInfoDic, "variable", "id=" + variableInfo.id); 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> DeleteOneVariable(int id) { ApiResult apiResult = new ApiResult(); try { string sWhere = @$"deleted = 0 AND id = @Id"; object param = new { Id = id }; var variableInfo = await backendRepository.GetOneAsync("variable", sWhere, param); if (variableInfo == null) { apiResult.Code = "9998"; apiResult.Msg = "查無該系統變數"; return apiResult; } await backendRepository.DeleteOne(id.ToString(), "variable", "id"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "id=" + id.ToString()); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }