CosmosQueryHelper.cs 15 KB

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