StatsNoticeController.cs 3.8 KB

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