ItemSticsController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using TEAMModelOS.Models;
  5. using TEAMModelOS.SDK.DI;
  6. using System.Threading.Tasks;
  7. using System.Collections.Generic;
  8. using Azure.Cosmos;
  9. using System.Text.Json;
  10. using TEAMModelBI.Tool;
  11. using System;
  12. using System.Text;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Context.BI;
  16. namespace TEAMModelBI.Controllers.Census
  17. {
  18. [Route("item")]
  19. [ApiController]
  20. public class ItemSticsController : ControllerBase
  21. {
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly AzureStorageFactory _azureStorage;
  24. private readonly DingDing _dingDing;
  25. private readonly Option _option;
  26. public ItemSticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  27. {
  28. _azureCosmos = azureCosmos;
  29. _dingDing = dingDing;
  30. _azureStorage = azureStorage;
  31. _option = option?.Value;
  32. }
  33. /// <summary>
  34. /// 查询所有试题数量
  35. /// </summary>
  36. /// <param name="jsonElement"></param>
  37. /// <returns></returns>
  38. [ProducesDefaultResponseType]
  39. [HttpPost("get-totals")]
  40. public async Task<IActionResult> GetTotals(JsonElement jsonElement)
  41. {
  42. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  43. jsonElement.TryGetProperty("site", out JsonElement site);
  44. var cosmosClient = _azureCosmos.GetCosmosClient();
  45. if ($"{site}".Equals(BIConst.Global))
  46. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  47. if (!string.IsNullOrEmpty($"{tmdId}"))
  48. {
  49. List<ItemTatols> itemTatols = new List<ItemTatols>();
  50. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  51. foreach (var scid in schoolIds)
  52. {
  53. School school = new();
  54. var rsponse = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(scid, new PartitionKey("Base"));
  55. if (rsponse.Status == 200)
  56. {
  57. using var json = await JsonDocument.ParseAsync(rsponse.ContentStream);
  58. school = json.ToObject<School>();
  59. }
  60. ItemTatols itempTatols = new ItemTatols() { id = scid, name = school.name != null ? school.name : scid };
  61. string sqlTxt = $"select value(COUNT(c.id)) from c where c.code='Item-{scid}'";
  62. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  63. itempTatols.totals = totals;
  64. itemTatols.Add(itempTatols);
  65. }
  66. return Ok(new { state = 200, itemTatols });
  67. }
  68. else
  69. {
  70. string sqlTxt = $"select value(COUNT(c.id)) from c where c.pk='Item'";
  71. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  72. return Ok(new { state = 200, totals });
  73. }
  74. }
  75. /// <summary>
  76. /// 试题类型统计
  77. /// </summary>
  78. /// <returns></returns>
  79. [ProducesDefaultResponseType]
  80. [HttpPost("get-type")]
  81. public async Task<IActionResult> GetTypeCount(JsonElement jsonElement)
  82. {
  83. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  84. if (!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
  85. jsonElement.TryGetProperty("site", out JsonElement site);
  86. var (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  87. var cosmosClient = _azureCosmos.GetCosmosClient();
  88. if ($"{site}".Equals(BIConst.Global))
  89. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  90. List<string> itemType = new List<string> { "single", "multiple", "judge", "complete", "subjective", "connector", "correct", "compose" };
  91. List<object> typeCount = new List<object>();
  92. if (!string.IsNullOrEmpty($"{tmdId}"))
  93. {
  94. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  95. foreach (var schoolId in schoolIds)
  96. {
  97. School school = new();
  98. var rsponse = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolId, new PartitionKey("Base"));
  99. if (rsponse.Status == 200)
  100. {
  101. using var json = await JsonDocument.ParseAsync(rsponse.ContentStream);
  102. school = json.ToObject<School>();
  103. }
  104. Census tempCensus = new Census() { id = schoolId, name = school.name != null ? school.name : schoolId };
  105. foreach (var type in itemType)
  106. {
  107. StringBuilder sqlTxt = new($"select value(COUNT(c.id)) from c where c.code='Item-{schoolId}' and c.type='{type}'");
  108. if (bool.Parse($"{term}") == true)
  109. {
  110. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  111. }
  112. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  113. tempCensus.census.Add(new KeyValuePair<object, long>(type, totals));
  114. }
  115. typeCount.Add(tempCensus);
  116. }
  117. }
  118. else
  119. {
  120. foreach (var type in itemType)
  121. {
  122. StringBuilder sqlTxt = new($"select value(COUNT(c.id)) from c where c.pk='Item' and c.type='{type}'");
  123. if (bool.Parse($"{term}") == true)
  124. {
  125. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  126. }
  127. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  128. typeCount.Add(new KeyValuePair<string, object>(type, totals));
  129. }
  130. }
  131. return Ok(new { state = 200, typeCount });
  132. }
  133. /// <summary>
  134. /// 依据难度统计
  135. /// </summary>
  136. /// <returns></returns>
  137. [ProducesDefaultResponseType]
  138. [HttpPost("get-level")]
  139. public async Task<IActionResult> GetLevelCount(JsonElement jsonElement)
  140. {
  141. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  142. if(!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
  143. jsonElement.TryGetProperty("site", out JsonElement site);
  144. var cosmosClient = _azureCosmos.GetCosmosClient();
  145. if ($"{site}".Equals(BIConst.Global))
  146. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  147. var (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  148. List<object> levelCount = new List<object>();
  149. if (!string.IsNullOrEmpty($"{tmdId}"))
  150. {
  151. List<string> schoolIds = new List<string>();
  152. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  153. foreach (var schoolId in schoolIds)
  154. {
  155. School school = new();
  156. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolId, new PartitionKey("Base"));
  157. if (response.Status == 200)
  158. {
  159. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  160. school = json.ToObject<School>();
  161. }
  162. Census tempCensus = new Census() { id = schoolId, name = school.name != null ? school.name : schoolId };
  163. for (int i = 1; i < 6; i++)
  164. {
  165. StringBuilder sqlTxt = new($"SELECT value(COUNT(c.id)) FROM c WHERE c.code='Item-{schoolId}' and c.level={i}");
  166. if (bool.Parse($"{term}") == true)
  167. {
  168. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  169. }
  170. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  171. tempCensus.census.Add(new KeyValuePair<object, long>(i, totals));
  172. }
  173. levelCount.Add(tempCensus);
  174. }
  175. }
  176. else
  177. {
  178. for (int i = 1; i < 6; i++)
  179. {
  180. StringBuilder sqlTxt = new($"select value(COUNT(c.id)) from c where c.pk='Item' and c.level={i}");
  181. if (bool.Parse($"{term}") == true)
  182. {
  183. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  184. }
  185. long total = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  186. levelCount.Add(new KeyValuePair<int, long>(i, total));
  187. }
  188. }
  189. return Ok(new { state = 200, levelCount });
  190. }
  191. /// <summary>
  192. /// 依据层次查询
  193. /// </summary>
  194. /// <returns></returns>
  195. [ProducesDefaultResponseType]
  196. [HttpPost("get-layer")]
  197. public async Task<IActionResult> GetLayerCount(JsonElement jsonElement)
  198. {
  199. jsonElement.TryGetProperty("site", out JsonElement site);
  200. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  201. if (!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
  202. var (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  203. var cosmosClient = _azureCosmos.GetCosmosClient();
  204. if ($"{site}".Equals(BIConst.Global))
  205. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  206. List<object> layerCount = new List<object>();
  207. if (!string.IsNullOrEmpty($"{tmdId}"))
  208. {
  209. List<string> schoolIds = new List<string>();
  210. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  211. foreach (var schoolId in schoolIds)
  212. {
  213. School school = new();
  214. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolId, new PartitionKey("Base"));
  215. if (response.Status == 200)
  216. {
  217. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  218. school = json.ToObject<School>();
  219. }
  220. Census tempCensus = new Census() { id = schoolId, name = school.name != null ? school.name : schoolId };
  221. for (int i = 1; i <= 6; i++)
  222. {
  223. StringBuilder sqlTxt = new($"select value(COUNT(c.id)) from c where c.code='Item-{schoolId}' and c.field={i}");
  224. if (bool.Parse($"{term}") == true)
  225. {
  226. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  227. }
  228. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  229. tempCensus.census.Add(new KeyValuePair<object, long>(i, totals));
  230. }
  231. layerCount.Add(tempCensus);
  232. }
  233. }
  234. else
  235. {
  236. for (int i = 1; i <= 6; i++)
  237. {
  238. StringBuilder sqlTxt = new StringBuilder($"select value(COUNT(c.id)) from c where c.pk='Item' and c.field={i}");
  239. if (bool.Parse($"{term}") == true)
  240. {
  241. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  242. }
  243. long totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
  244. layerCount.Add(new KeyValuePair<object, long>(i, totals));
  245. }
  246. }
  247. return Ok(new { state = 200, layerCount });
  248. }
  249. /// <summary>
  250. /// 依据试题id查询试题信息 数据管理工具——查询工具
  251. /// </summary>
  252. /// <param name="jsonElement"></param>
  253. /// <returns></returns>
  254. [ProducesDefaultResponseType]
  255. [HttpPost("get-info")]
  256. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  257. {
  258. if (!jsonElement.TryGetProperty("itemId", out JsonElement itemId)) return BadRequest();
  259. jsonElement.TryGetProperty("isPersonal", out JsonElement isPersonal);
  260. jsonElement.TryGetProperty("site", out JsonElement site);
  261. var cosmosClient = _azureCosmos.GetCosmosClient();
  262. if ($"{site}".Equals(BIConst.Global))
  263. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  264. List<object> objItems = new List<object>();
  265. string sqlTxt = $"select * from c where c.id='{itemId}' and c.pk='Item'";
  266. if (!string.IsNullOrEmpty($"{isPersonal}"))
  267. {
  268. string tableName = "School";
  269. if (bool.Parse($"{isPersonal}") == true)
  270. {
  271. tableName = "Teacher";
  272. }
  273. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", tableName).GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  274. {
  275. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  276. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  277. {
  278. objItems.Add(obj.ToObject<object>());
  279. }
  280. }
  281. }
  282. else
  283. {
  284. List<string> tableName = new List<string> { "School", "Teacher" };
  285. foreach (string key in tableName)
  286. {
  287. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", key).GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  288. {
  289. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  290. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  291. {
  292. objItems.Add(obj.ToObject<object>());
  293. }
  294. }
  295. }
  296. }
  297. return Ok(new { state = 200, items = objItems });
  298. }
  299. public record ItemTatols
  300. {
  301. /// <summary>
  302. /// 学校ID
  303. /// </summary>
  304. public string id { get; set; }
  305. /// <summary>
  306. /// 学校名称
  307. /// </summary>
  308. public string name { get; set; }
  309. /// <summary>
  310. /// 总数
  311. /// </summary>
  312. public long totals { get; set; }
  313. }
  314. /// <summary>
  315. /// 统计实体
  316. /// </summary>
  317. public record Census
  318. {
  319. /// <summary>
  320. /// 学校ID
  321. /// </summary>
  322. public string id { get; set; }
  323. /// <summary>
  324. /// 学校名称
  325. /// </summary>
  326. public string name { get; set; }
  327. /// <summary>
  328. /// 统计信息
  329. /// </summary>
  330. public List<KeyValuePair<object, long>> census { get; set; } = new List<KeyValuePair<object, long>>();
  331. }
  332. }
  333. }