OperateLogController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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<OptLog> operateLogs = null;
  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. operateLogs = await _azureStorage.QueryWhereString<OptLog>(tableSql.ToString());
  58. return Ok(new { state = 200, operateLogs });
  59. }
  60. catch (Exception ex)
  61. {
  62. await _dingDing.SendBotMsg($"BI,{_option.Location} \n {ex.Message}{ex.StackTrace} /operatelog/get-record",GroupNames.成都开发測試群組);
  63. return BadRequest();
  64. }
  65. }
  66. /// <summary>
  67. /// 删除操作记录
  68. /// </summary>
  69. /// <param name="jsonElement"></param>
  70. /// <returns></returns>
  71. [ProducesDefaultResponseType]
  72. [AuthToken(Roles = "admin")]
  73. [HttpPost("del-record")]
  74. public async Task<IActionResult> DelOperateLogRecord(JsonElement jsonElement)
  75. {
  76. try
  77. {
  78. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  79. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  80. jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
  81. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  82. //var temp = await _azureStorage.Delete<OperateLog>(partitionKey: "OperateLog-BI", rowKey: $"{startDate}"); //删除单个
  83. StringBuilder operateStr = new StringBuilder($"{_tmdName}【{_tmdId}】账户删除操作记录,");
  84. StringBuilder tableStrWhere = new StringBuilder();
  85. if (!string.IsNullOrEmpty($"{rowKey}"))
  86. {
  87. tableStrWhere.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
  88. operateStr.Append($"删除的时间戳:{rowKey}");
  89. }
  90. else
  91. {
  92. tableStrWhere.Append($"time ge {startDate}L and time le {endDate}L ");
  93. operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
  94. }
  95. var temp = await _azureStorage.DeleteStringWhere<OptLog>(rowKey: tableStrWhere.ToString());
  96. //保存操作记录
  97. await _azureStorage.SaveLog("operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
  98. if (temp.Count > 0)
  99. {
  100. return Ok(new { state = 200 });
  101. }
  102. else return Ok(new { state = 400 });
  103. }
  104. catch (Exception ex)
  105. {
  106. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/del-record {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  107. return BadRequest();
  108. }
  109. }
  110. /// <summary>
  111. /// 依据醍摩豆账户查询日志记录
  112. /// </summary>
  113. /// <param name="jsonElement"></param>
  114. /// <returns></returns>
  115. [ProducesDefaultResponseType]
  116. [HttpPost("get-logtmdid")]
  117. public async Task<IActionResult> GetLogTmdId(JsonElement jsonElement)
  118. {
  119. try
  120. {
  121. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  122. jsonElement.TryGetProperty("platform", out JsonElement platform);
  123. string storageSql = null;
  124. if (!string.IsNullOrEmpty($"{platform}"))
  125. storageSql = $"tmdId eq '{tmdId}' and platform eq '{platform}'";
  126. else storageSql = $"tmdId eq '{tmdId}'";
  127. List<OptLog> optLogs = new List<OptLog>();
  128. optLogs = await _azureStorage.QueryWhereString<OptLog>(storageSql);
  129. return Ok(new { state = 200, optLogs });
  130. }
  131. catch (Exception ex)
  132. {
  133. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/get-logtmdid {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  134. return BadRequest();
  135. }
  136. }
  137. }
  138. }