ReportService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using log4net;
  2. using System;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using Dapper;
  6. using System.Windows;
  7. using Agent.Models;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. namespace Agent.Services
  11. {
  12. class ReportService
  13. {
  14. private static readonly ILog log = LogManager.GetLogger(typeof(ReportService));
  15. /// <summary>
  16. /// 레포트 추가
  17. /// </summary>
  18. /// <param name="policyTid"></param>
  19. /// <param name="fileName"></param>
  20. /// <param name="filePath"></param>
  21. /// <param name="workType"></param>
  22. /// <param name="isFail"></param>
  23. /// <returns></returns>
  24. private int AddReport(int policyTid, string fileName, string filePath, string workType, bool isFail)
  25. {
  26. try
  27. {
  28. var userTid = Application.Current.Properties["user_tid"];
  29. using (var con = new SqlConnection(App._myConnection))
  30. {
  31. con.Open();
  32. var sql = $@"
  33. INSERT INTO [di_report]
  34. ([user_tid]
  35. ,[policy_tid]
  36. ,[name]
  37. ,[path]
  38. ,[work_type]
  39. ,[create_date]
  40. ,[is_fail]
  41. ,[sys_plant_cd]
  42. )
  43. VALUES
  44. (@UserTid
  45. ,@PolicyTid
  46. ,@Name
  47. ,@Path
  48. ,@WorkType
  49. ,GETDATE()
  50. ,@IsFail
  51. ,@sysPlantCd
  52. )
  53. ";
  54. var data = new {
  55. UserTid = userTid,
  56. PolicyTid = policyTid,
  57. Name = fileName,
  58. Path = filePath,
  59. WorkType = workType,
  60. IsFail = isFail,
  61. sysPlantCd = App._sysPlantCd
  62. };
  63. return con.Execute(sql, data);
  64. }
  65. }
  66. catch (Exception e)
  67. {
  68. log.Error(e);
  69. return 0;
  70. }
  71. }
  72. /// <summary>
  73. /// 백업 레포트 추가
  74. /// </summary>
  75. /// <param name="policyTid"></param>
  76. /// <param name="fileName"></param>
  77. /// <param name="filePath"></param>
  78. /// <param name="isFail"></param>
  79. /// <returns></returns>
  80. public int AddBackupReport(int policyTid, string fileName, string filePath, bool isFail)
  81. {
  82. return AddReport(policyTid, fileName, filePath, "BACKUP", isFail);
  83. }
  84. /// <summary>
  85. /// 복구 레포트 추가
  86. /// </summary>
  87. /// <param name="policyTid"></param>
  88. /// <param name="fileName"></param>
  89. /// <param name="filePath"></param>
  90. /// <param name="isFail"></param>
  91. /// <returns></returns>
  92. public int AddRestoreReport(int policyTid, string fileName, string filePath, bool isFail)
  93. {
  94. return AddReport(policyTid, fileName, filePath, "RESTORE", isFail);
  95. }
  96. /// <summary>
  97. /// 재시도 레포트 추가
  98. /// </summary>
  99. /// <param name="policyTid"></param>
  100. /// <param name="fileName"></param>
  101. /// <param name="filePath"></param>
  102. /// <param name="isFail"></param>
  103. /// <returns></returns>
  104. public int AddRetryReport(int policyTid, string fileName, string filePath, bool isFail)
  105. {
  106. return AddReport(policyTid, fileName, filePath, "RETRY", isFail);
  107. }
  108. /// <summary>
  109. /// 레포트 조회
  110. /// </summary>
  111. /// <returns></returns>
  112. public List<Report> GetReports()
  113. {
  114. try
  115. {
  116. var userTid = Application.Current.Properties["user_tid"];
  117. using (var con = new SqlConnection(App._myConnection))
  118. {
  119. con.Open();
  120. var sql = @"
  121. SELECT [tid]
  122. ,[user_tid]
  123. ,[policy_tid]
  124. ,[name]
  125. ,[path]
  126. ,dbo.FN_WORK_TYPE_NM([work_type]) AS [work_type]
  127. ,[create_date]
  128. ,[is_fail]
  129. FROM [di_report]
  130. WHERE user_tid = @UserTid
  131. ORDER BY [create_date] DESC
  132. ";
  133. return con.Query<Report>(sql, new { UserTid = userTid }).ToList();
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. log.Error(e);
  139. return new List<Report>();
  140. }
  141. }
  142. /// <summary>
  143. /// 일일 레포트 조회
  144. /// </summary>
  145. /// <returns></returns>
  146. public List<Report> GetDailyReports(string start, string end)
  147. {
  148. try
  149. {
  150. var userTid = Application.Current.Properties["user_tid"];
  151. using (var con = new SqlConnection(App._myConnection))
  152. {
  153. con.Open();
  154. var sql = @"
  155. SELECT [tid]
  156. ,[user_tid]
  157. ,[policy_tid]
  158. ,[name]
  159. ,[path]
  160. ,dbo.FN_WORK_TYPE_NM([work_type]) AS [work_type]
  161. ,[create_date]
  162. ,[is_fail]
  163. FROM [di_report]
  164. WHERE 1=1
  165. AND user_tid = @UserTid
  166. AND ([create_date] >= @MinDate AND [create_date] <= @MaxDate)
  167. ORDER BY [create_date] DESC
  168. ";
  169. return con.Query<Report>(sql, new { UserTid = userTid, MinDate = $"{start} 00:00:00", MaxDate = $"{end} 23:59:59" }).ToList();
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. log.Error(e);
  175. return new List<Report>();
  176. }
  177. }
  178. }
  179. }