OperateLogController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models.Cosmos.BI;
  12. using System.Text;
  13. using TEAMModelOS.SDK.Models.Table;
  14. using TEAMModelBI.Filter;
  15. using TEAMModelBI.Tool.Extension;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Context.BI;
  18. namespace TEAMModelBI.Controllers.BITable
  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. [ProducesDefaultResponseType]
  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. ////分开部署,就不需要,一站多用时,取消注释
  62. //if ($"{site}".Equals(BIConst.Global))
  63. // table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("BIOptLog");
  64. //lambda 表达式排序
  65. operateLogs = await table.QueryWhereString<BIOptLog>(tableSql.ToString());
  66. switch (orderby.GetString())
  67. {
  68. case "asc":
  69. //lambda 排序 升序
  70. operateLogs.Sort((x, y) => x.time.CompareTo(y.time));
  71. return Ok(new { state = 200, operateLogs });
  72. case "desc":
  73. //lambda 排序 降序
  74. operateLogs.Sort((x, y) => y.time.CompareTo(x.time));
  75. return Ok(new { state = 200, operateLogs });
  76. default:
  77. return Ok(new { state = 200, operateLogs });
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. await _dingDing.SendBotMsg($"BI,{_option.Location} /operatelog/get-record \n {ex.Message}\n{ex.StackTrace} ",GroupNames.成都开发測試群組);
  83. return BadRequest();
  84. }
  85. }
  86. /// <summary>
  87. /// 删除操作记录
  88. /// </summary>
  89. /// <param name="jsonElement"></param>
  90. /// <returns></returns>
  91. [ProducesDefaultResponseType]
  92. [AuthToken(Roles = "admin")]
  93. [HttpPost("del-record")]
  94. public async Task<IActionResult> DelOperateLogRecord(JsonElement jsonElement)
  95. {
  96. try
  97. {
  98. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  99. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  100. jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
  101. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  102. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  103. //var temp = await _azureStorage.Delete<OperateLog>(partitionKey: "OperateLog-BI", rowKey: $"{startDate}"); //删除单个
  104. StringBuilder operateStr = new($"{_tmdName}【{_tmdId}】账户删除操作记录,");
  105. StringBuilder tableStrWhere = new();
  106. if (!string.IsNullOrEmpty($"{rowKey}"))
  107. {
  108. tableStrWhere.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
  109. operateStr.Append($"删除的时间戳:{rowKey}");
  110. }
  111. else
  112. {
  113. tableStrWhere.Append($"time ge {startDate}L and time le {endDate}L ");
  114. operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
  115. }
  116. var tableClient = _azureStorage.GetCloudTableClient();
  117. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  118. //if ($"{site}".Equals(BIConst.Global))
  119. //{
  120. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  121. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  122. //}
  123. var table = tableClient.GetTableReference("BIOptLog");
  124. var temp = await table.DeleteStringWhere<BIOptLog>(rowKey: tableStrWhere.ToString());
  125. //保存操作记录
  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. }