Learn.VideoAnalysis/VideoAnalysis/Controllers/LJZK_Controller.cs

199 lines
7.2 KiB
C#

using VideoAnalysisCore.Common;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using MapsterMapper;
using Mapster;
using VideoAnalysisCore.AICore.SherpaOnnx;
using UserCenter.Model.Enum;
using VideoAnalysisCore.AICore.GPT.ChatGPT;
using VideoAnalysisCore.AICore.GPT;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using VideoAnalysisCore.Model.Enum;
using FFmpeg.NET.Services;
using static FFmpeg.NET.MetaData;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Yitter.IdGenerator;
namespace Learn.VideoAnalysis.Controllers
{
/// <summary>
/// 蓝鲸字库接口
/// </summary>
[ApiController]
[Route("LJZK/[action]")]
public class LJZK_Controller : ControllerBase
{
private readonly ILogger<LJZK_Controller> _logger;
private readonly IMapper mp;
private readonly Repository<NodeSubscription> nodesubscriptionDB;
private readonly Repository<VideoTask> videoTaskDB;
private readonly Repository<VideoKonwPoint> videoKonwPointDB;
private readonly Repository<NodePackageInfo> nodePackageInfoDB;
private readonly IBserGPT chatGPT;
public LJZK_Controller(ILogger<LJZK_Controller> logger,
IMapper mp, IBserGPT chatGPT, Repository<NodeSubscription> nodesubscriptionDB,
Repository<VideoTask> videoTaskDB = null, Repository<VideoKonwPoint> videoKonwPointDB = null
, Repository<NodePackageInfo> nodePackageInfoDB = null)
{
_logger = logger;
this.mp = mp;
this.chatGPT = chatGPT;
this.nodesubscriptionDB = nodesubscriptionDB;
this.videoTaskDB = videoTaskDB;
this.videoKonwPointDB = videoKonwPointDB;
this.nodePackageInfoDB = nodePackageInfoDB;
}
/// <summary>
/// 蓝鲸智库_添加文件节点监控
/// </summary>
/// <param name="req">请求体</param>
/// <returns></returns>
[HttpPost(Name = "NodeSubscription")]
public async Task<IActionResult> NodeSubscription(NodeMonitoringReq req)
{
if (req is null || req.NodeId == 0)
return BadRequest("无效的提交数据");
if (nodesubscriptionDB.IsAny(s => s.NodeId == req.NodeId))
return BadRequest("重复添加了节点监控任务" + req.NodeId);
var res = await nodesubscriptionDB.InsertReturnEntityAsync(new NodeSubscription()
{
NodeId = req.NodeId,
TaskType = req.Type ?? default,
Subject = req.Subject ?? default,
});
return Ok(res);
}
/// <summary>
/// 蓝鲸智库_文件包订阅
/// </summary>
/// <param name="req">请求体</param>
/// <returns></returns>
[HttpPost(Name = "NodePackage")]
public async Task<IActionResult> NodePackage(NodePackageReq req)
{
if (req is null || req.NodeId == 0)
return BadRequest("无效的提交数据");
if (req.AnalyzeItems is null || req.AnalyzeItems.Count() == 0)
return BadRequest("无效视频列表数据");
var videos = new List<VideoTask>(req.AnalyzeItems.Count);
var nodePackages = new List<NodePackageInfo>(req.AnalyzeItems.Count);
var videoIdArr = videoTaskDB.AsQueryable().Select(v => v.TagId).Distinct().ToArray();
foreach (var s in req.AnalyzeItems)
{
var np = new NodePackageInfo()
{
MaterialId = s.MaterialId,
VideoCode = s.VideoCode,
NodeId = req.NodeId,
StructurePageContentId = s.StructurePageContentId,
};
nodePackages.Add(np);
if (videoIdArr.Contains(s.VideoCode))
{
Console.WriteLine($"重复任务");
continue;
}
videos.Add(new VideoTask()
{
Id = YitIdHelper.NextId(),
ComeFrom = "127.0.0.1",
ApiToken = "",
Type = req.TaskType,
Subject = req.SubjectType,
Tag = req.NodeId.ToString(),
TagId = s.VideoCode,
MediaUrl =string.Empty,
MediaName = s.VideoName
});
}
await nodePackageInfoDB.InsertRangeAsync(nodePackages);
await videoTaskDB.InsertRangeAsync(videos);
var ids = videos.Select(s => s.Id).ToArray();
RedisExpand.JoinQueue(ids);
return Ok();
}
/// <summary>
/// 获取任务类型
/// </summary>
/// <returns></returns>
[HttpGet(Name = "TaskTypList")]
public IActionResult TaskType()
{
Type type = typeof(TaskTypeEnum);
return Ok(Enum.GetValues(type).Cast<object>()
.Select(s => new { Text = s.ToString(), Value = (int)s }));
}
/// <summary>
/// 获取学科类型
/// </summary>
/// <returns></returns>
[HttpGet(Name = "SubjectList")]
public IActionResult Subject()
{
Type type = typeof(SubjectEnum);
return Ok(Enum.GetValues(type).Cast<object>()
.Select(s => new { Text = s.ToString(), Value = (int)s }));
}
/// <summary>
/// 获取视频知识点片段<para>taskId/tagId二选一</para>
/// </summary>
/// <param name="taskId"></param>
/// <param name="tagId">自定义id</param>
/// <returns></returns>
[HttpGet(Name = "TaskKnowInfo")]
public async Task<IActionResult> TaskKnowInfo(long taskId, string? tagId)
{
if (taskId == 0 && string.IsNullOrEmpty(tagId))
return BadRequest();
var task = await videoTaskDB.AsQueryable()
.WhereIF(taskId != 0, s => s.Id == taskId)
.WhereIF(!string.IsNullOrEmpty(tagId), s => s.TagId == tagId)
.FirstAsync();
if (task is null)
return BadRequest("无效任务");
var konwArr = await videoKonwPointDB.AsQueryable()
.Where(s => s.VideoTaskId == task.Id)
.ToArrayAsync();
if (konwArr is null || konwArr.Length == 0)
return BadRequest("无效任务");
return Ok(new TaskKnowRes()
{
TagId = task.TagId,
Status = task.LastEnum,
VideoTaskId = task.Id,
KnowBlockArr = konwArr
.GroupBy(s => s.StartTime)
.Select(s => new TaskKnowBlock()
{
Id = s.First().Id,
Content = s.First().Content,
StartTime = s.First().StartTime,
EndTime = s.First().EndTime,
Theme = s.First().Theme,
Know = s.Select(x => new TaskKnowInfo()
{
Id = x.Id,
KnowPoint = x.KnowPoint,
KnowPointId = x.KnowPointId
}).ToArray()
}).ToArray()
});
}
}
}