CrazyIter_Bin 2 anni fa
parent
commit
6c7710af97

+ 145 - 2
TEAMModelOS.FunctionV4/HttpTrigger/IESHttpTrigger.cs

@@ -1,12 +1,16 @@
 using Azure.Cosmos;
 using Azure.Storage.Blobs.Models;
 using Azure.Storage.Sas;
+using DocumentFormat.OpenXml.Spreadsheet;
 using HTEXLib.COMM.Helpers;
 using HTEXLib.Models;
 using Microsoft.AspNetCore.Components;
 using Microsoft.Azure.Cosmos.Table;
 using Microsoft.Azure.Functions.Worker;
 using Microsoft.Azure.Functions.Worker.Http;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Options;
 using OpenXmlPowerTools;
 using StackExchange.Redis;
 using System;
@@ -23,12 +27,15 @@ using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
 using System.Web;
+using TEAMModelOS.Models;
 using TEAMModelOS.SDK;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
+using TEAMModelOS.SDK.Models.Service;
 using TEAMModelOS.SDK.Models.Table;
 using static TEAMModelOS.SDK.Models.Teacher;
+using static TEAMModelOS.SDK.Services.BlobService;
 
 namespace TEAMModelOS.FunctionV4
 {
@@ -39,15 +46,24 @@ namespace TEAMModelOS.FunctionV4
         private readonly AzureStorageFactory _azureStorage;
         private readonly AzureRedisFactory _azureRedis;
         private readonly IHttpClientFactory _httpClient;
-        public IESHttpTrigger( AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
-      , AzureRedisFactory azureRedis, IHttpClientFactory httpClient)
+        private readonly Option _option;
+        private readonly CoreAPIHttpService _coreAPIHttpService;
+        private readonly IConfiguration _configuration;
+        public IESHttpTrigger( AzureCosmosFactory azureCosmos, DingDing dingDing, CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage
+      , AzureRedisFactory azureRedis, IHttpClientFactory httpClient, IOptionsSnapshot<Option> option,
+             IConfiguration configuration)
         {
             _azureCosmos = azureCosmos;
             _dingDing = dingDing;
             _azureStorage = azureStorage;
             _azureRedis = azureRedis;
             _httpClient = httpClient;
+            _coreAPIHttpService = coreAPIHttpService;
+            _option = option?.Value;
+            _configuration = configuration;
         }
+
+
         /// <summary>
         /// </summary>
         /// <param name="req"></param>
@@ -82,6 +98,133 @@ namespace TEAMModelOS.FunctionV4
 
 
         }
+        /// <summary>
+        /// 空间不足发送通知
+        /// </summary>
+        /// <param name="req"></param>
+        /// <param name="log"></param>
+        /// <returns></returns>
+        [Function("surplus-space-notify")]
+        public async Task<HttpResponseData> SurplusSpaceNotify([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req) {
+            var response = req.CreateResponse(HttpStatusCode.OK);
+            string msg = "";
+            try
+            {
+                string data = await new StreamReader(req.Body).ReadToEndAsync();
+                var json = JsonDocument.Parse(data).RootElement;
+                json.TryGetProperty("name", out JsonElement _name);
+                json.TryGetProperty("scope", out JsonElement _scope);
+                json.TryGetProperty("percent", out JsonElement _percent);
+                double percent= _percent.GetDouble();
+                string name = _name.ToString();
+                string scope = _scope.ToString();
+               
+                int tag = 11;
+                if (percent <= 10 && percent > 5)
+                {
+                    tag = 10;
+                }
+                else if (percent <= 5 && percent > 0)
+                {
+                    tag = 5;
+                }
+                else if (percent <= 0)
+                {
+                    tag = 0;
+                }
+               
+                string notifyCode = string.Empty;
+                List<IdNameCode> ids = new List<IdNameCode>();
+                Teacher teacher = null;
+                School school = null;
+
+                if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
+                {
+                    school = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(name, new PartitionKey("Base"));
+                    notifyCode = "blob-space-school-notify";
+                    string sql = $"select   value c from c    where c.code='Teacher-{name}' and c.status='join'  and  array_contains(c.roles,'admin') ";
+                    List<SchoolTeacher> adminTeachers = new List<SchoolTeacher>();
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
+                        .GetItemQueryIterator<SchoolTeacher>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{name}") }))
+                    {
+                        adminTeachers.Add(item);
+                    }
+                    if (adminTeachers.IsNotEmpty())
+                    {
+
+                        string sqlAdmin = $"select c.id,c.lang  as code ,c.name from c where c.id in ({string.Join(",", adminTeachers.Select(z => $"'{z.id}'"))}) ";
+                        await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
+                            .GetItemQueryIterator<IdNameCode>(queryText: sqlAdmin, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
+                        {
+                            ids.Add(item);
+                        }
+
+                    }
+                }
+                else
+                {
+                    notifyCode = "blob-space-private-notify";
+                    teacher=  await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(name, new PartitionKey("Base"));
+                    ids.Add(new IdNameCode
+                    {
+                        id = teacher.id,
+                        name = teacher.name,
+                        code = teacher.lang
+                    });
+                } 
+                //如果已经扩容请忽略此通知!
+                string key = scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? $"Blob:Space:School:Notify:{name}" : $"Blob:Space:Private:Notify:{name}";
+                foreach (var idnamecode in ids)
+                {
+                    string filed = $"{idnamecode.id}-{tag}";
+                    BlobSpaceNotify? blobSpaceNotify = null;
+                    RedisValue value = await _azureRedis.GetRedisClient(8).HashGetAsync(key, filed);
+                    if (value != default && !value.IsNullOrEmpty)
+                    {
+                        blobSpaceNotify = value.ToString().ToObject<BlobSpaceNotify>();
+                    }
+                    if (tag < 11)
+                    {
+
+                        if (blobSpaceNotify == null)
+                        {
+                            blobSpaceNotify = new BlobSpaceNotify { };
+                            await _azureRedis.GetRedisClient(8).HashSetAsync(key, name, blobSpaceNotify.ToJsonString());
+
+                            if ("school".Equals(scope, StringComparison.OrdinalIgnoreCase))
+                            {
+                                _coreAPIHttpService.PushNotify(ids, $"blob-space-school-notify", Constant.NotifyType_IES5_Management,
+                                new Dictionary<string, object> { { "tmdname", name }, { "schoolName", school.name }, { "percent", $"{tag}" }, { "schoolId", $"{school.id}" }, { "tmdid", idnamecode.id } },
+                                $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing, "");
+
+                            }
+                            else
+                            {
+                                _coreAPIHttpService.PushNotify(ids, $"blob-space-private-notify", Constant.NotifyType_IES5_Management,
+                                    new Dictionary<string, object> { { "tmdname", name }, { "percent", $"{tag}" }, { "tmdid", idnamecode.id } },
+                                    $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing, "");
+                            }
+                        }
+                        else { 
+                            //已经发送过的不在提交
+                        }
+                    }
+                    else
+                    {
+                        if (blobSpaceNotify != null)
+                        {
+                            //撤销
+                            var index =  blobSpaceNotify.notifyIndex;
+                            await _azureRedis.GetRedisClient(8).HashDeleteAsync(key, filed);
+                        }
+                        //等撤销
+                    }
+                }
+               
+            }
+            catch (Exception ex) { }
+            return response;
+        }
 
         /// <summary>
         /// 区级艺术评价变更,异步同步已开启数据同步的学校。

+ 8 - 33
TEAMModelOS.SDK/Models/Service/Common/BlobService.cs

@@ -2,13 +2,16 @@ using Azure;
 using Azure.Cosmos;
 using Azure.Messaging.ServiceBus;
 using Azure.Storage.Blobs;
+using DocumentFormat.OpenXml.Spreadsheet;
 using DocumentFormat.OpenXml.Wordprocessing;
 using HTEXLib.COMM.Helpers;
 using Microsoft.Azure.Cosmos.Table;
 using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Hosting;
 using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
+using System.Configuration;
 using System.Linq;
 using System.Text;
 using System.Text.Json;
@@ -17,6 +20,7 @@ using TEAMModelOS.Models;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
+using TEAMModelOS.SDK.Models.Service;
 
 namespace TEAMModelOS.SDK.Services
 {
@@ -60,6 +64,7 @@ namespace TEAMModelOS.SDK.Services
             long total = 0;
             // 剩余的
             long surplus = 0;
+            Teacher teacher = null;
             Dictionary<string, double?> catalog = new Dictionary<string, double?>();
             try
             {
@@ -89,7 +94,7 @@ namespace TEAMModelOS.SDK.Services
                     }
                     else
                     {
-                        Teacher teacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(name, new PartitionKey("Base"));
+                         teacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(name, new PartitionKey("Base"));
                         total = teacher.size;
                         foreach (var school in teacher.schools)
                         {
@@ -175,40 +180,10 @@ namespace TEAMModelOS.SDK.Services
                 return (usedSize, teach, total, surplus, catalog);
             }
             if (usedSize > 0 && total > 0) {
-                double percent= surplus / total * 1073741824 *100;
-                int  tag =11;
-                if (percent <= 10 && percent > 5)
-                {
-                    tag = 10;
-                }
-                else if (percent <= 5 && percent > 0)
-                {
-                    tag = 5;
-                }
-                else if (percent <= 0)
-                {
-                    tag = 0;
-                }
-                //如果已经扩容请忽略此通知!
-                string key = scope.Equals("school",StringComparison.OrdinalIgnoreCase)? $"Blob:Space:School:Notify:{name}" : "Blob:Space:Private:Notify";
-                //string filed=
-                if (tag < 11)
-                {
-                    BlobSpaceNotify? blobSpaceNotify = null;
-                    RedisValue value  = _azureRedis.GetRedisClient(8).HashGet(key, name);
-                    if (value != default && !value.IsNullOrEmpty)
-                    {
-                        JsonElement record = value.ToString().ToObject<JsonElement>();
-                    }
-                     blobSpaceNotify = new BlobSpaceNotify { };
-                    await _azureRedis.GetRedisClient(8).HashSetAsync(key, name, blobSpaceNotify.ToJsonString());
-                }
-                else { 
-                    
-                }
+
+                double percent = surplus / total * 1073741824 * 100;
 
 
-               
             }
             return (usedSize, teach, total, surplus, catalog);
         }

+ 1 - 1
TEAMModelOS/Lang/zh-cn.json

@@ -31,7 +31,7 @@
   "art-template-comment5": "你在本次考核中{pointLow}等知识有待提高,希望进一步加强。",
   "art-template-subject_music": "其实音乐殿堂的门槛并没有你想的那么高,对吧?希望你继续努力能够在这里欣赏到更美丽的风景!跳动的音符充满魅力,每个人的生活都离不开音乐,加油吧,相信你在音乐中会有所收获的。",
   "art-template-subject_painting": "你敢于探素,乐于欣赏,在五彩的画笔中快乐成长,如果你再多一点耐心、仔细刻画,一定会更棒!你积极热爱美术,总是充满快乐,希望你能够坚持美术学习,永不放弃,在美术的道路上快乐地成长!",
-  "blob-space-school-notify": "亲爱的{schoolName}管理员{tmdname}您好,提醒您贵校剩余空间不足{percent}%,请联系官方客服购买空间,详情请登入IES(teammodel.net)查看,如果已经扩容请忽略此通知。",
+  "blob-space-school-notify": "亲爱的{schoolName}管理员{tmdname}您好,提醒您贵校剩余空间不足{percent}%,请联系官方客服购买空间,详情请登入IES(teammodel.cn)查看,如果已经扩容请忽略此通知。",
   "blob-space-private-notify": "亲爱的{tmdname}您好,提醒您的帐号{tmdid}授权的剩余空间不足{percent}%,请联系学校管理员配置或是购买个人空间,如果已经扩容请忽略此通知。",
   "notify-status": {
     "code0": "凭据失效",