123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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;
- namespace TEAMModeBI.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-operatelogbydate")]
- public async Task<IActionResult> GetOperateLogByDate(JsonElement jsonElement)
- {
- try
- {
- jsonElement.TryGetProperty("startDate", out JsonElement startDate);
- jsonElement.TryGetProperty("endDate", out JsonElement endDate);
- List<OperateLog> operateLogs = null;
- StringBuilder tableSql = new StringBuilder();
- if (!string.IsNullOrEmpty($"{startDate}"))
- {
- operateLogs = await _azureStorage.QueryWhereString<OperateLog>($"RowKey {QueryComparisons.GreaterThanOrEqual} '{startDate}'");
- }
- else if (!string.IsNullOrEmpty($"{startDate}") && !string.IsNullOrEmpty($"{endDate}"))
- {
- operateLogs = await _azureStorage.QueryWhereString<OperateLog>($"RowKey {QueryComparisons.GreaterThanOrEqual} '{startDate}' {TableOperators.And} RowKey {QueryComparisons.LessThanOrEqual} '{endDate}'");
- }
- else
- {
- operateLogs = await _azureStorage.QueryWhereString<OperateLog>();
- //operateLogs = await _azureStorage.FindListByDict<OperateLog>(dic);
- }
- return Ok(new { state = 200, operateLogs });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /operatelog/get-operatelogbydate {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 删除操作记录
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("del-operatelogbydate")]
- public async Task<IActionResult> DelOperateLogByDate(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);
- //操作记录
- OperateLog operateLog = new OperateLog();
- string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
- operateLog.PartitionKey = "OperateLog-BI";
- operateLog.RowKey = blobOrTable;
- operateLog.recordID = blobOrTable;
- operateLog.platformSource = "BI";
- operateLog.tmdId = $"{_tmdId}";
- operateLog.tmdName = $"{_tmdName}";
- operateLog.visitApi = "/operatelog/del-operatelogbydate";
- operateLog.operateTime = DateTime.Now;
- //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($"RowKey {QueryComparisons.GreaterThanOrEqual} '{startDate}'{TableOperators.And} RowKey {QueryComparisons.LessThanOrEqual} '{endDate}'");
- operateStr.Append($"删除的时间戳,开始——结束时间戳:{startDate}-{endDate}");
- }
- var temp = await _azureStorage.DeleteStringWhere<OperateLog>(rowKey: tableStrWhere.ToString());
- operateLog.operateDescribe = operateStr.ToString();
- await _azureStorage.Save<OperateLog>(operateLog); //保存操作记录
- 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-operatelogbydate {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- }
- }
|