using Azure.Cosmos; using HTEXLib.COMM.Helpers; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using static TEAMModelOS.SDK.Models.Teacher; namespace TEAMModelOS.SDK.Models { public static class ThirdService { //自动加入学校,加入培训名单,并根据学科进行分组 public static async Task GetScTeacher(ScBindData scBind, Teacher teacher, AzureStorageFactory _azureStorage, AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, IConfiguration _configuration,DingDing _dingDing) { List schools = await _azureStorage.FindListByDict(new Dictionary { { "PartitionKey", "ScSchool" }, { "RowKey", scBind.sid } }); List scTeachers = await _azureStorage.FindListByDict(new Dictionary { { "PartitionKey", "ScTeacher" }, { "TID", scBind.userid }, { "RowKey", $"{scBind.pxid}" } }); if (schools.IsNotEmpty()) { ScSchool scSchool = schools[0]; if (!string.IsNullOrEmpty(scSchool.schoolCode)) { try { School school = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync(scSchool.schoolCode, new PartitionKey("Base")); if (school != null) { if (scTeachers.IsNotEmpty()) { if (string.IsNullOrEmpty(scTeachers[0].tmdid)) { scTeachers[0].tmdid = teacher.id; scTeachers[0].schoolCode = scSchool.schoolCode; scTeachers[0].areaId = school.areaId; await _azureStorage.SaveOrUpdate(scTeachers[0]); } } var sc = teacher.schools.Find(x => x.schoolId.Equals(scSchool.schoolCode)); if (sc != null) { if (string.IsNullOrEmpty(sc.status) || !sc.status.Equals("join")) { sc.status = "join"; try { SchoolTeacher schoolTeacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync(teacher.id, new PartitionKey($"Teacher-{school.id}")); if (schoolTeacher != null) { if (schoolTeacher.roles.IsEmpty() || !schoolTeacher.roles.Contains("teacher")) { schoolTeacher.roles = new List { "teacher" }; } schoolTeacher.status = "join"; schoolTeacher.pk = "Teacher"; schoolTeacher.name = teacher.name; schoolTeacher.picture = teacher.picture; schoolTeacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); schoolTeacher.ttl = -1; schoolTeacher.permissions = schoolTeacher.permissions.IsNotEmpty() ? schoolTeacher.permissions : new List(); await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(schoolTeacher); } } catch (CosmosException ex) { SchoolTeacher schoolTeacher = new SchoolTeacher { id = teacher.id, code = $"Teacher-{school.id}", roles = new List { "teacher" }, permissions = new List(), pk = "Teacher", name = teacher.name, picture = teacher.picture, status = "join", createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), ttl = -1 }; await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(schoolTeacher); } } } else { teacher.schools.Add(new Teacher.TeacherSchool { schoolId = school.id, name = school.name, areaId = school.areaId, picture = school.picture, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), status = "join" }); SchoolTeacher schoolTeacher = new SchoolTeacher { id = teacher.id, code = $"Teacher-{school.id}", roles = new List { "teacher" }, permissions = new List(), pk = "Teacher", name = teacher.name, picture = teacher.picture, status = "join", createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(schoolTeacher); } await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(teacher, teacher.id, new PartitionKey("Base")); //处理培训名单 StringBuilder queryText = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='yxtrain'"); List yxtrain = new List(); await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator(queryText: queryText.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{scSchool.schoolCode}") })) { yxtrain.Add(item); } if (yxtrain.IsNotEmpty()) { string groupName = null; string groupId = Guid.NewGuid().ToString(); if (scTeachers.IsNotEmpty()) { groupName = scTeachers[0].TeacherXK; if (!string.IsNullOrEmpty(groupName)) { var mebers = yxtrain.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.groupName) && y.groupName.Equals(groupName)); if (mebers != null && mebers.Count() > 0) { groupId = mebers.First().groupId; } } else { groupId = null; } } else { groupId = null; } var meber = yxtrain.SelectMany(x => x.members).Where(y => y.id.Equals(teacher.id)); //不在研修名单 if (meber == null || meber.Count() <= 0) { yxtrain[0].members.Add(new Member { id = teacher.id, type = 1, groupId = groupId, groupName = groupName }); await GroupListService.UpsertList(yxtrain[0], _azureCosmos, _configuration, _serviceBus); } else { if (string.IsNullOrEmpty(meber.First().groupId) || string.IsNullOrEmpty(meber.First().groupName)) { meber.ToList().ForEach(x => { x.groupId = groupId; x.groupName = groupName; }); await GroupListService.UpsertList(yxtrain[0], _azureCosmos, _configuration, _serviceBus); } } } else { string groupName = null; if (scTeachers.IsNotEmpty()) { groupName = scTeachers[0].TeacherXK; } string groupId = null; if (!string.IsNullOrEmpty(groupName)) { groupId = Guid.NewGuid().ToString(); } GroupList groupList = new GroupList() { id = Guid.NewGuid().ToString(), code = $"GroupList-{scSchool.schoolCode}", creatorId = teacher.id, type = "yxtrain", year = DateTimeOffset.UtcNow.Year, members = new List { new Member { id = teacher.id, type = 1, groupId = groupId, groupName = groupName } }, scope = "school", school = scSchool.schoolCode, name = "研修名单", pk = "GroupList", ttl = -1 }; await GroupListService.UpsertList(groupList, _azureCosmos, _configuration, _serviceBus); } } } catch (Exception ex) { await _dingDing.SendBotMsg($"OS\n自动加入学校,加入研修名单出现异常:{ex.StackTrace},{ex.Message}GetScTeacher", GroupNames.成都开发測試群組); } } } return null; } public static async Task<(string accessConfig, Area area, AreaSetting setting)> GetAccessConfig(CosmosClient client,string standard) { Area area = null; await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal"). GetItemQueryIterator($"select value(c) from c where c.standard='{standard}'", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base-Area") })) { area = item; break; } AreaSetting setting = null; if (area != null) { try { setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync(area.id, new PartitionKey("AreaSetting")); } catch (CosmosException) { setting = null; } } if (setting == null|| string.IsNullOrEmpty(setting.accessConfig)) { return (null,null,null); } else { return (setting.accessConfig,area,setting); } } public static async Task> GetDiagnosisList(CosmosClient client, string standard,DingDing dingDing, AreaSetting setting, HttpTrigger httpTrigger, Teacher teacher, TEAMModelOS.Models.Option _option, AzureStorageFactory _azureStorage) { List abilityNos = new List() ; var config= setting.accessConfig.ToObject(); config.TryGetProperty("config", out JsonElement _config); if ($"{_config}".Equals("scsyxpt")) { var binds = teacher.binds.FindAll(x => x.type.Equals("scsyxpt")); var datas = binds.SelectMany(x => x.data).Where(y=>y.Contains("scsyxpt")); HashSet pxids = new HashSet (); if (datas != null) { datas.ToList().ForEach(x => { var data = x.ToObject(); if (!string.IsNullOrEmpty(data?.pxid)) { pxids.Add(data.pxid); } }); } foreach (var pxid in pxids) { List teachers = await _azureStorage.FindListByDict(new Dictionary { { "PartitionKey", "ScTeacher" }, { "PXID", pxid } }); Dictionary dict = new Dictionary(); if (teachers.IsNotEmpty()) { dict = new Dictionary() { { "accessConfig", setting.accessConfig }, { "pxid", pxid }, { "areaId", setting.id } ,{ "schoolCode", teachers[0].schoolCode} }; } else { dict = new Dictionary() { { "accessConfig", setting.accessConfig }, { "pxid", pxid }, { "areaId", setting.id } }; } (int status, string json) = await httpTrigger.RequestHttpTrigger(dict, _option.Location, "GetDiagnosisListByProject_V2"); if (status == 200) { List nos = json.ToObject>(); if (nos.IsNotEmpty()) { abilityNos.AddRange(nos); } } } } //获取能力点 List abilities = null; if (abilityNos.IsNotEmpty()) { abilities = new List(); StringBuilder sql = new StringBuilder($"select value(c) from c where c.no in ({string.Join(",", abilityNos.Select(x => $"'{x}'"))})"); await foreach (var item in client.GetContainer("TEAMModelOS", "Normal") .GetItemQueryIterator(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") })) { abilities.Add(item); } } return abilities; } } }