using AlibabaCloud.SDK.Vod20170321;
using Coravel.Invocable;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UserCenter.Model.Enum;
using VideoAnalysisCore.Common;
using VideoAnalysisCore.Model;
using VideoAnalysisCore.Model.Dto;
using VideoAnalysisCore.Model.蓝鲸智库;
using Yitter.IdGenerator;
namespace VideoAnalysisCore.Job
{
///
/// [蓝鲸智库] 查找未处理的视频
///
public class NodeSubscriptionJob : IInvocable
{
///
/// 每个扫描文件包每次取出{20}个
///
private readonly int TopLength = 20;
private readonly Repository nodesubscriptionDB;
private readonly Repository attachmentsDB;
private readonly Repository videotaskDB;
private readonly Client vodClient;
public NodeSubscriptionJob(Repository videoTaskDB, Repository nodesubscriptionDB, Repository videotaskDB, Client vodClient)
{
this.attachmentsDB = videoTaskDB;
this.nodesubscriptionDB = nodesubscriptionDB;
this.videotaskDB = videotaskDB;
this.vodClient = vodClient;
}
public async Task Invoke()
{
Console.WriteLine($"{DateTime.Now} Invoke=>{this.GetType().FullName}");
var tasks = await nodesubscriptionDB.GetListAsync(s => s.Enable && s.Subject ==SubjectEnum.数学);
foreach (var item in tasks)
{
var fileNodeId = item.NodeId;
var data = attachmentsDB.Context.Ado
.SqlQuery($"""
SELECT top {TopLength}
*
FROM
Attachments
WHERE
Id IN (
SELECT
AttachmentsId
FROM
Material
WHERE
id IN (
SELECT
MaterialId
FROM
FileContentMaterial
WHERE
FileContentId IN ( SELECT id FROM FileContent WHERE BagId IN ( SELECT Id FROM FileDirectory WHERE Id={fileNodeId} AND types = 1 AND DeleteState = 0 ) )
AND DeleteState = 0
AND ( MaterialName NOT LIKE '%PPT%' OR MaterialName NOT LIKE '%ppt%' )
)
)
AND Type = '录播视频'
AND (
NAME NOT LIKE '%PPT%'
OR NAME NOT LIKE '%ppt%'
) and id>{item.LastId}
""");
if (data is null || data.Count == 0)
continue;
Console.WriteLine($"{DateTime.Now} 视频订阅=>Node:{item.NodeId} 数量{data.Count}");
var videos = new List(data.Count);
foreach (var s in data)
{
var videoInfo = await vodClient.GetPlayInfoAsync(new AlibabaCloud.SDK.Vod20170321.Models.GetPlayInfoRequest()
{
VideoId = s.VideoCode, Formats="mp4",OutputType = "cdn", AuthTimeout = 3600 * 24 * 3,
});
if (videoInfo is null || videoInfo.StatusCode != 200 && !videoInfo.Body.PlayInfoList.PlayInfo.Any())
{
Console.WriteLine($"{DateTime.Now} 视频订阅=>获取阿里云视频信息失败 VideoCode {s.VideoCode} StatusCode {videoInfo?.StatusCode}");
continue;
}
videos.Add(new VideoTask()
{
Id = YitIdHelper.NextId(),
ComeFrom = "127.0.0.1",
ApiToken = "",
Type = item.TaskType,
Subject = item.Subject,
Tag = item.NodeId.ToString(),
TagId = s.VideoCode,
MediaUrl = videoInfo.Body.PlayInfoList.PlayInfo.First().PlayURL,
MediaName = s.Name
});
}
var maxId = data.Max(s => s.Id);
//入库 更新最后扫描记录
await nodesubscriptionDB.AsUpdateable()
.SetColumns(it => it.LastId == maxId)
.Where(it => it.Id == item.Id)
.ExecuteCommandAsync();
await videotaskDB.InsertRangeAsync(videos);
var ids = videos.Select(s => s.Id).ToArray();
RedisExpand.JoinQueue(ids);
}
}
}
}