From ae6e5ce836ce200bec6fb2a8ad511d0052756879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=82=A5=E7=BE=8A?= <1048382248@qq.com> Date: Thu, 6 Mar 2025 18:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VideoAnalysis/Components/Resources/I18n.cs | 2 +- VideoAnalysis/Controllers/Dto/ApiDto.cs | 4 ++ VideoAnalysis/Controllers/LJZK_Controller.cs | 11 +++++ .../Expand}/CoravelExpand.cs | 6 +-- VideoAnalysis/Expand/CorsExpand.cs | 41 +++++++++++++++++++ .../Expand}/SqlSugarExpand.cs | 20 ++++----- VideoAnalysis/GlobalUsings.cs | 3 +- VideoAnalysis/Program.cs | 4 ++ VideoAnalysisCore/Common/RedisExpand.cs | 36 +++++++++------- VideoAnalysisCore/Job/NodeSubscriptionJob.cs | 26 ++++++++++-- VideoAnalysisCore/Model/NodeSubscription.cs | 4 ++ VideoAnalysisCore/Model/VideoTask.cs | 2 +- 12 files changed, 125 insertions(+), 34 deletions(-) rename {VideoAnalysisCore/Common => VideoAnalysis/Expand}/CoravelExpand.cs (87%) create mode 100644 VideoAnalysis/Expand/CorsExpand.cs rename {VideoAnalysisCore/Common => VideoAnalysis/Expand}/SqlSugarExpand.cs (92%) diff --git a/VideoAnalysis/Components/Resources/I18n.cs b/VideoAnalysis/Components/Resources/I18n.cs index b0fb331..bc4604e 100644 --- a/VideoAnalysis/Components/Resources/I18n.cs +++ b/VideoAnalysis/Components/Resources/I18n.cs @@ -1,4 +1,4 @@ -namespace VideoAnalysisRazor.Resources; +namespace Learn.VideoAnalysis.Components.Resources; internal class I18n diff --git a/VideoAnalysis/Controllers/Dto/ApiDto.cs b/VideoAnalysis/Controllers/Dto/ApiDto.cs index 512610d..2a6139a 100644 --- a/VideoAnalysis/Controllers/Dto/ApiDto.cs +++ b/VideoAnalysis/Controllers/Dto/ApiDto.cs @@ -21,6 +21,10 @@ namespace Learn.VideoAnalysis.Controllers.Dto /// 任务类型 /// public TaskTypeEnum? Type { get; set; } + /// + /// 学科类型 + /// + public SubjectEnum? Subject { get; set; } } /// diff --git a/VideoAnalysis/Controllers/LJZK_Controller.cs b/VideoAnalysis/Controllers/LJZK_Controller.cs index 5efa27e..8ced3cc 100644 --- a/VideoAnalysis/Controllers/LJZK_Controller.cs +++ b/VideoAnalysis/Controllers/LJZK_Controller.cs @@ -64,6 +64,17 @@ namespace Learn.VideoAnalysis.Controllers return Ok(Enum.GetValues(type).Cast() .Select(s => new { Text = s.ToString(), Value = (int)s })); } + /// + /// ȡѧ + /// + /// + [HttpGet(Name = "SubjectList")] + public IActionResult Subject() + { + Type type = typeof(SubjectEnum); + return Ok(Enum.GetValues(type).Cast() + .Select(s => new { Text = s.ToString(), Value = (int)s })); + } diff --git a/VideoAnalysisCore/Common/CoravelExpand.cs b/VideoAnalysis/Expand/CoravelExpand.cs similarity index 87% rename from VideoAnalysisCore/Common/CoravelExpand.cs rename to VideoAnalysis/Expand/CoravelExpand.cs index 720fdb7..77d3aa2 100644 --- a/VideoAnalysisCore/Common/CoravelExpand.cs +++ b/VideoAnalysis/Expand/CoravelExpand.cs @@ -8,12 +8,12 @@ using System.Text; using System.Threading.Tasks; using VideoAnalysisCore.Job; -namespace VideoAnalysisCore.Common +namespace Learn.VideoAnalysis.Expand { public static class CoravelExpand { public static int MyProperty { get; set; } - public static void Init(IServiceCollection service) + public static void Init(IServiceCollection service) { service.AddScheduler(); @@ -24,7 +24,7 @@ namespace VideoAnalysisCore.Common provider.UseScheduler(scheduler => { //每5分钟执行一次 未处理视频扫描 - scheduler.Schedule().EveryFiveMinutes(); + scheduler.Schedule().EveryFiveMinutes(); }); } } diff --git a/VideoAnalysis/Expand/CorsExpand.cs b/VideoAnalysis/Expand/CorsExpand.cs new file mode 100644 index 0000000..c521851 --- /dev/null +++ b/VideoAnalysis/Expand/CorsExpand.cs @@ -0,0 +1,41 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace Learn.VideoAnalysis.Expand +{ + /// + /// + /// + public static class CorsExpand + { + /// + /// 添加跨域拓展 + /// + /// + public static void AddCorsExpand(this IServiceCollection services) + { + services.AddCors(c => + { + c.AddPolicy("All", policy => + { + policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); + }); + }); + } + /// + /// 使用跨域 + /// + /// + public static void UseCorsExpand(this WebApplication app) + { + app.UseCors("All"); + // 获取配置文件中的允许跨域的地址 + app.UseCors(options => + { + options.WithOrigins("*") // 允许跨域请求的地址 + .AllowAnyHeader() // 允许的请求标头 + .AllowAnyMethod(); // 允许跨域请求的类型 (GET,POST等) + }); + } + } +} \ No newline at end of file diff --git a/VideoAnalysisCore/Common/SqlSugarExpand.cs b/VideoAnalysis/Expand/SqlSugarExpand.cs similarity index 92% rename from VideoAnalysisCore/Common/SqlSugarExpand.cs rename to VideoAnalysis/Expand/SqlSugarExpand.cs index a9ebbcf..8847e02 100644 --- a/VideoAnalysisCore/Common/SqlSugarExpand.cs +++ b/VideoAnalysis/Expand/SqlSugarExpand.cs @@ -1,5 +1,4 @@ - -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MySqlConnector; using SqlSugar; @@ -11,13 +10,14 @@ using System.Linq; using System.Reflection; using System.Xml.Linq; using VideoAnalysisCore.Common; +using Yitter.IdGenerator; -namespace VideoAnalysisCore.Common +namespace Learn.VideoAnalysis.Expand { public static class SqlSugarExpand { public static bool ShowSQL = false; - public static void InitSqlSugar( this IServiceCollection services) + public static void InitSqlSugar(this IServiceCollection services) { #region SqlSugar注入 var dbList = new List() { @@ -31,9 +31,9 @@ namespace VideoAnalysisCore.Common }; dbList.AddRange(AppCommon.Config.OtherDBArr.Select(s => new IocConfig() { - ConfigId=s.ConfigId, - ConnectionString=s.ConnectionString, - DbType=s.SqlType, + ConfigId = s.ConfigId, + ConnectionString = s.ConnectionString, + DbType = s.SqlType, IsAutoCloseConnection = true })); services.AddSingleton(typeof(Repository<>)); @@ -79,10 +79,10 @@ namespace VideoAnalysisCore.Common if (entityInfo.EntityColumnInfo.IsPrimarykey && !entityInfo.EntityColumnInfo.IsIdentity && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long)) { var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue); - //if (id == null || (long)id == 0) - // entityInfo.SetValue(YitIdHelper.NextId()); + if (id == null || (long)id == 0) + entityInfo.SetValue(YitIdHelper.NextId()); } - if (entityInfo.PropertyName == "CreateTime") + if (entityInfo.PropertyName == "CreateTime" && entityInfo.EntityValue is null) entityInfo.SetValue(DateTime.Now); } if (entityInfo.OperationType == DataFilterType.UpdateByObject) diff --git a/VideoAnalysis/GlobalUsings.cs b/VideoAnalysis/GlobalUsings.cs index cd883c0..db8f5ee 100644 --- a/VideoAnalysis/GlobalUsings.cs +++ b/VideoAnalysis/GlobalUsings.cs @@ -1,5 +1,4 @@ -global using VideoAnalysisRazor.Resources; -global using AntDesign; +global using AntDesign; global using VideoAnalysisCore.Model; global using VideoAnalysisCore.Model.Dto; diff --git a/VideoAnalysis/Program.cs b/VideoAnalysis/Program.cs index a51080b..0394844 100644 --- a/VideoAnalysis/Program.cs +++ b/VideoAnalysis/Program.cs @@ -10,6 +10,7 @@ using VideoAnalysisCore.AICore.GPT.ChatGPT; using Microsoft.Extensions.FileProviders; using VideoAnalysisCore.AICore.GPT.DeepSeek; using Microsoft.Extensions.DependencyInjection; +using Learn.VideoAnalysis.Expand; @@ -85,6 +86,7 @@ namespace Learn.VideoAnalysis builder.Services.AddAntDesign(); builder.Services.AddMapster(); + builder.Services.AddCorsExpand(); builder.Services.Configure(builder.Configuration.GetSection("ProSettings")); @@ -125,6 +127,8 @@ namespace Learn.VideoAnalysis app.MapControllers(); + app.UseCorsExpand(); + //Զ Ӧ SqlSugarExpand.InitDB(); CoravelExpand.Run(app.Services); diff --git a/VideoAnalysisCore/Common/RedisExpand.cs b/VideoAnalysisCore/Common/RedisExpand.cs index 365d26b..fedaf31 100644 --- a/VideoAnalysisCore/Common/RedisExpand.cs +++ b/VideoAnalysisCore/Common/RedisExpand.cs @@ -93,8 +93,8 @@ namespace VideoAnalysisCore.Common public static void Init() { Console.WriteLine("初始化 redis"); - Redis.Serialize = obj => System.Text.Json.JsonSerializer.Serialize(obj); - Redis.Deserialize = (json, type) => System.Text.Json.JsonSerializer.Deserialize(json, type); + Redis.Serialize = obj => JsonSerializer.Serialize(obj); + Redis.Deserialize = (json, type) => JsonSerializer.Deserialize(json, type); Task.Run(() => { Thread.Sleep(1000 * 10); @@ -108,11 +108,19 @@ namespace VideoAnalysisCore.Common /// public static void SetTaskGPTCached(object taskId, object? data) { - Redis.Set(RedisExpandKey.TaskGPT(taskId)+":Res_" + DateTime.Now.ToString("yyyy/MM/dd_HH/mm/ss"), data, 3600 * 24); + Redis.Set(RedisExpandKey.TaskGPT(taskId) + ":Res_" + DateTime.Now.ToString("yyyy/MM/dd_HH/mm/ss"), data, 3600 * 24); } public static void SetTaskGPTReqCached(object taskId, object? data) { - Redis.Set(RedisExpandKey.TaskGPT(taskId)+":Req_"+ DateTime.Now.ToString("yyyy/MM/dd_HH/mm/ss"), data, 3600*24); + Redis.Set(RedisExpandKey.TaskGPT(taskId) + ":Req_" + DateTime.Now.ToString("yyyy/MM/dd_HH/mm/ss"), data, 3600 * 24); + } + /// + /// 加入到消费队列 + /// + /// + public static void JoinQueue(params long[] taskId) + { + Redis.LPush(RedisExpandKey.ChannelKey, taskId); } /// /// 获取任务进度 @@ -152,8 +160,8 @@ namespace VideoAnalysisCore.Common Redis.HMSet(RedisExpandKey.Task(taskId), "StartTime", startTime); - if(!SubscribeList.ContainsKey(@enum)) - throw new Exception(@enum+" 未实现"); + if (!SubscribeList.ContainsKey(@enum)) + throw new Exception(@enum + " 未实现"); SubscribeList[@enum].Invoke(taskId.ToString()); } @@ -161,7 +169,7 @@ namespace VideoAnalysisCore.Common public static async Task TaskEnd(string task) { var tId = long.Parse(task); - var gptRes = (await RedisExpand.Redis + var gptRes = (await Redis .HMGetAsync(RedisExpandKey.Task(task), "ChatAnalysis")).FirstOrDefault(); if (gptRes is null) throw new Exception("未能读取到GPT处理结果"); @@ -169,13 +177,13 @@ namespace VideoAnalysisCore.Common var taskData = await DbScoped.SugarScope.Queryable() .FirstAsync(s => s.Id == tId); if (taskData.Captions == "[]") - taskData.Captions = (await Redis.HMGetAsync(RedisExpandKey.Task(task), "Captions")).First(); + taskData.Captions = (await Redis.HMGetAsync(RedisExpandKey.Task(task), "Captions")).First(); if (taskData.Speaker == "[]") taskData.Speaker = (await Redis.HMGetAsync(RedisExpandKey.Task(task), "Speaker")).First(); - taskData.ChatAnalysis =JsonSerializer.Serialize(gptRes); + taskData.ChatAnalysis = JsonSerializer.Serialize(gptRes); taskData.ChatAnalysisScore = gptRes?.Assessment?.Merit?.Sum(s => s.Score) ?? 0; taskData.ErrorMessage = string.Empty; taskData.LastEnum = RedisChannelEnum.EndTask; @@ -239,7 +247,7 @@ namespace VideoAnalysisCore.Common public static async Task ReceivingTaskAsync() { var oldTask = await Redis.GetAsync(RedisExpandKey.IDTask); - if (!string.IsNullOrEmpty(oldTask)) + if (!string.IsNullOrEmpty(oldTask)) { var lastEnum = (await Redis.HMGetAsync(RedisExpandKey.Task(oldTask), "LastEnum")).FirstOrDefault(); await SetTaskErrorMessage(long.Parse(oldTask), null); @@ -268,7 +276,7 @@ namespace VideoAnalysisCore.Common public static async Task SetTaskErrorMessage(long taskID, Exception? ex) { var error = string.Empty; - if (ex != null) + if (ex != null) { //执行任务时出现异常 error = ex.Message + ex.StackTrace; @@ -282,7 +290,7 @@ namespace VideoAnalysisCore.Common } Redis.HMSet(RedisExpandKey.Task(taskID), "ErrorMessage", error); - + return await DbScoped.SugarScope.Updateable() .SetColumns(it => it.ErrorMessage == error)//SetColumns是可以叠加的 写2个就2个字段赋值 .Where(it => it.Id == taskID) @@ -313,7 +321,7 @@ namespace VideoAnalysisCore.Common .SetColumns(it => it.LastEnum == key) .Where(it => it.Id == tID) .ExecuteCommandAsync(); - await action(taskId); + await action(taskId); return; } catch (Exception ex) @@ -324,7 +332,7 @@ namespace VideoAnalysisCore.Common Console.WriteLine(ex.StackTrace); Console.WriteLine("=============================================="); Thread.Sleep(1000); - Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 稍后后重试." + key + " " + taskId ); + Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 稍后后重试." + key + " " + taskId); } } await SetTaskErrorMessage(tID, errArr.First()); diff --git a/VideoAnalysisCore/Job/NodeSubscriptionJob.cs b/VideoAnalysisCore/Job/NodeSubscriptionJob.cs index 2a39c7f..834eb0e 100644 --- a/VideoAnalysisCore/Job/NodeSubscriptionJob.cs +++ b/VideoAnalysisCore/Job/NodeSubscriptionJob.cs @@ -2,10 +2,12 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using VideoAnalysisCore.Common; using VideoAnalysisCore.Model; +using VideoAnalysisCore.Model.Dto; using VideoAnalysisCore.Model.蓝鲸智库; namespace VideoAnalysisCore.Job @@ -17,17 +19,19 @@ namespace VideoAnalysisCore.Job { private readonly Repository nodesubscriptionDB; private readonly Repository attachmentsDB; - public NodeSubscriptionJob(Repository videoTaskDB, Repository nodesubscriptionDB) + private readonly Repository videotaskDB; + public NodeSubscriptionJob(Repository videoTaskDB, Repository nodesubscriptionDB, Repository videotaskDB) { this.attachmentsDB = videoTaskDB; this.nodesubscriptionDB = nodesubscriptionDB; + this.videotaskDB = videotaskDB; } public async Task Invoke() { Console.WriteLine($"{DateTime.Now} Invoke=>{this.GetType().FullName}"); - var tasks = await nodesubscriptionDB.GetListAsync(s => s.Enable); - foreach (var item in tasks) + var tasks = await nodesubscriptionDB.GetListAsync(s => s.Enable); + foreach (var item in tasks) { var fileNodeId = item.NodeId; var data = attachmentsDB.Context.Ado @@ -61,6 +65,22 @@ namespace VideoAnalysisCore.Job ) """); + var videos = data.Select(s => new VideoTask() + { + ComeFrom = "127.0.0.1", + ApiToken = "", + Type = item.TaskType, + Subject = item.Subject, + Tag = string.Empty, + TagId = s.VideoCode, + MediaUrl = s.Url, + MediaName = s.Name + }).ToArray(); + + //入库 + await videotaskDB.InsertRangeAsync(videos); + var ids = videos.Select(s => s.Id).ToArray(); + RedisExpand.JoinQueue(ids); } } diff --git a/VideoAnalysisCore/Model/NodeSubscription.cs b/VideoAnalysisCore/Model/NodeSubscription.cs index 9be18cb..4f6c826 100644 --- a/VideoAnalysisCore/Model/NodeSubscription.cs +++ b/VideoAnalysisCore/Model/NodeSubscription.cs @@ -36,6 +36,10 @@ namespace VideoAnalysisCore.Model [DisplayName("绑定学科")] public TaskTypeEnum TaskType { get; set; } /// + /// 学科 + /// + public SubjectEnum Subject { get; set; } + /// /// 是否启用 /// [DisplayName("是否启用")] diff --git a/VideoAnalysisCore/Model/VideoTask.cs b/VideoAnalysisCore/Model/VideoTask.cs index 3fc49e6..1217c50 100644 --- a/VideoAnalysisCore/Model/VideoTask.cs +++ b/VideoAnalysisCore/Model/VideoTask.cs @@ -21,7 +21,7 @@ namespace VideoAnalysisCore.Model /// 任务id /// 视频音频文件地址都使用taskID能获取 /// - [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [SugarColumn(IsPrimaryKey = true)] public long Id { get; set; } /// /// 媒体路径