OperateLogController.cs 7.4 KB

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