CosmosQueryHelper.cs 15 KB

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