BatchAreaController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. using Azure.Cosmos;
  13. using DingTalk.Api;
  14. using DingTalk.Api.Request;
  15. using DingTalk.Api.Response;
  16. namespace TEAMModeBI.Controllers.BISchool
  17. {
  18. [Route("batcharea")]
  19. [ApiController]
  20. public class BatchAreaController : ControllerBase
  21. {
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly DingDing _dingDing;
  24. private readonly Option _option;
  25. private readonly AzureStorageFactory _azureStorage;
  26. private readonly IConfiguration _configuration;
  27. public BatchAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _dingDing = dingDing;
  31. _azureStorage = azureStorage;
  32. _option = option?.Value;
  33. _configuration = configuration;
  34. }
  35. /// <summary>
  36. /// 批量创区
  37. /// </summary>
  38. /// <param name="areas"></param>
  39. /// <returns></returns>
  40. [ProducesDefaultResponseType]
  41. [HttpPost("batch-createarea")]
  42. public async Task<IActionResult> batchCreateArea(List<Area> areas)
  43. {
  44. try
  45. {
  46. List<Area> standards = new List<Area>();
  47. bool isCreate = true;
  48. if (areas.Count > 0)
  49. {
  50. foreach (Area itemarea in areas)
  51. {
  52. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  53. {
  54. if (item.standard.Equals(itemarea.standard))
  55. {
  56. standards.Add(itemarea);
  57. isCreate = false;
  58. }
  59. }
  60. if (isCreate == true)
  61. {
  62. Area addArea = new Area()
  63. {
  64. id = Guid.NewGuid().ToString(),
  65. code = $"Base-Area",
  66. name = itemarea.name,
  67. provCode = itemarea.provCode,
  68. provName = itemarea.provName,
  69. cityCode = itemarea.cityCode,
  70. cityName = itemarea.cityName,
  71. standard = itemarea.standard,
  72. standardName = itemarea.standardName,
  73. institution = itemarea.institution
  74. };
  75. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  76. }
  77. }
  78. }
  79. else return Ok(new { sate = 1 ,message="区域参数为空"});
  80. if (standards.Count > 0)
  81. return Ok(new { state = 201, message = "已有部分区域批量创建成功;标准项已重复!请检查标准项!", standards = standards });
  82. else return Ok(new { state = 200, message = "批量创区全部完成", });
  83. }
  84. catch (Exception ex)
  85. {
  86. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/batch-createarea \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  87. return BadRequest();
  88. }
  89. }
  90. public record TempArea
  91. {
  92. public string name { get; set; }
  93. public string provCode { get; set; }
  94. public string provName { get; set; }
  95. public string cityCode { get; set; }
  96. public string cityName { get; set; }
  97. public string standard { get; set; }
  98. public string standardName { get; set; }
  99. public string institution { get; set; }
  100. }
  101. }
  102. }