123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985 |
- using Azure.Core;
- using Azure.Cosmos;
- using DinkToPdf.Contracts;
- using DocumentFormat.OpenXml.Office2010.Excel;
- using DocumentFormat.OpenXml.Office2013.Excel;
- using DocumentFormat.OpenXml.Office2016.Excel;
- using DocumentFormat.OpenXml.Spreadsheet;
- using DocumentFormat.OpenXml.Wordprocessing;
- using MathNet.Numerics.RootFinding;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using NUnit.Framework;
- using OpenXmlPowerTools;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- using TEAMModelOS.Filter;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using Survey = TEAMModelOS.SDK.Models.Survey;
- namespace TEAMModelOS.Controllers.Common
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [Route("common/art")]
- [ApiController]
- public class ArtController : 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;
- private readonly IConverter _converter;
- public IConfiguration _configuration { get; set; }
- private readonly CoreAPIHttpService _coreAPIHttpService;
- public ArtController(IConverter converter, CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
- IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
- {
- _coreAPIHttpService = coreAPIHttpService;
- _azureCosmos = azureCosmos;
- _serviceBus = serviceBus;
- _snowflakeId = snowflakeId;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureStorage = azureStorage;
- _azureRedis = azureRedis;
- _configuration = configuration;
- _converter = converter;
- }
- /// <summary>
- /// 保存艺术评价信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("save")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Save(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("art", out JsonElement art)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- ArtEvaluation ae = art.ToObject<ArtEvaluation>();
- bool flag = false;
- ArtMusic music = new();
- if (request.TryGetProperty("ArtMusic", out JsonElement am))
- {
- music = am.ToObject<ArtMusic>();
- music.ttl = -1;
- music.code = "ArtMusic";
- flag = true;
- };
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- string code = ae.school;
- ae.ttl = -1;
- ae.progress = "going";
- ae.creatorId = userid;
- if (!ae.owner.Equals("area"))
- {
- ae.owner = "school";
- ae.code = "Art-" + code;
- ae.scope = "school";
- long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- ae.createTime = now;
- ae.publish = 0;
- }
- if (string.IsNullOrEmpty(ae.id))
- {
- ae.id = Guid.NewGuid().ToString();
- await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(ae, new PartitionKey($"{ae.code}"));
- if (flag)
- {
- music.id = ae.id;
- await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(music, new PartitionKey("ArtMusic"));
- }
- }
- else
- {
- if (flag)
- {
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(music, music.id, new PartitionKey("ArtMusic"));
- }
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(ae, ae.id, new PartitionKey($"{ae.code}"));
- }
- return Ok(new { ae });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/save()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500, msg = ex.Message });
- }
- }
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "teacher,admin,student")]
- [HttpPost("update-state")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> UpdateState(JsonElement request)
- {
- if (!request.TryGetProperty("isAnswer", out JsonElement isAnswer)) return BadRequest();
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- try
- {
- ArtMusic music = new();
- var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.GetString(), new PartitionKey("ArtMusic"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- music = json.ToObject<ArtMusic>();
- music.isAnswer = isAnswer.GetInt32();
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(music, music.id, new PartitionKey($"{music.code}"));
- }
- return Ok(music);
- }
- catch (Exception e)
- {
- return BadRequest(new { msg = e.Message });
- }
- }
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "teacher,admin,student")]
- [HttpPost("upload")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Upload(ArtRecord request)
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
- HttpContext.Items.TryGetValue("Scope", out object scope);
- int userType = $"{scope}".Equals(Constant.ScopeStudent) ? 2 : 1;
- request.school = school;
- request.stuId = userid;
- request.code = "ArtRecord";
- long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- request.createTime = now;
- ArtRecord record;
- StudentArtResult artResult;
- ArtEvaluation art;
- List<string> classIds = new();
- List<GroupListGrp> groups = await GroupListService.GetMemberInGroupList(_coreAPIHttpService, client, _dingDing, userid, userType, school, new List<string> { "class", "teach" });
- foreach (var grp in groups)
- {
- classIds.Add(grp.id);
- }
- if (string.IsNullOrEmpty(request.id))
- {
- request.id = Guid.NewGuid().ToString();
- record = await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(request, new PartitionKey($"{request.code}"));
- }
- else
- {
- record = await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(request, request.id, new PartitionKey($"{request.code}"));
- }
- string rId = string.Format("{0}{1}{2}", request.school, "-", userid);
- //首先根据大ID获取整个活动得内容
- var aresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(request.artId, new PartitionKey($"Art-{school}"));
- if (aresponse.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(aresponse.ContentStream);
- art = json.ToObject<ArtEvaluation>();
- var response = await client.GetContainer("TEAMModelOS", "Student").ReadItemStreamAsync(rId, new PartitionKey($"ArtResult-{request.artId}"));
- if (response.Status == 200)
- {
- using var json_1 = await JsonDocument.ParseAsync(response.ContentStream);
- artResult = json_1.ToObject<StudentArtResult>();
- List<Attachment> files = new();
- files = request.attachments;
- //bool flage = artResult.results.Exists(a => a.taskId == request.acId);
- artResult.results.ForEach(a =>
- {
- if (a.taskId == request.acId)
- {
- a.files = files;
- }
- });
- await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(artResult, artResult.id, new PartitionKey($"{artResult.code}"));
- }
- else
- {
- artResult = new StudentArtResult
- {
- id = rId,
- pk = "ArtResult",
- code = $"ArtResult-{request.artId}",
- studentId = userid,
- picture = picture,
- studentName = name,
- school = school,
- userType = userType,
- artId = request.artId,
- classIds = classIds,
- };
- foreach (var qIds in art.settings)
- {
- foreach (var task in qIds.task)
- {
- ArtQuotaResult quotaResult = new()
- {
- quotaId = qIds.id,
- quotaName = qIds.quotaname,
- quotaType = (int)task.type,
- subjectId = task.subject,
- taskId = task.acId
- };
- if (!string.IsNullOrEmpty(quotaResult.taskId))
- {
- if (quotaResult.taskId.Equals(request.acId))
- {
- quotaResult.files = request.attachments;
- }
- }
- artResult.results.Add(quotaResult);
- }
- }
- await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(artResult, new PartitionKey($"{artResult.code}"));
- }
- }
- return Ok(new { record, code = 200 });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/upload()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500, msg = ex.Message });
- }
- }
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "teacher,admin,student")]
- [HttpPost("upload-all")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> UploadAll(JsonElement element)
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- var (_, _, _, school) = HttpContext.GetAuthTokenInfo();
- if (!element.TryGetProperty("artId", out JsonElement artId)) return BadRequest();
- if (!element.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
- if (!element.TryGetProperty("quotaId", out JsonElement quotaId)) return BadRequest();
- if (!element.TryGetProperty("acId", out JsonElement acId)) return BadRequest();
- if (!element.TryGetProperty("subject", out JsonElement subject)) return BadRequest();
- if (!element.TryGetProperty("stus", out JsonElement stus)) return BadRequest();
- //HttpContext.Items.TryGetValue("Scope", out object scope);
- //int userType = $"{scope}".Equals(Constant.ScopeStudent) ? 2 : 1;
- List<stuFiles> stuFiles = stus.ToObject<List<stuFiles>>();
- List<string> value = new List<string>();
- await foreach (var s in stuTask(stuFiles, client, school, artId.GetString(), classId.GetString(), quotaId.GetString(), acId.GetString(), subject.GetString()))
- {
- if (s.code == 1)
- {
- value.Add(s.value);
- }
- }
- if (value.Count > 0)
- {
- return Ok(new { code = 1, msg = "学生ID导入异常", value = value });
- }
- else
- {
- return Ok(new { code = 0 });
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/uploadAll()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500, msg = ex.Message });
- }
- }
- private async IAsyncEnumerable<(int code, string value)> stuTask(List<stuFiles> stuFiles, CosmosClient client, string school, string artId, string classId, string quotaId, string acId, string subject)
- {
- string queryScore = $" select c.id from c where c.artId ='{artId}' and c.quotaId = '{quotaId}' and c.acId = '{acId}' and c.subject = '{subject}'";
- List<string> ids = new();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryStreamIterator
- (queryText: queryScore, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("ArtRecord") }))
- {
- 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())
- {
- if (obj.TryGetProperty("id", out JsonElement subScore))
- {
- string sId = obj.GetProperty("id").GetString();
- ids.Add(sId);
- }
- }
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemsAsync<ArtRecord>(ids, "ArtRecord");
- // await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, school, new List<string> { $"art/{artId}" });
- foreach (var request in stuFiles)
- {
- string value = "";
- int code = 0;
- try
- {
- long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- ArtRecord record = new()
- {
- school = school,
- stuId = request.stuId,
- artId = artId,
- classId = classId,
- quotaId = quotaId,
- acId = acId,
- subject = subject,
- createTime = now,
- code = "ArtRecord",
- attachments = request.attachments
- };
- StudentArtResult artResult;
- ArtEvaluation art;
- List<string> classIds = new();
- List<GroupListGrp> groups = await GroupListService.GetMemberInGroupList(_coreAPIHttpService, client, _dingDing, request.stuId, request.userType, school, new List<string> { "class", "teach" });
- foreach (var grp in groups)
- {
- classIds.Add(grp.id);
- }
- if (string.IsNullOrEmpty(record.id))
- {
- record.id = Guid.NewGuid().ToString();
- record = await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(record, new PartitionKey($"{record.code}"));
- }
- else
- {
- record = await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(record, record.id, new PartitionKey($"{record.code}"));
- }
- string rId = string.Format("{0}{1}{2}", record.school, "-", record.stuId);
- //首先根据大ID获取整个活动得内容
- var aresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(artId, new PartitionKey($"Art-{school}"));
- if (aresponse.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(aresponse.ContentStream);
- art = json.ToObject<ArtEvaluation>();
- var response = await client.GetContainer("TEAMModelOS", "Student").ReadItemStreamAsync(rId, new PartitionKey($"ArtResult-{artId}"));
- if (response.Status == 200)
- {
- using var json_1 = await JsonDocument.ParseAsync(response.ContentStream);
- artResult = json_1.ToObject<StudentArtResult>();
- List<Attachment> files = new();
- files = request.attachments;
- //bool flage = artResult.results.Exists(a => a.taskId == request.acId);
- artResult.results.ForEach(a =>
- {
- if (a.taskId == acId)
- {
- a.files = files;
- }
- });
- await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(artResult, artResult.id, new PartitionKey($"{artResult.code}"));
- }
- else
- {
- artResult = new StudentArtResult
- {
- id = rId,
- pk = "ArtResult",
- code = $"ArtResult-{artId}",
- studentId = request.stuId,
- //picture = picture,
- studentName = request.name,
- school = school,
- userType = request.userType,
- artId = artId,
- classIds = classIds,
- };
- foreach (var qIds in art.settings)
- {
- foreach (var task in qIds.task)
- {
- ArtQuotaResult quotaResult = new()
- {
- quotaId = qIds.id,
- quotaName = qIds.quotaname,
- quotaType = (int)task.type,
- subjectId = task.subject,
- taskId = task.acId
- };
- if (!string.IsNullOrEmpty(quotaResult.taskId))
- {
- if (quotaResult.taskId.Equals(acId))
- {
- quotaResult.files = request.attachments;
- }
- }
- artResult.results.Add(quotaResult);
- }
- }
- await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(artResult, new PartitionKey($"{artResult.code}"));
- }
- }
- }
- catch (Exception e)
- {
- value = request.stuId;
- code = 1;
- }
- yield return (code, value);
- }
- }
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("delete")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Delete(JsonElement request)
- {
- try
- {
- //object userScope = null;
- //object _standard = null;
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- /* string standard = null;
- HttpContext?.Items?.TryGetValue("Scope", out userScope);
- if (userScope != null && $"{userScope}".Equals(Constant.ScopeTeacher))
- {
- HttpContext?.Items?.TryGetValue("Standard", out _standard);
- standard = _standard != null && string.IsNullOrEmpty($"{userScope}") ? _standard.ToString() : null;
- }*/
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- var client = _azureCosmos.GetCosmosClient();
- var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Art-{code}"));
- if (sresponse.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
- ArtEvaluation art = json.ToObject<ArtEvaluation>();
- //必须是本人或者这个学校的管理者才能删除
- bool flag = false;
- if (art.creatorId == userid)
- {
- flag = true;
- }
- else
- {
- if (art.scope == "school" && art.school.Equals(school))
- {
- flag = true;
- }
- }
- /*try
- {
- (List<RMember> members, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, null, art.school, null);
- await StatisticsService.DoChange(new TeacherTrainChange
- { standard = standard, tmdids = members.Select(x => x.id)?.ToList(), school = art.school, update = new HashSet<string> { StatisticsService.OfflineRecord }, statistics = 0 }, _azureCosmos);
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/delete()ex\n{ex.Message}\n{ex.StackTrace},\n", GroupNames.醍摩豆服務運維群組);
- }*/
- if (flag)
- {
- art.status = 404;
- foreach (var info in art.settings)
- {
- /* if (info.TryGetProperty("examId", out JsonElement eId)) {
- }*/
- foreach (var acs in info.task)
- {
- if (!string.IsNullOrEmpty(acs.acId))
- {
- if (acs.type == 1)
- {
- Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(acs.acId, new PartitionKey($"Exam-{code}"));
- if (response.Status == 200)
- {
- ExamInfo data = JsonDocument.Parse(response.Content).RootElement.Deserialize<ExamInfo>();
- data.status = 404;
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Exam-{code}"));
- }
- }
- }
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(art, art.id, new PartitionKey($"{art.code}"));
- }
- }
- return Ok(new { id });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/delete()\n{e.Message}\n{e.StackTrace},\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("find")]
- public async Task<IActionResult> Find(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- request.TryGetProperty("periodId", out JsonElement period);
- request.TryGetProperty("periodType", out JsonElement periodType);
- if (string.IsNullOrWhiteSpace($"{period}") && string.IsNullOrWhiteSpace($"{periodType}"))
- {
- return BadRequest();
- }
- var client = _azureCosmos.GetCosmosClient();
- StringBuilder stringBuilder = new($"select c.id,c.img,c.name,c.classes,c.code,c.type,c.startTime,c.endTime,c.presenter,c.topic,c.address,c.owner,c.progress from c where (c.status<>404 or IS_DEFINED(c.status) = false )");
- string continuationToken = string.Empty;
- string token = default;
- if (!string.IsNullOrWhiteSpace($"{period}") && !string.IsNullOrWhiteSpace($"{periodType}"))
- {
- stringBuilder.Append($" and (c.period.id = '{period}' or c.periodType = '{periodType}')");
- }
- if (string.IsNullOrWhiteSpace($"{period}") && !string.IsNullOrWhiteSpace($"{periodType}"))
- {
- stringBuilder.Append($" and c.periodType = '{periodType}' ");
- }
- if (!string.IsNullOrWhiteSpace($"{period}") && string.IsNullOrWhiteSpace($"{periodType}"))
- {
- stringBuilder.Append($" and c.period.id = '{period}' ");
- }
- stringBuilder.Append("order by c.createTime desc");
- //是否需要进行分页查询,默认不分页
- bool iscontinuation = false;
- if (request.TryGetProperty("token", out JsonElement token_1))
- {
- token = token_1.GetString();
- iscontinuation = true;
- };
- //默认不指定返回大小
- int? topcout = null;
- if (request.TryGetProperty("count", out JsonElement jcount))
- {
- if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
- {
- topcout = data;
- }
- }
- List<ArtEvaluation> arts = new();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Art-{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())
- {
- arts.Add(obj.ToObject<ArtEvaluation>());
- }
- }
- if (iscontinuation)
- {
- continuationToken = item.GetContinuationToken();
- break;
- }
- }
- arts = arts.Where((x, i) => arts.FindIndex(z => z.id == x.id) == i).ToList();
- return Ok(new { arts });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/find()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin,student")]
- [HttpPost("find-summary")]
- public async Task<IActionResult> FindSummary(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- ArtEvaluation art = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ArtEvaluation>(id.GetString(), new PartitionKey($"Art-{code}"));
- ArtMusic music = new();
- ArtExam ae = new();
- if (art != null)
- {
- string queryArtExam = $" select value(c) from c where c.activityId = '{art.pId}' ";
- //List<ArtExam> aes = new List<ArtExam>();
- /* await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Common).GetItemQueryIterator<ArtExam>
- (queryText: queryArtExam, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtExam") }))
- {
- aes.Add(item);
- }*/
- if (!string.IsNullOrWhiteSpace(art.pId))
- {
- var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.pId, new PartitionKey("ArtMusic"));
- var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.pId, new PartitionKey("ArtExam"));
- if (sresponse.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
- ae = json.ToObject<ArtExam>();
- }
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- music = json.ToObject<ArtMusic>();
- }
- }
- else
- {
- var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.id, new PartitionKey("ArtMusic"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- music = json.ToObject<ArtMusic>();
- }
- }
- (List<RMember> rmembers, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, art.classes, art.school);
- var classes = art.classes.Select(c => new
- {
- id = c,
- groups.Where(g => g.id.Equals(c)).FirstOrDefault().name
- });
- rmembers.ForEach(x =>
- {
- groups.ForEach(z =>
- {
- if (z.members.Exists(y => y.id.Equals(x.id) && y.type == x.type))
- {
- x.groupListIds.Add(z.id);
- }
- });
- });
- List<ArtSubjectScore> subjectScores = new List<ArtSubjectScore>();
- art.subjects.ForEach(z => { subjectScores.Add(new ArtSubjectScore { subjectId = z.id, score = 0 }); });
- var students = rmembers.Select(z => new StudentArtResult
- {
- studentId = z.id,
- studentName = z.name,
- userType = z.type,
- classIds = z.groupListIds,
- school = z.schoolId,
- picture = z.picture,
- artId = $"{id}",
- id = $"{z.schoolId}-{z.id}",
- code = $"ArtResult-{id}",
- pk = "ArtResult",
- ttl = -1,
- subjectScores = subjectScores,
- }).ToList();
- //TODO 缺考人数的结算(多科评量检测,作业的提交)
- if (students.Any())
- {
- string query = $" select value c.id from c where c.id in({string.Join(",", students.Select(x => $"'{x.id}'"))}) ";
- List<string> list = new List<string>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<string>
- (queryText: query, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtResult-{id}") }))
- {
- list.Add(item);
- }
- students.RemoveAll(x => list.Contains(x.id));
- students.ForEach(x =>
- {
- art.settings.ForEach(a =>
- {
- a.task.ForEach(z =>
- {
- ArtQuotaResult quotaResult = new ArtQuotaResult
- {
- taskId = z.acId,
- subjectId = z.subject,
- quotaId = a.id,
- quotaType = z.type.Value,
- quotaName = a.quotaname
- };
- x.results.Add(quotaResult);
- });
- });
- });
- List<Task<ItemResponse<StudentArtResult>>> responses = new();
- students.ForEach(z =>
- {
- responses.Add(client.GetContainer(Constant.TEAMModelOS, Constant.Student).CreateItemAsync(z, new PartitionKey(z.code)));
- });
- if (responses.Count > 0)
- {
- await responses.TaskPage(10);
- }
- }
- if (!string.IsNullOrWhiteSpace(ae.id))
- {
- return Ok(new { art, classes, ae, music, code = 200 });
- }
- else
- {
- return Ok(new { art, classes, music, code = 200 });
- }
- }
- else
- {
- return Ok(new { art, code = 404 });
- }
- }
- catch (CosmosException ex) when (ex.Status == 404)
- {
- return Ok(new { code = 404 });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/FindSummary()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin,student")]
- [HttpPost("find-summary-by-student")]
- public async Task<IActionResult> findByStudent(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- ArtEvaluation art = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ArtEvaluation>(id.GetString(), new PartitionKey($"Art-{code}"));
- ArtMusic music = new();
- if (art != null)
- {
- List<StuActivity> stus = new();
- List<string> wIds = new();
- List<ArtRecord> works = new();
- if (!string.IsNullOrWhiteSpace(art.pId))
- {
- var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.pId, new PartitionKey("ArtMusic"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- music = json.ToObject<ArtMusic>();
- }
- }
- else
- {
- var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.id, new PartitionKey("ArtMusic"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- music = json.ToObject<ArtMusic>();
- }
- }
- foreach (var tt in art.settings)
- {
- foreach (var acs in tt.task)
- {
- if (acs.type == 1)
- {
- var response = await client.GetContainer("TEAMModelOS", "Student").ReadItemStreamAsync(acs.acId, new PartitionKey($"Activity-{school}-{userid}"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- StuActivity info = json.ToObject<StuActivity>();
- stus.Add(info);
- }
- }
- if (acs.type == 2)
- {
- wIds.Add(acs.acId);
- }
- }
- }
- if (wIds.Count > 0)
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<ArtRecord>(
- queryText: $"select value(c) from c where c.stuId = '{userid}' and c.acId in ({string.Join(",", wIds.Select(o => $"'{o}'"))})",
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ArtRecord") }))
- {
- works.Add(item);
- }
- }
- return Ok(new { art, stus, works, music, code = 200 });
- }
- else
- {
- return Ok(new { art, code = 404 });
- }
- }
- catch (CosmosException ex) when (ex.Status == 404)
- {
- return Ok(new { code = 404 });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-student()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("find-summary-by-work")]
- public async Task<IActionResult> findBywork(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
- if (!request.TryGetProperty("subject", out JsonElement subject)) return BadRequest();
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- List<object> works = new();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(
- queryText: $"select c.stuId,c.attachments from c where c.classId = '{classId}' and c.acId = '{id}' and c.subject = '{subject}'",
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ArtRecord") }))
- {
- 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())
- {
- works.Add(obj.ToObject<object>());
- }
- }
- }
- return Ok(new { works, code = 200 });
- }
- catch (CosmosException ex) when (ex.Status == 404)
- {
- return Ok(new { code = 404 });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-work()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("find-by-teacher")]
- public async Task<IActionResult> FindByTeacher(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- var query = $"select c.id,c.img,c.name,c.startTime,c.type,c.endTime,c.presenter,c.topic,c.address,c.owner from c join A0 in c.teachers where A0.id = '{tId}'";
- List<object> arts = new();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{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())
- {
- arts.Add(obj.ToObject<object>());
- }
- }
- }
- return Ok(new { arts });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-by-teacher()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("find-summary-by-teacher")]
- public async Task<IActionResult> FindSummaryByTeacher(JsonElement request)
- {
- try
- {
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- List<object> arts = new();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.teachers where A0.id = '{tId}' and c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{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())
- {
- arts.Add(obj.ToObject<object>());
- }
- }
- }
- return Ok(new { arts });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-teacher()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return Ok(new { code = 500 });
- }
- }
- private class stuFiles
- {
- public string stuId { get; set; }
- public int userType { get; set; }
- public string name { get; set; }
- public List<Attachment> attachments { get; set; } = new List<Attachment>();
- }
- }
- }
|