ProductStatisController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelBI.Tool;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models;
  15. namespace TEAMModelBI.Controllers.Census
  16. {
  17. [Route("product")]
  18. [ApiController]
  19. public class ProductStatisController : ControllerBase
  20. {
  21. //数据容器
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. //钉钉提示信息
  24. private readonly DingDing _dingDing;
  25. private readonly Option _option;
  26. //服务产品
  27. private readonly Dictionary<string, string> prodict = new Dictionary<string, string>() { { "YMPCVCIM", "学情分析模组" }, { "IPDYZYLC", "智慧学校管理服务" }, { "3CLYJ6NP", "AClass ONE智慧学伴" }, { "IPALJ6NY", "数据储存服务空间" }, { "VABAJ6NV", "卷卡合一阅卷系统" } };
  28. public ProductStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _dingDing = dingDing;
  32. _option = option?.Value;
  33. }
  34. /// <summary>
  35. /// 统计模组数量
  36. /// </summary>
  37. /// <returns></returns>
  38. [ProducesDefaultResponseType]
  39. [HttpPost("get-allcount")]
  40. public async Task<IActionResult> GetProductSum()
  41. {
  42. var cosmosClient = _azureCosmos.GetCosmosClient();
  43. List<ProductStatis> productStatis = new List<ProductStatis>();
  44. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT c.prodinfo,c.service FROM c",requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("ProductSum") }))
  45. {
  46. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  47. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  48. {
  49. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  50. {
  51. //软体产品
  52. List<SchoolProductSumProdInfo> prodInfo = itemCount.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  53. foreach (var tempProdInfo in prodInfo)
  54. {
  55. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(tempProdInfo.prodCode));
  56. if (!string.IsNullOrEmpty($"{tempPerod}"))
  57. {
  58. tempPerod.Count += 1;
  59. }
  60. else
  61. {
  62. ProductStatis tempProd = new ProductStatis()
  63. {
  64. prodCode = tempProdInfo.prodCode,
  65. prodName = tempProdInfo.prodName,
  66. //dataType = tempProdInfo.dataType,
  67. Count = 1,
  68. };
  69. productStatis.Add(tempProd);
  70. }
  71. }
  72. //服务产品
  73. List<SchoolProductSumData> service = itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>();
  74. foreach (var ser in service)
  75. {
  76. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(ser.prodCode));
  77. if (!string.IsNullOrEmpty($"{tempPerod}"))
  78. {
  79. tempPerod.Count += 1;
  80. }
  81. else
  82. {
  83. ProductStatis tempProd = new ProductStatis()
  84. {
  85. prodCode = ser.prodCode,
  86. prodName = prodict[ser.prodCode],
  87. //dataType = "",
  88. Count = 1,
  89. };
  90. productStatis.Add(tempProd);
  91. }
  92. }
  93. }
  94. }
  95. //if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  96. //{
  97. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  98. // while (accounts.MoveNext())
  99. // {
  100. // JsonElement account = accounts.Current;
  101. // List<SchoolProductSumProdInfo> prodInfo = account.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  102. // foreach (var tempProdInfo in prodInfo)
  103. // {
  104. // ProductStatis tempPerod = productStatis.Find(x => x.prodName.Equals(tempProdInfo.prodName));
  105. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  106. // {
  107. // tempPerod.Count += 1;
  108. // }
  109. // else
  110. // {
  111. // ProductStatis tempProd = new ProductStatis()
  112. // {
  113. // prodCode = tempProdInfo.prodCode,
  114. // prodName = tempProdInfo.prodName,
  115. // dataType = tempProdInfo.dataType,
  116. // Count = 1,
  117. // };
  118. // productStatis.Add(tempProd);
  119. // }
  120. // }
  121. // }
  122. //}
  123. }
  124. return Ok(new { state = 200, productStatis });
  125. }
  126. /// <summary>
  127. /// 单个学校的模组
  128. /// </summary>
  129. /// <param name="jsonElement"></param>
  130. /// <returns></returns>
  131. [HttpPost("get-assistschool")]
  132. public async Task<IActionResult> GetAssistSchoolId(JsonElement jsonElement)
  133. {
  134. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  135. var cosmosClient = _azureCosmos.GetCosmosClient();
  136. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  137. List<SchoolProduct> schoolProducts = new List<SchoolProduct>();
  138. foreach (var scid in schoolIds)
  139. {
  140. School school = new();
  141. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(scid, new PartitionKey("Base"));
  142. if (response.Status == 200)
  143. {
  144. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  145. school = json.ToObject<School>();
  146. }
  147. SchoolProduct sProduct = new SchoolProduct() { id = scid, name = school != null ? school.name : scid };
  148. List<ProductStatis> productStatis = new();
  149. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT c.prodinfo,c.service FROM c where c.id='{scid}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("ProductSum") }))
  150. {
  151. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  152. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  153. {
  154. //软件
  155. List<SchoolProductSumProdInfo> prodInfo = itemCount.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  156. foreach (var tempProdInfo in prodInfo)
  157. {
  158. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(tempProdInfo.prodCode));
  159. if (!string.IsNullOrEmpty($"{tempPerod}"))
  160. {
  161. tempPerod.Count += 1;
  162. }
  163. else
  164. {
  165. ProductStatis tempProd = new ProductStatis()
  166. {
  167. prodCode = tempProdInfo.prodCode,
  168. prodName = tempProdInfo.prodName,
  169. //dataType = tempProdInfo.dataType,
  170. Count = 1,
  171. };
  172. productStatis.Add(tempProd);
  173. }
  174. }
  175. //服务产品
  176. List<SchoolProductSumData> service = itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>();
  177. foreach (var ser in service)
  178. {
  179. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(ser.prodCode));
  180. if (!string.IsNullOrEmpty($"{tempPerod}"))
  181. {
  182. tempPerod.Count += 1;
  183. }
  184. else
  185. {
  186. ProductStatis tempProd = new ProductStatis()
  187. {
  188. prodCode = ser.prodCode,
  189. prodName = prodict[ser.prodCode],
  190. //dataType = "",
  191. Count = 1,
  192. };
  193. productStatis.Add(tempProd);
  194. }
  195. }
  196. }
  197. }
  198. sProduct.product = productStatis;
  199. schoolProducts.Add(sProduct);
  200. }
  201. return Ok(new { state = 200, schoolProducts }) ;
  202. }
  203. public record SchoolProduct
  204. {
  205. public string id { get; set; }
  206. public string name { get; set; }
  207. public List<ProductStatis> product { get; set; }
  208. }
  209. public record ProductStatis
  210. {
  211. public string prodCode { get; set; }
  212. public string prodName { get; set; }
  213. //public string dataType { get; set; }
  214. public long Count { get; set; }
  215. }
  216. }
  217. }