CommonFind.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using Microsoft.Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelBI.Models;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Models;
  10. namespace TEAMModelBI.Tool
  11. {
  12. public class CommonFind
  13. {
  14. /// <summary>
  15. /// 查询学校教师角色列表
  16. /// </summary>
  17. /// <param name="cosmosClient"></param>
  18. /// <param name="schoolId">学校Id</param>
  19. /// <param name="roles">查询的角色</param>
  20. /// <returns>返回学校角色列表</returns>
  21. public static async Task<List<SchoolTeacherRoles>> FindSchoolRoles(CosmosClient cosmosClient, string schoolId, string roles)
  22. {
  23. List<SchoolTeacherRoles> strs = new();
  24. try
  25. {
  26. string managerSql = $"SELECT DISTINCT REPLACE(c.code, 'Teacher-', '') AS schoolId, c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.roles, '{roles}', true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.code = 'Teacher-{schoolId}'";
  27. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: managerSql, requestOptions: new QueryRequestOptions() { }))
  28. {
  29. using var json = await JsonDocument.ParseAsync(item.Content);
  30. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  31. {
  32. SchoolTeacherRoles str = new()
  33. {
  34. tmdId = obj.GetProperty("id").GetString(),
  35. tmdName = obj.GetProperty("name").GetString()
  36. };
  37. strs.Add(str);
  38. }
  39. }
  40. return strs;
  41. }
  42. catch
  43. {
  44. return strs;
  45. }
  46. }
  47. /// <summary>
  48. /// 通过醍摩豆账户查询关联学校ID
  49. /// </summary>
  50. /// <param name="cosmosClient">cosmosDB连接</param>
  51. /// <param name="tmdId">醍摩豆账户</param>
  52. /// <returns>返回顾问相关的学校ID集合</returns>
  53. public static async Task<List<string>> FindSchoolIds(CosmosClient cosmosClient, string tmdId,string roles = "assist", bool isMany = false)
  54. {
  55. List<string> schoolIds = new();
  56. //string schoolSql = $"SELECT DISTINCT REPLACE(c.code,'Teacher-','') AS schoolId,c.code,c.roles,c.id,c.name From c where ARRAY_CONTAINS(c.roles,'assist',true) AND c.status = 'join' AND c.id='{tmdId}'";
  57. //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: schoolSql, requestOptions: new QueryRequestOptions() { }))
  58. //{
  59. // using var json = await JsonDocument.ParseAsync(item.Content);
  60. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  61. // {
  62. // schoolIds.Add(obj.GetProperty("schoolId").GetString());
  63. // }
  64. //}
  65. StringBuilder schoolSql = new($"SELECT value(REPLACE(c.code, 'Teacher-', '')) FROM c where c.pk='Teacher' and c.id='{tmdId}'");
  66. if (isMany == true)
  67. schoolSql.Append($" and (array_contains(c.roles,'assist',true) or array_contains(c.roles,'sales',true))");
  68. else
  69. schoolSql.Append($" and array_contains(c.roles,'{roles}',true)");
  70. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText: schoolSql.ToString(), requestOptions: new QueryRequestOptions() { }))
  71. {
  72. schoolIds.Add(item);
  73. }
  74. return schoolIds;
  75. }
  76. /// <summary>
  77. /// 通过sql语句查询单列集合
  78. /// </summary>
  79. /// <param name="cosmosClient">连接字符</param>
  80. /// <param name="container">容器名称</param>
  81. /// <param name="sqlTxt">sql语句 带value</param>
  82. /// <param name="code"></param>
  83. /// <returns></returns>
  84. public static async Task<List<string>> GetValueSingle(CosmosClient cosmosClient, string container, string sqlTxt, string code = null)
  85. {
  86. List<string> ids = new();
  87. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<string>(queryText: sqlTxt, requestOptions: !string.IsNullOrEmpty(code) ? new QueryRequestOptions() { PartitionKey = new PartitionKey(code) } : new QueryRequestOptions() { }))
  88. {
  89. ids.Add(item);
  90. }
  91. return ids;
  92. }
  93. /// <summary>
  94. /// 通过sql语句查询单列集合
  95. /// </summary>
  96. /// <param name="cosmosClient"></param>
  97. /// <param name="container"></param>
  98. /// <param name="sqlTxt"></param>
  99. /// <param name="single"></param>
  100. /// <param name="code"></param>
  101. /// <returns></returns>
  102. public async Task<List<string>> GetStreamSingle(CosmosClient cosmosClient, string container, string sqlTxt, string single = "id", string code = null)
  103. {
  104. List<string> ids = new();
  105. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: !string.IsNullOrEmpty(code) ? new QueryRequestOptions() { PartitionKey = new PartitionKey(code) } : new QueryRequestOptions() { }))
  106. {
  107. using var json = await JsonDocument.ParseAsync(item.Content);
  108. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  109. {
  110. ids.Add(obj.GetProperty(single).GetString());
  111. }
  112. }
  113. return ids;
  114. }
  115. /// <summary>
  116. /// 通过语句查询学校ID
  117. /// </summary>
  118. /// <param name="cosmosClient">cosmosDB连接</param>
  119. /// <param name="sqlTxt">sql语句</param>
  120. /// <param name="code">数据分区键</param>
  121. /// <returns>返回学校ID的集合</returns>
  122. public static async Task<List<string>> FindScIds(CosmosClient cosmosClient, string sqlTxt, string code)
  123. {
  124. List<string> schoolIds = new();
  125. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  126. {
  127. schoolIds.Add(item);
  128. }
  129. //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  130. //{
  131. // using var json = await JsonDocument.ParseAsync(item.Content);
  132. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  133. // {
  134. // schoolIds.Add(obj.GetProperty("id").GetString());
  135. // }
  136. //}
  137. return schoolIds;
  138. }
  139. /// <summary>
  140. /// 依据学校查询教师列表
  141. /// </summary>
  142. /// <param name="cosmosClient"></param>
  143. /// <param name="schools">学校列表</param>
  144. /// <param name="roles">不传默认教师角色</param>
  145. /// <returns></returns>
  146. public static async Task<List<string>> FindRolesId(CosmosClient cosmosClient, List<string> schools, string roles = null)
  147. {
  148. string rolesName = "teacher";
  149. if (roles != null)
  150. {
  151. rolesName = roles;
  152. }
  153. List<string> teachers = new();
  154. foreach (var school in schools)
  155. {
  156. string sqlTxt = $"select value(c.id) from c where ARRAY_CONTAINS(c.roles,'{rolesName}',true) and c.status = 'join'";
  157. await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  158. {
  159. teachers.Add(itemTeac);
  160. }
  161. //string sqlTxt = $"select c.id from c where ARRAY_CONTAINS(c.roles,'{rolesName}',true) and c.status = 'join'";
  162. //await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() {PartitionKey =new PartitionKey($"Teacher-{school}") }))
  163. //{
  164. // using var json = await JsonDocument.ParseAsync(itemTeac.Content);
  165. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  166. // {
  167. // teachers.Add(obj.GetProperty("id").GetString());
  168. // }
  169. //}
  170. }
  171. return teachers;
  172. }
  173. /// <summary>
  174. /// 单个容器数据统计
  175. /// </summary>
  176. /// <param name="cosmosClient"></param>
  177. /// <param name="container"></param>
  178. /// <param name="SqlTxt"></param>
  179. /// <param name="code"></param>
  180. /// <returns></returns>
  181. public static async Task<int> GetSqlValueCount(CosmosClient cosmosClient, string container, string SqlTxt,string code = null)
  182. {
  183. int totals = 0;
  184. try
  185. {
  186. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<int>(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  187. {
  188. totals = item;
  189. }
  190. }
  191. catch{ }
  192. return totals;
  193. }
  194. /// <summary>
  195. /// 多个容器数据统计
  196. /// </summary>
  197. /// <param name="cosmosClient"></param>
  198. /// <param name="containers"></param>
  199. /// <param name="SqlTxt"></param>
  200. /// <param name="code"></param>
  201. /// <returns></returns>
  202. public static async Task<int> GetSqlValueCount(CosmosClient cosmosClient, List<string> containers, string SqlTxt, string code = null)
  203. {
  204. int totals = 0;
  205. foreach (var container in containers)
  206. {
  207. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<int>(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  208. {
  209. totals += item;
  210. }
  211. }
  212. return totals;
  213. }
  214. /// <summary>
  215. /// 单个容器数据统计 double
  216. /// </summary>
  217. /// <param name="cosmosClient"></param>
  218. /// <param name="container"></param>
  219. /// <param name="SqlTxt"></param>
  220. /// <param name="code"></param>
  221. /// <returns></returns>
  222. public static async Task<double> GetSqlValueDoubleCounnt(CosmosClient cosmosClient, string container, string SqlTxt, string code = null)
  223. {
  224. double totals = 0;
  225. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<double>(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  226. {
  227. totals = item;
  228. }
  229. return totals;
  230. }
  231. /// <summary>
  232. /// 多个容器数据统计 double
  233. /// </summary>
  234. /// <param name="cosmosClient"></param>
  235. /// <param name="container"></param>
  236. /// <param name="SqlTxt"></param>
  237. /// <param name="code"></param>
  238. /// <returns></returns>
  239. public static async Task<double> GetSqlValueDoubleCounnt(CosmosClient cosmosClient, List<string> containers, string SqlTxt, string code = null)
  240. {
  241. double totals = 0;
  242. foreach (var container in containers)
  243. {
  244. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<double>(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  245. {
  246. totals += item;
  247. }
  248. }
  249. return totals;
  250. }
  251. /// <summary>
  252. /// 通过SQL 语句返回实体信息
  253. /// </summary>
  254. /// <typeparam name="T"></typeparam>
  255. /// <param name="cosmosClient"></param>
  256. /// <param name="containers"></param>
  257. /// <param name="sqlTxt"></param>
  258. /// <param name="code"></param>
  259. /// <returns></returns>
  260. public static async Task<List<T>> GetObject<T>(CosmosClient cosmosClient, List<string> containers, string sqlTxt, string code = null)
  261. {
  262. List<T> temps = new();
  263. foreach (var container in containers)
  264. {
  265. await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<T>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  266. {
  267. temps.Add(items);
  268. }
  269. }
  270. return temps;
  271. }
  272. /// <summary>
  273. /// 通过SQL 语句返回实体信息
  274. /// </summary>
  275. /// <typeparam name="T"></typeparam>
  276. /// <param name="cosmosClient"></param>
  277. /// <param name="containers"></param>
  278. /// <param name="sqlTxt"></param>
  279. /// <param name="code"></param>
  280. /// <returns></returns>
  281. public static async Task<List<T>> GetObject<T>(CosmosClient cosmosClient, string containers, string sqlTxt, string code = null)
  282. {
  283. List<T> temps = new();
  284. await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", containers).GetItemQueryIteratorSql<T>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  285. {
  286. temps.Add(items);
  287. }
  288. return temps;
  289. }
  290. }
  291. }