AipDbRepository.cs 16 KB

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