ProductStatisController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /// <summary>
  27. /// 软体产品
  28. /// </summary>
  29. private readonly Dictionary<string, string> _serials = new Dictionary<string, string>() { { "3222NIYD", "ezStation 2" }, { "J223IZ6M", "HiTeach STD" }, { "3222C6D2", "HiTeach TBL" }, { "J223IZAM", "HiTeach PRO" }, { "J2236ZCX", "HiTeach Lite" }, { "3222DNG2", "HiTeach Mobile" }, { "3222IAVN", "HiTeach Premium" }, { "BYJ6LZ6Z", "HiTeach5" } };
  30. /// <summary>
  31. /// 服务产品
  32. /// </summary>
  33. private readonly Dictionary<string, string> _services = new Dictionary<string, string>() { { "YMPCVCIM", "学情分析模组" }, { "IPDYZYLC", "智慧学校管理服务" }, { "3CLYJ6NP", "AClass ONE智慧学伴" }, { "IPALJ6NY", "数据储存服务空间" }, { "VABAJ6NV", "卷卡合一阅卷系统" } };
  34. public ProductStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  35. {
  36. _azureCosmos = azureCosmos;
  37. _dingDing = dingDing;
  38. _option = option?.Value;
  39. }
  40. /// <summary>
  41. /// 统计模组数量
  42. /// </summary>
  43. /// <returns></returns>
  44. [ProducesDefaultResponseType]
  45. [HttpPost("get-allcount")]
  46. public async Task<IActionResult> GetProductSum()
  47. {
  48. var cosmosClient = _azureCosmos.GetCosmosClient();
  49. List<ProductStatis> productStatis = new List<ProductStatis>();
  50. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT c.serial,c.service FROM c",requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("ProductSum") }))
  51. {
  52. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  53. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  54. {
  55. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  56. {
  57. ////所有的产品
  58. //List<SchoolProductSumProdInfo> prodInfo = itemCount.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  59. //foreach (var tempProdInfo in prodInfo)
  60. //{
  61. // ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(tempProdInfo.prodCode));
  62. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  63. // {
  64. // tempPerod.Count += 1;
  65. // }
  66. // else
  67. // {
  68. // ProductStatis tempProd = new ProductStatis()
  69. // {
  70. // prodCode = tempProdInfo.prodCode,
  71. // prodName = tempProdInfo.prodName,
  72. // //dataType = tempProdInfo.dataType,
  73. // Count = 1,
  74. // };
  75. // productStatis.Add(tempProd);
  76. // }
  77. //}
  78. //软体产品
  79. List<SchoolProductSumData> tempSerials = itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>();
  80. foreach (var serial in tempSerials)
  81. {
  82. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(serial.prodCode));
  83. if (!string.IsNullOrEmpty($"{tempPerod}"))
  84. {
  85. tempPerod.Count += 1;
  86. }
  87. else
  88. {
  89. ProductStatis tempProd = new ProductStatis()
  90. {
  91. prodCode = serial.prodCode,
  92. prodName = _serials[serial.prodCode],
  93. //dataType = "",
  94. Count = 1,
  95. };
  96. productStatis.Add(tempProd);
  97. }
  98. }
  99. //服务产品
  100. List<SchoolProductSumData> service = itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>();
  101. foreach (var ser in service)
  102. {
  103. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(ser.prodCode));
  104. if (!string.IsNullOrEmpty($"{tempPerod}"))
  105. {
  106. tempPerod.Count += 1;
  107. }
  108. else
  109. {
  110. ProductStatis tempProd = new ProductStatis()
  111. {
  112. prodCode = ser.prodCode,
  113. prodName = _services[ser.prodCode],
  114. //dataType = "",
  115. Count = 1,
  116. };
  117. productStatis.Add(tempProd);
  118. }
  119. }
  120. }
  121. }
  122. //if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  123. //{
  124. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  125. // while (accounts.MoveNext())
  126. // {
  127. // JsonElement account = accounts.Current;
  128. // List<SchoolProductSumProdInfo> prodInfo = account.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  129. // foreach (var tempProdInfo in prodInfo)
  130. // {
  131. // ProductStatis tempPerod = productStatis.Find(x => x.prodName.Equals(tempProdInfo.prodName));
  132. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  133. // {
  134. // tempPerod.Count += 1;
  135. // }
  136. // else
  137. // {
  138. // ProductStatis tempProd = new ProductStatis()
  139. // {
  140. // prodCode = tempProdInfo.prodCode,
  141. // prodName = tempProdInfo.prodName,
  142. // dataType = tempProdInfo.dataType,
  143. // Count = 1,
  144. // };
  145. // productStatis.Add(tempProd);
  146. // }
  147. // }
  148. // }
  149. //}
  150. }
  151. return Ok(new { state = 200, productStatis });
  152. }
  153. /// <summary>
  154. /// 单个学校的模组
  155. /// </summary>
  156. /// <param name="jsonElement"></param>
  157. /// <returns></returns>
  158. [HttpPost("get-assistschool")]
  159. public async Task<IActionResult> GetAssistSchoolId(JsonElement jsonElement)
  160. {
  161. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  162. var cosmosClient = _azureCosmos.GetCosmosClient();
  163. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  164. List<SchoolProduct> schoolProducts = new List<SchoolProduct>();
  165. foreach (var scid in schoolIds)
  166. {
  167. School school = new();
  168. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(scid, new PartitionKey("Base"));
  169. if (response.Status == 200)
  170. {
  171. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  172. school = json.ToObject<School>();
  173. }
  174. SchoolProduct sProduct = new SchoolProduct() { id = scid, name = school != null ? school.name : scid };
  175. List<ProductStatis> productStatis = new();
  176. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT c.serial,c.service FROM c where c.id='{scid}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("ProductSum") }))
  177. {
  178. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  179. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  180. {
  181. ////所有的产品信息
  182. //List<SchoolProductSumProdInfo> prodInfo = itemCount.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  183. //foreach (var tempProdInfo in prodInfo)
  184. //{
  185. // ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(tempProdInfo.prodCode));
  186. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  187. // {
  188. // tempPerod.Count += 1;
  189. // }
  190. // else
  191. // {
  192. // ProductStatis tempProd = new ProductStatis()
  193. // {
  194. // prodCode = tempProdInfo.prodCode,
  195. // prodName = tempProdInfo.prodName,
  196. // //dataType = tempProdInfo.dataType,
  197. // Count = 1,
  198. // };
  199. // productStatis.Add(tempProd);
  200. // }
  201. //}
  202. //软体产品
  203. List<SchoolProductSumData> tempSerials = itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>();
  204. foreach (var serial in tempSerials)
  205. {
  206. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(serial.prodCode));
  207. if (!string.IsNullOrEmpty($"{tempPerod}"))
  208. {
  209. tempPerod.Count += 1;
  210. }
  211. else
  212. {
  213. ProductStatis tempProd = new ProductStatis()
  214. {
  215. prodCode = serial.prodCode,
  216. prodName = _serials[serial.prodCode],
  217. //dataType = "",
  218. Count = 1,
  219. };
  220. productStatis.Add(tempProd);
  221. }
  222. }
  223. //服务产品
  224. List<SchoolProductSumData> service = itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>();
  225. foreach (var ser in service)
  226. {
  227. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(ser.prodCode));
  228. if (!string.IsNullOrEmpty($"{tempPerod}"))
  229. {
  230. tempPerod.Count += 1;
  231. }
  232. else
  233. {
  234. ProductStatis tempProd = new ProductStatis()
  235. {
  236. prodCode = ser.prodCode,
  237. prodName = _services[ser.prodCode],
  238. //dataType = "",
  239. Count = 1,
  240. };
  241. productStatis.Add(tempProd);
  242. }
  243. }
  244. }
  245. }
  246. sProduct.product = productStatis;
  247. schoolProducts.Add(sProduct);
  248. }
  249. return Ok(new { state = 200, schoolProducts }) ;
  250. }
  251. public record SchoolProduct
  252. {
  253. public string id { get; set; }
  254. public string name { get; set; }
  255. public List<ProductStatis> product { get; set; }
  256. }
  257. public record ProductStatis
  258. {
  259. public string prodCode { get; set; }
  260. public string prodName { get; set; }
  261. //public string dataType { get; set; }
  262. public long Count { get; set; }
  263. }
  264. }
  265. }