BizCustomizeController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using TEAMModelOS.SDK.DI;
  5. using TEAMModelOS.SDK;
  6. using TEAMModelOS.Models;
  7. using Microsoft.Extensions.Options;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.Models.Dtos;
  10. using TEAMModelOS.SDK.Context.Constant;
  11. using System.Collections.Generic;
  12. using System.Text.Json;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.Filter;
  15. using TEAMModelOS.SDK.Models;
  16. using Azure.Cosmos;
  17. using TEAMModelOS.SDK.Models.Cosmos.OpenEntity;
  18. using System;
  19. using System.Text;
  20. namespace TEAMModelOS.Controllers
  21. { /// <summary>
  22. /// 企业定制化接口
  23. /// </summary>
  24. [Route("business")]
  25. [ApiController]
  26. public class BizCustomizeController : ControllerBase
  27. {
  28. public AzureCosmosFactory _azureCosmos;
  29. private readonly AzureStorageFactory _azureStorage;
  30. private readonly AzureRedisFactory _azureRedis;
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. private readonly IConfiguration _configuration;
  34. private readonly CoreAPIHttpService _coreAPIHttpService;
  35. private readonly AzureServiceBusFactory _serviceBus;
  36. private readonly SnowflakeId _snowflakeId;
  37. public BizCustomizeController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, AzureServiceBusFactory serviceBus, DingDing dingDing, CoreAPIHttpService coreAPIHttpService, IOptionsSnapshot<Option> option, IConfiguration configuration, SnowflakeId snowflakeId)
  38. {
  39. _azureCosmos = azureCosmos;
  40. _azureStorage = azureStorage;
  41. _azureRedis = azureRedis;
  42. _dingDing = dingDing;
  43. _option = option?.Value;
  44. _configuration = configuration;
  45. _coreAPIHttpService = coreAPIHttpService;
  46. _serviceBus = serviceBus;
  47. _snowflakeId = snowflakeId;
  48. }
  49. #region 杉达公司
  50. /// <summary>
  51. /// 开课或者上传课例信息 是因支持VR/AR的公司(杉达)
  52. /// </summary>
  53. /// <param name="jsonElement"></param>
  54. /// <returns></returns>
  55. [ProducesResponseType(StatusCodes.Status200OK)]
  56. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  57. [ProducesDefaultResponseType]
  58. [HttpPost("create-vrar-lesson-shanda")]
  59. [ApiToken(Auth = "2003", Name = "VR·AR开课/上传课例信息", TName = "VR·AR開課/上傳課例信息", EName = "Start VR·AR lesson/upload VR·AR lesson ", RWN = "W", Limit = false)]
  60. public async Task<IActionResult> UpLessonRec(JsonElement jsonElement)
  61. {
  62. var (id, school) = HttpContext.GetApiTokenInfo();
  63. try
  64. {
  65. jsonElement.TryGetProperty("baseJson", out JsonElement baseJson); //课例基础文件信息json
  66. jsonElement.TryGetProperty("timLineJson", out JsonElement timLineJson); //时间json
  67. jsonElement.TryGetProperty("lessonId", out JsonElement lessonId); //课例id
  68. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return Ok(new { responseDate = new ResponseData<dynamic>() { code = RespondCode.ParamsError, msg = "tmdId参数错误", data = null } }); //上课人ID
  69. jsonElement.TryGetProperty("tmdName", out JsonElement tmdName); //上课人昵称
  70. jsonElement.TryGetProperty("tmdPicture", out JsonElement tmdpicture); //上课人头像
  71. if (!jsonElement.TryGetProperty("startTime", out JsonElement startTime)) return Ok(new { responseDate = new ResponseData<dynamic>() { code = RespondCode.ParamsError, msg = "startTime参数错误", data = null } }); //开始时间
  72. if (!jsonElement.TryGetProperty("duration", out JsonElement duration)) return Ok(new { responseDate = new ResponseData<dynamic>() { code = RespondCode.ParamsError, msg = "startTime参数错误", data = null } }); //上课时长
  73. jsonElement.TryGetProperty("periodId", out JsonElement periodId); //学段id
  74. jsonElement.TryGetProperty("periodName", out JsonElement periodName); //学段名称
  75. jsonElement.TryGetProperty("groupIds", out JsonElement _groupIds); //名单id
  76. jsonElement.TryGetProperty("groupNames", out JsonElement _groupNames); //名单名称
  77. jsonElement.TryGetProperty("gradeIds", out JsonElement _gradeIds); //年级id
  78. jsonElement.TryGetProperty("gradeNames", out JsonElement _gradeNames); //年级名称
  79. jsonElement.TryGetProperty("subjectId", out JsonElement subjectId); //科目id
  80. jsonElement.TryGetProperty("subjecName", out JsonElement subjecName); //科目名称
  81. jsonElement.TryGetProperty("courseId", out JsonElement courseId); //课程id
  82. jsonElement.TryGetProperty("courseName", out JsonElement courseName); //课程名称
  83. LessonRecord lessonRecord = new();
  84. LessonBase lessonBase = null;
  85. TimeLine timeLine = null;
  86. if (!string.IsNullOrEmpty($"{baseJson}"))
  87. {
  88. lessonBase = baseJson.ToObject<LessonBase>();
  89. lessonRecord.upload = 1;
  90. }
  91. else
  92. lessonRecord.upload = 0;
  93. if (!string.IsNullOrEmpty($"{timLineJson}"))
  94. {
  95. timeLine = timLineJson.ToObject<TimeLine>();
  96. lessonRecord.upload = 1;
  97. }
  98. else
  99. lessonRecord.upload = 0;
  100. List<string> groupIds = new();
  101. List<string> groupNames = new();
  102. List<string> gradeIds = new();
  103. List<string> gradeNames = new();
  104. if (!string.IsNullOrEmpty($"{_groupIds}"))
  105. groupIds = _groupIds.ToObject<List<string>>();
  106. if (!string.IsNullOrEmpty($"{_groupNames}"))
  107. groupNames = _groupNames.ToObject<List<string>>();
  108. if (!string.IsNullOrEmpty($"{_gradeIds}"))
  109. gradeIds = _gradeIds.ToObject<List<string>>();
  110. if (!string.IsNullOrEmpty($"{_gradeNames}"))
  111. gradeNames = _gradeNames.ToObject<List<string>>();
  112. var cosmosClient = _azureCosmos.GetCosmosClient();
  113. string code = $"LessonRecord-{school}";
  114. var resLess = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{lessonId}", new PartitionKey($"{code}"));
  115. if (resLess.Status == 200)
  116. {
  117. JsonDocument jsonD = JsonDocument.Parse(resLess.Content);
  118. lessonRecord = jsonD.RootElement.ToObject<LessonRecord>();
  119. }
  120. else
  121. {
  122. lessonRecord.id = _snowflakeId.NextId().ToString();
  123. lessonRecord.code = code;
  124. }
  125. lessonRecord.tmdid = $"{tmdId}";
  126. lessonRecord.tmdname = $"{tmdName}";
  127. lessonRecord.tmdpicture = $"{tmdpicture}";
  128. lessonRecord.school = school;
  129. lessonRecord.scope = "school";
  130. lessonRecord.startTime = long.Parse($"{startTime}");
  131. lessonRecord.duration = double.Parse($"{duration}");
  132. lessonRecord.periodId = $"{periodId}";
  133. lessonRecord.periodName = $"{periodName}";
  134. lessonRecord.subjectId = $"{subjectId}";
  135. lessonRecord.subjecName = $"{subjecName}";
  136. lessonRecord.courseId = $"{courseId}";
  137. lessonRecord.courseName = $"{courseName}";
  138. lessonRecord.groupIds = groupIds;
  139. lessonRecord.groupNames = groupNames;
  140. lessonRecord.grade = gradeIds;
  141. lessonRecord.gradeName = gradeNames;
  142. if (!string.IsNullOrEmpty($"{baseJson}"))
  143. {
  144. lessonRecord.name = lessonBase.summary.activityName;
  145. lessonRecord.attendCount = lessonBase.summary.attendCount;
  146. lessonRecord.clientCount = lessonBase.summary.clientCount;
  147. lessonRecord.attendRate = lessonBase.summary.attendRate;
  148. lessonRecord.groupCount = lessonBase.summary.groupCount;
  149. lessonRecord.collateTaskCount = lessonBase.summary.collateTaskCount;
  150. lessonRecord.collateCount = lessonBase.summary.collateCount;
  151. lessonRecord.pushCount = lessonBase.summary.pushCount;
  152. lessonRecord.totalPoint = lessonBase.summary.totalPoint;
  153. lessonRecord.examQuizCount = lessonBase.summary.examQuizCount;
  154. lessonRecord.interactionCount = lessonBase.summary.interactionCount;
  155. lessonRecord.examPointRate = lessonBase.summary.examPointRate;
  156. lessonRecord.clientInteractionCount = lessonBase.summary.clientInteractionCount;
  157. lessonRecord.clientInteractionAverge = lessonBase.summary.clientInteractionAverge;
  158. lessonRecord.examCount = lessonBase.summary.examCount;
  159. lessonRecord.totalInteractPoint = lessonBase.summary.totalInteractPoint;
  160. lessonRecord.learningCategory = lessonBase.summary.learningCategory;
  161. lessonRecord.source = 1;
  162. //计算TP灯
  163. {
  164. int T = -1;
  165. int P = -1;
  166. if (lessonRecord.clientInteractionAverge <= 0)
  167. {
  168. T = 0;
  169. }
  170. else if (lessonRecord.clientInteractionAverge >= 1 && lessonRecord.clientInteractionAverge <= 2)
  171. {
  172. T = 1;
  173. }
  174. else
  175. {
  176. T = 2;
  177. }
  178. //if (lessonRecord.examCount > 0)
  179. //{
  180. // //有评测次数大于0则P是直接绿灯
  181. // P = 2;
  182. //}
  183. //else {
  184. //}
  185. int a = lessonRecord.hitaClientCmpCount;
  186. int b = lessonRecord.pushCount;
  187. int c = lessonRecord.examCount;
  188. switch (true)
  189. {
  190. case bool when T == 0:
  191. P = 0;
  192. break;
  193. case bool when T == 1 || T == 2:
  194. if (a == 0 && b == 0 && c == 0)
  195. {
  196. P = 0;
  197. }
  198. else if ((a > 0 && b > 0) || (a > 0 && c > 0) || (b > 0 && c > 0))
  199. {
  200. P = 2;
  201. }
  202. else
  203. {
  204. P = 1;
  205. }
  206. break;
  207. }
  208. lessonRecord.tLevel = T;
  209. lessonRecord.pLevel = P;
  210. }
  211. }
  212. if (resLess.Status == 200)
  213. lessonRecord = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<LessonRecord>(lessonRecord, lessonRecord.id, new PartitionKey($"{code}"));
  214. else
  215. lessonRecord = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<LessonRecord>(lessonRecord, new PartitionKey($"{code}"));
  216. var blobClient = _azureStorage.GetBlobContainerClient(containerName: $"{school}");
  217. if (!string.IsNullOrEmpty($"{baseJson}"))
  218. //保存Basejson文件
  219. await AzureStorageBlobExtensions.UploadFileByContainer(blobClient, lessonBase.ToJsonString(), $"records/{lessonRecord.id}/IES", $"Base.json");
  220. if (!string.IsNullOrEmpty($"{timLineJson}"))
  221. //保存TimeLinejson文件
  222. await AzureStorageBlobExtensions.UploadFileByContainer(blobClient, timeLine.ToJsonString(), $"records/{lessonRecord.id}/IES", $"TimeLine.json");
  223. return Ok(new
  224. {
  225. responseData = new ResponseData<dynamic>()
  226. {
  227. code = RespondCode.Ok,
  228. msg = "成功",
  229. data = new
  230. {
  231. lessonRecord,
  232. analysis = new
  233. {
  234. lessonRecord.id,
  235. lessonRecord.name,
  236. lessonRecord.subjectId,
  237. lessonRecord.courseId,
  238. intIndex = lessonRecord.tLevel,
  239. learning = lessonRecord.pLevel,
  240. atendCnt = lessonRecord.attendCount,
  241. grCnt = lessonRecord.groupCount,
  242. taskCnt = lessonRecord.collateTaskCount,
  243. worksCnt = lessonRecord.collateCount,
  244. pushCnt = lessonRecord.pushCount,
  245. scoreCnt = lessonRecord.totalPoint,
  246. emCnt = lessonRecord.examCount,
  247. intTopicCnt = lessonRecord.interactionCount,
  248. emPct = $"{lessonRecord.examPointRate} %",
  249. stuIntCnt = lessonRecord.clientInteractionCount,
  250. study = new
  251. {
  252. coopCnt = lessonRecord.learningCategory.cooperation,
  253. testCnt = lessonRecord.learningCategory.exam,
  254. intCnt = lessonRecord.learningCategory.interaction,
  255. dCnt = lessonRecord.learningCategory.diffential,
  256. taskCnt = lessonRecord.learningCategory.task,
  257. }
  258. }
  259. }
  260. }
  261. });
  262. }
  263. catch (Exception ex)
  264. {
  265. await _dingDing.SendBotMsg($"OpenApi,{Environment.GetEnvironmentVariable("Option:Location")} BizOverallEducation/UpLessonRec() 参数:bizId:{id},school:{school},json:{jsonElement.ToJsonString()} \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  266. return Ok(new { responseData = new ResponseData<dynamic>() { code = RespondCode.Error, msg = "服务端异常" } });
  267. }
  268. }
  269. /// <summary>
  270. /// 获取学校课例 是因支持VR/AR的公司(杉达)
  271. /// </summary>
  272. /// <param name="jsonElement"></param>
  273. /// <returns></returns>
  274. [ProducesDefaultResponseType]
  275. [HttpPost("get-lesson-relords")]
  276. [ApiToken(Auth = "2004", Name = "获取学校课例", TName = "獲取學校課例", EName = "Get school lessons", RWN = "R", Limit = false)]
  277. public async Task<IActionResult> GetLessonRec(JsonElement jsonElement)
  278. {
  279. var (id, school) = HttpContext.GetApiTokenInfo();
  280. jsonElement.TryGetProperty("lessonId", out JsonElement lessId);
  281. var cosmosClient = _azureCosmos.GetCosmosClient();
  282. StringBuilder sql = new("select value(c) from c where c.source=1");
  283. if (!string.IsNullOrEmpty($"{lessId}"))
  284. sql.Append($" and c.id='{lessId}'");
  285. List<OLessonRecord> lessonRecords = new();
  286. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<OLessonRecord>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{school}") }))
  287. {
  288. lessonRecords.Add(item);
  289. }
  290. return Ok(new { responseData = new ResponseData<dynamic>() { code = RespondCode.Ok, msg = "成功", data = lessonRecords } });
  291. }
  292. /// <summary>
  293. /// 获取学校课例分析结果 是因支持VR/AR的公司(杉达)
  294. /// </summary>
  295. /// <param name="jsonElement"></param>
  296. /// <returns></returns>
  297. [ProducesDefaultResponseType]
  298. [HttpPost("get-lesson-analysis")]
  299. [ApiToken(Auth = "2006", Name = "获取学校课例", TName = "獲取學校課例", EName = "Get school lessons", RWN = "R", Limit = false)]
  300. public async Task<IActionResult> GetLessonStats(JsonElement jsonElement)
  301. {
  302. var (id, school) = HttpContext.GetApiTokenInfo();
  303. if (!jsonElement.TryGetProperty("lessonId", out JsonElement lessonId)) return Ok(new { responseDate = new ResponseData<dynamic>() { code = RespondCode.ParamsError, msg = "lessId参数错误", data = null } }); //上课人ID ;
  304. var cosmosClient = _azureCosmos.GetCosmosClient();
  305. dynamic analysis = null;
  306. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id,c.name,c.subjectId,c.courseId,c.tLevel,c.pLevel,c.attendCount,c.groupCount,c.collateTaskCount,c.collateCount,c.pushCount,c.totalPoint,c.examCount,c.interactionCount,c.examPointRate,c.clientInteractionCount,c.learningCategory from c where c.id='{lessonId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{school}") }))
  307. {
  308. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  309. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  310. {
  311. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  312. while (accounts.MoveNext())
  313. {
  314. JsonElement account = accounts.Current;
  315. LearningCategory learningCategory = account.GetProperty("learningCategory").ToObject<LearningCategory>();
  316. analysis = new
  317. {
  318. id = account.GetProperty("id").GetString(),
  319. name = account.GetProperty("name").GetString(),
  320. subjectId = account.GetProperty("subjectId").GetString(),
  321. courseId = account.GetProperty("courseId").GetString(),
  322. intIndex = account.GetProperty("tLevel").GetInt64(),
  323. learning = account.GetProperty("pLevel").GetInt64(),
  324. atendCnt = account.GetProperty("attendCount").GetInt64(),
  325. grCnt = account.GetProperty("groupCount").GetInt64(),
  326. taskCnt = account.GetProperty("collateTaskCount").GetInt64(),
  327. worksCnt = account.GetProperty("collateCount").GetInt64(),
  328. pushCnt = account.GetProperty("pushCount").GetInt64(),
  329. scoreCnt = account.GetProperty("totalPoint").GetInt64(),
  330. emCnt = account.GetProperty("examCount").GetInt64(),
  331. intTopicCnt = account.GetProperty("interactionCount").GetInt64(),
  332. emPct = $"{account.GetProperty("examPointRate").GetDouble()} %",
  333. stuIntCnt = account.GetProperty("clientInteractionCount").GetInt64(),
  334. study = new
  335. {
  336. coopCnt = learningCategory.cooperation,
  337. testCnt = learningCategory.exam,
  338. intCnt = learningCategory.interaction,
  339. dCnt = learningCategory.diffential,
  340. taskCnt = learningCategory.task,
  341. }
  342. };
  343. //var ids = account.GetProperty("id").GetString();
  344. //var name = account.GetProperty("name").GetString();
  345. //var intIndex = account.GetProperty("tLevel").GetInt64();
  346. //var learning = account.GetProperty("pLevel").GetInt64();
  347. //var atendCnt = account.GetProperty("attendCount").GetInt64();
  348. //var grCnt = account.GetProperty("groupCount").GetInt64();
  349. //var taskCnt = account.GetProperty("collateTaskCount").GetInt64();
  350. //var worksCnt = account.GetProperty("collateCount").GetInt64();
  351. //var pushCnt = account.GetProperty("pushCount").GetInt64();
  352. //var scoreCnt = account.GetProperty("totalPoint").GetInt64();
  353. //var emCnt = account.GetProperty("examCount").GetInt64();
  354. //var intTopicCnt = account.GetProperty("interactionCount").GetInt64();
  355. //var emPct = $"{account.GetProperty("examPointRate").GetDouble()} %";
  356. //var stuIntCnt = account.GetProperty("clientInteractionCount").GetInt64();
  357. //var study = new
  358. //{
  359. // coopCnt = learningCategory.cooperation,
  360. // testCnt = learningCategory.exam,
  361. // intCnt = learningCategory.interaction,
  362. // dCnt = learningCategory.diffential
  363. //};
  364. }
  365. }
  366. }
  367. return Ok(new { responseData = new ResponseData<dynamic>() { code = RespondCode.Ok, msg = "成功", data = analysis } });
  368. }
  369. #endregion
  370. #region 智音公司
  371. /// <summary>
  372. /// 添加/修改学生艺术评测(音乐)答题结果
  373. /// </summary>
  374. /// <param name="jsonElement"></param>
  375. /// <returns></returns>
  376. [ProducesDefaultResponseType]
  377. [HttpPost("set-stdent-score")]
  378. [ApiToken(Auth = "2005", Name = "添加/修改学生艺术评测(音乐)答题结果", TName = "添加/修改學生藝術評測音樂答題結果", EName = "Add/modify the music answer results of student art evaluation", RWN = "W", Limit = false)]
  379. public async Task<IActionResult> SetStudnetScore(JsonElement jsonElement)
  380. {
  381. var (id, school) = HttpContext.GetApiTokenInfo();
  382. if (!jsonElement.TryGetProperty("oAnswer", out JsonElement _oAnswer)) return Ok(new { responseDate = new ResponseData<dynamic>() { code = RespondCode.ParamsError, msg = "oaswer参数错误", data = null } });
  383. OAnswerUp oAnswer = _oAnswer.ToObject<OAnswerUp>();
  384. StudentArtResult studentArtResult = new();
  385. var cosmosClient = _azureCosmos.GetCosmosClient();
  386. var resArt = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Student").ReadItemStreamAsync($"{school}-{oAnswer.studentId}", new PartitionKey($"ArtResult-{oAnswer.artId}"));
  387. if (resArt.Status == 200)
  388. {
  389. JsonDocument jsonD = JsonDocument.Parse(resArt.Content);
  390. studentArtResult = jsonD.RootElement.ToObject<StudentArtResult>();
  391. var temp = studentArtResult.results.Find(f => f.taskId.Equals(oAnswer.thirdAnswerId));
  392. temp.score = oAnswer.score;
  393. temp.source = 1;
  394. studentArtResult.oAnswer.questionId = oAnswer.questionId;
  395. studentArtResult.oAnswer.thirdAnswerId = oAnswer.thirdAnswerId;
  396. studentArtResult.oAnswer.score = oAnswer.score;
  397. studentArtResult.oAnswer.detail = oAnswer.detail;
  398. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StudentArtResult>(studentArtResult, studentArtResult.id, new PartitionKey(studentArtResult.code));
  399. }
  400. else
  401. return Ok(new { responseDate = new ResponseData<dynamic>() { code = RespondCode.NotFound, msg = "未找到活动相关学生答题信息", data = null } });
  402. return Ok(new { responseData = new ResponseData<dynamic>() { code = RespondCode.Ok, msg = "成功", data = oAnswer } });
  403. }
  404. #endregion
  405. }
  406. }