123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- using Azure.Cosmos;
- using HTEXLib.COMM.Helpers;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelFunction;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- namespace TEAMModelOS.Controllers
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- //[Authorize(Roles = "IES5")]
- [Route("school/classroom")]
- [ApiController]
- public class ClassController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- private readonly Option _option;
- private readonly DingDing _dingDing;
- public ClassController(AzureCosmosFactory azureCosmos, DingDing dingDing,
- IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("upsert")]
- public async ValueTask<IActionResult> Upsert(JsonElement requert)
- {
- try
- {
- if (!requert.TryGetProperty("classroom", out JsonElement room)) return BadRequest();
- Class classroom = room.ToObject<Class>();
- var client = _azureCosmos.GetCosmosClient();
- classroom.code = "Class-" + classroom.school;
- if (string.IsNullOrEmpty(classroom.id))
- {
- List<string> resultIds = new List<string>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.periodId = '{classroom.periodId}' and c.no = '{classroom.no}' and c.year = '{classroom.year}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(classroom.code) }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- resultIds.Add(account.GetProperty("id").GetString());
- }
- }
- }
- if (resultIds.Count > 0)
- {
- return Ok(new { error = ResponseCode.DATA_EXIST, V = "班级编码已经存在!" });
- }
- classroom.id = Guid.NewGuid().ToString();
- classroom = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(classroom, new PartitionKey(classroom.code));
- }
- else {
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(classroom.id, new PartitionKey(classroom.code));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- Class @class = json.ToObject<Class>();
- if (!@class.no.Equals(classroom.no))
- {
- List<string> resultIds = new List<string>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.year = '{classroom.year}' and c.no = '{classroom.no}' and c.periodId = '{classroom.periodId}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(classroom.code) }))
- {
- using var document = await JsonDocument.ParseAsync(item.ContentStream);
- if (document.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = document.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- resultIds.Add(account.GetProperty("id").GetString());
- }
- }
- }
- if (resultIds.Count > 0)
- {
- return Ok(new { error = ResponseCode.DATA_EXIST, V = "班级编码已经存在!" });
- }
- }
- classroom = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(classroom, classroom.id, new PartitionKey(classroom.code));
- }
- else
- {
- classroom = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(classroom, new PartitionKey(classroom.code));
- }
- }
- return Ok(new { classroom });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},class/Upsert()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("upsert-group")]
- public async ValueTask<IActionResult> UpsertGroup(JsonElement requert)
- {
- try
- {
- List<Student> students = new List<Student>();
- if (!requert.TryGetProperty("students", out JsonElement stus)) return BadRequest();
- students = stus.ToObject<List<Student>>();
- var client = _azureCosmos.GetCosmosClient();
- foreach (Student stu in students)
- {
- var response = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemStreamAsync(stu.id, new PartitionKey($"{stu.code}"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- Student stuInfo = json.ToObject<Student>();
- stuInfo.groupId = stu.groupId;
- stuInfo.groupName = stu.groupName;
- await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync(stuInfo, stuInfo.id, new PartitionKey($"{stuInfo.code}"));
- }
- }
- /*Class classroom = new Class();
- if (!requert.TryGetProperty("classroom", out JsonElement room)) return BadRequest();
- //if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
- classroom = room.ToObject<Class>();
-
- var sresponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(classroom.id, new PartitionKey($"{classroom.code}"));
- if (sresponse.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
- Class classroom1 = json.ToObject<Class>();
- //classroom1.students = classroom.students;
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(classroom1, classroom1.id, new PartitionKey($"{classroom.code}"));
- }*/
- return Ok(new { students });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},class/UpsertGroup()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("find")]
- public async Task<IActionResult> Find(JsonElement requert)
- {
- //ResponseBuilder builder = ResponseBuilder.custom();
- if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- List<object> classrooms = new List<object>();
- StringBuilder sql = new StringBuilder();
- sql.Append("select c.id,c.no,c.point,c.name,c.teacher,c.periodId,c.gradeId,c.sn,c.style,c.scope,c.type,c.code,c.openType,c.x,c.y,ARRAY_LENGTH(c.students) AS studCount from c ");
- Dictionary<string, object> dict = new Dictionary<string, object>();
- var emobj = requert.EnumerateObject();
- while (emobj.MoveNext())
- {
- dict[emobj.Current.Name] = emobj.Current.Value;
- }
- //处理code
- if (dict.TryGetValue("school_code", out object _))
- {
- dict.Remove("school_code");
- }
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_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())
- {
- classrooms.Add(obj.ToObject<object>());
- }
- }
- }
- return Ok(new { classrooms });
- /*List<Classroom> sc = await _azureCosmos.FindByDict<Classroom>(request);
- return builder.Data(sc).build();*/
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},class/Find()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- //
-
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("find-new-students")]
- public async Task<IActionResult> FindNewStudent(JsonElement requert)
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- if (!requert.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
- requert.TryGetProperty("school_code", out JsonElement schoolId);
- List<string> classes = ids.ToObject<List<string>>();
- List<dynamic> list = new List<dynamic>();
- foreach (string cls in classes) {
- (List<TmdInfo> tmdids, List<StuInfo> students, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuListInStu(client, _dingDing, new List<string> { cls }, $"{schoolId}");
- if (classLists.IsNotEmpty()) {
- list.Add(new { tmdids, students,@class= classLists.First() });
- }
- }
- return Ok(new { list });
- }
- catch (CosmosException ex) {
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("find-students")]
- public async Task<IActionResult> FindStudent(JsonElement requert)
- {
- //ResponseBuilder builder = ResponseBuilder.custom();
- //if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- if (!requert.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
- if (!requert.TryGetProperty("school_code", out JsonElement schoolId)) return BadRequest();
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- List<object> stus = new List<object>();
- List<string> stuIds = new List<string>();
- string info = "";
- for (int i = 0; i < ids.GetArrayLength(); i++)
- {
- //ids.Add(id[i].ToJsonString());
- info += ids[i].ToJsonString() + ",";
- }
- //List<object> scList = new List<object>();
- List<object> suList = new List<object>();
- List<(string id, string name, string pic, string code, string classId ,string groupId, string groupName, string no)> listStudent = new List<(string id, string name, string pic, string code, string classId, string groupId, string groupName, string no)>();
- //var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(ids[i].GetString(), new PartitionKey($"Class-{schoolId}"));
- string className = "";
- /* if (response.Status == 200)
- {
- using var document = await JsonDocument.ParseAsync(response.ContentStream);
- Class @class = document.ToObject<Class>();
- className = @class.name;
- }*/
- List<(string id, string name)> listClassList = new List<(string id,string name)>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(
- queryText: $"select c.id,c.name from c where c.id in ({info[0..^1]})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schoolId}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- listClassList.Add((account.GetProperty("id").GetString(), account.GetProperty("name").GetString()));
- //stuIds.Add(account.GetProperty("id").GetString());
- }
- }
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(
- queryText: $"select c.id,c.name,c.classId,c.code,c.groupId,c.groupName,c.no,c.picture from c where c.classId in ({info[0..^1]})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- stuIds.Add(account.GetProperty("id").GetString());
- listStudent.Add((account.GetProperty("id").GetString(),
- account.GetProperty("name").GetString(),
- account.GetProperty("picture").GetString(),
- account.GetProperty("code").GetString(),
- account.GetProperty("classId").GetString(),
- account.GetProperty("groupId").GetString(),
- account.GetProperty("groupName").GetString(),
- account.GetProperty("no").GetString()));
- }
- }
- }
- var grpBalance = listStudent.GroupBy(m => new { m.classId }).Distinct().Select(t =>t.ToList().Select(o =>
- new
- {
- o.id,
- o.name,
- o.pic,
- o.code,
- o.classId,
- className = listClassList.FirstOrDefault(c => c.id == o.classId).name,
- o.groupId,
- o.groupName,
- o.no
- })
- );
- stus.AddRange(grpBalance);
- //处理校本的自定义名单
- List<(string id, string name, string pic, string code, string classId, string groupId, string groupName, string no)> listStuList = new List<(string id, string name, string pic, string code, string classId, string groupId, string groupName, string no)>();
- List<(string id, string code, string stuId, string name)> stuListMore = new List<(string id, string code, string stuId, string name)>();
- if (scope.ToString().Equals("school", StringComparison.OrdinalIgnoreCase))
- {
- List<StuList> stuLists = new List<StuList>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({info[0..^1]})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StuList-{schoolId}") }))
- {
- stuLists.Add(item);
- }
- List<string> stuInfo = new List<string>();
- if (stuLists.Count > 0)
- {
- foreach (StuList stuList in stuLists)
- {
- foreach (Students stu in stuList.students)
- {
- stuInfo.Add(stu.id);
- stuListMore.Add((stu.id,
- stu.code,
- stuList.id,
- stuList.name));
- }
- }
- //根据学生ID CODE获取学生基本信息
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(
- queryText: $"select c.id,c.name,c.classId,c.code,c.groupId,c.groupName,c.no,c.picture from c where c.id in ({string.Join(",", stuInfo.Select(o => $"'{o}'"))})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- listStuList.Add((account.GetProperty("id").GetString(),
- account.GetProperty("name").GetString(),
- account.GetProperty("picture").GetString(),
- account.GetProperty("code").GetString(),
- account.GetProperty("classId").GetString(),
- account.GetProperty("groupId").GetString(),
- account.GetProperty("groupName").GetString(),
- account.GetProperty("no").GetString()));
- }
- }
- }
- var infos = stuListMore.GroupBy(m => new { m.stuId }).Distinct().Select(t =>
- t.ToList().Select(o =>
- new
- {
- o.id,
- o.code,
- name = listStuList.FirstOrDefault(c => c.id == o.id).name,
- pic = listStuList.FirstOrDefault(c => c.id == o.id).pic,
- classId = o.stuId,
- className = o.name,
- groupId = listStuList.FirstOrDefault(c => c.id == o.id).groupId,
- groupName = listStuList.FirstOrDefault(c => c.id == o.id).groupName,
- no = listStuList.FirstOrDefault(c => c.id == o.id).no
- })
- );
- stus.AddRange(infos);
- }
- }
- else
- {
- //处理发布对象为自选名单(个人)
- List<StuList> stuLists1 = new List<StuList>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({info[0..^1]})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StuList") }))
- {
- stuLists1.Add(item);
- }
- if (stuLists1.Count > 0)
- {
- foreach (StuList stuList in stuLists1)
- {
- foreach (Students students in stuList.students)
- {
- //根据学生ID CODE获取学生基本信息
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(
- queryText: $"select c.id,c.name,c.classId,c.code,c.groupId,c.groupName,c.no,c.picture from c where c.id = '{students.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{students.code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- listStuList.Add((account.GetProperty("id").GetString(),
- account.GetProperty("name").GetString(),
- account.GetProperty("picture").GetString(),
- account.GetProperty("code").GetString(),
- account.GetProperty("classId").GetString(),
- account.GetProperty("groupId").GetString(),
- account.GetProperty("groupName").GetString(),
- account.GetProperty("no").GetString()));
- }
- }
- }
- stuListMore.Add((students.id,
- students.code,
- stuList.id,
- stuList.name));
- }
- if (stuList.tmids.Count > 0)
- {
- foreach (string tid in stuList.tmids)
- {
- stuListMore.Add((tid,
- default,
- stuList.id,
- stuList.name));
- }
- }
- }
- var infos = stuListMore.GroupBy(m => new { m.stuId }).Distinct().Select(t =>
- t.ToList().Select(o =>
- new
- {
- o.id,
- o.code,
- name = listStuList.FirstOrDefault(c => c.id == o.id).name,
- pic = listStuList.FirstOrDefault(c => c.id == o.id).pic,
- classId = o.stuId,
- className = o.name,
- groupId = listStuList.FirstOrDefault(c => c.id == o.id).groupId,
- groupName = listStuList.FirstOrDefault(c => c.id == o.id).groupName,
- no = listStuList.FirstOrDefault(c => c.id == o.id).no
- })
- );
- stus.AddRange(infos);
- }
- }
- return Ok(new { stus });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},class/FindStudent()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("delete")]
- public async Task<IActionResult> Delete(JsonElement request)
- {
- if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- //if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- try
- {
- //string school_code = code.ToString().Substring(6);
- string school_code = code.ToString();
- Class classroom = new Class();
- var client = _azureCosmos.GetCosmosClient();
- /* if (scope.ToString().Equals("school"))
- {*/
- classroom = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemAsync<Class>(id.ToString(), new PartitionKey($"Class-{school_code}"));
- //List<TeacherCourse> classes = new List<TeacherCourse>();
- //await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"CourseManagement-{school_code}"));
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.classes where A0.id = '{id}'"))
- //{
- // 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())
- // {
- // classes.Add(obj.ToObject<TeacherCourse>());
- // }
- // }
- //}
- //for (int i = 0; i < classes.Count; i++)
- //{
- // bool flag = false;
- // for (int j = 0; j < classes[i].classes.Count; j++)
- // {
- // if (classes[i].classes[j].id.Equals(id.ToString()))
- // {
- // classes[i].classes.Remove(classes[i].classes[j]);
- // flag = true;
- // }
- // }
- // if (flag)
- // {
- // await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(classes[i], classes[i].id, new PartitionKey($"{classes[i].code}"));
- // }
- //}
- List<Student> students = new List<Student>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select * from c where c.classId='{id}' ", requestOptions:new QueryRequestOptions { PartitionKey= new PartitionKey ($"Base-{code}") }))
- {
- item.classId = "";
- students.Add(item);
- }
- List<Task<ItemResponse<Student>>> tasks = new List<Task<ItemResponse<Student>>>();
- foreach (var stu in students) {
- tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync(stu, stu.id, new PartitionKey(($"Base-{code}"))));
- }
- await Task.WhenAll(tasks);
- //}
- return Ok(new { classroom });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},class/Delete()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- //ResponseBuilder builder = ResponseBuilder.custom();
- /*List<IdPk> idPks = new List<IdPk>();
- if (request.TryGetProperty("id", out JsonElement id))
- {
- List<Classroom> sc = await _azureCosmos.FindByDict<Classroom>(request);
- if (sc.IsNotEmpty())
- {
- await _azureCosmos.DeleteAll<ClassStudent>(new Dictionary<string, object> { { "id", sc.Select(x => x.code).ToArray() } });
- idPks = await _azureCosmos.DeleteAll<Classroom>(sc);
- //builder.Data(idPks);
- }
- }
- else
- {
- return Ok(new { erro = ResponseCode.PARAMS_ERROR, V = "参数未定义!" });
- }
- return Ok(new { idPks });*/
- /*List<VoteRecord> sc = await _azureCosmos.FindByDict<VoteRecord>(request);
- await _azureCosmos.DeleteAll<VoteRecord>(sc);
- return Ok();*/
- //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync("", new PartitionKey($"Class-{code}"));
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("hiteach-link")]
- public async Task<IActionResult> HiteachLink(JsonElement request)
- {
- // 必要檢查
- if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
- // if (!request.TryGetProperty("linkList", out JsonElement links)) return BadRequest();
- if (!request.TryGetProperty("uuid", out JsonElement a)) return BadRequest();
- if (!request.TryGetProperty("uuid2", out JsonElement b)) return BadRequest();
- if (!request.TryGetProperty("pid", out JsonElement d)) return BadRequest();
- if (!request.TryGetProperty("action", out JsonElement e)) return BadRequest();
- if (!request.TryGetProperty("classId", out JsonElement c)) return BadRequest();
- // 如果是link classId 必填
- string action = e.ToString();
- if (action != "link" && action != "unLink") return BadRequest();
- try
- {
- // [變數宣告]
- string school_code = code.GetString(); // 學校簡碼
- string uuid = a.GetString(); // uuid
- string uuid2 = b.GetString(); // uuid2
- string pid = d.GetString(); // id
- string classId = c.GetString(); // classId
- // [取得DB資料]
- var client = _azureCosmos.GetCosmosClient();
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
- var error = 0;
- if (response.Status == 200)
- {
- var json = await JsonDocument.ParseAsync(response.ContentStream);
- //軟體
- SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
- SerialInfoBaseWithdeviceBound updSerialInfo = schoolProductItem.serial.Where(s => s.id == pid).FirstOrDefault();
- if (updSerialInfo != null)
- {
- deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.uuid == uuid && d.uuid2 == uuid2).FirstOrDefault();
- if (updDeviceBound != null)
- {
- if (action .Equals("link"))
- {
- if (updDeviceBound.classId != null)
- {
- error = 1;
- }
- else
- {
- updDeviceBound.classId = classId;
- }
- }
- else if (action .Equals("unLink"))
- {
- updDeviceBound.classId = null;
- }
- if (error == 0) await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
- }
- }
- }
- return Ok(new { error });
- }
- catch (Exception ex)
- {
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("hiteach-unlink-classId")]
- public async Task<IActionResult> HiteachUnlinkByClassId(JsonElement request)
- {
- // 必要檢查
- if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("classId", out JsonElement id)) return BadRequest();
- try
- {
- // [變數宣告]
- string school_code = code.ToString(); // 學校簡碼
- string classId = id.ToString(); // 教室ID
- // [取得DB資料]
- var client = _azureCosmos.GetCosmosClient();
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
- if (response.Status == 200)
- {
- var json = await JsonDocument.ParseAsync(response.ContentStream);
- //軟體
- SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
- foreach (SerialInfoBaseWithdeviceBound updSerialInfo in schoolProductItem.serial)
- {
- if (updSerialInfo.deviceBound != null)
- {
- deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.classId == classId).FirstOrDefault();
- if (updDeviceBound != null)
- {
- updDeviceBound.classId = null;
- }
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
- }
- return Ok(new { error = 0 });
- }
- catch (Exception ex)
- {
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "Teacher")]
- [HttpPost("name")]
- public async Task<IActionResult> GetNameList(JsonElement request)
- {
- if (!request.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- //List<string> id = ids.ToObject<List<string>>();
- string info = "";
- for (int i = 0; i < ids.GetArrayLength(); i++)
- {
- //ids.Add(id[i].ToJsonString());
- info += ids[i].ToJsonString() + ",";
- }
- List<object> ClassName = new List<object>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(
- queryText: $"select c.id,c.name from c where c.id in ({info[0..^1]})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{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())
- {
- ClassName.Add(obj.ToObject<object>());
- }
- }
- }
- return Ok(new { ClassName });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},class/name()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- }
- }
|