OperateLogController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 TEAMModelBI.Tool.Context;
  19. namespace TEAMModelBI.Controllers.OperateRecord
  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.GlobalSite))
  62. table = _azureStorage.GetCloudTableClient(BIConst.GlobalSite).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 table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  116. if ($"{site}".Equals(BIConst.GlobalSite))
  117. table = _azureStorage.GetCloudTableClient(BIConst.GlobalSite).GetTableReference("BIOptLog");
  118. var temp = await table.DeleteStringWhere<BIOptLog>(rowKey: tableStrWhere.ToString());
  119. //保存操作记录
  120. await _azureStorage.SaveBILog("operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
  121. if (temp.Count > 0)
  122. {
  123. return Ok(new { state = 200 });
  124. }
  125. else return Ok(new { state = 400 });
  126. }
  127. catch (Exception ex)
  128. {
  129. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/del-record \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  130. return BadRequest();
  131. }
  132. }
  133. /// <summary>
  134. /// 依据醍摩豆账户查询日志记录
  135. /// </summary>
  136. /// <param name="jsonElement"></param>
  137. /// <returns></returns>
  138. [ProducesDefaultResponseType]
  139. [HttpPost("get-logtmdid")]
  140. public async Task<IActionResult> GetLogTmdId(JsonElement jsonElement)
  141. {
  142. try
  143. {
  144. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  145. jsonElement.TryGetProperty("platform", out JsonElement platform);
  146. jsonElement.TryGetProperty("site", out JsonElement site);
  147. string storageSql = null;
  148. if (!string.IsNullOrEmpty($"{platform}"))
  149. storageSql = $"tmdId eq '{tmdId}' and platform eq '{platform}'";
  150. else storageSql = $"tmdId eq '{tmdId}'";
  151. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
  152. if ($"{site}".Equals(BIConst.GlobalSite))
  153. table = _azureStorage.GetCloudTableClient(BIConst.GlobalSite).GetTableReference("BIOptLog");
  154. var optLogs = await table.QueryWhereString<BIOptLog>(storageSql);
  155. return Ok(new { state = 200, optLogs });
  156. }
  157. catch (Exception ex)
  158. {
  159. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/get-logtmdid \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  160. return BadRequest();
  161. }
  162. }
  163. }
  164. }