SchoolWay.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Microsoft.Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelBI.Models;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.Models.Cosmos.BI;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  13. namespace TEAMModelBI.Tool.CosmosBank
  14. {
  15. public class SchoolWay
  16. {
  17. /// <summary>
  18. /// 依据钉钉人员信息中的学校列表
  19. /// </summary>
  20. /// <param name="cosmosClient"></param>
  21. /// <param name="schoolIds"></param>
  22. /// <returns></returns>
  23. public async static Task<List<BaseInfo>> GetSchoolInfos(CosmosClient cosmosClient, List<string> schoolIds)
  24. {
  25. List<BaseInfo> advSchools = new();
  26. foreach (var item in schoolIds)
  27. {
  28. 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") }))
  29. {
  30. advSchools.Add(info);
  31. }
  32. }
  33. return advSchools;
  34. }
  35. /// <summary>
  36. /// 移除和添加顾问方法
  37. /// </summary>
  38. /// <param name="cosmosClient"></param>
  39. /// <param name="ddUserInfo"></param>
  40. /// <param name="schoolIds"></param>
  41. /// <param name="busy"></param>
  42. /// <returns></returns>
  43. public async static Task<List<string>> SchoolAdviser(CosmosClient cosmosClient, DingDingUserInfo ddUserInfo, List<string> schoolIds, string role, string busy)
  44. {
  45. List<string> noSchool = new();
  46. foreach (var item in schoolIds)
  47. {
  48. BIRelation bIRelation = new();
  49. var resBiRel = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(item, new PartitionKey($"BIRel"));
  50. if (resBiRel.StatusCode == System.Net.HttpStatusCode.OK)
  51. {
  52. JsonDocument jsonD = JsonDocument.Parse(resBiRel.Content);
  53. bIRelation = jsonD.RootElement.ToObject<BIRelation>();
  54. }
  55. if (role.Equals("assist"))
  56. {
  57. switch ($"{busy}")
  58. {
  59. case "add":
  60. var addAss = bIRelation.assists.Find(f => f.id.Equals(ddUserInfo.tmdId));
  61. if (addAss == null)
  62. bIRelation.assists.Add(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
  63. break;
  64. case "del":
  65. var delAss = bIRelation.assists.Find(f => f.id.Equals(ddUserInfo.tmdId));
  66. if (delAss != null)
  67. bIRelation.assists.Remove(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
  68. break;
  69. }
  70. }
  71. if (role.Equals("sales"))
  72. {
  73. switch ($"{busy}")
  74. {
  75. case "add":
  76. var tempSa = bIRelation.sales.Find(f => f.id.Equals(ddUserInfo.tmdId));
  77. if (tempSa == null)
  78. bIRelation.sales.Add(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
  79. break;
  80. case "del":
  81. var delSa = bIRelation.sales.Find(f => f.id.Equals(ddUserInfo.tmdId));
  82. if (delSa != null)
  83. bIRelation.sales.Remove(new IdInfo() { id = ddUserInfo.tmdId, name = ddUserInfo.tmdName, picture = ddUserInfo.picture });
  84. break;
  85. }
  86. }
  87. bIRelation.upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  88. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<BIRelation>(bIRelation, bIRelation.id, new PartitionKey("BIRel"));
  89. }
  90. return noSchool;
  91. //List<string> noSchool = new();
  92. //Teacher teacher = null;
  93. //bool isReadTec = false;
  94. //bool isAddUpd = false;
  95. //string joinStatus = "";
  96. //if (role.Equals("assist"))
  97. // joinStatus = "顾问";
  98. //if (role.Equals("sales"))
  99. // joinStatus = "销售";
  100. //var tecResponse = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(ddUserInfo.tmdId, new PartitionKey($"Base"));
  101. //if (tecResponse.Status == 200)
  102. //{
  103. // JsonDocument jsonDocument = JsonDocument.Parse(tecResponse.Content);
  104. // teacher = jsonDocument.RootElement.ToObject<Teacher>();
  105. //}
  106. //else
  107. //{
  108. // teacher = new Teacher()
  109. // {
  110. // id = ddUserInfo.tmdId,
  111. // name = ddUserInfo.tmdName,
  112. // picture = ddUserInfo.picture,
  113. // size = 1,
  114. // code = "Base",
  115. // pk = "Base",
  116. // };
  117. // isAddUpd = true;
  118. //}
  119. //List<Task<ItemResponse<SchoolTeacher>>> addSchoolTeache = new();
  120. //List<Task<ItemResponse<SchoolTeacher>>> updSchoolTeache = new();
  121. //foreach (var schoolId in schoolIds)
  122. //{
  123. // School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>(schoolId, new PartitionKey("Base"));
  124. // var tempSc = teacher.schools.Find(x => x.schoolId == schoolId);
  125. // if (tempSc == null)
  126. // {
  127. // teacher.schools.Add(new Teacher.TeacherSchool { schoolId = school.id, name = school.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), picture = school.picture, areaId = school.areaId });
  128. // isReadTec = true;
  129. // }
  130. // SchoolTeacher schoolTeacher = new();
  131. // var stResponse = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(ddUserInfo.tmdId, new PartitionKey($"Teacher-{schoolId}"));
  132. // if (stResponse.Status == 200)
  133. // {
  134. // JsonDocument tempJson = JsonDocument.Parse(stResponse.Content);
  135. // schoolTeacher = tempJson.RootElement.ToObject<SchoolTeacher>();
  136. // if ($"{busy}".Equals("add"))
  137. // {
  138. // if (!schoolTeacher.roles.Contains($"{role}"))
  139. // {
  140. // schoolTeacher.roles.Add($"{role}");
  141. // addSchoolTeache.Add(cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{schoolId}")));
  142. // }
  143. // else
  144. // noSchool.Add(schoolId);
  145. // }
  146. // if ($"{busy}".Equals("del"))
  147. // {
  148. // if (schoolTeacher.roles.Contains($"{role}"))
  149. // {
  150. // schoolTeacher.roles.Remove($"{role}");
  151. // teacher.schools.Remove(teacher.schools.Find(x => x.schoolId == schoolId));
  152. // updSchoolTeache.Add(cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{schoolId}")));
  153. // }
  154. // else
  155. // noSchool.Add(schoolId);
  156. // }
  157. // }
  158. // else
  159. // {
  160. // schoolTeacher = new SchoolTeacher()
  161. // {
  162. // id = ddUserInfo.tmdId,
  163. // name = ddUserInfo.tmdName,
  164. // picture = ddUserInfo.picture,
  165. // job = joinStatus,
  166. // subjectIds = new List<string>(),
  167. // roles = new List<string> { $"{role}" },
  168. // permissions = new List<string>(),
  169. // status = "join",
  170. // code = $"Teacher-{schoolId}",
  171. // pk = "Teacher",
  172. // createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  173. // };
  174. // addSchoolTeache.Add(cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync(schoolTeacher, new PartitionKey(schoolTeacher.code)));
  175. // }
  176. //}
  177. //if (isReadTec == true)
  178. //{
  179. // if (isAddUpd == true)
  180. // {
  181. // await cosmosClient.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey($"Base"));
  182. // }
  183. // else
  184. // {
  185. // await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  186. // }
  187. //}
  188. //if (updSchoolTeache.Count > 0)
  189. //{
  190. // if (updSchoolTeache.Count < 256)
  191. // {
  192. // await Task.WhenAll(updSchoolTeache);
  193. // }
  194. // else
  195. // {
  196. // int pages = (updSchoolTeache.Count + 255) / 256;
  197. // for (int i = 0; i < pages; i++)
  198. // {
  199. // List<Task<ItemResponse<SchoolTeacher>>> tempAbility = updSchoolTeache.Skip((i) * 256).Take(256).ToList();
  200. // await Task.WhenAll(tempAbility);
  201. // }
  202. // }
  203. //}
  204. //if (addSchoolTeache.Count > 0)
  205. //{
  206. // if (addSchoolTeache.Count < 256)
  207. // {
  208. // await Task.WhenAll(addSchoolTeache);
  209. // }
  210. // else
  211. // {
  212. // int pages = (addSchoolTeache.Count + 255) / 256;
  213. // for (int i = 0; i < pages; i++)
  214. // {
  215. // List<Task<ItemResponse<SchoolTeacher>>> tempAbility = addSchoolTeache.Skip((i) * 256).Take(256).ToList();
  216. // await Task.WhenAll(tempAbility);
  217. // }
  218. // }
  219. //}
  220. //return noSchool;
  221. }
  222. }
  223. }