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