using AlibabaCloud.SDK.Vod20170321; using Coravel.Invocable; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http.Json; using System.Reflection; using System.Security.Cryptography; using System.Security.Policy; using System.Text; using System.Text.Json; 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 NodePackageJob : IInvocable { /// /// 每个扫描文件包每次取出{20}个 /// private readonly int TopLength = 20; private readonly Repository nodePackageInfoDB; private readonly Repository attachmentsDB; private readonly Repository videoTaskDB; private readonly IHttpClientFactory _clientFactory; public NodePackageJob(Repository videoTaskDB, Repository nodePackageInfoDB, Repository videotaskDB, IHttpClientFactory clientFactory) { this.attachmentsDB = videoTaskDB; this.nodePackageInfoDB = nodePackageInfoDB; this.videoTaskDB = videotaskDB; _clientFactory = clientFactory; } public async Task Invoke() { Console.WriteLine($"{DateTime.Now} 执行=>文件包任务"); var taskArr = await nodePackageInfoDB.AsQueryable() .Where(s => s.SuccessTime == null) .ToArrayAsync(); var videoArr = await videoTaskDB.AsQueryable() .Where(s => s.EndTime != null) .Select(s =>new VideoTask { Id = s.Id, TagId = s.TagId, VideoType=s.VideoType }) .ToArrayAsync(); var videoDic = videoArr .Where(s=>s.TagId != null) .GroupBy(s => s.TagId!) .ToDictionary(s => s.Key,s=>s.First()); var videoIdArr = videoArr.Select(s => s.TagId).ToArray(); var videoPPTArr = await videoTaskDB.AsQueryable() .Where(s => s.EndTime != null) .Select(s => s.PPTVideoCode) .ToArrayAsync(); var postData = new List(); foreach (var item in taskArr) { if (videoIdArr.Contains(item.VideoCode) || videoPPTArr.Contains(item.VideoCode)) { if (videoDic.ContainsKey(item.VideoCode)) { var v = videoDic[item.VideoCode]; if (v.VideoType!=null && v.VideoType != item.CourseType) item.CourseType = v.VideoType.Value;//额外同步课程类型[新课 复习课] } postData.Add(item); item.SuccessTime = DateTime.Now; } } Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 任务回调 数量{postData.Count} ..."); if (postData.Count() == 0) return; foreach (var item in postData) { HttpResponseMessage? responseMessage = null; try { var postUrl = !string.IsNullOrWhiteSpace(item.CallBackUrl) ? item.CallBackUrl : AppCommon.Config.Subsystem.蓝鲸智库.APIUrl + "/api/callback/platform/videosAnalyze"; var apiClent = _clientFactory.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Post, postUrl); request.Headers.Add("Area", item.Area); // 直接添加到本次请求头 request.Headers.Add("HostIP", item.HostIP); // 直接添加到本次请求头 request.Content = new StringContent(new object[] { item }.ToJson(), Encoding.UTF8, "application/json"); responseMessage = await apiClent.SendAsync(request); if (responseMessage.IsSuccessStatusCode) { var res = await responseMessage.Content.ReadAsStringAsync(); Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 回调结果 {res}"); await nodePackageInfoDB.AsUpdateable(item) .UpdateColumns(it => new { it.SuccessTime }) .ExecuteCommandAsync(); } else { var res = await responseMessage.Content.ReadAsStringAsync(); Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 回调失败!!! {responseMessage.StatusCode} {res}"); } } catch (Exception ex) { if (responseMessage != null) { var res = (await responseMessage?.Content?.ReadAsStringAsync()) ?? string.Empty; Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 回调失败!!! {responseMessage?.StatusCode} {res}"); } Console.WriteLine(ex); } await Task.Delay(1000); } Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 已完成任务回调 数量{postData.Count}"); } } }