AipDbRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using Aip.Service.Configurations;
  2. using Aip.Service.Entities;
  3. using Aip.Service.Infrastructures;
  4. using Aip.Service.Models.Dto;
  5. using Aip.Service.Models.Response;
  6. using Aip.Service.Utils;
  7. using Dapper;
  8. using System.Data;
  9. using System.Diagnostics;
  10. namespace Aip.Service.Repositories;
  11. public class AipDbRepository : IAipDbRepository
  12. {
  13. private readonly ILogger<AipDbRepository> _log;
  14. private readonly IDatabaseFactory _dbContext;
  15. private readonly AipSettings _aipSetting = new AipSettings();
  16. public AipDbRepository(ILogger<AipDbRepository> log, IConfiguration configuration, IDatabaseFactory dbContext)
  17. {
  18. _log = log;
  19. _dbContext = dbContext;
  20. LoadApplicationConfig(configuration);
  21. }
  22. private void LoadApplicationConfig(IConfiguration configuration)
  23. {
  24. if (!int.TryParse(configuration["Id"], out int aipServerId))
  25. {
  26. aipServerId = 1;
  27. }
  28. _aipSetting.AipServerId = aipServerId;
  29. if (!int.TryParse(configuration["Port"], out int port))
  30. {
  31. port = 9871;
  32. }
  33. _aipSetting.Port = port;
  34. if (!int.TryParse(configuration["AipPort"], out int aipPort))
  35. {
  36. aipPort = 7000;
  37. }
  38. _aipSetting.AipPort = aipPort;
  39. int coreCount = Environment.ProcessorCount;
  40. if (!int.TryParse(configuration["AipBindings"], out int aipBindings))
  41. {
  42. aipBindings = (coreCount * 2);
  43. }
  44. _aipSetting.AipBindings = aipBindings;
  45. // appsettings.json 설정정보 로딩
  46. string? appName = configuration["Name"];
  47. if (appName == null)
  48. {
  49. appName = "AIP Gateway API";
  50. }
  51. string? version = configuration["Version"];
  52. if (version == null)
  53. {
  54. version = "1.0.0";
  55. }
  56. string? mipData = configuration["MipDataPath"];
  57. if (mipData == null)
  58. {
  59. mipData = "App_Data\\mip_data";
  60. }
  61. string? sourceFilePath = configuration["SourceFilePath"];
  62. if (sourceFilePath == null)
  63. {
  64. sourceFilePath = "c:\\Data\\Source\\";
  65. }
  66. string? targetFilePath = configuration["TargetFilePath"];
  67. if (targetFilePath == null)
  68. {
  69. targetFilePath = "c:\\Data\\Target\\";
  70. }
  71. _aipSetting.SetValue("AppName", appName);
  72. _aipSetting.SetValue("AppVersion", version);
  73. _aipSetting.SetValue("MipData", mipData);
  74. _aipSetting.SetValue("SourceFileDir", sourceFilePath);
  75. _aipSetting.SetValue("TargetFileDir", targetFilePath);
  76. }
  77. public async Task<AipSettings> LoadAipConfig()
  78. {
  79. var start = Stopwatch.GetTimestamp();
  80. _log.LogInformation("Loading System Configurations from database.");
  81. using var connection = _dbContext.GetDbConnection();
  82. string sql = "SELECT * FROM TB_AIP_CONFIG WHERE AipServerId = @AipServerId";
  83. // 시스템 기본 설정 정보 조회
  84. IEnumerable<TbAipConfig> results = await connection.QueryAsync<TbAipConfig>(
  85. sql,
  86. new
  87. {
  88. AipServerId = 0,
  89. },
  90. commandType: CommandType.Text
  91. );
  92. foreach (TbAipConfig config in results)
  93. {
  94. _log.LogInformation("{0}, {1}, {2}", config.Id, config.ConfigKey, config.ConfigValue);
  95. _aipSetting.SetValue(config.ConfigKey, config.ConfigValue);
  96. }
  97. // 어플리케이션 설정 정보 조회
  98. results = await connection.QueryAsync<TbAipConfig>(
  99. sql,
  100. new
  101. {
  102. _aipSetting.AipServerId,
  103. },
  104. commandType: CommandType.Text
  105. );
  106. foreach (TbAipConfig config in results)
  107. {
  108. _log.LogInformation("{0}, {1}, {2}", config.Id, config.ConfigKey, config.ConfigValue);
  109. _aipSetting.SetValue(config.ConfigKey, config.ConfigValue);
  110. }
  111. _log.LogInformation("Loading System Configurations from database. Completed {0} ms.", TimeUtils.GetElapsedMilliseconds(start));
  112. return _aipSetting;
  113. }
  114. public async Task<List<LinkedApiKey>> LoadLinkedApiKeys()
  115. {
  116. using var connection = _dbContext.GetDbConnection();
  117. string sql = @"SELECT A.Id AS ApiKeyId,
  118. A.ApiKey,
  119. A.policyLookupYn,
  120. A.fileInfoLookupYn,
  121. A.applyLabelYn,
  122. A.releaseLabelYn,
  123. A.encryptionFileYn,
  124. A.decryptionFileYn,
  125. A.ExpiredAt,
  126. B.ServerId, B.ServerIpAddr, B.ServerDesc,
  127. C.SystemId, C.SystemName
  128. FROM TB_LINKED_API_KEY A
  129. INNER JOIN TB_LINKED_SERVER B
  130. ON A.ServerId = B.ServerId
  131. AND A.UseYn = 1
  132. AND B.UseYn = 1
  133. INNER JOIN TB_LINKED_SYSTEM C
  134. ON B.SystemId = C.SystemId
  135. AND C.UseYn = 1";
  136. IEnumerable<LinkedApiKey> results = await connection.QueryAsync<LinkedApiKey>(sql, commandType: CommandType.Text);
  137. return results.ToList();
  138. }
  139. public async Task<List<LinkedDecryptKey>> LoadLinkedDecryptKeys()
  140. {
  141. using var connection = _dbContext.GetDbConnection();
  142. string sql = @"SELECT A.Id AS DecryptKeyId, A.DecryptKey, A.ExpiredAt,
  143. B.ServerId, B.ServerIpAddr, B.ServerDesc,
  144. C.SystemId, C.SystemName
  145. FROM TB_LINKED_DECRYPT_KEY A
  146. INNER JOIN TB_LINKED_SERVER B
  147. ON A.ServerId = B.ServerId
  148. AND A.UseYn = 1
  149. AND B.UseYn = 1
  150. INNER JOIN TB_LINKED_SYSTEM C
  151. ON B.SystemId = C.SystemId
  152. AND C.UseYn = 1";
  153. IEnumerable<LinkedDecryptKey> results = await connection.QueryAsync<LinkedDecryptKey>(sql, commandType: CommandType.Text);
  154. return results.ToList();
  155. }
  156. public async Task<List<TbAipLabel>> LoadAipLabels()
  157. {
  158. using var connection = _dbContext.GetDbConnection();
  159. string sql = "SELECT t.LabelId, t.CreatedAt, t.DeletedAt, t.LabelDesc, t.LabelGuid, t.LabelName, t.UseYn FROM TB_AIP_LABEL AS t";
  160. IEnumerable<TbAipLabel> results = await connection.QueryAsync<TbAipLabel>(sql, commandType: CommandType.Text);
  161. return results.ToList();
  162. }
  163. public async Task<int> UpdateAipLables(List<TbAipLabel> updLabels)
  164. {
  165. int effectedRows = 0;
  166. using var connection = _dbContext.GetDbConnection();
  167. string sql = "UPDATE TB_AIP_LABEL SET LabelDesc = @LabelDesc, LabelName = @LabelName WHERE LabelId = @LabelId";
  168. foreach (TbAipLabel obj in updLabels)
  169. {
  170. effectedRows += await connection.ExecuteAsync(
  171. sql,
  172. new
  173. {
  174. obj.LabelDesc,
  175. obj.LabelName,
  176. obj.LabelId,
  177. },
  178. commandType: CommandType.Text
  179. );
  180. }
  181. return effectedRows;
  182. }
  183. public async Task<int> InsertAipLables(List<TbAipLabel> newLabels)
  184. {
  185. int effectedRows = 0;
  186. using var connection = _dbContext.GetDbConnection();
  187. string sql = "INSERT INTO TB_AIP_LABEL(LabelGuid, LabelName, LabelDesc, UseYn) VALUES(@LabelGuid, @LabelName, @LabelDesc, 1)";
  188. foreach (TbAipLabel obj in newLabels)
  189. {
  190. effectedRows += await connection.ExecuteAsync(
  191. sql,
  192. new
  193. {
  194. obj.LabelGuid,
  195. obj.LabelName,
  196. obj.LabelDesc,
  197. },
  198. commandType: CommandType.Text
  199. );
  200. }
  201. return effectedRows;
  202. }
  203. public async Task<List<TbAipPolicy>> LoadAipPolicies()
  204. {
  205. using var connection = _dbContext.GetDbConnection();
  206. string sql = "SELECT t.PolicyId, t.CreatedAt, t.DeletedAt, t.PolicyDesc, t.PolicyGuid, t.PolicyName, t.UseYn FROM TB_AIP_POLICY AS t";
  207. IEnumerable<TbAipPolicy> results = await connection.QueryAsync<TbAipPolicy>(sql, commandType: CommandType.Text);
  208. return results.ToList();
  209. }
  210. public async Task<int> UpdateAipPolicies(List<TbAipPolicy> updLabels)
  211. {
  212. int effectedRows = 0;
  213. using var connection = _dbContext.GetDbConnection();
  214. string sql = "UPDATE TB_AIP_POLICY SET PolicyDesc = @PolicyDesc, PolicyName = @PolicyName WHERE PolicyId = @PolicyId";
  215. foreach (TbAipPolicy obj in updLabels)
  216. {
  217. effectedRows += await connection.ExecuteAsync(
  218. sql,
  219. new
  220. {
  221. obj.PolicyDesc,
  222. obj.PolicyName,
  223. obj.PolicyId,
  224. },
  225. commandType: CommandType.Text
  226. );
  227. }
  228. return effectedRows;
  229. }
  230. public async Task<int> InsertAipPolicies(List<TbAipPolicy> newLabels)
  231. {
  232. int effectedRows = 0;
  233. using var connection = _dbContext.GetDbConnection();
  234. string sql = "INSERT INTO TB_AIP_POLICY(PolicyGuid, PolicyName, PolicyDesc, UseYn) VALUES(@PolicyGuid, @PolicyName, @PolicyDesc, 1)";
  235. foreach (TbAipPolicy obj in newLabels)
  236. {
  237. effectedRows += await connection.ExecuteAsync(
  238. sql,
  239. new
  240. {
  241. obj.PolicyGuid,
  242. obj.PolicyName,
  243. obj.PolicyDesc,
  244. },
  245. commandType: CommandType.Text
  246. );
  247. }
  248. return effectedRows;
  249. }
  250. public async Task<List<TbAipProtection>> LoadAipTemplates()
  251. {
  252. using var connection = _dbContext.GetDbConnection();
  253. string sql = "SELECT t.ProtectionId, t.CreatedAt, t.DeletedAt, t.ProtectionDesc, t.ProtectionGuid, t.ProtectionName, t.UseYn FROM TB_AIP_PROTECTION AS t";
  254. IEnumerable<TbAipProtection> results = await connection.QueryAsync<TbAipProtection>(sql, commandType: CommandType.Text);
  255. return results.ToList();
  256. }
  257. public async Task<int> UpdateAipTemplates(List<TbAipProtection> updLabels)
  258. {
  259. int effectedRows = 0;
  260. using var connection = _dbContext.GetDbConnection();
  261. string sql = "UPDATE TB_AIP_PROTECTION SET ProtectionDesc = @ProtectionDesc, ProtectionName = @ProtectionName WHERE ProtectionId = @ProtectionId";
  262. foreach (TbAipProtection obj in updLabels)
  263. {
  264. effectedRows += await connection.ExecuteAsync(
  265. sql,
  266. new
  267. {
  268. obj.ProtectionDesc,
  269. obj.ProtectionName,
  270. obj.ProtectionId,
  271. },
  272. commandType: CommandType.Text
  273. );
  274. }
  275. return effectedRows;
  276. }
  277. public async Task<int> InsertAipTemplates(List<TbAipProtection> newLabels)
  278. {
  279. int effectedRows = 0;
  280. using var connection = _dbContext.GetDbConnection();
  281. string sql = "INSERT INTO TB_AIP_PROTECTION(ProtectionGuid, ProtectionName, ProtectionDesc, UseYn) VALUES(@ProtectionGuid, @ProtectionName, @ProtectionDesc, 1)";
  282. foreach (TbAipProtection obj in newLabels)
  283. {
  284. effectedRows += await connection.ExecuteAsync(
  285. sql,
  286. new
  287. {
  288. obj.ProtectionGuid,
  289. obj.ProtectionName,
  290. obj.ProtectionDesc,
  291. },
  292. commandType: CommandType.Text
  293. );
  294. }
  295. return effectedRows;
  296. }
  297. public async Task<GeneralResponse> ReloadDatabase()
  298. {
  299. await Task.Run(() => Thread.Sleep(100));
  300. return new GeneralResponse
  301. {
  302. errorCode = 0,
  303. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  304. effectCount = 0,
  305. };
  306. }
  307. public async Task<List<LinkedSystemDto>> GetLinkedSystems()
  308. {
  309. using var connection = _dbContext.GetDbConnection();
  310. string sql = "SELECT t.SystemId, t.SystemName, t.CreatedAt, t.UseYn, t.SystemDesc, t.DeletedAt FROM TB_LINKED_SYSTEM AS t";
  311. IEnumerable<LinkedSystemDto> results = await connection.QueryAsync<LinkedSystemDto>(sql, commandType: CommandType.Text);
  312. return results.ToList();
  313. }
  314. public async Task<List<LinkedServerDto>> GetLinkedServers()
  315. {
  316. using var connection = _dbContext.GetDbConnection();
  317. string sql = "SELECT t.ServerId, t.SystemId, t.ServerIpAddr, t.ServerDesc, t.CreatedAt, t.UseYn, t.DeletedAt FROM TB_LINKED_SERVER AS t";
  318. IEnumerable<LinkedServerDto> results = await connection.QueryAsync<LinkedServerDto>(sql, commandType: CommandType.Text);
  319. return results.ToList();
  320. }
  321. public async Task<List<LinkedApiKeyDto>> GetLinkedApiKeys()
  322. {
  323. using var connection = _dbContext.GetDbConnection();
  324. string sql = "SELECT t.Id, t.ServerId, t.ApiKey, t.ExpiredAt, t.CreatedAt, t.UseYn, t.DeletedAt FROM TB_LINKED_API_KEY AS t";
  325. IEnumerable<LinkedApiKeyDto> results = await connection.QueryAsync<LinkedApiKeyDto>(sql, commandType: CommandType.Text);
  326. return results.ToList();
  327. }
  328. public async Task<List<LinkedDecryptKeyDto>> GetLinkedDecryptKeys()
  329. {
  330. using var connection = _dbContext.GetDbConnection();
  331. string sql = "SELECT t.Id, t.ServerId, t.DecryptKey, t.ExpiredAt, t.CreatedAt, t.UseYn, t.DeletedAt FROM TB_LINKED_DECRYPT_KEY AS t";
  332. IEnumerable<LinkedDecryptKeyDto> results = await connection.QueryAsync<LinkedDecryptKeyDto>(sql, commandType: CommandType.Text);
  333. return results.ToList();
  334. }
  335. }