OperateLogController.cs 8.8 KB

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