123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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;
- 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);
- List<OptLog> operateLogs = null;
- 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 le {startDate}L " : $"time le {startDate}L ");
- if (!string.IsNullOrEmpty($"{endDate}"))
- tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and time ge {endDate}L " : $" time ge {endDate}L ");
- if (!string.IsNullOrEmpty($"{platform}"))
- tableSql.Append(!string.IsNullOrEmpty(tableSql.ToString()) ? $" and platform eq '{platform}' " : $" platform eq '{platform}' ");
- operateLogs = await _azureStorage.QueryWhereString<OptLog>(tableSql.ToString());
- return Ok(new { state = 200, operateLogs });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} \n {ex.Message}{ex.StackTrace} /operatelog/get-record",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
- {
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest(); //醍摩豆账户
- if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest(); //醍摩豆账号名称
- jsonElement.TryGetProperty("startDate", out JsonElement startDate);
- jsonElement.TryGetProperty("endDate", out JsonElement endDate);
- jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
- //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 temp = await _azureStorage.DeleteStringWhere<OptLog>(rowKey: tableStrWhere.ToString());
- //保存操作记录
- await _azureStorage.SaveLog("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 {ex.Message}{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);
- string storageSql = null;
- if (!string.IsNullOrEmpty($"{platform}"))
- storageSql = $"tmdId eq '{tmdId}' and platform eq '{platform}'";
- else storageSql = $"tmdId eq '{tmdId}'";
- List<OptLog> optLogs = new List<OptLog>();
- optLogs = await _azureStorage.QueryWhereString<OptLog>(storageSql);
- return Ok(new { state = 200, optLogs });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI, {_option.Location} /operatelog/get-logtmdid {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- }
- }
|