EntityController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using FMSAdmin.Data;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.EntityFrameworkCore;
  6. using System.Data;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using FMSAdmin.Entities;
  10. using System;
  11. using FMSAdmin.Helpers;
  12. namespace FMSAdmin.Controllers {
  13. [Authorize]
  14. [ApiController]
  15. [ApiVersion("1")]
  16. [Route("api/[controller]")]
  17. public class EntityController : Controller {
  18. private readonly ILogger<EntityController> _logger;
  19. private readonly FMSContext _context;
  20. public EntityController(
  21. ILogger<EntityController> logger,
  22. FMSContext context
  23. ) {
  24. _logger = logger;
  25. _context = context;
  26. }
  27. /// <summary>
  28. /// 셀렉트
  29. /// </summary>
  30. [HttpGet]
  31. [Route("[action]")]
  32. public IActionResult Select(string siteId, string entity, string id, string name, string fields, string sort = "name", string where = null, bool useCheck = false, bool businessAuth = false) {
  33. if (string.IsNullOrEmpty(entity)) {
  34. return BadRequest("누락된 정보가 있습니다.");
  35. }
  36. if (string.IsNullOrEmpty(id)) {
  37. if (entity.StartsWith("Cm")) {
  38. id = entity.Substring(2) + "Id";
  39. } else if (entity.StartsWith("Bems")) {
  40. id = entity.Substring(4) + "Id";
  41. } else if (entity.StartsWith("Fms")) {
  42. id = entity.Substring(3) + "Id";
  43. }
  44. }
  45. if (string.IsNullOrEmpty(name)) {
  46. name = "Name";
  47. }
  48. CmUser user = null;
  49. if (entity == "CmBusinessField" && businessAuth) {
  50. user = _context.CmUser.FirstOrDefault(x => x.UserId == User.Identity.Name);
  51. }
  52. // fields 로 넘어온 추가 필드 처리
  53. var additionalFields = (fields ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries);
  54. var additionalFieldsSelect = "";
  55. if (!string.IsNullOrEmpty(fields)) {
  56. additionalFieldsSelect = "," + fields;
  57. }
  58. // Entity 검증 일단 SKIP
  59. IList<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
  60. using (var command = _context.Database.GetDbConnection().CreateCommand()) {
  61. string wheresql = "";
  62. IList<string> conds = new List<string>();
  63. if (!string.IsNullOrEmpty(siteId)) {
  64. conds.Add($"SiteId = {siteId}");
  65. }
  66. if (useCheck) {
  67. conds.Add("IsUse = 1");
  68. }
  69. if (entity == "CmBusinessField" && businessAuth && user != null && user.BusinessFieldId != null) {
  70. // User 의 BusinessFieldId 와 공통 만 조회되도록 변경
  71. var commonBusinessField = _context.CmBusinessField.FirstOrDefault(
  72. x => x.Name == "공통"
  73. );
  74. if (commonBusinessField != null) {
  75. conds.Add($"BusinessFieldId IN ({commonBusinessField.BusinessFieldId},{user.BusinessFieldId})");
  76. } else {
  77. conds.Add($"BusinessFieldId IN ({user.BusinessFieldId})");
  78. }
  79. }
  80. if (!string.IsNullOrEmpty(where)) {
  81. conds.Add(where);
  82. }
  83. if (conds.Count > 0) {
  84. wheresql = $"WHERE {string.Join(" AND ", conds)}";
  85. }
  86. string ordersql = "";
  87. if (entity == "CmSite") {
  88. ordersql = $"ORDER BY SortOrderNo";
  89. } else {
  90. if (sort == "name") {
  91. ordersql = $"ORDER BY {name}";
  92. } else if (sort == "id") {
  93. ordersql = $"ORDER BY {id}";
  94. }
  95. }
  96. string sql = $"SELECT {id},{name}{additionalFieldsSelect} FROM {entity} {wheresql} {ordersql}";
  97. //_logger.LogInformation(sql);
  98. command.CommandText = sql;
  99. _context.Database.OpenConnection();
  100. using (var reader = command.ExecuteReader()) {
  101. if (reader.HasRows) {
  102. while (reader.Read()) {
  103. var row = new Dictionary<string, object>();
  104. row["Id"] = reader.GetValue(id);
  105. row["Name"] = reader.GetValue(name);
  106. // fields 로 넘어온 추가 필드의 값을 전달
  107. if (additionalFields != null && additionalFields.Length > 0) {
  108. foreach (var field in additionalFields) {
  109. row[field] = reader.GetValue(field);
  110. }
  111. }
  112. list.Add(row);
  113. }
  114. }
  115. }
  116. }
  117. return Ok(list);
  118. }
  119. }
  120. }