NormalController.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Azure.Cosmos;
  2. using DinkToPdf;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Options;
  8. using System.Net.Http;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.Context.Constant;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.DI.CoreAPI;
  16. using TEAMModelOS.SDK.Models;
  17. namespace TEAMModelBI.Controllers.RepairApi
  18. {
  19. [Route("normal")]
  20. [ApiController]
  21. public class NormalController : ControllerBase
  22. {
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly DingDing _dingDing;
  25. private readonly Option _option;
  26. private readonly AzureStorageFactory _azureStorage;
  27. private readonly IConfiguration _configuration;
  28. private readonly NotificationService _notificationService;
  29. private readonly AzureServiceBusFactory _serviceBus;
  30. private readonly IHttpClientFactory _http;
  31. private readonly CoreAPIHttpService _coreAPIHttpService;
  32. private readonly IWebHostEnvironment _environment; //读取文件
  33. public NormalController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
  34. {
  35. _azureCosmos = azureCosmos;
  36. _dingDing = dingDing;
  37. _azureStorage = azureStorage;
  38. _option = option?.Value;
  39. _configuration = configuration;
  40. _notificationService = notificationService;
  41. _serviceBus = serviceBus;
  42. _http = http;
  43. _coreAPIHttpService = coreAPIHttpService;
  44. _environment = hostingEnvironment;
  45. }
  46. /// <summary>
  47. /// 修改学区名称、标准名称、单位
  48. /// </summary>
  49. /// <param name="jsonElement"></param>
  50. /// <returns></returns>
  51. [ProducesDefaultResponseType]
  52. [HttpPost("set-areabase")]
  53. public async Task<IActionResult> SetAreaBase(JsonElement jsonElement)
  54. {
  55. if (!jsonElement.TryGetProperty("oldName", out JsonElement oldName)) return BadRequest();
  56. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  57. if(!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
  58. var cosmosClient = _azureCosmos.GetCosmosClient();
  59. string sql = $"select value(c) from c where c.name = '{oldName}'";
  60. Area area = new();
  61. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  62. {
  63. area = item;
  64. };
  65. if (!string.IsNullOrEmpty(area.id))
  66. {
  67. area.name = $"{name}";
  68. switch ($"{type}")
  69. {
  70. case "all":
  71. area.institution = $"{name}";
  72. area.standardName = $"{name}";
  73. break;
  74. case "unit":
  75. area.institution = $"{name}";
  76. break;
  77. case "standard":
  78. area.standardName = $"{name}";
  79. break;
  80. }
  81. area = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<Area>(area, area.id, new PartitionKey("Base-Area"));
  82. }
  83. return Ok(new { state = RespondCode.Ok, area });
  84. }
  85. }
  86. }