ProductStatisController.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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.Context.BI;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Models;
  16. namespace TEAMModelBI.Controllers.Census
  17. {
  18. [Route("product")]
  19. [ApiController]
  20. public class ProductStatisController : ControllerBase
  21. {
  22. //数据容器
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. //钉钉提示信息
  25. private readonly DingDing _dingDing;
  26. private readonly Option _option;
  27. /// <summary>
  28. /// 软体产品
  29. /// </summary>
  30. private readonly Dictionary<string, string> _serials = new() { { "3222NIYD", "ezStation 2" }, { "J223IZ6M", "HiTeach STD" }, { "3222C6D2", "HiTeach TBL" }, { "J223IZAM", "HiTeach PRO" }, { "J2236ZCX", "HiTeach Lite" }, { "3222DNG2", "HiTeach Mobile" }, { "3222IAVN", "HiTeach Premium" }, { "BYJ6LZ6Z", "HiTeach5" } };
  31. /// <summary>
  32. /// 服务产品
  33. /// </summary>
  34. private readonly Dictionary<string, string> _services = new() { { "YMPCVCIM", "学情分析模组" }, { "IPDYZYLC", "智慧学校管理服务" }, { "3CLYJ6NP", "AClass ONE智慧学伴" }, { "IPALJ6NY", "数据储存服务空间" }, { "VABAJ6NV", "卷卡合一阅卷系统" } };
  35. public ProductStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  36. {
  37. _azureCosmos = azureCosmos;
  38. _dingDing = dingDing;
  39. _option = option?.Value;
  40. }
  41. /// <summary>
  42. /// 统计模组数量 已对接
  43. /// </summary>
  44. /// <returns></returns>
  45. [ProducesDefaultResponseType]
  46. [HttpPost("get-allcount")]
  47. public async Task<IActionResult> GetProductSum(JsonElement jsonElement)
  48. {
  49. jsonElement.TryGetProperty("site", out JsonElement site);
  50. var cosmosClient = _azureCosmos.GetCosmosClient();
  51. if ($"{site}".Equals(BIConst.Global))
  52. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  53. List<ProductStatis> productStatis = new List<ProductStatis>();
  54. 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") }))
  55. {
  56. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  57. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  58. {
  59. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  60. {
  61. ////所有的产品
  62. //List<SchoolProductSumProdInfo> prodInfo = itemCount.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  63. //foreach (var tempProdInfo in prodInfo)
  64. //{
  65. // ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(tempProdInfo.prodCode));
  66. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  67. // {
  68. // tempPerod.Count += 1;
  69. // }
  70. // else
  71. // {
  72. // ProductStatis tempProd = new ProductStatis()
  73. // {
  74. // prodCode = tempProdInfo.prodCode,
  75. // prodName = tempProdInfo.prodName,
  76. // //dataType = tempProdInfo.dataType,
  77. // Count = 1,
  78. // };
  79. // productStatis.Add(tempProd);
  80. // }
  81. //}
  82. //软体产品
  83. List<SchoolProductSumData> tempSerials = itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>();
  84. foreach (var serial in tempSerials)
  85. {
  86. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(serial.prodCode));
  87. if (!string.IsNullOrEmpty($"{tempPerod}"))
  88. {
  89. tempPerod.Count += 1;
  90. }
  91. else
  92. {
  93. ProductStatis tempProd = new ProductStatis()
  94. {
  95. prodCode = serial.prodCode,
  96. prodName = _serials[serial.prodCode],
  97. //dataType = "",
  98. Count = 1,
  99. };
  100. productStatis.Add(tempProd);
  101. }
  102. }
  103. //服务产品
  104. List<SchoolProductSumData> service = itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>();
  105. foreach (var ser in service)
  106. {
  107. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(ser.prodCode));
  108. if (!string.IsNullOrEmpty($"{tempPerod}"))
  109. {
  110. tempPerod.Count += 1;
  111. }
  112. else
  113. {
  114. ProductStatis tempProd = new ProductStatis()
  115. {
  116. prodCode = ser.prodCode,
  117. prodName = _services[ser.prodCode],
  118. //dataType = "",
  119. Count = 1,
  120. };
  121. productStatis.Add(tempProd);
  122. }
  123. }
  124. }
  125. }
  126. //if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  127. //{
  128. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  129. // while (accounts.MoveNext())
  130. // {
  131. // JsonElement account = accounts.Current;
  132. // List<SchoolProductSumProdInfo> prodInfo = account.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  133. // foreach (var tempProdInfo in prodInfo)
  134. // {
  135. // ProductStatis tempPerod = productStatis.Find(x => x.prodName.Equals(tempProdInfo.prodName));
  136. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  137. // {
  138. // tempPerod.Count += 1;
  139. // }
  140. // else
  141. // {
  142. // ProductStatis tempProd = new ProductStatis()
  143. // {
  144. // prodCode = tempProdInfo.prodCode,
  145. // prodName = tempProdInfo.prodName,
  146. // dataType = tempProdInfo.dataType,
  147. // Count = 1,
  148. // };
  149. // productStatis.Add(tempProd);
  150. // }
  151. // }
  152. // }
  153. //}
  154. }
  155. return Ok(new { state = 200, productStatis });
  156. }
  157. /// <summary>
  158. /// 依据学校ID查询产品信息 //已对接
  159. /// </summary>
  160. /// <param name="jsonElement"></param>
  161. /// <returns></returns>
  162. [ProducesDefaultResponseType]
  163. [HttpPost("get-school")]
  164. public async Task<IActionResult> GetSchoolSum(JsonElement jsonElement)
  165. {
  166. if (!jsonElement.TryGetProperty("schoolCode", out JsonElement schoolCode)) return BadRequest();
  167. jsonElement.TryGetProperty("site", out JsonElement site);
  168. var clientContainer = _azureCosmos.GetCosmosClient();
  169. if ($"{site}".Equals(BIConst.Global))
  170. clientContainer = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  171. List<SchoolProductSerial> serials = new(); //软体
  172. List<SchoolProductService> services = new(); //服务
  173. List<SchoolProductHard> hards = new(); //硬体
  174. SchoolProductSum productSum = new(); //产品状态
  175. List<SchoolProductSumProdInfo> prodinfo = new(); //学校的产品信息
  176. List<SchoolProductSumData> serialRecord = new(); //软体购买记录
  177. List<SchoolProductSumDataService> serviceRecord = new(); //服务购买记录
  178. List<SchoolProductSumDataHard> hardRecord = new(); //硬体购买记录
  179. //取产品的数量
  180. var response = await clientContainer.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolCode.ToString(), new PartitionKey($"ProductSum"));
  181. if (response.Status == 200)
  182. {
  183. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  184. productSum = json.ToObject<SchoolProductSum>();
  185. prodinfo = productSum.prodinfo;
  186. serialRecord = productSum.serial;
  187. serviceRecord = productSum.service;
  188. hardRecord = productSum.hard;
  189. }
  190. //软体
  191. 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}") }))
  192. {
  193. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  194. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  195. {
  196. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  197. {
  198. serials.Add(obj.ToObject<SchoolProductSerial>());
  199. }
  200. }
  201. }
  202. //服务
  203. 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}") }))
  204. {
  205. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  206. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  207. {
  208. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  209. {
  210. services.Add(obj.ToObject<SchoolProductService>());
  211. }
  212. }
  213. }
  214. //硬体
  215. 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}") }))
  216. {
  217. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  218. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  219. {
  220. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  221. {
  222. hards.Add(obj.ToObject<SchoolProductHard>());
  223. }
  224. }
  225. }
  226. //学校教室
  227. List<Room> rooms = new();
  228. 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}") }))
  229. {
  230. rooms.Add(item);
  231. }
  232. return Ok(new { state = 200, serials, services, hards, productSum, rooms });
  233. }
  234. /// <summary>
  235. /// 顾问单个学校的模组
  236. /// </summary>
  237. /// <param name="jsonElement"></param>
  238. /// <returns></returns>
  239. [ProducesDefaultResponseType]
  240. [HttpPost("get-assistschool")]
  241. public async Task<IActionResult> GetAssistSchoolId(JsonElement jsonElement)
  242. {
  243. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  244. jsonElement.TryGetProperty("site", out JsonElement site);
  245. var cosmosClient = _azureCosmos.GetCosmosClient();
  246. if ($"{site}".Equals(BIConst.Global))
  247. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  248. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  249. List<SchoolProduct> schoolProducts = new();
  250. foreach (var scid in schoolIds)
  251. {
  252. School school = new();
  253. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(scid, new PartitionKey("Base"));
  254. if (response.Status == 200)
  255. {
  256. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  257. school = json.ToObject<School>();
  258. }
  259. SchoolProduct sProduct = new() { id = scid, name = school != null ? school.name : scid };
  260. List<ProductStatis> productStatis = new();
  261. 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") }))
  262. {
  263. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  264. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  265. {
  266. ////所有的产品信息
  267. //List<SchoolProductSumProdInfo> prodInfo = itemCount.GetProperty("prodinfo").ToObject<List<SchoolProductSumProdInfo>>();
  268. //foreach (var tempProdInfo in prodInfo)
  269. //{
  270. // ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(tempProdInfo.prodCode));
  271. // if (!string.IsNullOrEmpty($"{tempPerod}"))
  272. // {
  273. // tempPerod.Count += 1;
  274. // }
  275. // else
  276. // {
  277. // ProductStatis tempProd = new ProductStatis()
  278. // {
  279. // prodCode = tempProdInfo.prodCode,
  280. // prodName = tempProdInfo.prodName,
  281. // //dataType = tempProdInfo.dataType,
  282. // Count = 1,
  283. // };
  284. // productStatis.Add(tempProd);
  285. // }
  286. //}
  287. //软体产品
  288. List<SchoolProductSumData> tempSerials = itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>();
  289. foreach (var serial in tempSerials)
  290. {
  291. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(serial.prodCode));
  292. if (!string.IsNullOrEmpty($"{tempPerod}"))
  293. {
  294. tempPerod.Count += 1;
  295. }
  296. else
  297. {
  298. ProductStatis tempProd = new()
  299. {
  300. prodCode = serial.prodCode,
  301. prodName = _serials[serial.prodCode],
  302. //dataType = "",
  303. Count = 1,
  304. };
  305. productStatis.Add(tempProd);
  306. }
  307. }
  308. //服务产品
  309. List<SchoolProductSumData> service = itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>();
  310. foreach (var ser in service)
  311. {
  312. ProductStatis tempPerod = productStatis.Find(x => x.prodCode.Equals(ser.prodCode));
  313. if (!string.IsNullOrEmpty($"{tempPerod}"))
  314. {
  315. tempPerod.Count += 1;
  316. }
  317. else
  318. {
  319. ProductStatis tempProd = new()
  320. {
  321. prodCode = ser.prodCode,
  322. prodName = _services[ser.prodCode],
  323. //dataType = "",
  324. Count = 1,
  325. };
  326. productStatis.Add(tempProd);
  327. }
  328. }
  329. }
  330. }
  331. sProduct.product = productStatis;
  332. schoolProducts.Add(sProduct);
  333. }
  334. return Ok(new { state = 200, schoolProducts }) ;
  335. }
  336. /// <summary>
  337. /// 所有模组统计
  338. /// </summary>
  339. /// <returns></returns>
  340. [ProducesDefaultResponseType]
  341. [HttpPost("get-moduleanalys")]
  342. public async Task<IActionResult> GetModuleAnalys(JsonElement jsonElement)
  343. {
  344. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  345. jsonElement.TryGetProperty("site", out JsonElement site);
  346. var cosmosClient = _azureCosmos.GetCosmosClient();
  347. if ($"{site}".Equals(BIConst.Global))
  348. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  349. long buyServiceCount = 0; //所有购买服务数量
  350. long buySerialsCount = 0; //所有购买软体数量
  351. long lastYearServiceCount = 0; //去年购买服务数量
  352. long yearServiceCount = 0; //今年购买服务数量
  353. long lastYearSerialsCount = 0; //去年购买软体数量
  354. long yearSerialsCount = 0; //今年购买软体数量
  355. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year", false); //计算去年开始/结束时间
  356. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year", false); //计算今年开始/结束时间
  357. if (!string.IsNullOrEmpty($"{tmdId}"))
  358. {
  359. List<RecSchoolAnalys> schoolAnalys = new();
  360. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  361. foreach (var scId in schoolIds)
  362. {
  363. //软体产品
  364. List<SchoolProductSumData> Serials = new();
  365. //软体产品
  366. List<SchoolProductSumData> Services = new();
  367. School school = new();
  368. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(scId, new PartitionKey("Base"));
  369. if (response.Status == 200)
  370. {
  371. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  372. school = json.ToObject<School>();
  373. }
  374. 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") }))
  375. {
  376. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  377. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  378. {
  379. //软体产品
  380. Serials.AddRange(itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>().Where(x => x.avaliable > 0).ToList());
  381. //服务产品
  382. Services.AddRange(itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>().Where(x => x.avaliable > 0).ToList());
  383. }
  384. }
  385. long tempBuySerc = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.dataType='service' and c.pk='Product'", $"Product-{scId}");
  386. long tempBuySeri = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.dataType='serial' and c.pk='Product'", $"Product-{scId}");
  387. string sqlTxt = "select value(count(c.id)) from c where c.pk='Product' and c.dataType='{0}' and c.regDate>={1} and c.regDate<={2}";
  388. long tempLastYearSerc = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "service", lastYearStart, lastYearEnd), $"Product-{scId}"); //计算去年购买的服务数量
  389. long tempyearSerc = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "service", yearStart, yearEnd), $"Product-{scId}"); //今年购买的服务数量
  390. long tempLastYearSeri = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "serial", lastYearStart, lastYearEnd), $"Product-{scId}"); //计算去年购买的软体数量
  391. long tempYearSeri = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "serial", yearStart, yearEnd), $"Product-{scId}"); //今年购买的软体数量
  392. schoolAnalys.Add(new RecSchoolAnalys { id = school.id, name = school.name, picture = school.picture, size = school.size, scale = school.scale, buySerc = tempBuySerc, lastYearSerc = tempLastYearSerc, yearSerc = tempyearSerc, buySeri = tempBuySeri, lastYearSeri = tempLastYearSeri, yearSeri = tempYearSeri });
  393. lastYearServiceCount += tempLastYearSerc;
  394. yearServiceCount += tempyearSerc;
  395. buyServiceCount += tempBuySerc;
  396. lastYearSerialsCount += tempLastYearSeri;
  397. yearSerialsCount += tempYearSeri;
  398. buySerialsCount += tempBuySeri;
  399. }
  400. return Ok(new { state = 200, buyServiceCount, buySerialsCount, lastYearServiceCount, yearServiceCount, lastYearSerialsCount, yearSerialsCount, schoolAnalys });
  401. }
  402. else
  403. {
  404. //软体产品
  405. List<SchoolProductSumData> Serials = new();
  406. //软体产品
  407. List<SchoolProductSumData> Services = new();
  408. 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") }))
  409. {
  410. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  411. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  412. {
  413. //软体产品
  414. Serials.AddRange(itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>().Where(x => x.avaliable > 0).ToList());
  415. //服务产品
  416. Services.AddRange(itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>().Where(x => x.avaliable > 0).ToList());
  417. }
  418. }
  419. var serialsCout = Serials.GroupBy(kv => new { kv.prodCode }).Select(y => new { key = y.Key.prodCode, value = y.Count() }).ToList();
  420. var servicesCout = Services.GroupBy(kv => new { kv.prodCode }).Select(y => new { key = y.Key.prodCode, value = y.Count() }).ToList();
  421. string containers = "School";
  422. buyServiceCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, "select value(count(c.id)) from c where c.dataType='service' and c.pk='Product'");
  423. buySerialsCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, "select value(count(c.id)) from c where c.dataType='serial' and c.pk='Product'");
  424. string sqlTxt = "select count(c.id) as totals from c where c.pk='Product' and c.dataType='{0}' and c.regDate>={1} and c.regDate<={2}";
  425. lastYearServiceCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(sqlTxt, "service", lastYearStart, lastYearEnd)); //计算去年购买的服务数量
  426. yearServiceCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(sqlTxt, "service", yearStart, yearEnd)); //今年购买的服务数量
  427. lastYearSerialsCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(sqlTxt, "serial", lastYearStart, lastYearEnd)); //计算去年购买的软体数量
  428. yearSerialsCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(sqlTxt, "serial", yearStart, yearEnd)); //今年购买的软体数量
  429. return Ok(new { state = 200, buyServiceCount, buySerialsCount, lastYearServiceCount, yearServiceCount, lastYearSerialsCount, yearSerialsCount, serialsCout, servicesCout });
  430. }
  431. }
  432. /// <summary>
  433. /// 顾问关联的学校模组
  434. /// </summary>
  435. /// <param name="jsonElement"></param>
  436. /// <returns></returns>
  437. [ProducesDefaultResponseType]
  438. [HttpPost("get-adviserschool")]
  439. public async Task<IActionResult> GetAdviserSchool(JsonElement jsonElement)
  440. {
  441. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  442. jsonElement.TryGetProperty("site", out JsonElement site);
  443. var cosmosClient = _azureCosmos.GetCosmosClient();
  444. if ($"{site}".Equals(BIConst.Global))
  445. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  446. long buyServiceCount = 0; //购买服务数量
  447. long buySerialsCount = 0; //购买软体数量
  448. long lastYearServiceCount = 0; //去年购买服务数量
  449. long yearServiceCount = 0; //今年购买服务数量
  450. long lastYearSerialsCount = 0; //去年购买软体数量
  451. long yearSerialsCount = 0; //今年购买软体数量
  452. List<RecSchoolAnalys> schoolAnalys = new();
  453. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year", false); //计算去年开始/结束时间
  454. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year", false); //计算今年开始/结束时间
  455. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  456. foreach (var scId in schoolIds)
  457. {
  458. //软体产品
  459. List<SchoolProductSumData> Serials = new();
  460. //软体产品
  461. List<SchoolProductSumData> Services = new();
  462. School school = new();
  463. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(scId, new PartitionKey("Base"));
  464. if (response.Status == 200)
  465. {
  466. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  467. school = json.ToObject<School>();
  468. }
  469. 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") }))
  470. {
  471. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  472. foreach (var itemCount in json.RootElement.GetProperty("Documents").EnumerateArray())
  473. {
  474. //软体产品
  475. Serials.AddRange(itemCount.GetProperty("serial").ToObject<List<SchoolProductSumData>>().Where(x => x.avaliable > 0).ToList());
  476. //服务产品
  477. Services.AddRange(itemCount.GetProperty("service").ToObject<List<SchoolProductSumData>>().Where(x => x.avaliable > 0).ToList());
  478. }
  479. }
  480. long tempBuySerc = await CommonFind.GetSqlValueCount(cosmosClient, "select value(count(c.id)) from c where c.dataType='service' and c.pk='Product'", "School", $"Product-{scId}");
  481. long tempBuySeri = await CommonFind.GetSqlValueCount(cosmosClient, "select value(count(c.id)) from c where c.dataType='serial' and c.pk='Product'", "School", $"Product-{scId}");
  482. string sqlTxt = "select value(count(c.id)) from c where c.pk='Product' and c.dataType='{0}' and c.regDate>={1} and c.regDate<={2}";
  483. long tempLastYearSerc = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "service", lastYearStart, lastYearEnd), $"Product-{scId}"); //计算去年购买的服务数量
  484. long tempyearSerc = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "service", yearStart, yearEnd), $"Product-{scId}"); //今年购买的服务数量
  485. long tempLastYearSeri = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "serial", lastYearStart, lastYearEnd), $"Product-{scId}"); //计算去年购买的软体数量
  486. long tempYearSeri = await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(sqlTxt, "serial", yearStart, yearEnd), $"Product-{scId}"); //今年购买的软体数量
  487. schoolAnalys.Add(new RecSchoolAnalys { id = school.id, name = school.name, picture = school.picture, size = school.size, scale = school.scale, buySerc = tempBuySerc, lastYearSerc = tempLastYearSerc, yearSerc = tempyearSerc, buySeri = tempBuySeri, lastYearSeri = tempLastYearSeri, yearSeri = tempYearSeri });
  488. lastYearServiceCount += tempLastYearSerc;
  489. yearServiceCount += tempyearSerc;
  490. buyServiceCount += tempBuySerc;
  491. lastYearSerialsCount += tempLastYearSeri;
  492. yearSerialsCount += tempYearSeri;
  493. buySerialsCount += tempBuySeri;
  494. }
  495. return Ok(new { state = 200, buyServiceCount, buySerialsCount, lastYearServiceCount, yearServiceCount, lastYearSerialsCount, yearSerialsCount, schoolAnalys });
  496. }
  497. /// <summary>
  498. /// 显示学校的模组分析
  499. /// </summary>
  500. public record RecSchoolAnalys
  501. {
  502. public string id { get; set; }
  503. public string name { get; set; }
  504. public string picture { get; set; }
  505. public int size { get; set; }
  506. public int scale { get; set; }
  507. public long buySerc { get; set; }
  508. public long lastYearSerc { get; set; }
  509. public long yearSerc { get; set; }
  510. public long buySeri { get; set; }
  511. public long lastYearSeri { get; set; }
  512. public long yearSeri { get; set; }
  513. }
  514. public record SchoolProduct
  515. {
  516. public string id { get; set; }
  517. public string name { get; set; }
  518. public List<ProductStatis> product { get; set; }
  519. }
  520. public record ProductStatis
  521. {
  522. public string prodCode { get; set; }
  523. public string prodName { get; set; }
  524. //public string dataType { get; set; }
  525. public long Count { get; set; }
  526. }
  527. }
  528. }