ActivitySticsController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. using System.Text.Json;
  13. using TEAMModelBI.Tool;
  14. using System.Text;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelBI.Models;
  17. namespace TEAMModelBI.Controllers.Census
  18. {
  19. [Route("activity")]
  20. [ApiController]
  21. public class ActivitySticsController : ControllerBase
  22. {
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly DingDing _dingDing;
  26. private readonly Option _option;
  27. public readonly List<string> types = new List<string> { "Exam", "Survey", "Vote", "Homework" };
  28. public ActivitySticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _dingDing = dingDing;
  32. _azureStorage = azureStorage;
  33. _option = option?.Value;
  34. }
  35. /// <summary>
  36. /// 统计活动 传醍摩豆则查询相关的学校活动
  37. /// </summary>
  38. /// <param name="jsonElement"></param>
  39. /// <returns></returns>
  40. [ProducesDefaultResponseType]
  41. [HttpPost("get-count")]
  42. public async Task<IActionResult> GetCount(JsonElement jsonElement)
  43. {
  44. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  45. if(!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
  46. long start = 0, end = 0;
  47. if (bool.Parse($"{term}") == true)
  48. {
  49. (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  50. }
  51. var cosmosClient = _azureCosmos.GetCosmosClient();
  52. List<object> activityCount = new List<object>();
  53. if (!string.IsNullOrEmpty($"{tmdId}"))
  54. {
  55. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  56. foreach (var itemId in schoolIds)
  57. {
  58. School school = new();
  59. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  60. if (response.Status == 200)
  61. {
  62. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  63. school = json.ToObject<School>();
  64. }
  65. ActivityCount tempCount = new ActivityCount() { id = itemId, name = school.name != null ? school.name : itemId };
  66. foreach (var type in types)
  67. {
  68. StringBuilder sqlTxt = new StringBuilder($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{itemId}' ");
  69. if (bool.Parse($"{term}") == true)
  70. {
  71. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  72. }
  73. long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
  74. tempCount.census.Add(new KeyValuePair<object, long>(type, totals));
  75. }
  76. activityCount.Add(tempCount);
  77. }
  78. }
  79. else
  80. {
  81. foreach (var type in types)
  82. {
  83. StringBuilder sqlTxt = new StringBuilder($"SELECT COUNT(c.id) AS totals FROM c where c.pk='{type}' ");
  84. if (bool.Parse($"{term}") == true)
  85. {
  86. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  87. }
  88. long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
  89. activityCount.Add(new KeyValuePair<string, object>(type, totals));
  90. }
  91. }
  92. return Ok(new { state = 200, activityCount });
  93. }
  94. /// <summary>
  95. /// 统计所有的评量活动,问卷调查,投票活动,作业
  96. /// </summary>
  97. /// <returns></returns>
  98. [HttpPost("get-allactivity")]
  99. public async Task<IActionResult> GetAllActivity()
  100. {
  101. try
  102. {
  103. var cosmosClient = _azureCosmos.GetCosmosClient();
  104. List<KeyValuePair<string, long>> typeCount = new List<KeyValuePair<string, long>>();
  105. foreach (var type in types)
  106. {
  107. string querySql = $"SELECT Count(c.id) as totals FROM c where c.pk='{type}' ";
  108. long totals = await CommonFind.FindTotals(cosmosClient, querySql, new List<string>() { "Common" });
  109. KeyValuePair<string, long> valuePair = new KeyValuePair<string, long>(type, totals);
  110. typeCount.Add(valuePair);
  111. }
  112. return Ok(new { state = 200, typeCount });
  113. }
  114. catch (Exception ex)
  115. {
  116. await _dingDing.SendBotMsg($"BI,{_option.Location} /activity/get-allactivity \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  117. return BadRequest();
  118. }
  119. }
  120. /// <summary>
  121. /// 统计顾问关联的学校活动数量:评测、问卷、投票、作业
  122. /// </summary>
  123. /// <param name="jsonElement"></param>
  124. /// <returns></returns>
  125. [ProducesDefaultResponseType]
  126. [HttpPost("get-assistactivity")]
  127. public async Task<IActionResult> GetAssistSchoolActivity(JsonElement jsonElement)
  128. {
  129. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  130. var cosmosClient = _azureCosmos.GetCosmosClient();
  131. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  132. List<ActivityCount> activityCounts = new List<ActivityCount>();
  133. foreach (var itemId in schoolIds)
  134. {
  135. School school = new();
  136. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  137. if (response.Status == 200)
  138. {
  139. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  140. school = json.ToObject<School>();
  141. }
  142. ActivityCount activityCount = new ActivityCount() { id = itemId, name = school.name != null ? school.name : itemId };
  143. foreach (var type in types)
  144. {
  145. string activitySql = $"SELECT Count(c.id) as totals FROM c WHERE c.pk='{type}' AND c.school='{itemId}'";
  146. long totals = await CommonFind.FindTotals(cosmosClient, activitySql, new List<string>() { "Common" });
  147. activityCount.census.Add(new KeyValuePair<object, long>(type, totals));
  148. }
  149. activityCounts.Add(activityCount);
  150. }
  151. return Ok(new { state = 200, activityCounts });
  152. }
  153. /// <summary>
  154. /// 统计当前学期的活动,传醍摩豆账户则统计该账户当前学期
  155. /// </summary>
  156. /// <param name="jsonElement"></param>
  157. /// <returns></returns>
  158. [HttpPost("get-termactivity")]
  159. public async Task<IActionResult> GetTermActivity(JsonElement jsonElement)
  160. {
  161. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  162. var cosmosClient = _azureCosmos.GetCosmosClient();
  163. var (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  164. if (!string.IsNullOrEmpty($"{tmdId}"))
  165. {
  166. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  167. List<ActivityCount> activityCounts = new List<ActivityCount>();
  168. foreach (var schoolId in schoolIds)
  169. {
  170. School school = new();
  171. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(schoolId, new PartitionKey("Base"));
  172. if (response.Status == 200)
  173. {
  174. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  175. school = json.ToObject<School>();
  176. }
  177. ActivityCount activityCount = new ActivityCount() { id = schoolId, name = school.name != null ? school.name : schoolId };
  178. foreach (var type in types)
  179. {
  180. string activitySql = $"SELECT COUNT(c.id) AS totals FROM c WHERE c.pk='{type}' AND c.school='{school}' and c.createTime >= {start} and c.createTime <= {end}";
  181. long totals = await CommonFind.FindTotals(cosmosClient, activitySql, new List<string>() { "Common" });
  182. activityCount.census.Add(new KeyValuePair<object, long>(type, totals));
  183. }
  184. activityCounts.Add(activityCount);
  185. }
  186. return Ok(new { state = 200, activityCounts });
  187. }
  188. else
  189. {
  190. List<KeyValuePair<string, object>> typeCount = new List<KeyValuePair<string, object>>();
  191. foreach (var type in types)
  192. {
  193. string querySql = $"SELECT COUNT(c.id) AS totals FROM c where c.pk='{type}' and c.createTime >= {start} and c.createTime <= {end}";
  194. long totals = await CommonFind.FindTotals(cosmosClient, querySql, new List<string>() { "Common" });
  195. KeyValuePair<string, object> valuePair = new KeyValuePair<string, object>(type, totals);
  196. typeCount.Add(valuePair);
  197. }
  198. return Ok(new { state = 200, typeCount });
  199. }
  200. }
  201. /// <summary>
  202. /// 依据活动Id查询活动详情信息 数据管理工具——查询工具
  203. /// </summary>
  204. /// <param name="jsonElement"></param>
  205. /// <returns></returns>
  206. [HttpPost("get-info")]
  207. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  208. {
  209. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  210. jsonElement.TryGetProperty("activity", out JsonElement activity);
  211. jsonElement.TryGetProperty("isPersonal", out JsonElement isPersonal);
  212. //if (jsonElement.TryGetProperty("", out JsonElement code)) return BadRequest();
  213. var cosmosClient = _azureCosmos.GetCosmosClient();
  214. List<object> infos = new List<object>();
  215. StringBuilder sqlTxt = new StringBuilder($"select * from c where c.id='{id}'");
  216. if (!string.IsNullOrEmpty($"{activity}"))
  217. {
  218. sqlTxt.Append($" and c.pk='{activity}'");
  219. }
  220. if (!string.IsNullOrEmpty($"{isPersonal}"))
  221. {
  222. if (bool.Parse($"{isPersonal}") == true)
  223. {
  224. sqlTxt.Append($" and c.scope='private'");
  225. }
  226. else
  227. {
  228. sqlTxt.Append($" and c.scope='school'");
  229. }
  230. }
  231. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { }))
  232. {
  233. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  234. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  235. {
  236. infos.Add(obj.ToObject<object>());
  237. }
  238. }
  239. return Ok(new { state = 200, infos });
  240. }
  241. public record SchoolActivity
  242. {
  243. public string id { get; set; }
  244. public string name { get; set; }
  245. public long total { get; set; }
  246. }
  247. public record ActivityCount
  248. {
  249. public string id { get; set; }
  250. public string name { get; set; }
  251. public List<KeyValuePair<object, long>> census { get; set; } = new List<KeyValuePair<object, long>>();
  252. }
  253. }
  254. }