StatsNoticeController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using System.Net.Http;
  6. using TEAMModelOS.SDK.DI;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.Models;
  9. using Microsoft.Extensions.Options;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.SDK.Context.Constant;
  13. using System;
  14. using Azure.Cosmos;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models.Cosmos.BI.BICommon;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. using DocumentFormat.OpenXml.Office2010.Excel;
  20. namespace TEAMModelBI.Controllers.BICommon
  21. {
  22. [Route("statsnotice")]
  23. [ApiController]
  24. public class StatsNoticeController : ControllerBase
  25. {
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly DingDing _dingDing;
  28. private readonly Option _option;
  29. private readonly AzureStorageFactory _azureStorage;
  30. private readonly IConfiguration _configuration;
  31. private readonly AzureServiceBusFactory _serviceBus;
  32. private readonly IHttpClientFactory _http;
  33. private readonly CoreAPIHttpService _coreAPIHttpService;
  34. private readonly IWebHostEnvironment _environment; //读取文件
  35. private readonly HttpClient _httpClient;
  36. public StatsNoticeController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment, HttpClient httpClient)
  37. {
  38. _azureCosmos = azureCosmos;
  39. _dingDing = dingDing;
  40. _azureStorage = azureStorage;
  41. _option = option?.Value;
  42. _configuration = configuration;
  43. _serviceBus = serviceBus;
  44. _http = http;
  45. _coreAPIHttpService = coreAPIHttpService;
  46. _environment = hostingEnvironment;
  47. _httpClient = httpClient;
  48. }
  49. /// <summary>
  50. /// 获取统计通知
  51. /// </summary>
  52. /// <param name="jsonElement"></param>
  53. /// <returns></returns>
  54. [ProducesDefaultResponseType]
  55. [HttpPost("get-infos")]
  56. public async Task<IActionResult> GetInfos(JsonElement jsonElement)
  57. {
  58. try
  59. {
  60. jsonElement.TryGetProperty("dateTime", out JsonElement dateTime);
  61. jsonElement.TryGetProperty("id", out JsonElement id);
  62. var cosmosClient = _azureCosmos.GetCosmosClient();
  63. string dt = "";
  64. if (string.IsNullOrEmpty($"{dateTime}"))
  65. {
  66. dt = DateTimeOffset.UtcNow.ToString("yyyyMMdd");
  67. }
  68. else
  69. {
  70. dt = $"{dateTime}";
  71. }
  72. StringBuilder sqlTxt = new($"select value(c) from c where c.day='{dt}'");
  73. if (!string.IsNullOrEmpty($"{id}"))
  74. {
  75. sqlTxt.Append($" and c.rangeId = '{id}'");
  76. }
  77. List<StatsNotice> statsNotices = new();
  78. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<StatsNotice>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("StatsNotice") }))
  79. {
  80. statsNotices.Add(item);
  81. }
  82. return Ok(new { state = RespondCode.Ok, statsNotices });
  83. }
  84. catch (Exception ex)
  85. {
  86. await _dingDing.SendBotMsg($"BI,{_option.Location},/statsnotice/get-infos \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  87. return BadRequest();
  88. }
  89. }
  90. }
  91. }