SchoolStatsWay.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using Azure.Cosmos;
  2. using MathNet.Numerics.LinearAlgebra.Double;
  3. using Newtonsoft.Json;
  4. using StackExchange.Redis;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net.Http.Headers;
  9. using System.Net.Http;
  10. using System.Net;
  11. using System.Text;
  12. using System.Text.Json;
  13. using System.Threading.Tasks;
  14. using TEAMModelOS.Models;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models.Cosmos.BI;
  18. using TEAMModelOS.SDK.Models.Service.BI;
  19. using Microsoft.Extensions.Configuration;
  20. using System.Net.Http.Json;
  21. namespace TEAMModelOS.SDK.Models.Service.BIStatsWay
  22. {
  23. public static class SchoolStatsWay
  24. {
  25. /// <summary>
  26. /// 统计单个学校
  27. /// </summary>
  28. /// <param name="cosmosClient"></param>
  29. /// <param name="scId"></param>
  30. /// <returns></returns>
  31. public static async Task<StatsInfo> GetSingleSc(CosmosClient cosmosClient, IDatabase redisClinet, string scId, int year = 0)
  32. {
  33. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  34. if (year < dateTime.Year)
  35. dateTime = new(year, 12, 31, 23, 59, 59, TimeSpan.Zero);
  36. long userSize = 0;
  37. RedisValue redisValue = redisClinet.HashGet($"Blob:Record", scId);
  38. if (redisValue != default && !redisValue.IsNullOrEmpty)
  39. {
  40. JsonElement record = redisValue.ToString().ToObject<JsonElement>();
  41. if (record.TryGetInt64(out userSize)) { }
  42. }
  43. StatsInfo statsInfo = new() { id = $"{year}-{scId}" };
  44. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间 13位
  45. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  46. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  47. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  48. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  49. var (lastMthS, LastmthE) = TimeHelper.GetStartOrEnd(dateTime, "lastMonth"); //上月开始/结束时间
  50. var (mthS, mthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  51. var (lastYearS, lastYearE) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{dateTime.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  52. var (yearS, yearE) = TimeHelper.GetStartOrEnd(dateTime, "year"); //计算今年开始/结束时间
  53. var (weekDayS, weekDayE) = TimeHelper.GetLongToTime(weekS, weekE);
  54. var (monthDayS, monthDayE) = TimeHelper.GetLongToTime(mthS, mthE);
  55. string currSql = "select value(count(c.id)) from c";
  56. string dayWhereSql = $"c.createTime >= {dayS} and c.createTime <= {dayE}";
  57. string weekWhereSql = $"c.createTime >= {weekS} and c.createTime <= {weekE}";
  58. string monthWhereSql = $"c.createTime >= {mthS} and c.createTime <= {mthE}";
  59. ScBase scBase = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<ScBase>(scId, new PartitionKey("Base"));
  60. statsInfo.schoolId = scBase.id;
  61. statsInfo.name = scBase.name;
  62. statsInfo.picture = scBase.picture;
  63. statsInfo.size = scBase.size;
  64. statsInfo.scCreateTime = scBase.createTime;
  65. statsInfo.areaId = scBase.areaId;
  66. statsInfo.year = year;
  67. statsInfo.useSize = userSize;
  68. string tchSql = $"{currSql} where ARRAY_CONTAINS(c.roles, 'teacher', true) AND c.status = 'join'";
  69. statsInfo.tch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Teacher-{scBase.id}", tchSql);
  70. List<double> tchDoubles = await UserStatsWay.GetYearTecherStudent(cosmosClient, scBase.id, year, 1);
  71. statsInfo.tchYear = tchDoubles;
  72. statsInfo.dayTch = ((int)tchDoubles[dateTime.DayOfYear - 1]);
  73. DenseMatrix tchDouble = DenseMatrix.OfColumns(new List<List<double>>() { tchDoubles });
  74. int weekDay = 0;
  75. if ((dateTime.DayOfYear - weekDayS.DayOfYear) <= 7)
  76. weekDay = dateTime.DayOfYear - weekDayS.DayOfYear + 1;
  77. else
  78. weekDay = 7;
  79. statsInfo.weekTch = ((int)tchDouble.SubMatrix(weekDayS.DayOfYear - 1, weekDay, 0, tchDouble.ColumnCount).ColumnSums().Sum());
  80. statsInfo.monthTch += ((int)tchDouble.SubMatrix(monthDayS.DayOfYear - 1, (dateTime.DayOfYear - monthDayS.DayOfYear + 1), 0, tchDouble.ColumnCount).ColumnSums().Sum());
  81. //statsInfo.dayTch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Teacher-{scBase.id}", $"{tchSql} and {dayWhereSql} ");
  82. //statsInfo.weekTch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Teacher-{scBase.id}", $"{tchSql} and {weekWhereSql}");
  83. //statsInfo.monthTch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Teacher-{scBase.id}", $"{tchSql} and {monthWhereSql}");
  84. statsInfo.tchUpTime = dateTime.ToUnixTimeMilliseconds();
  85. statsInfo.stu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"Base-{scBase.id}", currSql);
  86. List<double> stuDoubles = await UserStatsWay.GetYearTecherStudent(cosmosClient, scBase.id, year, 2);
  87. statsInfo.stuYear = stuDoubles;
  88. statsInfo.dayStu = ((int)stuDoubles[dateTime.DayOfYear]);
  89. DenseMatrix stuDouble = DenseMatrix.OfColumns(new List<List<double>>() { stuDoubles });
  90. statsInfo.weekStu = ((int)stuDouble.SubMatrix(weekDayS.DayOfYear - 1, weekDay, 0, stuDouble.ColumnCount).ColumnSums().Sum());
  91. statsInfo.monthStu += ((int)stuDouble.SubMatrix(monthDayS.DayOfYear - 1, (dateTime.DayOfYear - monthDayS.DayOfYear + 1), 0, stuDouble.ColumnCount).ColumnSums().Sum());
  92. //statsInfo.dayStu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"Base-{scBase.id}", $"{currSql} where {dayWhereSql}");
  93. //statsInfo.weekStu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"Base-{scBase.id}", $"{currSql} where {weekWhereSql}");
  94. //statsInfo.monthStu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"Base-{scBase.id}", $"{currSql} where {monthWhereSql}");
  95. statsInfo.stuUpTime = dateTime.ToUnixTimeMilliseconds();
  96. statsInfo.room = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Room-{scBase.id}", currSql);
  97. //statsInfo.witRoom = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Room-{scBase.id}", $"{currSql} where (c.serial != null or c.serial != '' or IS_DEFINED(c.serial) = true)");
  98. statsInfo.resourceCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Bloblog-{scBase.id}", currSql);
  99. statsInfo.upTime = dateTime.ToUnixTimeMilliseconds();
  100. statsInfo.lesson = await LessonRecordStatsWay.GetSchoolAll(cosmosClient, statsInfo.schoolId, year);
  101. statsInfo.activity = await ActivityStatsWay.GetSchoolAll(cosmosClient, statsInfo.schoolId, year);
  102. statsInfo.study = await StudyStatsWay.GetSchoolAll(cosmosClient, statsInfo.schoolId);
  103. return statsInfo;
  104. }
  105. /// <summary>
  106. /// 多个学校统计
  107. /// </summary>
  108. /// <param name="cosmosClient"></param>
  109. /// <param name="statsInfos"></param>
  110. /// <param name="_httpTrigger"></param>
  111. /// <param name="_option"></param>
  112. /// <param name="area"></param>
  113. /// <returns></returns>
  114. public static StatsInfo GetAreaStats(CosmosClient cosmosClient, Option _option, List<StatsInfo> statsInfos, List<string> scIds = null, Area area = null)
  115. {
  116. StatsInfo areaInfo = new()
  117. {
  118. id = area == null ? "" : $"{DateTimeOffset.UtcNow.Year}-{area.id}",
  119. pk = "Statistics",
  120. code = "Statistics",
  121. name = area == null ? "" : area.name,
  122. areaId = area == null ? "" : area.id,
  123. picture = "",
  124. tch = statsInfos.Select(s => s.tch).Sum(),
  125. dayTch = statsInfos.Select(s => s.dayTch).Sum(),
  126. weekTch = statsInfos.Select(s => s.weekTch).Sum(),
  127. monthTch = statsInfos.Select(s => s.monthTch).Sum(),
  128. stu = statsInfos.Select(s => s.stu).Sum(),
  129. dayStu = statsInfos.Select(s => s.dayStu).Sum(),
  130. weekStu = statsInfos.Select(s => s.weekStu).Sum(),
  131. monthStu = statsInfos.Select(s => s.monthStu).Sum(),
  132. room = statsInfos.Select(s => s.room).Sum(),
  133. witRoom = statsInfos.Select(s => s.witRoom).Sum(),
  134. resourceCnt = statsInfos.Select(s => s.resourceCnt).Sum(),
  135. size = statsInfos.Select(s => s.size).Sum(),
  136. useSize = statsInfos.Select(s => s.useSize).Sum()
  137. };
  138. areaInfo.lesson.all = statsInfos.Select(s => s.lesson.all).Sum();
  139. areaInfo.lesson.open = statsInfos.Select(s => s.lesson.open).Sum();
  140. areaInfo.lesson.less = statsInfos.Select(s => s.lesson.less).Sum();
  141. areaInfo.lesson.lastDay = statsInfos.Select(s => s.lesson.lastDay).Sum();
  142. areaInfo.lesson.day = statsInfos.Select(s => s.lesson.day).Sum();
  143. areaInfo.lesson.lastWeek = statsInfos.Select(s => s.lesson.lastWeek).Sum();
  144. areaInfo.lesson.week = statsInfos.Select(s => s.lesson.week).Sum();
  145. areaInfo.lesson.lastTerm = statsInfos.Select(s => s.lesson.lastTerm).Sum();
  146. areaInfo.lesson.term = statsInfos.Select(s => s.lesson.term).Sum();
  147. areaInfo.lesson.lastDayInter = statsInfos.Select(s => s.lesson.lastDayInter).Sum();
  148. areaInfo.lesson.dayInter = statsInfos.Select(s => s.lesson.dayInter).Sum();
  149. //areaInfo.lesson.lastMonthInter = statsInfos.Select(s => s.lesson.lastMonthInter).Sum();
  150. //areaInfo.lesson.monthInter = statsInfos.Select(s => s.lesson.monthInter).Sum();
  151. areaInfo.lesson.lastYearInter = statsInfos.Select(s => s.lesson.lastYearInter).Sum();
  152. //areaInfo.lesson.yearInter = statsInfos.Select(s => s.lesson.yearInter).Sum();
  153. areaInfo.lesson.yearInters = BICommonWay.ManyDoubleMerge(statsInfos.Select(s => s.lesson.yearInters).ToList());
  154. areaInfo.lesson.year = BICommonWay.ManyDoubleMerge(statsInfos.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList());
  155. areaInfo.activity.all = statsInfos.Select(s => s.activity.all).Sum();
  156. areaInfo.activity.exam = statsInfos.Select(s => s.activity.exam).Sum();
  157. areaInfo.activity.survey = statsInfos.Select(s => s.activity.survey).Sum();
  158. areaInfo.activity.vote = statsInfos.Select(s => s.activity.vote).Sum();
  159. areaInfo.activity.homework = statsInfos.Select(s => s.activity.homework).Sum();
  160. areaInfo.activity.lastDay = statsInfos.Select(s => s.activity.lastDay).Sum();
  161. areaInfo.activity.dayCnt = statsInfos.Select(s => s.activity.dayCnt).Sum();
  162. areaInfo.activity.lastWeek = statsInfos.Select(s => s.activity.lastWeek).Sum();
  163. areaInfo.activity.week = statsInfos.Select(s => s.activity.week).Sum();
  164. areaInfo.activity.lastTerm = statsInfos.Select(s => s.activity.lastTerm).Sum();
  165. areaInfo.activity.term = statsInfos.Select(s => s.activity.term).Sum();
  166. areaInfo.activity.lastMonth = statsInfos.Select(s => s.activity.lastMonth).Sum();
  167. areaInfo.activity.month = statsInfos.Select(s => s.activity.month).Sum();
  168. //areaInfo.activity.LastYear = BICommonWay.ManyDoubleMerge(statsInfos.Select(s => s.activity.LastYear).ToList());
  169. areaInfo.activity.year = BICommonWay.ManyDoubleMerge(statsInfos.Select(s => s.activity.year).Where(w => w.Count > 0).ToList());
  170. return areaInfo;
  171. }
  172. /// <summary>
  173. /// 计算学校智慧教室数
  174. /// </summary>
  175. /// <param name="_azureCosmos"></param>
  176. /// <param name="_azureRedis"></param>
  177. /// <param name="_configuration"></param>
  178. /// <param name="_httpClient"></param>
  179. /// <param name="code"></param>
  180. /// <returns></returns>
  181. public static async Task<double> GetShoolWisdomRoomCount(AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis, IConfiguration _configuration, IHttpClientFactory _httpClient, DingDing _dingDing, string code)
  182. {
  183. try
  184. {
  185. var db = _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School");
  186. var r8 = _azureRedis.GetRedisClient(8);
  187. List<SchoolProductSerial> serial = new(); //承接DB資料用:序號
  188. List<deviceForCoreService> uuidList = new(); //要向CoreService詢問deviceID及硬體資訊的UUID列表
  189. long UTCNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
  190. //軟體
  191. List<string> serialPermitList = StaticValue.GetSerialPermitProdcodeList();
  192. string serialPermitJsonStr = JsonConvert.SerializeObject(serialPermitList);
  193. string serialQueryText = $"SELECT * FROM c WHERE c.dataType = 'serial' AND ARRAY_CONTAINS({serialPermitJsonStr}, c.prodCode)";
  194. await foreach (var itemsr in db.GetItemQueryStreamIterator(queryText: serialQueryText, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Product-{code}") }))
  195. {
  196. using var json = await JsonDocument.ParseAsync(itemsr.ContentStream);
  197. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  198. {
  199. deviceForCoreService uuidForCore;
  200. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  201. {
  202. serial.Add(obj.ToObject<SchoolProductSerial>());
  203. if (obj.TryGetProperty("deviceBound", out JsonElement deviceBoundJobj) && !string.IsNullOrWhiteSpace(deviceBoundJobj.ToString()))
  204. {
  205. foreach (var deviceBoundTmpRow in deviceBoundJobj.EnumerateArray())
  206. {
  207. uuidForCore = new deviceForCoreService
  208. {
  209. sn = (!string.IsNullOrWhiteSpace(Convert.ToString(obj.GetProperty("serial")))) ? Convert.ToString(obj.GetProperty("serial")) : null,
  210. uuid1 = (!string.IsNullOrWhiteSpace(Convert.ToString(deviceBoundTmpRow.GetProperty("uuid")))) ? Convert.ToString(deviceBoundTmpRow.GetProperty("uuid")) : null,
  211. uuid2 = (!string.IsNullOrWhiteSpace(Convert.ToString(deviceBoundTmpRow.GetProperty("uuid2")))) ? Convert.ToString(deviceBoundTmpRow.GetProperty("uuid2")) : null,
  212. device_id = (!string.IsNullOrWhiteSpace(Convert.ToString(deviceBoundTmpRow.GetProperty("deviceId")))) ? Convert.ToString(deviceBoundTmpRow.GetProperty("deviceId")) : null,
  213. class_id = (!string.IsNullOrWhiteSpace(Convert.ToString(deviceBoundTmpRow.GetProperty("classId")))) ? Convert.ToString(deviceBoundTmpRow.GetProperty("classId")) : null
  214. };
  215. uuidList.Add(uuidForCore);
  216. }
  217. }
  218. }
  219. }
  220. }
  221. ////取得DeviceInfo From Core 並更新序號資料
  222. List<deviceFromCoreService> coreUuidList = (List<deviceFromCoreService>)GetDeviceFromCoreAsync(uuidList, _configuration, _httpClient).GetAwaiter().GetResult();
  223. List<deviceBoundExt> deviceBoundArray;
  224. List<deviceBoundExt> counts = new();
  225. List<deviceFromCoreService> coreUuid;
  226. deviceForCoreService deviceBoundRow;
  227. deviceBoundExt deviceBoundExt;
  228. List<SchoolProductSerial> updSchoolProductSerialList = new List<SchoolProductSerial>(); //更新學校產品序號用
  229. foreach (SchoolProductSerial serialRow in serial)
  230. {
  231. deviceBoundArray = new List<deviceBoundExt>();
  232. coreUuid = coreUuidList
  233. .Where((deviceFromCoreService x) => x.sn == serialRow.serial)
  234. .ToList();
  235. foreach (deviceFromCoreService deviceRow in coreUuid)
  236. {
  237. if (!string.IsNullOrWhiteSpace(deviceRow.uuid1) || !string.IsNullOrWhiteSpace(deviceRow.uuid2) || !string.IsNullOrWhiteSpace(deviceRow.device_id)) //uuid1、uuid2、device_id 任一欄有值
  238. {
  239. //前端顯示用
  240. deviceBoundRow = uuidList.Where(u => u.sn == deviceRow.sn && u.uuid1 == deviceRow.uuid1 && u.uuid2 == deviceRow.uuid2).FirstOrDefault();
  241. deviceBoundExt = new deviceBoundExt
  242. {
  243. uuid = deviceBoundRow.uuid1,
  244. uuid2 = deviceBoundRow.uuid2,
  245. classId = deviceBoundRow.class_id,
  246. deviceId = deviceRow.device_id,
  247. os = deviceRow.os,
  248. ip = deviceRow.local_ip,
  249. cpu = deviceRow.cpu,
  250. pcname = deviceRow.pc_name,
  251. osver = deviceRow.os_ver
  252. };
  253. deviceBoundArray.Add(deviceBoundExt);
  254. }
  255. }
  256. if (deviceBoundArray.Count.Equals(0)) //無法取得CS的硬體資訊,則用序號的硬體資訊帶入
  257. {
  258. if (serialRow.deviceBound != null)
  259. {
  260. foreach (deviceBound serialRowDeviceBound in serialRow.deviceBound)
  261. {
  262. if (!string.IsNullOrEmpty(serialRowDeviceBound.uuid) || !string.IsNullOrEmpty(serialRowDeviceBound.uuid2))
  263. {
  264. deviceBoundExt = new deviceBoundExt
  265. {
  266. uuid = serialRowDeviceBound.uuid,
  267. uuid2 = serialRowDeviceBound.uuid2,
  268. classId = serialRowDeviceBound.classId,
  269. deviceId = serialRowDeviceBound.deviceId
  270. };
  271. deviceBoundArray.Add(deviceBoundExt);
  272. }
  273. }
  274. }
  275. }
  276. //序號更新
  277. //updSchoolProductSerialList.Add(serialRow);
  278. counts.AddRange(deviceBoundArray);
  279. //await db.ReplaceItemAsync<SchoolProductSerial>(serialRow, serialRow.id, new PartitionKey($"Product-{school_code}"));
  280. }
  281. //取得CC授權使用狀態
  282. var hashs = await r8.HashGetAllAsync($"CC:License:{code}");
  283. var ccuser = hashs.Select(x => JsonDocument.Parse(x.Value.ToString())).ToList();
  284. //更新學校產品序號
  285. //_ = UpdupdSchoolProductSerialListAsync(updSchoolProductSerialList, school_code.GetString());
  286. double roomCount = counts.Count + ccuser.Count;
  287. return roomCount;
  288. }
  289. catch (Exception ex)
  290. {
  291. await _dingDing.SendBotMsg($"BI, {Environment.GetEnvironmentVariable("Option:Location")},GetShoolWisdomRoomCount() {code} \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  292. return 0;
  293. }
  294. }
  295. private static async Task<List<deviceFromCoreService>> GetDeviceFromCoreAsync(List<deviceForCoreService> uuidList, IConfiguration _configuration, IHttpClientFactory _httpClient)
  296. {
  297. List<deviceFromCoreService> result = new List<deviceFromCoreService>();
  298. try
  299. {
  300. string url = _configuration.GetValue<string>("HaBookAuth:CoreService:deviceinfo");
  301. string AccessToken = await getCoreAccessToken(_configuration, _httpClient);
  302. var client = _httpClient.CreateClient();
  303. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
  304. string uuidListJson = JsonConvert.SerializeObject(uuidList);
  305. var content = new StringContent(uuidListJson, Encoding.UTF8, "application/json");
  306. HttpResponseMessage responseMessage = await client.PostAsync(url, content);
  307. if (responseMessage.StatusCode == HttpStatusCode.OK)
  308. {
  309. string responseBody = responseMessage.Content.ReadAsStringAsync().Result;
  310. result = System.Text.Json.JsonSerializer.Deserialize<List<deviceFromCoreService>>(responseBody.ToString());
  311. }
  312. return result;
  313. }
  314. catch (Exception ex)
  315. {
  316. return result;
  317. }
  318. }
  319. private static async Task<string> getCoreAccessToken(IConfiguration _configuration, IHttpClientFactory _httpClient)
  320. {
  321. string AccessToken = "";
  322. try
  323. {
  324. string Url = _configuration.GetValue<string>("HaBookAuth:CoreAPI") + "/oauth2/token";
  325. string GrantType = "device";
  326. string ClientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  327. string Secret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  328. var content = new { grant_type = GrantType, client_id = ClientID, client_secret = Secret };
  329. var response = await _httpClient.CreateClient().PostAsJsonAsync($"{Url}", content);
  330. if (response.IsSuccessStatusCode)
  331. {
  332. string responseBody = response.Content.ReadAsStringAsync().Result;
  333. using (JsonDocument document = JsonDocument.Parse(responseBody.ToString()))
  334. {
  335. if (document.RootElement.TryGetProperty("access_token", out JsonElement AccessTokenObj))
  336. {
  337. AccessToken = AccessTokenObj.ToString();
  338. }
  339. }
  340. }
  341. return AccessToken;
  342. }
  343. catch (Exception ex)
  344. {
  345. return AccessToken;
  346. }
  347. }
  348. }
  349. }