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
{
///
/// 汇总统计通知
///
/// 连接
/// 错误消息发送
/// 统计类型
/// 账号类型 private:个人 school 学校
/// 账号id
/// 数量
/// 创建时间
///
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.StatusCode == System.Net.HttpStatusCode.OK)
{
using var fileJson = await JsonDocument.ParseAsync(resStsInfo.Content);
statsNotice = fileJson.ToObject();
}
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.StatusCode == System.Net.HttpStatusCode.OK)
{
statsNotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(statsNotice, statsNotice.id, new PartitionKey("StatsNotice"));
}
else
{
statsNotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(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.成都开发測試群組);
}
}
///
/// 发送每日课程信息通知
///
///
///
///
///
///
///
public static async Task MonitorStatsNotice(CoreAPIHttpService _coreAPIHttpService, CosmosClient cosmosClient, DingDing _dingDing, IConfiguration _configuration,JsonElement jsonElement)
{
try
{
StatsNotice statsNotice = jsonElement.ToObject();
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 statsNotices = new();
await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("StatsNotice") }))
{
statsNotices.Add(item);
}
foreach (var item in statsNotices)
{
List tacherIds = new();
if (item.type == "school")
{
await foreach (var tchId in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql(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(), $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing);
}
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},MonitorStatsNotice(),参数:{jsonElement}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
}
}