CrazyIter_Bin 5 months ago
parent
commit
4cb42a1d2e

+ 48 - 12
TEAMModelOS.SDK/Models/Service/EvaluationSyncInfoService.cs

@@ -4,6 +4,7 @@ using Microsoft.Azure.Cosmos;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net.Http;
 using System.Reflection.Metadata.Ecma335;
 using System.Text;
 using System.Text.Json;
@@ -30,7 +31,8 @@ namespace TEAMModelOS.SDK.Models.Service
         /// <param name="type"></param>
         /// <param name="azureCosmos"></param>
         /// <param name="azureStorage"></param>
-        public static async Task<EvaluationSyncInfo> PackageEvaluation( string id,string scope, string ownerId, string type, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage,CoreAPIHttpService _coreAPIHttpService, DingDing _dingDing)
+        public static async Task<EvaluationSyncInfo> PackageEvaluation( string id,string scope, string ownerId, string type, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage,
+            CoreAPIHttpService _coreAPIHttpService, DingDing _dingDing, IHttpClientFactory _httpClientFactory)
         {
             EvaluationSource evaluationSource = new EvaluationSource() { type=type,id=id};
             EvaluationSyncInfo evaluationSyncInfo= null;
@@ -47,18 +49,52 @@ namespace TEAMModelOS.SDK.Models.Service
             string schoolCode = null;
             if (scope.Equals("school"))
             {
-                School school = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(ownerId, new PartitionKey("Base"));
-                schoolCode= ownerId;
-                evaluationSource.school = school;
-                ownerName = school.name;
-                ownerPicture = school.picture;
+                try {
+                    School school = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(ownerId, new PartitionKey("Base"));
+                    schoolCode= ownerId;
+                    evaluationSource.school = school;
+                    ownerName = school.name;
+                    ownerPicture = school.picture;
+                    if (!string.IsNullOrWhiteSpace(ownerPicture))
+                    {
+                        //学校logo 下载到本地、
+                        HttpResponseMessage message = await _httpClientFactory.CreateClient().GetAsync(ownerPicture);
+                        if (message.IsSuccessStatusCode)
+                        {
+                            string base64Prefix = "data:image/jpeg;base64,";
+                            byte[] fileBytes = await message.Content.ReadAsByteArrayAsync();
+                            ownerPicture =$"{base64Prefix}{Convert.ToBase64String(fileBytes)}";
+                        }
+                    }
+                }
+                catch (Exception ex) {
+                    await _dingDing.SendBotMsg($"打包评测数据,获取学校信息报错,{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
+                    return null;
+                }
             }
             else 
             {
-                Teacher teacher = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(ownerId, new PartitionKey("Base"));
-                evaluationSource.teacher = teacher;
-                ownerName = teacher.name;
-                ownerPicture = teacher.picture;
+                try {
+                    Teacher teacher = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(ownerId, new PartitionKey("Base"));
+                    evaluationSource.teacher = teacher;
+                    ownerName = teacher.name;
+                    ownerPicture = teacher.picture;
+                    if (!string.IsNullOrWhiteSpace(ownerPicture))
+                    {
+                        //学校logo 下载到本地、
+                        HttpResponseMessage message = await _httpClientFactory.CreateClient().GetAsync(ownerPicture);
+                        if (message.IsSuccessStatusCode)
+                        {
+                            string base64Prefix = "data:image/jpeg;base64,";
+                            byte[] fileBytes = await message.Content.ReadAsByteArrayAsync();
+                            ownerPicture =$"{base64Prefix}{Convert.ToBase64String(fileBytes)}";
+                        }
+                    }
+                }
+                catch (Exception ex) { 
+                    await _dingDing.SendBotMsg($"打包评测数据,获取教师信息报错,{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
+                    return null;
+                }
             }
             var responseEvaluationSyncInfo = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).ReadItemStreamAsync(id, new PartitionKey("EvaluationSyncInfo"));
             if (responseEvaluationSyncInfo.IsSuccessStatusCode)
@@ -308,7 +344,7 @@ namespace TEAMModelOS.SDK.Models.Service
                     dataStr.Append(evaluationSyncInfo.name);
                     dataStr.Append(evaluationSyncInfo.type);
                     dataStr.Append(evaluationSyncInfo.owner);
-                    dataStr.Append(evaluationSyncInfo.ow);
+                    dataStr.Append(evaluationSyncInfo.ownerId);
                     dataStr.Append(evaluationSyncInfo.scode);
                     dataStr.Append(evaluationSyncInfo.scope);
                     dataStr.Append(evaluationSyncInfo.grouplistHash);
@@ -369,7 +405,7 @@ namespace TEAMModelOS.SDK.Models.Service
                 await azureStorage.GetBlobContainerClient(ownerId).UploadFileByContainer(groupListJson, $"package/{id}", "groupList.json");
                 await azureStorage.GetBlobContainerClient(ownerId).UploadFileByContainer(new { evaluationClient, evaluationExams }.ToJsonString(), $"package/{id}", "evaluation.json");
                 evaluationSyncInfo.dataSize = dataSize;
-                //学校logo 下载到本地、
+              
                 await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).UpsertItemAsync<EvaluationSyncInfo>(evaluationSyncInfo, new PartitionKey("EvaluationSyncInfo"));
                 // await azureStorage.GetBlobContainerClient(owner).UploadFileByContainer(evaluationSyncInfo.ToJsonString(), $"package/{id}", "syncinfo.json");
             }

+ 17 - 7
TEAMModelOS/Controllers/Both/EvaluationSyncInfoController.cs

@@ -16,6 +16,7 @@ using TEAMModelOS.SDK.Models.Service;
 using TEAMModelOS.SDK.Models.Cosmos;
 using System.Text;
 using System.Linq;
+using System.Net.Http;
 
 
 namespace TEAMModelOS.Controllers.Both
@@ -28,15 +29,17 @@ namespace TEAMModelOS.Controllers.Both
     [ApiController]
     public class EvaluationSyncInfoController : ControllerBase
     {
-        private AzureCosmosFactory _azureCosmos;
+        private readonly AzureCosmosFactory _azureCosmos;
         private readonly DingDing _dingDing;
         private readonly CoreAPIHttpService _coreAPIHttpService;
         private readonly Option _option;
         private readonly AzureServiceBusFactory _serviceBus;
         private readonly AzureStorageFactory _azureStorage;
         private readonly AzureRedisFactory _azureRedis;
-        public IConfiguration _configuration { get; set; }
-        public EvaluationSyncInfoController(AzureRedisFactory azureRedis, AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService coreAPIHttpService, AzureServiceBusFactory serviceBus, AzureStorageFactory azureStorage, IConfiguration configuration)
+        public readonly IConfiguration _configuration;
+        public readonly IHttpClientFactory _httpClientFactory;
+        public EvaluationSyncInfoController(AzureRedisFactory azureRedis, AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService coreAPIHttpService,
+            AzureServiceBusFactory serviceBus, AzureStorageFactory azureStorage, IConfiguration configuration, IHttpClientFactory httpClientFactory)
         {
             _azureCosmos = azureCosmos;
             _dingDing = dingDing;
@@ -45,6 +48,7 @@ namespace TEAMModelOS.Controllers.Both
             _configuration = configuration;
             _azureStorage = azureStorage;
             _azureRedis = azureRedis; _coreAPIHttpService = coreAPIHttpService;
+            _httpClientFactory = httpClientFactory;
         }
 
         /// <summary>
@@ -111,8 +115,11 @@ namespace TEAMModelOS.Controllers.Both
                 {
                     foreach (var item in result.list) 
                     {
-                        EvaluationSyncInfo syncInfo = await EvaluationSyncInfoService.PackageEvaluation(item.id, item.scope, item.school, "Art", _azureCosmos, _azureStorage, _coreAPIHttpService, _dingDing);
-                        infos.Add(syncInfo);
+                        EvaluationSyncInfo syncInfo = await EvaluationSyncInfoService.PackageEvaluation(item.id, item.scope, item.school, "Art", _azureCosmos, _azureStorage, _coreAPIHttpService, _dingDing,_httpClientFactory);
+                        if (syncInfo!=null) 
+                        {
+                            infos.Add(syncInfo);
+                        }
                     }
                 }
             }
@@ -126,8 +133,11 @@ namespace TEAMModelOS.Controllers.Both
                     return BadRequest(validResult);
                 }
 
-                EvaluationSyncInfo syncInfo = await EvaluationSyncInfoService.PackageEvaluation(activity.id, activity.scope, activity.owner, activity.type, _azureCosmos, _azureStorage, _coreAPIHttpService, _dingDing);
-                infos.Add(syncInfo);
+                EvaluationSyncInfo syncInfo = await EvaluationSyncInfoService.PackageEvaluation(activity.id, activity.scope, activity.owner, activity.type, _azureCosmos, _azureStorage, _coreAPIHttpService, _dingDing, _httpClientFactory);
+                if (syncInfo!=null)
+                {
+                    infos.Add(syncInfo);
+                }
             }
             return Ok(new { infos });
         }