CrazyIter_Bin 1 year ago
parent
commit
568e76ddf8

+ 2 - 2
TEAMModelOS.SDK/Models/Cosmos/School/Elegant.cs

@@ -49,12 +49,12 @@ namespace TEAMModelOS.SDK.Models
      
         public List<Attachment> attachments { get; set; } = new List<Attachment>();
         public string target { get; set; }
-      
+
         /// <summary>
         /// 业务类型。elegant 德育, art 艺术,弃用
         /// </summary>
         // public List<string> bizCode { get; set; }
-        public List<string> bizType { get; set; }
+        public List<string> bizType { get; set; } = new List<string>();
         /// <summary>
         /// image  video
         /// </summary>

+ 70 - 0
TEAMModelOS.TEST/Program.cs

@@ -17,6 +17,76 @@ namespace TEAMModelOS.TEST
             //var jsonAuth = System.IO.File.ReadAllText("C:\\Users\\CrazyIter\\Downloads\\492266088181141504\\ActivityInfo.json", Encoding.UTF8);
             //var jsonData = jsonAuth.ToObject<LessonRecordActivityInfo>();
             string path = "C:\\Users\\CrazyIter\\Downloads\\消费清单(2022-2023)\\bill";
+            List<List<string>> inputArray = new List<List<string>>
+            {  
+                new List<string> { "2", "11" },
+                new List<string> { "1", "22" },
+                new List<string> { "1", "11", "111" },
+                new List<string> { "2", "22", "222" },
+                new List<string> { "1", "11" },
+                new List<string> { "1", "11" },
+                new List<string> { "1" },
+                new List<string> { "2", "22", "222" },
+                new List<string> { "2", "22" }  ,
+            };
+
+            // 转换为层级结构
+            List<ClassifiedItem> result = ClassifyHierarchy(inputArray);
+
+            // 输出结果
+            foreach (var item in result)
+            {
+                Console.WriteLine($"id: {item.Id}, count: {item.Count}, pid: {item.Pid}");
+            }
+        }
+        static List<ClassifiedItem> ClassifyHierarchy(List<List<string>> inputArray)
+        {
+            Dictionary<string, int> hierarchyCount = new Dictionary<string, int>();
+            List<ClassifiedItem> result = new List<ClassifiedItem>();
+
+            foreach (var list in inputArray)
+            {
+                for (int i = 0; i < list.Count; i++)
+                {
+                    string currentId = list[i];
+                    string parentId = (i > 0) ? list[i - 1] : null;
+
+                    string hierarchyKey = $"{currentId}|{parentId}";
+
+                    if (hierarchyCount.ContainsKey(hierarchyKey))
+                    {
+                        hierarchyCount[hierarchyKey]++;
+                    }
+                    else
+                    {
+                        hierarchyCount[hierarchyKey] = 1;
+                    }
+                    var item = result.Find(item => item.Id == currentId && item.Pid == parentId);
+                    if (item== null  )
+                    {
+                        result.Add(new ClassifiedItem
+                        {
+                            Id = currentId,
+                            Pid = parentId,
+                            Count = 0
+                        });
+                    }
+                }
+            }
+
+            foreach (var item in result)
+            {
+                string hierarchyKey = $"{item.Id}|{item.Pid}";
+                item.Count = hierarchyCount.ContainsKey(hierarchyKey) ? hierarchyCount[hierarchyKey] : 0;
+            }
+
+            return result;
+        }
+        class ClassifiedItem
+        {
+            public string Id { get; set; }
+            public int Count { get; set; }
+            public string Pid { get; set; }
         }
     }
 

+ 170 - 1
TEAMModelOS/Controllers/School/ElegantController.cs

@@ -5,6 +5,8 @@ using System.Dynamic;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
+using System.Net.Http.Json;
 using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
@@ -16,6 +18,7 @@ using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Cryptography.KeyDerivation;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Options;
+using Newtonsoft.Json;
 using TEAMModelOS.Filter;
 using TEAMModelOS.Models;
 using TEAMModelOS.SDK.DI;
@@ -23,6 +26,8 @@ using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos;
 using TEAMModelOS.SDK.Models.Cosmos.Common;
+using TEAMModelOS.SDK.Models.Dtos;
+using TEAMModelOS.SDK.Models.Service;
 namespace TEAMModelOS.Controllers
 {
     [Route("school/elegant")]
@@ -34,17 +39,181 @@ namespace TEAMModelOS.Controllers
         private readonly AzureStorageFactory _azureStorage;
         private readonly DingDing _dingDing;
         private readonly Option _option;
+        private IHttpClientFactory _httpClientFactory;
         public ElegantController(
            AzureCosmosFactory azureCosmos,
            AzureStorageFactory azureStorage,
            DingDing dingDing,
-           IOptionsSnapshot<Option> option
+           IOptionsSnapshot<Option> option,IHttpClientFactory httpClientFactory
            )
         {
             _azureCosmos = azureCosmos;
             _azureStorage = azureStorage;
             _dingDing = dingDing;
             _option = option?.Value;
+            _httpClientFactory = httpClientFactory;
+        }
+
+        /// <summary>
+        /// 新增 或 修改
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("statistics")]
+#if !DEBUG
+        [Authorize(Roles = "IES")]
+        [AuthToken(Roles = "teacher,admin,area")]
+#endif
+        public async Task<IActionResult> Statistics(JsonElement  json)
+        {
+            if (!json.TryGetProperty("scope", out JsonElement _scope))
+            {
+                return BadRequest();
+            }
+            if (!json.TryGetProperty("code", out JsonElement _code))
+            {
+                return BadRequest();
+            }
+            List<Elegant> elegants = new List<Elegant>();
+            if ($"{_scope}".Equals("area"))
+            {
+                string sql = $"select value c.id  from c where c.areaId ='{_code}'";
+                var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<string>(sql, "Base");
+                if (result.list.IsNotEmpty())
+                {
+                    string sqlE = $"select value c from c where c.school in ({string.Join(",", result.list.Select(z => $"'{z}'"))})";
+                    var resultE = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Elegant>(sqlE);
+                    if (resultE.list.IsNotEmpty())
+                    {
+                        elegants.AddRange(resultE.list);
+                    }
+                }
+            }
+            else if ($"{_scope}".Equals("school"))
+            {
+                string sqlE = $"select value c from c ";
+                var resultE = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Elegant>(sqlE,$"Elegant-{_code}");
+                if (resultE.list.IsNotEmpty())
+                {
+                    elegants.AddRange(resultE.list);
+                }
+            }
+            List<ClassifiedItem> items = ClassifyHierarchy(elegants);
+            string rs = string.Empty;
+            List<dynamic> comments = new List<dynamic>();
+            foreach (var es in elegants) {
+                comments.Add(new { comment = $"{es.title} {es.content}" });
+            }
+            if (comments.IsNotEmpty()) {
+                rs= comments.ToJsonString();
+            }
+            foreach (var item in items)
+            {
+                var es = elegants.FindAll(z => item.sid.Contains(z.id));
+                if (es.IsNotEmpty()) {
+                    var video = es.SelectMany(z => z.attachments).Where(a =>!string.IsNullOrWhiteSpace(a.type) &&  a.type.Equals("video"));
+                    if (video.Any()) { 
+                        item.videoCount = video.Count();
+                    }
+                    var image = es.SelectMany(z => z.attachments).Where(a => !string.IsNullOrWhiteSpace(a.type) &&a.type.Equals("image"));
+                    if (image.Any())
+                    {
+                        item.imageCount = image.Count();
+                    }
+                    var doc = es.SelectMany(z => z.attachments).Where(a => !string.IsNullOrWhiteSpace(a.type) && a.type.Equals("doc"));
+                    if (doc.Any())
+                    {
+                        item.docCount = doc.Count();
+                    }
+                    var other = es.SelectMany(z => z.attachments).Where(a => string.IsNullOrWhiteSpace(a.type) ||  (!a.type.Equals("doc") && !a.type.Equals("video") && !a.type.Equals("image")  && !a.type.Equals("doc")));
+                    if (other.Any())
+                    {
+                        item.otherCount = other.Count();
+                    }
+                }
+            }
+            List<CommentKeyCount> keyCounts = new List<CommentKeyCount>();
+            if (!string.IsNullOrWhiteSpace(rs)) {
+               var httpClient=  _httpClientFactory.CreateClient();
+                if (!httpClient.DefaultRequestHeaders.Contains("x-functions-key")) {
+                    httpClient.DefaultRequestHeaders.Add("x-functions-key", "2BcXFR_hvzG1pZjqIkaM7Dx74Hcu6m0PwwOacFpDpq44AzFuHJBRXA==");
+                }
+                string paramJson = JsonConvert.SerializeObject(new { rs = $"{rs}", fmt = "0" });
+                var content = new StringContent(paramJson, Encoding.UTF8, "application/json");
+                HttpResponseMessage httpResponse=  await httpClient.PostAsync("https://malearn.teammodel.cn/api/txtwc", content);
+                if (httpResponse.IsSuccessStatusCode) {
+                    string str = await httpResponse.Content.ReadAsStringAsync();
+                    if (str.Contains("freq"))
+                    { keyCounts= str.ToObject<List<CommentKeyCount>>(); }
+                }
+            }
+            return Ok(new { keyCounts,items= items.Select(z=>new {z.id ,z.pid,z.count,z.videoCount,z.docCount,z.imageCount,z.otherCount })});
+        }
+
+        private  List<ClassifiedItem> ClassifyHierarchy(List<Elegant> inputArray)
+        {
+            Dictionary<string, int> hierarchyCount = new Dictionary<string, int>();
+            List<ClassifiedItem> result = new List<ClassifiedItem>();
+
+            foreach (var list in inputArray)
+            {
+                if (list.bizType.IsEmpty()) {
+                    list.bizType.Add("德育风采");
+                }
+                for (int i = 0; i < list.bizType.Count; i++)
+                {
+                    string currentId = list.bizType[i];
+                    string parentId = (i > 0) ? list.bizType[i - 1] : null;
+                    string hierarchyKey = $"{currentId}|{parentId}";
+                    if (hierarchyCount.ContainsKey(hierarchyKey))
+                    {
+                        hierarchyCount[hierarchyKey]++;
+                    }
+                    else
+                    {
+                        hierarchyCount[hierarchyKey] = 1;
+                    }
+                    var item =  result.Find(item => item.id == currentId && item.pid == parentId);
+                    if (item== null)
+                    {
+                        result.Add(new ClassifiedItem
+                        {
+                            id = currentId,
+                            pid = parentId,
+                            count = 0,
+                            sid= new List<string> {list.id }
+                        });
+                    }
+                    else {
+                        item.sid.Add(list.id);
+                    }
+                }
+            }
+            foreach (var item in result)
+            {
+                string hierarchyKey = $"{item.id}|{item.pid}";
+                item.count = hierarchyCount.ContainsKey(hierarchyKey) ? hierarchyCount[hierarchyKey] : 0;
+            }
+            return result;
+        }
+        class CommentKeyCount
+        { 
+            public int id { get; set; }
+            public string word { get; set; }
+            public int freq { get; set; }
+
+        }
+        class ClassifiedItem
+        {
+            public List<string> sid { get; set; } = new List<string>();
+            public string id { get; set; }
+            public int count { get; set; }
+            public string pid { get; set; }
+            public int videoCount { get; set; }
+            public int imageCount { get; set; }
+            public int docCount { get; set; }
+            public int otherCount { get; set; }
         }
         /// <summary>
         /// 新增 或 修改