123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- using Azure.Cosmos;
- using HTEXLib.COMM.Helpers;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Filter;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelOS.Controllers
- {
- /// <summary>
- /// 课堂记录
- /// </summary>
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [Route("common/lesson-record")]
- [ApiController]
-
- public class LessonRecordController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly SnowflakeId _snowflakeId;
- private readonly AzureServiceBusFactory _serviceBus;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- public IConfiguration _configuration { get; set; }
- public LessonRecordController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
- IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
- {
- _azureCosmos = azureCosmos;
- _serviceBus = serviceBus;
- _snowflakeId = snowflakeId;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureStorage = azureStorage;
- _azureRedis = azureRedis;
- _configuration = configuration;
- }
- /// <summary>
- /// 获取开课记录
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher,admin")]
- [HttpPost("get-lesson-record-count")]
- public async Task<IActionResult> GetLessonRecordCont(JsonElement request)
- {
-
- if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
- StringBuilder sql = new StringBuilder();
- sql.Append("select value(count(1)) from c ");
- Dictionary<string ,object> dict = GetLessonCond(request);
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- string tbname = "";
- string code = "";
- if (_scope.GetString().Equals("school"))
- {
- if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
- if (!string.IsNullOrEmpty($"{_school}"))
- {
- code = $"LessonRecord-{_school}";
- tbname = "School";
- }
- else {
- return BadRequest();
- }
-
- }
- else if ($"{_scope}".Equals("private"))
- {
- if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
- if (!string.IsNullOrEmpty($"{_tmdid}"))
- {
- code = $"LessonRecord-{_tmdid}";
- tbname = "Teacher";
- }
- else
- {
- return BadRequest();
- }
- }
- else
- {
- return BadRequest();
- }
- int count=0;
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<int>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
- {
- count = item;
- }
- return Ok(new { count=count });
- }
-
- /// <summary>
- /// 获取开课记录
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- // [AuthToken(Roles = "teacher,admin")]
- [HttpPost("get-lesson-record")]
- public async Task<IActionResult> GetLessonRecord(JsonElement request)
- {
- if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
- StringBuilder sql = new StringBuilder();
- sql.Append("select value(c) from c ");
- int pageCount = 10;
- Dictionary<string, object> dict = GetLessonCond(request);
- if (request.TryGetProperty("pageCount", out JsonElement _pageCount))
- {
- int.TryParse($"{_pageCount}", out int pcount);
- if (pcount > 0)
- {
- pageCount = pcount;
- }
- }
- if (request.TryGetProperty("DESC", out JsonElement desc))
- {
- dict.Add("@DESC", desc.ToString());
- }
- if (request.TryGetProperty("ASC", out JsonElement asc))
- {
- dict.Add("@ASC", asc.ToString());
- }
- string continuationToken = null;
- if (request.TryGetProperty("continuationToken", out JsonElement _continuationToken))
- {
- if (!string.IsNullOrEmpty($"{_continuationToken}"))
- {
- continuationToken = $"{_continuationToken}";
- }
- }
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- string tbname = "";
- string code = "";
- if (_scope.GetString().Equals("school") )
- {
- if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
- if (!string.IsNullOrEmpty($"{_school}"))
- {
- code = $"LessonRecord-{_school}";
- tbname = "School";
- }
- else
- {
- return BadRequest();
- }
- }
- else if ($"{_scope}".Equals("private"))
- {
- if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
- if (!string.IsNullOrEmpty($"{_tmdid}"))
- {
- code = $"LessonRecord-{_tmdid}";
- tbname = "Teacher";
- }
- else {
- return BadRequest();
- }
- }
- else
- {
- return BadRequest();
- }
- List<LessonRecord> lessonRecords = new List<LessonRecord>();
- try {
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
- .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, continuationToken: continuationToken,
- requestOptions: new QueryRequestOptions() { MaxItemCount = pageCount, PartitionKey = new PartitionKey(code) }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- lessonRecords.Add(obj.ToObject<LessonRecord>());
- }
- continuationToken = item.GetContinuationToken();
- break;
- }
- }
- var tmdids= lessonRecords.Select(x => x.tmdid).ToHashSet();
- if (tmdids != null && tmdids.Count > 0) {
- List< IdNameCode > codes= new List<IdNameCode> ();
- string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText:sqltmd,requestOptions:new QueryRequestOptions { PartitionKey=new PartitionKey("Base") })){
- codes.Add(item);
- }
- if (codes.IsNotEmpty()) {
- lessonRecords.ForEach(x => {
- var tmd= codes.Find(z => z.id.Equals(x.tmdid));
- if (tmd != null) {
- x.tmdname = tmd.name;
- x.tmdpicture= tmd.picture;
- }
- });
- }
- }
- return Ok(new { currCount=lessonRecords.Count, continuationToken, lessonRecords });
- } catch (Exception ex) {
- continuationToken = null;
- return Ok(new { currCount=0, continuationToken = continuationToken, lessonRecords });
- }
-
- }
- private Dictionary<string, object> GetLessonCond(JsonElement request)
- {
- Dictionary<string, object> dict = new Dictionary<string, object>();
- if (request.TryGetProperty("tmdid", out JsonElement tmdid) && !string.IsNullOrWhiteSpace($"{tmdid}"))
- {
- dict.Add("tmdid", tmdid);
- }
- if (request.TryGetProperty("courseId", out JsonElement courseId) && !string.IsNullOrWhiteSpace($"{courseId}"))
- {
- dict.Add("courseId", courseId);
- }
- if (request.TryGetProperty("periodId", out JsonElement periodId) && !string.IsNullOrWhiteSpace($"{periodId}"))
- {
- dict.Add("periodId", periodId);
- }
- if (request.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- dict.Add("subjectId[*]", subjectId);
- }
- if (request.TryGetProperty("groupIds", out JsonElement groupIds))
- {
- dict.Add("groupIds[*]", groupIds);
- }
- if (request.TryGetProperty("grade", out JsonElement grade))
- {
- dict.Add("grade[*]", grade);
- }
- if (request.TryGetProperty("category", out JsonElement category))
- {
- dict.Add("category[*]", category);
- }
- if (request.TryGetProperty("doubleGreen", out JsonElement doubleGreen) && doubleGreen.GetBoolean())
- {
- dict.Add(">=.tScore", 70);
- dict.Add(">=.pScore", 70);
- }
- if (request.TryGetProperty("quality", out JsonElement quality) && quality.GetBoolean())
- {
- dict.Add(">=.discuss", 1);
- }
- if (request.TryGetProperty("name", out JsonElement name) && !string.IsNullOrWhiteSpace($"{name}"))
- {
- dict.Add("$.name", name);
- }
- return dict;
- }
- }
- }
|