123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- using 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;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Extension;
- using static TEAMModelOS.Controllers.SchoolTeacherController;
- using HTEXLib.COMM.Helpers;
- using TEAMModelOS.Filter;
- using static TEAMModelOS.Controllers.AbilitySubController;
- using Microsoft.AspNetCore.Authorization;
- namespace TEAMModelOS.Controllers
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [Route("research")]
- [ApiController]
- public class AbilityStatisticsController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public AbilityStatisticsController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 更新教师最终学习分数。
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [HttpPost("upsert-teacher-finalScore")]
- [AuthToken(Roles = "admin,teacher", Permissions = "train-appraise")]
- public async Task<IActionResult> UpsertFinalScore(JsonElement request)
- {
- var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
- var client = _azureCosmos.GetCosmosClient();
- if (!request.TryGetProperty("finalScore", out JsonElement _finalScore)) return BadRequest();
- if (!request.TryGetProperty("tmdids", out JsonElement _tmdids)) return BadRequest();
- int finalScore = -999;
- int.TryParse($"{_finalScore}", out finalScore);
- List<string> tmdids = _tmdids.ToObject<List<string>>();
- if (tmdids.IsNotEmpty() && finalScore >= -1 && finalScore <= 2)
- {
- List<TeacherTrain> trains = new List<TeacherTrain>();
- string insql = $"select value(c) from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher")
- .GetItemQueryIterator<TeacherTrain>(queryText: insql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TeacherTrain-{school}") }))
- {
- trains.Add(item);
- }
- List<Task<ItemResponse<TeacherTrain>>> teacherTrains = new List<Task<ItemResponse<TeacherTrain>>>();
- trains.ForEach(x => {
- x.finalScore = finalScore;
- teacherTrains.Add(client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(x, x.id, new PartitionKey(x.code)));
- });
- int pagesize = 50;
- if (teacherTrains.Count <= pagesize)
- {
- await Task.WhenAll(teacherTrains);
- }
- else
- {
- int pages = (teacherTrains.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
- for (int i = 0; i < pages; i++)
- {
- var listssb = teacherTrains.Skip((i) * pagesize).Take(pagesize).ToList();
- await Task.WhenAll(listssb);
- }
- }
- return Ok(new { status = 200 });
- }
- else
- {
- return Ok(new { });
- }
- }
- /// <summary>
- /// 提交学习总结、。
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("upsert-teacher-summary")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin,area")]
- public async Task<IActionResult> UpsertSummary(JsonElement request)
- {
- var client = _azureCosmos.GetCosmosClient();
- request.TryGetProperty("summary", out JsonElement _summary);
- if (string.IsNullOrEmpty($"{_summary}"))
- {
- return BadRequest();
- }
- var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
- if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
- try
- {
- TeacherTrain teacherTrain = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherTrain>(userid, new PartitionKey($"TeacherTrain-{school}"));
- teacherTrain.name = name;
- teacherTrain.picture = picture;
- teacherTrain.summary = $"{_summary}";
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<TeacherTrain>(teacherTrain, userid, new PartitionKey($"TeacherTrain-{school}"));
- return Ok(new { status = 200 });
- }
- catch (CosmosException ex)
- {
- return Ok(new { error = 1, msg = "你没有学习记录" });
- }
- }
- /// <summary>
- /// 区级数据统计
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("statistics-area")]
- [AuthToken(Roles = "teacher,admin,area")]
- public async Task<IActionResult> StatisticsArea(JsonElement request)
- {
- var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
- request.TryGetProperty("standard", out JsonElement _standard);
- string standard = $"{_standard}";
- if (string.IsNullOrEmpty(standard))
- {
- return BadRequest();
- }
- var client = _azureCosmos.GetCosmosClient();
- Area area = null;
- string sql = $"select value(c) from c where c.standard='{standard}'";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
- {
- area = item;
- }
- AreaSetting setting = null;
- if (area != null)
- {
- try
- {
- setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
- }
- catch (CosmosException)
- {
- setting = null;
- }
- }
- if (setting == null)
- {
- setting = new AreaSetting
- {
- allTime = 50,
- classTime = 5,
- submitTime = 15,
- onlineTime = 20,
- offlineTime = 10,
- lessonMinutes = 45,
- };
- }
- try
- {
- List<TeacherTrain> teacherTrains = new List<TeacherTrain>();
- List<School> schools = new List<School>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: $"select value(c) from c where c.areaId='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
- {
- schools.Add(item);
- }
- List<Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>> trains = new List<Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>>();
- int countArea = 0;
- int appraiseArea = 0;
- List<SchoolInfos> schoolInfos = new List<SchoolInfos>();
- foreach (var school in schools)
- {
- int count = 0;
- int appraise = 0;
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<bool>(queryText: $"select value(ARRAY_CONTAINS( c.permissions,'train-appraise')) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school.id}") }))
- {
- if (item)
- {
- appraise += 1;
- appraiseArea += 1;
- }
- countArea += 1;
- count += 1;
- }
- schoolInfos.Add(new SchoolInfos { schoolId = school.id, schoolName = school.name, picture = school.picture, teacherCount = count, appraiseCount = appraise });
- //增加评审人员总人数,学习总人数。
- trains.Add(StatisticsService.StatisticsSchool(school.id, setting, area, client, _dingDing, null));
- }
- int pagesize = 100;
- if (trains.Count <= pagesize)
- {
- (List<TeacherTrain> trains, List<RGroupList> yxtrain)[] tasks = await Task.WhenAll(trains);
- tasks.ToList().ForEach(x => {
- teacherTrains.AddRange(x.trains);
- schoolInfos.ForEach(y => {
- var list = x.yxtrain.Find(z => z.school.Equals(y.schoolId));
- if (list != null)
- {
- y.trainCount = list.members.Count;
- }
- });
- });
- }
- else
- {
- int pages = (trains.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
- for (int i = 0; i < pages; i++)
- {
- var listssb = trains.Skip((i) * pagesize).Take(pagesize).ToList();
- (List<TeacherTrain> trains, List<RGroupList> yxtrain)[] tasks = await Task.WhenAll(listssb);
- tasks.ToList().ForEach(x => {
- teacherTrains.AddRange(x.trains);
- schoolInfos.ForEach(y => {
- var list = x.yxtrain.Find(z => z.school.Equals(y.schoolId));
- if (list != null)
- {
- y.trainCount = list.members.Count;
- }
- });
- });
- }
- }
- long totalTime = teacherTrains.Select(x => x.totalTime).Sum();
- int hgcount = teacherTrains.Where(x => x.finalScore == 1 || x.finalScore == 2).Count();
- var teacherAilities = teacherTrains.SelectMany(x => x.currency.teacherAilities);
- setting.accessConfig = null;
- teacherTrains.ForEach(x => {
- x.currency.videoTime = (int)x.currency.videoTime;
- x.currency.teacherAilities.ForEach(y => {
- y.videoTime = (int)y.videoTime;
- y.onlineTime = (int)y.onlineTime;
- y.debateOrther = y.debateOrther > 0 ? y.debateOrther : 0;
- });
- });
- return Ok(new { teacherTrains, setting, schools = schoolInfos, totalTime, hgcount, teacherAilities, teacherCount = countArea, appraiseCount = appraiseArea });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},UpsertSubmitScore/UpsertSubmitScore()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest(new { ex.Message, ex.StackTrace });
- }
- }
- public class SchoolInfos
- {
- public string schoolId { get; set; }
- public string schoolName { get; set; }
- public string picture { get; set; }
- public int teacherCount { get; set; }
- public int appraiseCount { get; set; }
- public int trainCount { get; set; }
- }
- /// <summary>
- /// 校级数据统计
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-school-appraise")]
- [AuthToken(Roles = "teacher,admin,area")]
- public async Task<IActionResult> GetSchoolAppraise(JsonElement request)
- {
- request.TryGetProperty("school", out JsonElement _school);
- string school = "";
- var (userid, _, _, __school) = HttpContext.GetAuthTokenInfo();
- if (!string.IsNullOrEmpty($"{_school}"))
- {
- school = $"{_school}";
- }
- else
- {
- school = __school;
- }
- request.TryGetProperty("update", out JsonElement _update);
- HashSet<string> update = null;
- if (_update.ValueKind.Equals(JsonValueKind.Array))
- {
- update = _update.ToObject<HashSet<string>>();
- }
- if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- Area area = null;
- string sql = $"select value(c) from c where c.standard='{standard}'";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
- {
- area = item;
- }
- AreaSetting setting = null;
- if (area != null)
- {
- try
- {
- //优先找校级
- setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>(school, new PartitionKey("AreaSetting"));
- }
- catch (CosmosException)
- {
- try
- {
- setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
- }
- catch (CosmosException)
- {
- setting = null;
- }
- }
- }
- if (setting == null)
- {
- setting = new AreaSetting
- {
- allTime = 50,
- classTime = 5,
- submitTime = 15,
- onlineTime = 20,
- offlineTime = 10,
- lessonMinutes = 45,
- };
- }
- try
- {
- int count = 0;
- int appraise = 0;
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<bool>(queryText: $" select value(ARRAY_CONTAINS( c.permissions,'train-appraise') ) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
- {
- if (item)
- {
- appraise += 1;
- }
- count += 1;
- }
- //增加评审人员总人数,学习总人数。
- (List<TeacherTrain> teacherTrains, List<RGroupList> yxtrain) = await StatisticsService.StatisticsSchool(school, setting, area, client, _dingDing, update);
- teacherTrains.ForEach(x => {
- x.currency.videoTime = (int)x.currency.videoTime;
- x.currency.teacherAilities.ForEach(y => {
- y.videoTime = (int)y.videoTime;
- y.onlineTime = (int)y.onlineTime;
- y.debateOrther= y.debateOrther>0? y.debateOrther: 0;
- });
- });
- //try
- //{
- // teacherTrain = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherTrain>(userid, new PartitionKey($"TeacherTrain-{school}"));
- //}
- //catch (CosmosException)
- //{
- // await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<TeacherTrain>(teacherTrain, new PartitionKey($"TeacherTrain-{school}"));
- //}
- setting.accessConfig = null;
- return Ok(new { teacherTrains, setting, teacherCount = count, appraiseCount = appraise, yxtrain });
- }
- catch (Exception ex)
- {
- return BadRequest(new { ex.Message, ex.StackTrace });
- }
- }
- /// <summary>
- /// 教师个人数据统计
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("statistics-teacher")]
- [AuthToken(Roles = "teacher,admin,area")]
- public async Task<IActionResult> StatisticsTeacher(JsonElement request)
- {
- //{"tmdid":"1595321354","school":"hbcn"}
- var (_userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
- if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- request.TryGetProperty("update", out JsonElement _update);
- HashSet<string> update = null;
- if (_update.ValueKind.Equals(JsonValueKind.Array))
- {
- update = _update.ToObject<HashSet<string>>();
- }
- request.TryGetProperty("tmdid", out JsonElement _tmdid);
- string tmdid = "";
- if (_tmdid.ValueKind.Equals(JsonValueKind.String))
- {
- tmdid = $"{_tmdid}";
- }
- else
- {
- tmdid = $"{_userid}";
- }
- Area area = null;
- string sql = $"select value(c) from c where c.standard='{standard}'";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
- {
- area = item;
- }
- AreaSetting setting = null;
- if (area != null)
- {
- try
- {
- //优先找校级
- setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>(school, new PartitionKey("AreaSetting"));
- }
- catch (CosmosException)
- {
- try
- {
- setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
- }
- catch (CosmosException)
- {
- setting = null;
- }
- }
- }
- if (setting == null)
- {
- setting = new AreaSetting
- {
- allTime = 50,
- classTime = 5,
- submitTime = 15,
- onlineTime = 20,
- offlineTime = 10,
- lessonMinutes = 45,
- };
- }
- try
- {
- TeacherTrain teacherTrain = null;
- try
- {
- teacherTrain = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherTrain>(tmdid, new PartitionKey($"TeacherTrain-{school}"));
- teacherTrain.name = name;
- teacherTrain.picture = picture;
- if (update != null && update.Count > 0)
- {
- foreach (var up in update)
- {
- teacherTrain.update.Add(up);
- }
- }
- ///强制教师个人信息统计的时候 都再去统计一次。
- teacherTrain.update.Add(StatisticsService.TeacherAbility);
- if (teacherTrain.update.Count > 0)
- {
- await StatisticsService.StatisticsTeacher(teacherTrain, setting, area, client, null);
- }
- }
- catch (CosmosException)
- {
- teacherTrain = await StatisticsService.StatisticsTeacher(new TeacherTrain
- {
- pk = "TeacherTrain",
- id = tmdid,
- code = $"TeacherTrain-{school}",
- tmdid = tmdid,
- school = school,
- name = name,
- picture = picture,
- update = new HashSet<string> { StatisticsService.TeacherAbility,
- StatisticsService.TeacherClass, StatisticsService.OfflineRecord }
- }, setting, area, client, null);
- }
- setting.accessConfig = null;
- teacherTrain.currency.videoTime = (int)teacherTrain.currency.videoTime;
- teacherTrain.currency.teacherAilities.ForEach(y => {
- y.videoTime = (int)y.videoTime;
- y.onlineTime = (int)y.onlineTime;
- y.debateOrther = y.debateOrther > 0 ? y.debateOrther : 0;
- });
- return Ok(new { teacherTrain, setting });
- }
- catch (Exception ex)
- {
- return BadRequest(new { ex.Message, ex.StackTrace });
- }
- }
- }
- }
|