123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using Microsoft.Azure.Cosmos;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
- namespace TEAMModelBI.Tool.CosmosBank
- {
- public class SchoolWay
- {
- /// <summary>
- /// 依据钉钉人员信息中的学校列表
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="schoolIds"></param>
- /// <returns></returns>
- public async static Task<List<BaseInfo>> GetSchoolInfos(CosmosClient cosmosClient, List<string> schoolIds)
- {
- List<BaseInfo> advSchools = new();
- foreach (var item in schoolIds)
- {
- await foreach (var info in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<BaseInfo>(queryText: $"select c.id,c.name,c.picture,c.areaId from c where c.id='{item}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- advSchools.Add(info);
- }
- }
- return advSchools;
- }
- /// <summary>
- /// 移除和添加顾问方法
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="ddUserInfo"></param>
- /// <param name="schoolIds"></param>
- /// <param name="busy"></param>
- /// <returns></returns>
- public async static Task<List<string>> SchoolAdviser(CosmosClient cosmosClient, DingDingUserInfo ddUserInfo, List<string> schoolIds, string role, string busy)
- {
- List<string> noSchool = new();
- foreach (var item in schoolIds)
- {
- BIRelation bIRelation = new();
- var resBiRel = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(item, new PartitionKey($"BIRel"));
- if (resBiRel.StatusCode == System.Net.HttpStatusCode.OK)
- {
- JsonDocument jsonD = JsonDocument.Parse(resBiRel.Content);
- bIRelation = jsonD.RootElement.ToObject<BIRelation>();
- }
- if (role.Equals("assist"))
- {
- switch ($"{busy}")
- {
- case "add":
- var addAss = bIRelation.assists.Find(f => f.id.Equals(ddUserInfo.tmdId));
- if (addAss == null)
- bIRelation.assists.Add(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
- break;
- case "del":
- var delAss = bIRelation.assists.Find(f => f.id.Equals(ddUserInfo.tmdId));
- if (delAss != null)
- bIRelation.assists.Remove(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
- break;
- }
- }
- if (role.Equals("sales"))
- {
- switch ($"{busy}")
- {
- case "add":
- var tempSa = bIRelation.sales.Find(f => f.id.Equals(ddUserInfo.tmdId));
- if (tempSa == null)
- bIRelation.sales.Add(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
- break;
- case "del":
- var delSa = bIRelation.sales.Find(f => f.id.Equals(ddUserInfo.tmdId));
- if (delSa != null)
- bIRelation.sales.Remove(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
- break;
- }
- }
- bIRelation.upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<BIRelation>(bIRelation, bIRelation.id, new PartitionKey("BIRel"));
- }
- return noSchool;
- //List<string> noSchool = new();
- //Teacher teacher = null;
- //bool isReadTec = false;
- //bool isAddUpd = false;
- //string joinStatus = "";
- //if (role.Equals("assist"))
- // joinStatus = "顾问";
- //if (role.Equals("sales"))
- // joinStatus = "销售";
- //var tecResponse = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(ddUserInfo.tmdId, new PartitionKey($"Base"));
- //if (tecResponse.Status == 200)
- //{
- // JsonDocument jsonDocument = JsonDocument.Parse(tecResponse.Content);
- // teacher = jsonDocument.RootElement.ToObject<Teacher>();
- //}
- //else
- //{
- // teacher = new Teacher()
- // {
- // id = ddUserInfo.tmdId,
- // name = ddUserInfo.tmdName,
- // picture = ddUserInfo.picture,
- // size = 1,
- // code = "Base",
- // pk = "Base",
- // };
- // isAddUpd = true;
- //}
- //List<Task<ItemResponse<SchoolTeacher>>> addSchoolTeache = new();
- //List<Task<ItemResponse<SchoolTeacher>>> updSchoolTeache = new();
- //foreach (var schoolId in schoolIds)
- //{
- // School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>(schoolId, new PartitionKey("Base"));
- // var tempSc = teacher.schools.Find(x => x.schoolId == schoolId);
- // if (tempSc == null)
- // {
- // teacher.schools.Add(new Teacher.TeacherSchool { schoolId = school.id, name = school.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), picture = school.picture, areaId = school.areaId });
- // isReadTec = true;
- // }
- // SchoolTeacher schoolTeacher = new();
- // var stResponse = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(ddUserInfo.tmdId, new PartitionKey($"Teacher-{schoolId}"));
- // if (stResponse.Status == 200)
- // {
- // JsonDocument tempJson = JsonDocument.Parse(stResponse.Content);
- // schoolTeacher = tempJson.RootElement.ToObject<SchoolTeacher>();
- // if ($"{busy}".Equals("add"))
- // {
- // if (!schoolTeacher.roles.Contains($"{role}"))
- // {
- // schoolTeacher.roles.Add($"{role}");
- // addSchoolTeache.Add(cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{schoolId}")));
- // }
- // else
- // noSchool.Add(schoolId);
- // }
- // if ($"{busy}".Equals("del"))
- // {
- // if (schoolTeacher.roles.Contains($"{role}"))
- // {
- // schoolTeacher.roles.Remove($"{role}");
- // teacher.schools.Remove(teacher.schools.Find(x => x.schoolId == schoolId));
- // updSchoolTeache.Add(cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{schoolId}")));
- // }
- // else
- // noSchool.Add(schoolId);
- // }
- // }
- // else
- // {
- // schoolTeacher = new SchoolTeacher()
- // {
- // id = ddUserInfo.tmdId,
- // name = ddUserInfo.tmdName,
- // picture = ddUserInfo.picture,
- // job = joinStatus,
- // subjectIds = new List<string>(),
- // roles = new List<string> { $"{role}" },
- // permissions = new List<string>(),
- // status = "join",
- // code = $"Teacher-{schoolId}",
- // pk = "Teacher",
- // createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
- // };
- // addSchoolTeache.Add(cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync(schoolTeacher, new PartitionKey(schoolTeacher.code)));
- // }
- //}
- //if (isReadTec == true)
- //{
- // if (isAddUpd == true)
- // {
- // await cosmosClient.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey($"Base"));
- // }
- // else
- // {
- // await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
- // }
- //}
- //if (updSchoolTeache.Count > 0)
- //{
- // if (updSchoolTeache.Count < 256)
- // {
- // await Task.WhenAll(updSchoolTeache);
- // }
- // else
- // {
- // int pages = (updSchoolTeache.Count + 255) / 256;
- // for (int i = 0; i < pages; i++)
- // {
- // List<Task<ItemResponse<SchoolTeacher>>> tempAbility = updSchoolTeache.Skip((i) * 256).Take(256).ToList();
- // await Task.WhenAll(tempAbility);
- // }
- // }
- //}
- //if (addSchoolTeache.Count > 0)
- //{
- // if (addSchoolTeache.Count < 256)
- // {
- // await Task.WhenAll(addSchoolTeache);
- // }
- // else
- // {
- // int pages = (addSchoolTeache.Count + 255) / 256;
- // for (int i = 0; i < pages; i++)
- // {
- // List<Task<ItemResponse<SchoolTeacher>>> tempAbility = addSchoolTeache.Skip((i) * 256).Take(256).ToList();
- // await Task.WhenAll(tempAbility);
- // }
- // }
- //}
- //return noSchool;
- }
- }
- }
|