Learn.VideoAnalysis/VideoAnalysisCore/Controllers/LJZK_Controller.cs

221 lines
8.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
using VideoAnalysisCore.AICore.GPT.Dto;
using VideoAnalysisCore.Model;
using VideoAnalysisCore.Controllers.Dto;
using VideoAnalysisCore.Model.Dto;
namespace VideoAnalysisCore.Controllers
{
/// <summary>
/// À¶¾¨×Ö¿â½Ó¿Ú
/// </summary>
[ApiController]
[Route("LJZK/[action]")]
public class LJZK_Controller : ControllerBase
{
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 Repository<VideoQuestion> videoQuestionDB;
private readonly Repository<VideoQuestionKonw> videoQuestionKonwDB;
public LJZK_Controller(IMapper mp, Repository<NodeSubscription> nodesubscriptionDB,
Repository<VideoTask> videoTaskDB = null, Repository<VideoKonwPoint> videoKonwPointDB = null
, Repository<NodePackageInfo> nodePackageInfoDB = null, Repository<VideoQuestion> videoQuestionDB = null, Repository<VideoQuestionKonw> videoQuestionKonwDB = null)
{
this.mp = mp;
this.nodesubscriptionDB = nodesubscriptionDB;
this.videoTaskDB = videoTaskDB;
this.videoKonwPointDB = videoKonwPointDB;
this.nodePackageInfoDB = nodePackageInfoDB;
this.videoQuestionDB = videoQuestionDB;
this.videoQuestionKonwDB = videoQuestionKonwDB;
}
/// <summary>
/// À¶¾¨ÖÇ¿â_Îļþ°ü¶©ÔÄ
/// </summary>
/// <param name="reqArr">ÇëÇóÌå</param>
/// <returns></returns>
[HttpPost(Name = "NodePackage")]
public async Task<IActionResult> NodePackage(NodePackageReq[] reqArr)
{
Console.WriteLine($"{DateTime.Now} Îļþ°ü¶©ÔÄÇëÇó req=" + reqArr.ToJson());
if (reqArr is null || reqArr.Count() == 0)
return BadRequest("ÎÞЧÊÓÆµÁбíÊý¾Ý");
var videos = new List<VideoTask>(reqArr.Count());
var nodePackages = new List<NodePackageInfo>(reqArr.Count());
var videoIdArr = videoTaskDB.AsQueryable().Select(v => v.TagId).Distinct().ToArray();
foreach (var sGroup in reqArr.GroupBy(s => s.ContentId))
{
var s = sGroup.FirstOrDefault(s => s.VideoType == VideoType.ÉãÏñÍ·);
if (s is null)
return BadRequest("ÎÞÓÐЧµÄÀÏʦÊÚ¿ÎÊÓÆµ");
var sPPT = sGroup.FirstOrDefault(s => s.VideoType == VideoType.PPT¿Î¼þ);
var np = new NodePackageInfo()
{
VideoCode = s.VideoCode,
MaterialId = s.MaterialId,
AttachmentId = s.AttachmentId,
Stage = s.StageId,
CourseId = s.CourseId,
SubjectType = s.SubjectId,
VideoUrl = s.VideoUrl,
CourseType = s.CourseType,
CallBackUrl = s.CallBackUrl,
Area = s.Area,
HostIP = s.HostIP,
};
nodePackages.Add(np);
if (videoIdArr.Contains(s.VideoCode))
continue;
var pptCode = sPPT != null ? sPPT.VideoCode : string.Empty;
videos.Add(new VideoTask()
{
Id = YitIdHelper.NextId(),
ComeFrom = GetClientIpAddress(),
ApiToken = "",
EducationStage = s.StageId,
CourseId = s.CourseId,
Subject = s.SubjectId,
TagId = s.VideoCode,
MediaUrl = s.VideoUrl,
PPTVideoCode = pptCode,
VideoType = s.CourseType
});
}
await nodePackageInfoDB.InsertRangeAsync(nodePackages);
await videoTaskDB.InsertRangeAsync(videos);
if (videos is null || videos.Count == 0)
return Ok();
var ids = videos.Select(s => s.Id).ToArray();
RedisExpand.JoinQueue(ids);
return Ok();
}
private string GetClientIpAddress()
{
// ¼ì²é X-Forwarded-For ÇëÇóÍ·
if (HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
&& !string.IsNullOrEmpty(HttpContext.Request.Headers["X-Forwarded-For"]))
return HttpContext.Request.Headers["X-Forwarded-For"].ToString();
if (HttpContext.Connection.RemoteIpAddress != null)
return HttpContext.Connection.RemoteIpAddress.ToString();
throw new Exception(´ÄÜ»ñÈ¡µ½¿Í»§¶ËipµØÖ·");
}
/// <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="tagId">×Ô¶¨Òåid</param>
/// <returns></returns>
[HttpGet(Name = "TaskKnowInfo")]
public async Task<IActionResult> TaskKnowInfo(string? tagId)
{
if (string.IsNullOrEmpty(tagId))
return BadRequest();
long taskId = 0;
var taskIdOK = long.TryParse(tagId, out taskId);
var task = await videoTaskDB.AsQueryable()
.Where(s => s.TagId == tagId || s.PPTVideoCode == tagId || (taskIdOK && s.Id == taskId))
.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("ÎÞЧÈÎÎñ");
var res = 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,
StageId = s.First().StageId,
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()
};
if (task.VideoType == AttachmentsInfoType.¸´Ï°)
{
var questionArr = await videoQuestionDB
.AsQueryable().Where(s => s.VideoTaskId == task.Id)
.Select<VideoQuestionShowDto>()
.ToArrayAsync();
var konwDic = (await videoQuestionKonwDB
.AsQueryable().Where(s => s.VideoTaskId == task.Id)
.ToArrayAsync()).GroupBy(s => s.VideoQuestionId)
.ToDictionary(s => s.Key);
foreach (var item in questionArr.Where(s => konwDic.ContainsKey(s.Id)))
item.KonwArr = konwDic[item.Id].ToArray();
foreach (var item in res.KnowBlockArr)
item.QuestionArr = questionArr
.Where(s => s.StageId == item.StageId).ToArray();
}
return Ok(res);
}
}
}