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 Yitter.IdGenerator;
using VideoAnalysisCore.AICore.GPT.Dto;
using VideoAnalysisCore.Model;
using VideoAnalysisCore.Controllers.Dto;
using VideoAnalysisCore.Model.Dto;
using SqlSugar;
using AlibabaCloud.SDK.Vod20170321;
using VideoAnalysisCore.Model.蓝鲸智库;
using AlibabaCloud.SDK.Vod20170321.Models;
namespace VideoAnalysisCore.Controllers
{
///
/// 蓝鲸字库接口
///
[ApiController]
[Route("LJZK/[action]")]
public class LJZK_Controller : ControllerBase
{
private readonly IMapper mp;
private readonly Repository nodesubscriptionDB;
private readonly Repository videoTaskDB;
private readonly Repository courseInfoDB;
private readonly Repository videoKonwPointDB;
private readonly Client vodClient;
private readonly Repository videoTaskStageDB;
private readonly Repository nodePackageInfoDB;
private readonly Repository videoQuestionDB;
private readonly Repository videoQuestionKonwDB;
private readonly RedisManager redisManager;
public LJZK_Controller(IMapper mp, Repository nodesubscriptionDB,
Repository videoTaskDB = null, Repository videoKonwPointDB = null
, Repository nodePackageInfoDB = null, Repository videoQuestionDB = null,
Repository videoQuestionKonwDB = null, Repository courseInfoDB = null,
RedisManager redisManager = null, Repository videoTaskStageDB = null, Client vodClient = null)
{
this.mp = mp;
this.nodesubscriptionDB = nodesubscriptionDB;
this.videoTaskDB = videoTaskDB;
this.videoKonwPointDB = videoKonwPointDB;
this.nodePackageInfoDB = nodePackageInfoDB;
this.videoQuestionDB = videoQuestionDB;
this.videoQuestionKonwDB = videoQuestionKonwDB;
this.courseInfoDB = courseInfoDB;
this.redisManager = redisManager;
this.videoTaskStageDB = videoTaskStageDB;
this.vodClient = vodClient;
}
///
/// 蓝鲸智库_文件包订阅
///
/// 请求体
///
[HttpPost(Name = "NodePackage")]
public async Task NodePackage(NodePackageReq[] reqArr)
{
if (reqArr is null || reqArr.Count() == 0)
return BadRequest("无效视频列表数据");
var videos = new List(reqArr.Count());
var nodePackages = new List(reqArr.Count());
var videoIdArr = videoTaskDB.AsQueryable().Select(v => v.TagId).Distinct().ToArray();
var courseArr = await courseInfoDB.AsQueryable().ToArrayAsync();
//系统可接收任务的学科
var subjectArr = new List
{
SubjectEnum.语文,
SubjectEnum.数学,
SubjectEnum.英语,
SubjectEnum.物理,
SubjectEnum.化学,
SubjectEnum.生物,
SubjectEnum.政治,
SubjectEnum.历史,
SubjectEnum.地理,
};
var courseTypeArr = new List
{
AttachmentsInfoType.无,
AttachmentsInfoType.新课,
AttachmentsInfoType.复习
};
foreach (var sGroup in reqArr.GroupBy(s => s.ContentId))
{
var s = sGroup.FirstOrDefault(s => s.VideoType == VideoType.摄像头);
if (s is null)
return BadRequest("无有效的老师授课视频");
//校验学科有效
if (!subjectArr.Contains(s.SubjectId)) continue;
//无效的课程类型
if (!courseTypeArr.Contains(s.CourseType)) continue;
var stageId = s.StageId.GetHashCode();
var subjectId = s.SubjectId.GetHashCode();
var course = courseArr.FirstOrDefault(x => stageId == x.Stage_Id && subjectId == x.Subject_Id);
if (course == null) continue;
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 = course.Id,
SubjectType = s.SubjectId,
VideoUrl = s.VideoUrl,
CourseType = s.CourseType,
CallBackUrl = s.CallBackUrl,
CloudSchoolId = s.UserCenterCloudSchoolId,
Area = s.Area,
HostIP = s.HostIP,
StageId = s.StageId,
GradeId = s.GradeId,
GradeYear = s.Trem == 0 ? null : s.Trem,
GradeSemester = s.GradeSemester,
TextBookVersionId = s.TextBookVersionId,
};
nodePackages.Add(np);
if (videoIdArr.Contains(s.VideoCode)) continue;
var pptCode = sPPT != null ? sPPT.VideoCode : string.Empty;
var pptUrl = sPPT != null ? sPPT.VideoUrl : string.Empty;
videos.Add(new VideoTask()
{
Id = YitIdHelper.NextId(),
ComeFrom = GetClientIpAddress(),
ApiToken = "",
EducationStage = s.StageId,
CourseId = course.Id,
Subject = s.SubjectId,
TagId = s.VideoCode,
MediaUrl = s.VideoUrl,
PPTVideoCode = pptCode,
PPTVideoUrl = pptUrl,
VideoType = s.CourseType,
CloudSchoolId = s.UserCenterCloudSchoolId,
TextBookVersionId = s.TextBookVersionId,
GradeSemester = s.GradeSemester,
CourseLevel = s.CourseLevel,
GradeId = s.GradeId,
GradeYear = np.GradeYear,
});
}
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();
redisManager.JoinQueue(RedisExpandKey.ChannelKey, 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地址");
}
///
/// 获取任务类型
///
///
[HttpGet(Name = "TaskTypList")]
public IActionResult TaskType()
{
Type type = typeof(TaskTypeEnum);
return Ok(Enum.GetValues(type).Cast