OperateLogController.cs 6.5 KB

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