123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Azure.Cosmos.Table;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using System.Text;
- using TEAMModelOS.SDK.Models.Table;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelBI.DI.BIAzureStorage;
- using TEAMModelOS.SDK.Context.BI;
- namespace TEAMModelBI.Controllers.OperateRecord
- {
- [Route("operatelog")]
- [ApiController]
- public class OperateLogController : ControllerBase
- {
- private readonly AzureStorageFactory _azureStorage;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public OperateLogController(AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 查询BI操作记录
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-record")]
- public async Task<IActionResult> GetOperateLogRecord(JsonElement jsonElement)
- {
- try
- {
- jsonElement.TryGetProperty("single", out JsonElement single);
- jsonElement.TryGetProperty("startDate", out JsonElement startDate);
- jsonElement.TryGetProperty("endDate", out JsonElement endDate);
- jsonElement.TryGetProperty("platform", out JsonElement platform);
- if(!jsonElement.TryGetProperty("reorder", out JsonElement orderby)) return BadRequest();
- jsonElement.TryGetProperty("site", out JsonElement site);
- List<BIOptLog> operateLogs = new();
- StringBuilder tableSql = new StringBuilder();
- if (!string.IsNullOrEmpty($"{single}"))
- tableSql.Append($" time eq {single}L ");
- if (!string.IsNullOrEmpty($"{startDate}"))
- tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time ge {startDate}L " : $"time ge {startDate}L ");
- if (!string.IsNullOrEmpty($"{endDate}"))
- tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time le {endDate}L " : $" time le {endDate}L ");
- if (!string.IsNullOrEmpty($"{platform}"))
- tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and platform eq '{platform}' " : $" platform eq '{platform}' ");
-
- var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
- if ($"{site}".Equals(BIConst.Global))
- table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("BIOptLog");
- //lambda 表达式排序
- operateLogs = await table.QueryWhereString<BIOptLog>(tableSql.ToString());
- switch (orderby.GetString())
- {
- case "asc":
- //lambda 排序 升序
- operateLogs.Sort((x, y) => x.time.CompareTo(y.time));
- return Ok(new { state = 200, operateLogs });
- case "desc":
- //lambda 排序 降序
- operateLogs.Sort((x, y) => y.time.CompareTo(x.time));
- return Ok(new { state = 200, operateLogs });
- default:
- return Ok(new { state = 200, operateLogs });
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /operatelog/get-record \n {ex.Message}\n{ex.StackTrace} ",GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 删除操作记录
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin")]
- [HttpPost("del-record")]
- public async Task<IActionResult> DelOperateLogRecord(JsonElement jsonElement)
- {
- try
- {
- jsonElement.TryGetProperty("startDate", out JsonElement startDate);
- jsonElement.TryGetProperty("endDate", out JsonElement endDate);
- jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
- jsonElement.TryGetProperty("site", out JsonElement site);
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- //var temp = await _azureStorage.Delete<OperateLog>(partitionKey: "OperateLog-BI", rowKey: $"{startDate}"); //删除单个
- StringBuilder operateStr = new StringBuilder($"{_tmdName}【{_tmdId}】账户删除操作记录,");
- StringBuilder tableStrWhere = new StringBuilder();
- if (!string.IsNullOrEmpty($"{rowKey}"))
- {
- tableStrWhere.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
- operateStr.Append($"删除的时间戳:{rowKey}");
- }
- else
- {
- tableStrWhere.Append($"time ge {startDate}L and time le {endDate}L ");
- operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
- }
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- if ($"{site}".Equals(BIConst.Global))
- {
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- }
- var table = tableClient.GetTableReference("BIOptLog");
- var temp = await table.DeleteStringWhere<BIOptLog>(rowKey: tableStrWhere.ToString());
- //保存操作记录
- //await _azureStorage.SaveBILog("operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "operatelog-del", operateStr?.ToString(), _dingDing, httpContext: HttpContext);
- if (temp.Count > 0)
- {
- return Ok(new { state = 200 });
- }
- else return Ok(new { state = 400 });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/del-record \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 依据醍摩豆账户查询日志记录
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-logtmdid")]
- public async Task<IActionResult> GetLogTmdId(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
- jsonElement.TryGetProperty("platform", out JsonElement platform);
- jsonElement.TryGetProperty("site", out JsonElement site);
- string storageSql = null;
- if (!string.IsNullOrEmpty($"{platform}"))
- storageSql = $"tmdId eq '{tmdId}' and platform eq '{platform}'";
- else storageSql = $"tmdId eq '{tmdId}'";
- var table = _azureStorage.GetCloudTableClient().GetTableReference("BIOptLog");
- if ($"{site}".Equals(BIConst.Global))
- table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("BIOptLog");
- var optLogs = await table.QueryWhereString<BIOptLog>(storageSql);
- return Ok(new { state = 200, optLogs });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/get-logtmdid \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- }
- }
|