123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.Context.BI;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelBI.Controllers.BIStudent
- {
- [Route("student")]
- [ApiController]
- public class StudentController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public StudentController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 依据学生Id查询详情 数据管理工具——查询工具
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-info")]
- public async Task<IActionResult> GetInfo(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("studentId", out JsonElement studentId)) return BadRequest();
- jsonElement.TryGetProperty("code", out JsonElement code);
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<object> objs = new();
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIteratorSql(queryText: $"select value(c) from c where c.id='{studentId}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- objs.Add(obj);
- }
- }
- }
- return Ok(new { state = 200, students = objs });
- }
- /// <summary>
- /// 学生活动中间表ClassIds去重
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-classIds")]
- public async Task<IActionResult> CorrectActivityClassIds(JsonElement jsonElement)
- {
- try
- {
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- //string sqlTxt = "SELECT select c.id,c.code,c.classIds FROM c where c.pk='Activity' and c.classIds <> []";
- string sqlTxt = "select c.id,c.code,c.classIds from c where c.school='cswznb' and c.classIds <> []";
- List<CorrectStu> correctStus = new();
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- obj.TryGetProperty("id", out JsonElement tempTd);
- obj.TryGetProperty("code", out JsonElement code);
- obj.TryGetProperty("classIds", out JsonElement _classIds);
- List<string> templist = _classIds.ToObject<List<string>>();
- //List<string> newList = templist.Distinct().ToList(); //Equals实现去重
- //List<string> newLis1 = templist.Where((x, i) => templist.FindIndex(z => z == x) == i).ToList(); //Lambda表达式去重
- HashSet<string> hashSet = new(templist);//哈希自动去重
- if (hashSet.Count != templist.Count)
- {
- CorrectStu correctList = new() { id = $"{tempTd}", code = $"{code}", classIds = hashSet.ToList() };
- correctStus.Add(correctList);
- }
- }
- }
- if (correctStus.Count > 0)
- {
- /*foreach (var correct in correctStus)
- {
- StuActivity stuActivity = await cosmosClient.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuActivity>(correct.id, new PartitionKey(correct.code));
- if (stuActivity != null)
- {
- stuActivity.classIds = correct.classIds;
- await cosmosClient.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuActivity>(stuActivity, stuActivity.id, new PartitionKey(stuActivity.code));
- }
- }*/
- return Ok(new { state = 200, msg = "去重成功" });
- }
- else
- {
- return Ok(new { state = 201, msg = "学生活动中间表无重复班级ID" });
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /stuactivity/get-classIds \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return Ok(new { state = 401 });
- }
- }
- public record CorrectStu
- {
- public string id { get; set; }
- public string code { get; set; }
- public List<string> classIds { get; set; }
- }
- }
- }
|