OperateLogController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 TEAMModeBI.Filter;
  16. namespace TEAMModeBI.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. jsonElement.TryGetProperty("single", out JsonElement single);
  40. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  41. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  42. jsonElement.TryGetProperty("platform", out JsonElement platform);
  43. List<OptLog> operateLogs = null;
  44. StringBuilder tableSql = new StringBuilder();
  45. if (!string.IsNullOrEmpty($"{single}"))
  46. tableSql.Append($" time eq {single}L ");
  47. if (!string.IsNullOrEmpty($"{startDate}"))
  48. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time le {startDate}L " : $"time le {startDate}L ");
  49. if (!string.IsNullOrEmpty($"{endDate}"))
  50. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time ge {endDate}L " : $" time ge {endDate}L ");
  51. if (!string.IsNullOrEmpty($"{platform}"))
  52. tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and platform eq '{platform}' " : $" platform eq '{platform}' ");
  53. operateLogs = await _azureStorage.QueryWhereString<OptLog>(tableSql.ToString());
  54. return Ok(new { state = 200, operateLogs });
  55. }
  56. /// <summary>
  57. /// 删除操作记录
  58. /// </summary>
  59. /// <param name="jsonElement"></param>
  60. /// <returns></returns>
  61. [ProducesDefaultResponseType]
  62. [AuthToken(Roles = "assist")]
  63. [HttpPost("del-record")]
  64. public async Task<IActionResult> DelOperateLogRecord(JsonElement jsonElement)
  65. {
  66. try
  67. {
  68. if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest(); //醍摩豆账户
  69. if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest(); //醍摩豆账号名称
  70. jsonElement.TryGetProperty("startDate", out JsonElement startDate);
  71. jsonElement.TryGetProperty("endDate", out JsonElement endDate);
  72. jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
  73. //var temp = await _azureStorage.Delete<OperateLog>(partitionKey: "OperateLog-BI", rowKey: $"{startDate}"); //删除单个
  74. StringBuilder operateStr = new StringBuilder($"{_tmdName}【{_tmdId}】账户删除操作记录,");
  75. StringBuilder tableStrWhere = new StringBuilder();
  76. if (!string.IsNullOrEmpty($"{rowKey}"))
  77. {
  78. tableStrWhere.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
  79. operateStr.Append($"删除的时间戳:{rowKey}");
  80. }
  81. else
  82. {
  83. tableStrWhere.Append($"time le {startDate}L and time ge {endDate}L ");
  84. operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
  85. }
  86. var temp = await _azureStorage.DeleteStringWhere<OptLog>(rowKey: tableStrWhere.ToString());
  87. //保存操作记录
  88. await _azureStorage.SaveLog("operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
  89. if (temp.Count > 0)
  90. {
  91. return Ok(new { state = 200 });
  92. }
  93. else return Ok(new { state = 400 });
  94. }
  95. catch (Exception ex)
  96. {
  97. await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/del-operatelogbydate {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  98. return BadRequest();
  99. }
  100. }
  101. public async Task<IActionResult> GetLogTmdId(JsonElement jsonElement)
  102. {
  103. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmd)) return BadRequest();
  104. return Ok(new { state = 200 });
  105. }
  106. }
  107. }