123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BICommon;
- using static TEAMModelOS.SDK.CoreAPIHttpService;
- namespace TEAMModelOS.SDK.Models.Service.BI
- {
- public static class BIStatsNotice
- {
- /// <summary>
- /// 汇总统计通知
- /// </summary>
- /// <param name="cosmosClient">连接</param>
- /// <param name="_dingDing">错误消息发送</param>
- /// <param name="type">统计类型</param>
- /// <param name="idType">账号类型 private:个人 school 学校</param>
- /// <param name="id">账号id</param>
- /// <param name="count">数量</param>
- /// <param name="careDate">创建时间</param>
- /// <returns></returns>
- public static async Task SetStatsNotice(CosmosClient cosmosClient, DingDing _dingDing, string type, string idType, string id, int count, long careDate = 0)
- {
- try
- {
- DateTimeOffset dateOff = DateTimeOffset.UtcNow;
- if (careDate != 0)
- {
- if (careDate > 1000000000000)
- dateOff = DateTimeOffset.FromUnixTimeMilliseconds(careDate);
- else
- dateOff = DateTimeOffset.FromUnixTimeSeconds(careDate);
- }
- string dt = dateOff.ToString("yyyyMMdd");
- StatsNotice statsNotice = new();
- var resStsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dt}-{id}", new PartitionKey("StatsNotice"));
- if (resStsInfo.Status == 200)
- {
- using var fileJson = await JsonDocument.ParseAsync(resStsInfo.Content);
- statsNotice = fileJson.ToObject<StatsNotice>();
- }
- else
- {
- statsNotice.id = $"{dt}-{id}";
- statsNotice.type = idType;
- statsNotice.rangeId = id;
- }
- switch (type)
- {
- case "Student":
- statsNotice.stuDayCnt += count;
- break;
- case "Classroom":
- statsNotice.classroomDayCnt += count;
- break;
- case "Weigh":
- statsNotice.weighDayCnt += count;
- break;
- case "Homework":
- statsNotice.homeworkDayCnt = count;
- break;
- }
- statsNotice.day = dt;
- statsNotice.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
-
- statsNotice.ttl = 86400;
- if (resStsInfo.Status == 200)
- {
- statsNotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<StatsNotice>(statsNotice, statsNotice.id, new PartitionKey("StatsNotice"));
- }
- else
- {
- statsNotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<StatsNotice>(statsNotice, new PartitionKey("StatsNotice"));
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetStatsNotice(),参数: 统计类型:{type},类型私人/学校:{idType},id:{id}, 增/减数:{count},时间戳:{careDate}\n {ex.Message}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- }
- /// <summary>
- /// 发送每日课程信息通知
- /// </summary>
- /// <param name="_coreAPIHttpService"></param>
- /// <param name="cosmosClient"></param>
- /// <param name="_dingDing"></param>
- /// <param name="_configuration"></param>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- public static async Task MonitorStatsNotice(CoreAPIHttpService _coreAPIHttpService, CosmosClient cosmosClient, DingDing _dingDing, IConfiguration _configuration,JsonElement jsonElement)
- {
- try
- {
- StatsNotice statsNotice = jsonElement.ToObject<StatsNotice>();
- DateTimeOffset dateOffset = DateTimeOffset.UtcNow;
- jsonElement.TryGetProperty("dateTime", out JsonElement dateTime);
- jsonElement.TryGetProperty("id", out JsonElement id);
- string dt = "";
- if (string.IsNullOrEmpty($"{dateTime}"))
- {
- dt = DateTimeOffset.UtcNow.ToString("yyyyMMdd");
- }
- else
- {
- dt = $"{dateTime}";
- }
- StringBuilder sqlTxt = new($"select value(c) from c where c.day='{dt}'");
- if (!string.IsNullOrEmpty($"{id}"))
- {
- sqlTxt.Append($" and c.rangeId = '{id}'");
- }
- List<StatsNotice> statsNotices = new();
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<StatsNotice>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("StatsNotice") }))
- {
- statsNotices.Add(item);
- }
- foreach (var item in statsNotices)
- {
- List<string> tacherIds = new();
- if (item.type == "school")
- {
- await foreach (var tchId in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<string>(queryText: "SELECT c.id FROM c WHERE ARRAY_CONTAINS(c.roles, 'admin', true) AND c.pk = 'Teacher' AND c.status = 'join'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{item.rangeId}") }))
- {
- tacherIds.Add(tchId);
- }
- }
- else if (item.type == "private")
- {
- tacherIds.Add(item.rangeId);
- }
- NotifyData notifyData = new() { hubName = "hita", sender = "IES", tags = tacherIds.Select(s => $"{s}-hata").ToList(), title = "每日课程信息", body = $"每日课程信息如下:\n\r学生名单数:{item.stuDayCnt} \n\r 课堂记录数:{item.classroomDayCnt} \n\r 评量活动数:{item.weighDayCnt} \n\r 作业活动数:{item.homeworkDayCnt}", eventId = $"IES_send", data = item.ToJsonString() };
- _coreAPIHttpService.CoursePushNotify(notifyData, new Dictionary<string, object>(), $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing);
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},MonitorStatsNotice(),参数:{jsonElement}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- }
- }
- }
|