ProductController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System.Collections.Generic;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.Context.BI;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Models;
  14. namespace TEAMModelBI.Controllers.BISchool
  15. {
  16. [Route("product")]
  17. [ApiController]
  18. public class ProductController : ControllerBase
  19. {
  20. //数据容器
  21. private readonly AzureCosmosFactory _azureCosmos;
  22. private readonly AzureStorageFactory _azureStorage;
  23. //钉钉提示信息
  24. private readonly DingDing _dingDing;
  25. private readonly Option _option;
  26. private readonly IConfiguration _configuration;
  27. public ProductController(AzureCosmosFactory azureCosmos,AzureStorageFactory azureStorage,DingDing dingDing,IOptionsSnapshot<Option> option,IConfiguration configuration)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _azureStorage= azureStorage;
  31. _dingDing= dingDing;
  32. _option = option?.Value;
  33. _configuration = configuration;
  34. }
  35. /// <summary>
  36. /// 依据学校ID查询产品信息
  37. /// </summary>
  38. /// <param name="jsonElement"></param>
  39. /// <returns></returns>
  40. [HttpPost("get-school")]
  41. public async Task<IActionResult> GetSchoolSum(JsonElement jsonElement)
  42. {
  43. if (!jsonElement.TryGetProperty("schoolCode", out JsonElement schoolCode)) return BadRequest();
  44. jsonElement.TryGetProperty("site", out JsonElement site);
  45. var clientContainer = _azureCosmos.GetCosmosClient();
  46. if ($"{site}".Equals(BIConst.Global))
  47. clientContainer = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  48. List<SchoolProductSerial> serials = new List<SchoolProductSerial>(); //软体
  49. List<SchoolProductService> services = new List<SchoolProductService>(); //服务
  50. List<SchoolProductHard> hards = new List<SchoolProductHard>(); //硬体
  51. SchoolProductSum productSum = new SchoolProductSum(); //产品状态
  52. List<SchoolProductSumProdInfo> prodinfo = new List<SchoolProductSumProdInfo>(); //学校的产品信息
  53. List<SchoolProductSumData> serialRecord = new List<SchoolProductSumData>(); //软体购买记录
  54. List<SchoolProductSumDataService> serviceRecord = new List<SchoolProductSumDataService>(); //服务购买记录
  55. List<SchoolProductSumDataHard> hardRecord = new List<SchoolProductSumDataHard>(); //硬体购买记录
  56. //取产品的数量
  57. var response = await clientContainer.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolCode.ToString(), new PartitionKey($"ProductSum"));
  58. if (response.Status == 200)
  59. {
  60. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  61. productSum = json.ToObject<SchoolProductSum>();
  62. prodinfo = productSum.prodinfo;
  63. serialRecord = productSum.serial;
  64. serviceRecord = productSum.service;
  65. hardRecord = productSum.hard;
  66. }
  67. //软体
  68. await foreach (var item in clientContainer.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: "SELECT * FROM c WHERE c.dataType='serial'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Product-{schoolCode}") }))
  69. {
  70. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  71. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  72. {
  73. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  74. {
  75. serials.Add(obj.ToObject<SchoolProductSerial>());
  76. }
  77. }
  78. }
  79. //服务
  80. await foreach (var item in clientContainer.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: "SELECT * FROM c WHERE c.dataType='service'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Product-{schoolCode}") }))
  81. {
  82. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  83. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  84. {
  85. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  86. {
  87. services.Add(obj.ToObject<SchoolProductService>());
  88. }
  89. }
  90. }
  91. //硬体
  92. await foreach (var item in clientContainer.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: "SELECT * FROM c WHERE c.dataType='hard'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Product-{schoolCode}") }))
  93. {
  94. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  95. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  96. {
  97. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  98. {
  99. hards.Add(obj.ToObject<SchoolProductHard>());
  100. }
  101. }
  102. }
  103. //学校教室
  104. List<Room> rooms = new List<Room>();
  105. await foreach (var item in clientContainer.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Room>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Room-{schoolCode}") }))
  106. {
  107. rooms.Add(item);
  108. }
  109. return Ok(new { state = 200, serials, services, hards, productSum , rooms });
  110. }
  111. }
  112. }