OperateLogController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. namespace TEAMModelBI.Controllers.OperateRecord
  19. {
  20. [Route("operatelog")]
  21. [ApiController]
  22. public class OperateLogController : ControllerBase
  23. {
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly DingDing _dingDing;
  26. private readonly Option _option;
  27. public OperateLogController(AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  28. {
  29. _azureStorage = azureStorage;
  30. _dingDing = dingDing;
  31. _option = option?.Value;
  32. }
  33. /// <summary>
  34. /// 查询BI操作记录
  35. /// </summary>
  36. /// <param name="jsonElement"></param>
  37. /// <returns></returns>
  38. [HttpPost("get-record")]
  39. public async Task<IActionResult> GetOperateLogRecord(JsonElement jsonElement)
  40. {
  41. try
  42. {
  43. jsonElement.TryGetProperty("single", out JsonElement single);
  44. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  45. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  46. jsonElement.TryGetProperty("platform", out JsonElement platform);
  47. List<BIOptLog> operateLogs = new();
  48. StringBuilder tableSql = new StringBuilder();
  49. if (!string.IsNullOrEmpty($"{single}"))
  50. tableSql.Append($" time eq {single}L ");
  51. if (!string.IsNullOrEmpty($"{startDate}"))
  52. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time ge {startDate}L " : $"time ge {startDate}L ");
  53. if (!string.IsNullOrEmpty($"{endDate}"))
  54. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time le {endDate}L " : $" time le {endDate}L ");
  55. if (!string.IsNullOrEmpty($"{platform}"))
  56. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and platform eq '{platform}' " : $" platform eq '{platform}' ");
  57. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  58. operateLogs = await table.QueryWhereString<BIOptLog>(tableSql.ToString());
  59. return Ok(new { state = 200, operateLogs });
  60. }
  61. catch (Exception ex)
  62. {
  63. await _dingDing.SendBotMsg($"BI,{_option.Location} \n {ex.Message}{ex.StackTrace} /operatelog/get-record",GroupNames.成都开发測試群組);
  64. return BadRequest();
  65. }
  66. }
  67. /// <summary>
  68. /// 删除操作记录
  69. /// </summary>
  70. /// <param name="jsonElement"></param>
  71. /// <returns></returns>
  72. [ProducesDefaultResponseType]
  73. [AuthToken(Roles = "admin")]
  74. [HttpPost("del-record")]
  75. public async Task<IActionResult> DelOperateLogRecord(JsonElement jsonElement)
  76. {
  77. try
  78. {
  79. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  80. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  81. jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
  82. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  83. //var temp = await _azureStorage.Delete<OperateLog>(partitionKey: "OperateLog-BI", rowKey: $"{startDate}"); //删除单个
  84. StringBuilder operateStr = new StringBuilder($"{_tmdName}【{_tmdId}】账户删除操作记录,");
  85. StringBuilder tableStrWhere = new StringBuilder();
  86. if (!string.IsNullOrEmpty($"{rowKey}"))
  87. {
  88. tableStrWhere.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
  89. operateStr.Append($"删除的时间戳:{rowKey}");
  90. }
  91. else
  92. {
  93. tableStrWhere.Append($"time ge {startDate}L and time le {endDate}L ");
  94. operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
  95. }
  96. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  97. var temp = await table.DeleteStringWhere<BIOptLog>(rowKey: tableStrWhere.ToString());
  98. //保存操作记录
  99. await _azureStorage.SaveBILog("operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
  100. if (temp.Count > 0)
  101. {
  102. return Ok(new { state = 200 });
  103. }
  104. else return Ok(new { state = 400 });
  105. }
  106. catch (Exception ex)
  107. {
  108. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/del-record {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  109. return BadRequest();
  110. }
  111. }
  112. /// <summary>
  113. /// 依据醍摩豆账户查询日志记录
  114. /// </summary>
  115. /// <param name="jsonElement"></param>
  116. /// <returns></returns>
  117. [ProducesDefaultResponseType]
  118. [HttpPost("get-logtmdid")]
  119. public async Task<IActionResult> GetLogTmdId(JsonElement jsonElement)
  120. {
  121. try
  122. {
  123. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  124. jsonElement.TryGetProperty("platform", out JsonElement platform);
  125. string storageSql = null;
  126. if (!string.IsNullOrEmpty($"{platform}"))
  127. storageSql = $"tmdId eq '{tmdId}' and platform eq '{platform}'";
  128. else storageSql = $"tmdId eq '{tmdId}'";
  129. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  130. var optLogs = await table.QueryWhereString<BIOptLog>(storageSql);
  131. return Ok(new { state = 200, optLogs });
  132. }
  133. catch (Exception ex)
  134. {
  135. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/get-logtmdid {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  136. return BadRequest();
  137. }
  138. }
  139. }
  140. }