using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.Models;
using TEAMModelOS.SDK.Models;
using TEAMModelOS.SDK.Models.Service; //通知
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using TEAMModelOS.SDK.Extension;
using System.Text.Json; //获取Json数据引用
using Azure.Cosmos;
using TEAMModelOS.Filter;
using HTEXLib.COMM.Helpers;
using TEAMModelOS.SDK;
namespace TEAMModeBI.Controllers.BIHome
{
///
/// 研修数据统计
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Route("bihome")]
[ApiController]
public class StudyStatisController : ControllerBase
{
private readonly AzureCosmosFactory _azureCosmos;
private readonly IConfiguration _configuration;
private readonly NotificationService _notificationService; //消息通知
private readonly DingDing _dingDing; //钉钉消息通知
private readonly Option _option; //记录信息
public StudyStatisController(AzureCosmosFactory azureCosmos,IConfiguration configuration, NotificationService notificationService,DingDing dingDing, IOptionsSnapshot option)
{
_azureCosmos = azureCosmos;
_configuration = configuration;
_notificationService = notificationService;
_dingDing = dingDing;
_option = option?.Value;
}
[ProducesDefaultResponseType]
[HttpPost("get-studyonline-statis")]
//[AuthToken(Roles= "teacher,student,admin,area")]
public async Task GetStudyOnLine(JsonElement jsonElement)
{
//var (userid, _, _, __school) = HttpContext.GetAuthTokenInfo();//取得token信息
try
{
if (!jsonElement.TryGetProperty("school", out JsonElement school)) return BadRequest();
if (!jsonElement.TryGetProperty("areaID", out JsonElement areaID)) return BadRequest();
var client = _azureCosmos.GetCosmosClient();
AreaSetting setting = null; //统计数据
//查询学校基础信息
School schoolBase = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync($"{school}", new PartitionKey("Base"));
//获取学校的地区ID
string areadid = schoolBase.areaId;
//查询区域信息
Area area = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync ($"{areaID}", new PartitionKey("Base-Area"));
if (area != null)
{
setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync(area.id, new PartitionKey("AreaSetting"));
}
if (setting == null)
{
setting = new AreaSetting
{
allTime = 50,
classTime = 5,
submitTime = 15,
onlineTime = 20,
offlineTime = 10,
lessonMinutes = 45,
};
}
List schoolList = new List();
//await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: $"select value(c) from c where c.areaId='{area.id}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: $"select value(c) from c where c.areaId='{area.id}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
{
schoolList.Add(item);
}
HashSet dimensions = new HashSet();
List abilities = new List();
List areaDimensionCount = new List();
await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator(queryText: $"select c.id,c.name,c.no,c.dimension,c.env from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{area.standard}") }))
{
dimensions.Add(item.dimension);
abilities.Add(item);
}
dimensions.ToList().ForEach(x =>
{
areaDimensionCount.Add(new DimensionCount { dimension = x, count = 0 });
});
List abilitie_Id = abilities.Select(x => x.id).ToList();
List areaAbilityCounts = abilities.Select(x => new AbilityCount { abilityEnv = x.env, abilityId = x.id, abilityName = x.name, abilityNo = x.no, dimension = x.dimension, count = 0 }).ToList();
List schoolDatas = new List();
int schoolCount = 0;
int teacherCount = 0;
//在线时长
int videoTime = 0;
//参与人数
int joinCount = 0;
//能力点订阅数量
int subCount = 0;
//已完成50个学时的人数
int ok50TimeCount = 0;
//已完成能力点的校园评审数量
int hasScoreCount = 0;
//已经提交作品的数量
int hasSubmitCount = 0;
// 未提交的
int unSubmitCount = 0;
//未完成能力点学习未完成的
int unDoneCount = 0;
//合格的能力点作品数量
int okCount = 0;
int schoolVideoTime = 0;
///人数
//线上完成人数
int onlineTeacherCount = 0;
//线下完成人数
int offlineTeacherCount = 0;
//应用考核完成人数
int schoolScoreTeacherCount = 0;
//课堂实录完成人数
int classVideoTeacherCount = 0;
///学时
//线上完成学时
int onlineTeacherTime = 0;
//线下完成学时
int offlineTeacherTime = 0;
//应用考核完成学时
int schoolScoreTeacherTime = 0;
//课堂实录完成学时
int classVideoTeacherTime = 0;
List studies = new List();
foreach (var schoolitem in schoolList)
{
List study_list = new List();
await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Study-{schoolitem.id}") }))
{
if (item.tchLists.IsNotEmpty())
{
study_list.Add(item);
}
}
HashSet groups = new HashSet();
HashSet groupNames = new HashSet();
List classVideos = new List();
List abilitySubs = new List();
await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ClassVideo-{schoolitem.id}") }))
{
classVideos.Add(item);
}
List tchLists = new List();
await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator(queryText: $"select value(c) from c where c.type='research' ",
requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{schoolitem.id}") }))
{
groups.Add(item.name);
tchLists.Add(item);
}
List ids = tchLists.Select(x => x.id).ToList();
if (!ids.IsNotEmpty())
{
ids.Add("default");
}
(List tmdInfos, List classInfos) = await GroupListService.GetStutmdidListids(client, _dingDing, ids, $"{school}");
// (List tmdInfos, List classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
//总人数
int teacherCount1 = tmdInfos.Count;
List groupMembers = new();
foreach (var tmd in tmdInfos)
{
await foreach (var sub in client.GetContainer("TEAMModelOS", "Teacher")
.GetItemQueryIterator(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilitySub-{schoolitem.id}-{tmd.id}") }))
{
abilitySubs.Add(sub);
}
//总学时
int alltime = 0;
//已学习的能力点
int allAbilityCount = 0;
//获得的在线学时
int onlineTime = 0;
//获得的线下学时
int offlinelTime = 0;
//获得的作品提交,并通过学校评审的学时
int schoolScoreTime = 0;
//获得的课堂实录
int classVideoTime = 0;
//教师应提交的作品数量
int tchSubmitCount = 0;
//教师未提交的作品数量
int tchUnSubmitCount = 0;
//通过能力点自测的数量
int hasAbilityExercise = 0;
//教师订阅的能力点数量
int thcSubCount = 0;
//教师完成能力点学校的数量
int thcSubDoneCount = 0;
//参加校本线下研修的活动
int offlineSchoolCount = 0;
//参加区级线下研修的活动
int offlineAreaCount = 0;
//完成的校本线下研修活动
int offlineSchoolDoneCount = 0;
//完成的区级线下研修或的
int offlineAreaDoneCount = 0;
int examCount = 0;
int voteCount = 0;
int surveyCount = 0;
int examDoneCount = 0;
int voteDoneCount = 0;
int surveyDoneCount = 0;
int examAreaCount = 0;
int voteAreaCount = 0;
int surveyAreaCount = 0;
int examAreaDoneCount = 0;
int voteAreaDoneCount = 0;
int surveyAreaDoneCount = 0;
await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Study' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{tmd.id}") }))
{
if (item.taskStatus > 0)
{
offlinelTime += 1;
}
if (!string.IsNullOrEmpty(item.owner))
{
if (item.owner.Equals("school"))
{
offlineSchoolCount += 1;
if (item.taskStatus > 0)
{
offlineSchoolDoneCount += 1;
}
}
else if (item.owner.Equals("area"))
{
offlineAreaCount += 1;
if (item.taskStatus > 0)
{
offlineAreaDoneCount += 1;
}
}
}
}
await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher") .GetItemQueryIterator(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{tmd.id}") }))
{
if (item.owner.Equals("school"))
{
examCount += 1;
if (item.taskStatus > 0)
{
examDoneCount += 1;
}
}
if (item.owner.Equals("area"))
{
examAreaCount += 1;
if (item.taskStatus > 0)
{
examAreaDoneCount += 1;
}
}
}
await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
.GetItemQueryIterator(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{tmd.id}") }))
{
if (item.owner.Equals("school"))
{
surveyCount += 1;
if (item.taskStatus > 0)
{
surveyDoneCount += 1;
}
}
if (item.owner.Equals("area"))
{
surveyAreaCount += 1;
if (item.taskStatus > 0)
{
surveyAreaDoneCount += 1;
}
}
}
await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{tmd.id}") }))
{
if (item.owner.Equals("school"))
{
voteCount += 1;
if (item.taskStatus > 0)
{
voteDoneCount += 1;
}
}
if (item.owner.Equals("area"))
{
voteAreaCount += 1;
if (item.taskStatus > 0)
{
voteAreaDoneCount += 1;
}
}
}
bool isJoinVideo = false;
ClassVideo classVideo = classVideos.Find(x => x.id.Equals(tmd.id));
if (classVideo != null && classVideo.files.IsNotEmpty())
{
isJoinVideo = true;
classVideo.files.ForEach(x =>
{
if (x.score > 0)
{
classVideoTime += 1;
}
});
}
List subs = new();
abilitySubs.Where(x => x.code.Equals($"AbilitySub-{schoolitem.id}-{tmd.id}")).ToList().ForEach(item =>
{
subCount += 1;
thcSubCount += 1;
if (item.exerciseScore > 0)
{
hasAbilityExercise += 1;
}
if (item.uploads.IsNotEmpty())
{
//已经有作品提交的
hasSubmitCount += 1;
tchSubmitCount += 1;
}
else
{
//未提交作品的
unSubmitCount += 1;
tchUnSubmitCount += 1;
}
if (!item.done)
{
unDoneCount += 1;
}
else
{
thcSubDoneCount += 1;
}
allAbilityCount += item.abilityCount;
//onlineTime += item.hour;
int vtime = (int)item.videoRcds.Sum(x => x.time);
videoTime += vtime;
schoolVideoTime = schoolVideoTime + vtime;
int lesson = vtime / setting.lessonMinutes;
onlineTime += lesson;
if (item.otherScore.IsNotEmpty())
{
var schoolScore = item.otherScore.Where(x => x.roleType.Equals("school")).FirstOrDefault();
if (schoolScore != null)
{
if (schoolScore.score > 0)
{
okCount += 1;
schoolScoreTime += 1;
}
else
{
hasScoreCount += 1;
}
}
}
Ability ability = abilities.Find(x => x.id.Equals(item.id));
if (ability != null)
{
subs.Add(new SubStatistic
{
abilityId = item.id,
code = item.code,
uploadCount = item.uploads.IsNotEmpty() ? item.uploads.Count : 0,
otherScoreCount = item.otherScore.IsNotEmpty() ? item.otherScore.Count : 0,
comidCount = 0,
self = item.self,
abilityName = ability.name,
no = ability.no,
dimension = ability.dimension,
env = ability.env
});
}
});
//处理单项 超标准 学时
alltime += onlineTime >= setting.onlineTime ? setting.onlineTime : onlineTime;
alltime += offlinelTime >= setting.offlineTime ? setting.offlineTime : offlinelTime;
alltime += schoolScoreTime >= setting.submitTime ? setting.submitTime : schoolScoreTime;
alltime += classVideoTime >= setting.classTime ? setting.classTime : classVideoTime;
//人数
onlineTeacherCount = onlineTime >= setting.onlineTime ? onlineTeacherCount + 1 : onlineTeacherCount;
offlineTeacherCount = offlinelTime >= setting.offlineTime ? offlineTeacherCount + 1 : offlineTeacherCount;
schoolScoreTeacherCount = schoolScoreTime >= setting.submitTime ? schoolScoreTeacherCount + 1 : schoolScoreTeacherCount;
classVideoTeacherCount = classVideoTime >= setting.classTime ? classVideoTeacherCount + 1 : classVideoTeacherCount;
//学时
onlineTeacherTime = onlineTime >= setting.onlineTime ? onlineTeacherTime + setting.onlineTime : onlineTeacherTime + onlineTime;
offlineTeacherTime = offlinelTime >= setting.offlineTime ? offlineTeacherTime + setting.offlineTime : offlineTeacherTime + offlinelTime;
schoolScoreTeacherTime = schoolScoreTime >= setting.submitTime ? schoolScoreTeacherTime + setting.submitTime : schoolScoreTeacherTime + schoolScoreTime;
classVideoTeacherTime = classVideoTime >= setting.classTime ? classVideoTeacherTime + setting.classTime : classVideoTeacherTime + classVideoTime;
if (alltime >= setting.allTime)
{
ok50TimeCount += 1;
}
var classes = classInfos.Where(y => y.members.Select(z => z.id).Contains(tmd.id)).ToList();
//表示有订阅,或者有线下活动参与的,或者有课堂实录视频上传的
if (subs.Count > 0 || schoolScoreTime > 0 || isJoinVideo)
{
//参训人数表示
joinCount += 1;
groupMembers.Add(new TeacherStatistic
{
tchSubmitCount = tchSubmitCount,
tchUnSubmitCount = tchUnSubmitCount,
thcSubCount = thcSubCount,
tmdname = tmd.name,
tmdid = tmd.id,
picture = tmd.picture,
groups = classes.Select(x => new TeacherGroup { groupId = x.id, groupName = x.name }).ToList(),
subs = subs,
alltime = alltime,
allAbilityCount = allAbilityCount,
onlineTime = onlineTime,
offlinelTime = offlinelTime,
schoolScoreTime = schoolScoreTime,
classVideoTime = classVideoTime,
hasAbilityExercise = hasAbilityExercise,
thcSubDoneCount = thcSubDoneCount,
offlineSchoolCount = offlineSchoolCount,
offlineAreaCount = offlineAreaCount,
offlineSchoolDoneCount = offlineSchoolDoneCount,
offlineAreaDoneCount = offlineAreaDoneCount,
examCount = examCount,
voteCount = voteCount,
surveyCount = surveyCount,
examDoneCount = examDoneCount,
voteDoneCount = voteDoneCount,
surveyDoneCount = surveyDoneCount,
examAreaCount = examAreaCount,
voteAreaCount = voteAreaCount,
surveyAreaCount = surveyAreaCount,
examAreaDoneCount = examAreaDoneCount,
voteAreaDoneCount = voteAreaDoneCount,
surveyAreaDoneCount = surveyAreaDoneCount,
videoTime = videoTime
});
}
}
List dimensionCount = new List();
foreach (var dms in dimensions)
{
var list = groupMembers.SelectMany(x => x.subs.Where(y => y.dimension.Equals(dms))).ToList();
dimensionCount.Add(new DimensionCount { dimension = dms, count = list.IsNotEmpty() ? list.Count : 0 });
}
List abilityCount = new List();
foreach (var abilityId in abilitie_Id)
{
var list = groupMembers.SelectMany(x => x.subs.Where(y => y.abilityId.Equals(abilityId))).ToList();
Ability ability = abilities.Find(x => x.id.Equals(abilityId));
abilityCount.Add(new AbilityCount { abilityId = abilityId, abilityName = ability.name, dimension = ability.dimension, abilityNo = ability.no, abilityEnv = ability.env, count = list.IsNotEmpty() ? list.Count : 0 });
}
SchoolCount schoolCountModel = new SchoolCount
{
name = schoolitem.name,
id = schoolitem.id,
picture = schoolitem.picture,
doneCount = ok50TimeCount,
joinCount = joinCount,
teacherCount = teacherCount,
onlineTeacherTime = onlineTeacherTime,
offlineTeacherTime = offlineTeacherTime,
schoolScoreTeacherTime = schoolScoreTeacherTime,
classVideoTeacherTime = classVideoTeacherTime,
onlineTeacherCount = onlineTeacherCount,
offlineTeacherCount = offlineTeacherCount,
schoolScoreTeacherCount = schoolScoreTeacherCount,
classVideoTeacherCount = classVideoTeacherCount,
videoTime = schoolVideoTime
};
}
Dictionary dict = new Dictionary();
studies.ForEach(x => {
dict[x.id] = x;
});
var studyTypes = dict.Values.ToList().GroupBy(x => x.type).Select(y => new { type = y.Key, count = y.Count() });
return Ok(new
{
setting,
areaDimensionCount,
areaAbilityCounts,
teacherCount,
joinCount,
schoolCount,
videoTime,
schoolDatas,
onlineTeacherCount,
offlineTeacherCount,
schoolScoreTeacherCount,
classVideoTeacherCount,
studyTypes,
onlineTeacherTime,
offlineTeacherTime,
schoolScoreTeacherTime,
classVideoTeacherTime,
ok50TimeCount
});
}
catch (Exception ex)
{
//await _dingDing.SendBotMsg($"OS,{_option.Location},\n{ex.Message}{ex.StackTrace}{jsonElement.ToJsonString()}{userid}{__school}", GroupNames.醍摩豆服務運維群組);
return Ok(new { error = 1 });
}
}
public record DimensionCount
{
public string dimension { get; set; }
public int count { get; set; }
}
public record AbilityCount
{
public string abilityId { get; set; }
public string abilityName { get; set; }
public string dimension { get; set; }
public string abilityNo { get; set; }
public string abilityEnv { get; set; }
public int count { get; set; }
}
public record SchoolCount
{
public string name { get; set; }
public string id { get; set; }
public string picture { get; set; }
public int teacherCount { get; set; }
public int joinCount { get; set; }
public int doneCount { get; set; }
public int videoTime { get; set; }
//人数
///线上完成人数
public int onlineTeacherCount { get; set; }
///线下完成人数
public int offlineTeacherCount { get; set; }
///应用考核完成人数
public int schoolScoreTeacherCount { get; set; }
//课堂实录完成人数
public int classVideoTeacherCount { get; set; }
///学时
///线上完成学时
public int onlineTeacherTime { get; set; }
///线下完成学时
public int offlineTeacherTime { get; set; }
///应用考核完成学时
public int schoolScoreTeacherTime { get; set; }
//课堂实录完成学时
public int classVideoTeacherTime { get; set; }
}
public record TeacherStatistic
{
public List subs { get; set; }
public string tmdname { get; set; }
public string tmdid { get; set; }
public string picture { get; set; }
public List groups { get; set; } = new List();
public int alltime { get; set; }
public int allAbilityCount { get; set; }
public int onlineTime { get; set; }
public int offlinelTime { get; set; }
public int schoolScoreTime { get; set; }
public int classVideoTime { get; set; }
public int tchSubmitCount { get; set; }
public int tchUnSubmitCount { get; set; }
public int thcSubCount { get; set; }
public int thcSubDoneCount { get; set; }
public int hasAbilityExercise { get; set; }
public int onlineCount { get; set; }
public int offlineSchoolCount { get; set; }
public int offlineAreaCount { get; set; }
public int offlineSchoolDoneCount { get; set; }
public int offlineAreaDoneCount { get; set; }
public int examCount { get; set; }
public int voteCount { get; set; }
public int surveyCount { get; set; }
public int examDoneCount { get; set; }
public int voteDoneCount { get; set; }
public int surveyDoneCount { get; set; }
public int examAreaCount { get; set; }
public int voteAreaCount { get; set; }
public int surveyAreaCount { get; set; }
public int examAreaDoneCount { get; set; }
public int voteAreaDoneCount { get; set; }
public int surveyAreaDoneCount { get; set; }
public int videoTime { get; set; }
}
public record SubStatistic
{
public string abilityId { get; set; }
public string code { get; set; }
public int uploadCount { get; set; }
public int otherScoreCount { get; set; }
public int self { get; set; }
public int comidCount { get; set; }
public string abilityName { get; set; }
public string no { get; set; }
public string dimension { get; set; }
public string env { get; set; }
}
public record TeacherGroup
{
public string groupName { get; set; }
public string groupId { get; set; }
}
}
}