OperateLogController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Azure.Cosmos.Table;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.Models;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI;
  13. using System.Text;
  14. using TEAMModelOS.SDK.Models.Table;
  15. using TEAMModelBI.Filter;
  16. using TEAMModelBI.Tool.Extension;
  17. using TEAMModelOS.SDK.Extension;
  18. using TEAMModelBI.DI.BIAzureStorage;
  19. using TEAMModelOS.SDK.Context.BI;
  20. namespace TEAMModelBI.Controllers.OperateRecord
  21. {
  22. [Route("operatelog")]
  23. [ApiController]
  24. public class OperateLogController : ControllerBase
  25. {
  26. private readonly AzureStorageFactory _azureStorage;
  27. private readonly DingDing _dingDing;
  28. private readonly Option _option;
  29. public OperateLogController(AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  30. {
  31. _azureStorage = azureStorage;
  32. _dingDing = dingDing;
  33. _option = option?.Value;
  34. }
  35. /// <summary>
  36. /// 查询BI操作记录
  37. /// </summary>
  38. /// <param name="jsonElement"></param>
  39. /// <returns></returns>
  40. [HttpPost("get-record")]
  41. public async Task<IActionResult> GetOperateLogRecord(JsonElement jsonElement)
  42. {
  43. try
  44. {
  45. jsonElement.TryGetProperty("single", out JsonElement single);
  46. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  47. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  48. jsonElement.TryGetProperty("platform", out JsonElement platform);
  49. if(!jsonElement.TryGetProperty("reorder", out JsonElement orderby)) return BadRequest();
  50. jsonElement.TryGetProperty("site", out JsonElement site);
  51. List<BIOptLog> operateLogs = new();
  52. StringBuilder tableSql = new StringBuilder();
  53. if (!string.IsNullOrEmpty($"{single}"))
  54. tableSql.Append($" time eq {single}L ");
  55. if (!string.IsNullOrEmpty($"{startDate}"))
  56. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time ge {startDate}L " : $"time ge {startDate}L ");
  57. if (!string.IsNullOrEmpty($"{endDate}"))
  58. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time le {endDate}L " : $" time le {endDate}L ");
  59. if (!string.IsNullOrEmpty($"{platform}"))
  60. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and platform eq '{platform}' " : $" platform eq '{platform}' ");
  61. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  62. if ($"{site}".Equals(BIConst.Global))
  63. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("BIOptLog");
  64. //lambda 表达式排序
  65. operateLogs = await table.QueryWhereString<BIOptLog>(tableSql.ToString());
  66. switch (orderby.GetString())
  67. {
  68. case "asc":
  69. //lambda 排序 升序
  70. operateLogs.Sort((x, y) => x.time.CompareTo(y.time));
  71. return Ok(new { state = 200, operateLogs });
  72. case "desc":
  73. //lambda 排序 降序
  74. operateLogs.Sort((x, y) => y.time.CompareTo(x.time));
  75. return Ok(new { state = 200, operateLogs });
  76. default:
  77. return Ok(new { state = 200, operateLogs });
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. await _dingDing.SendBotMsg($"BI,{_option.Location} /operatelog/get-record \n {ex.Message}\n{ex.StackTrace} ",GroupNames.成都开发測試群組);
  83. return BadRequest();
  84. }
  85. }
  86. /// <summary>
  87. /// 删除操作记录
  88. /// </summary>
  89. /// <param name="jsonElement"></param>
  90. /// <returns></returns>
  91. [ProducesDefaultResponseType]
  92. [AuthToken(Roles = "admin")]
  93. [HttpPost("del-record")]
  94. public async Task<IActionResult> DelOperateLogRecord(JsonElement jsonElement)
  95. {
  96. try
  97. {
  98. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  99. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  100. jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
  101. jsonElement.TryGetProperty("site", out JsonElement site);
  102. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  103. //var temp = await _azureStorage.Delete<OperateLog>(partitionKey: "OperateLog-BI", rowKey: $"{startDate}"); //删除单个
  104. StringBuilder operateStr = new StringBuilder($"{_tmdName}【{_tmdId}】账户删除操作记录,");
  105. StringBuilder tableStrWhere = new StringBuilder();
  106. if (!string.IsNullOrEmpty($"{rowKey}"))
  107. {
  108. tableStrWhere.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
  109. operateStr.Append($"删除的时间戳:{rowKey}");
  110. }
  111. else
  112. {
  113. tableStrWhere.Append($"time ge {startDate}L and time le {endDate}L ");
  114. operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
  115. }
  116. var tableClient = _azureStorage.GetCloudTableClient();
  117. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  118. if ($"{site}".Equals(BIConst.Global))
  119. {
  120. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  121. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  122. }
  123. var table = tableClient.GetTableReference("BIOptLog");
  124. var temp = await table.DeleteStringWhere<BIOptLog>(rowKey: tableStrWhere.ToString());
  125. //保存操作记录
  126. //await _azureStorage.SaveBILog("operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
  127. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
  128. if (temp.Count > 0)
  129. {
  130. return Ok(new { state = 200 });
  131. }
  132. else return Ok(new { state = 400 });
  133. }
  134. catch (Exception ex)
  135. {
  136. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/del-record \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  137. return BadRequest();
  138. }
  139. }
  140. /// <summary>
  141. /// 依据醍摩豆账户查询日志记录
  142. /// </summary>
  143. /// <param name="jsonElement"></param>
  144. /// <returns></returns>
  145. [ProducesDefaultResponseType]
  146. [HttpPost("get-logtmdid")]
  147. public async Task<IActionResult> GetLogTmdId(JsonElement jsonElement)
  148. {
  149. try
  150. {
  151. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  152. jsonElement.TryGetProperty("platform", out JsonElement platform);
  153. jsonElement.TryGetProperty("site", out JsonElement site);
  154. string storageSql = null;
  155. if (!string.IsNullOrEmpty($"{platform}"))
  156. storageSql = $"tmdId eq '{tmdId}' and platform eq '{platform}'";
  157. else storageSql = $"tmdId eq '{tmdId}'";
  158. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  159. if ($"{site}".Equals(BIConst.Global))
  160. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("BIOptLog");
  161. var optLogs = await table.QueryWhereString<BIOptLog>(storageSql);
  162. return Ok(new { state = 200, optLogs });
  163. }
  164. catch (Exception ex)
  165. {
  166. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/get-logtmdid \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  167. return BadRequest();
  168. }
  169. }
  170. }
  171. }