|
@@ -0,0 +1,120 @@
|
|
|
|
+using StackExchange.Redis;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
|
+
|
|
|
|
+namespace TEAMModelOS.SDK.Models.Service
|
|
|
|
+{
|
|
|
|
+ public class GenPDFService
|
|
|
|
+ {
|
|
|
|
+ public async Task<string> AddGenPdfQueue(AzureServiceBusFactory azureServiceBus , AzureRedisFactory azureRedis, GenPDFData data)
|
|
|
|
+ {
|
|
|
|
+ long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
+ List<PDFGenParam> genParams = new List<PDFGenParam>();
|
|
|
|
+ List<PDFGenParam> dbgenParams = new List<PDFGenParam>();
|
|
|
|
+ HashEntry[] datas = await azureRedis.GetRedisClient(8).HashGetAllAsync($"PDFGen:{data.sessionId}");
|
|
|
|
+ if (datas!= null && datas.Length > 0)
|
|
|
|
+ {
|
|
|
|
+ foreach (var item in datas)
|
|
|
|
+ {
|
|
|
|
+ dbgenParams.Add(item.Value.ToString().ToObject<PDFGenParam>());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ int countProcess= 0;
|
|
|
|
+ foreach (var item in data.datas)
|
|
|
|
+ {
|
|
|
|
+ var dbData = dbgenParams.Find(x => x.id.Equals(item.id));
|
|
|
|
+ if (dbData!=null)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ if (dbData.status==0|| dbData.status==1)
|
|
|
|
+ {
|
|
|
|
+ //不变的
|
|
|
|
+ countProcess+=1;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ //需要变更的
|
|
|
|
+ dbData.status = 0;
|
|
|
|
+ dbData.cost=0;
|
|
|
|
+ dbData.join= now;
|
|
|
|
+ dbData.wait=0;
|
|
|
|
+ genParams.Add(dbData);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ genParams.Add(new PDFGenParam { id = item.id, status=0, cost=0, wait=0, join=now });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //过期时间 当前个数+ reddis的个数
|
|
|
|
+ foreach (var item in genParams)
|
|
|
|
+ {
|
|
|
|
+ countProcess+=1;
|
|
|
|
+ await azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{data.sessionId}", item.id, item.ToJsonString());
|
|
|
|
+ }
|
|
|
|
+ long expire = (data.delay+data.timeout) * countProcess ;
|
|
|
|
+ var tiemSpan = TimeSpan.FromMilliseconds(expire);
|
|
|
|
+ await azureRedis.GetRedisClient(8).KeyExpireAsync($"PDFGen:{data.sessionId}", tiemSpan);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public class GenPDFData
|
|
|
|
+ {
|
|
|
|
+ public string pageUrl { get; set; }
|
|
|
|
+ public long timeout { get; set; } = 30000;
|
|
|
|
+ public long delay { get; set; } = 2000;
|
|
|
|
+ public string orientation { get; set; }
|
|
|
|
+ public string pageSize { get; set; }
|
|
|
|
+ public string pageWidth { get; set; }
|
|
|
|
+ public string pageHeight { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 生成会话id, 活动id
|
|
|
|
+ /// </summary>
|
|
|
|
+ public string sessionId { get; set; }
|
|
|
|
+
|
|
|
|
+ public List<PDFData> datas { get; set; } = new List<PDFData>();
|
|
|
|
+ }
|
|
|
|
+ public record PDFData{
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 学生id
|
|
|
|
+ /// </summary>
|
|
|
|
+ public string id { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 数据链接
|
|
|
|
+ /// </summary>
|
|
|
|
+ public string url { get; set; }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// redis 存储数据,超时时间设置为status=0 的count timeout* count+ delay*count
|
|
|
|
+ /// </summary>
|
|
|
|
+ public record PDFGenParam
|
|
|
|
+ {
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 学生id
|
|
|
|
+ /// </summary>
|
|
|
|
+ public string id { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 0 未执行,1 执行中,2 执行成功,3 执行失败,4超时,5 取消
|
|
|
|
+ /// </summary>
|
|
|
|
+ public int status { get; set; } = 0;
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 执行生成 毫秒
|
|
|
|
+ /// </summary>
|
|
|
|
+ public long cost { get; set;}
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 等候时间
|
|
|
|
+ /// </summary>
|
|
|
|
+ public long wait { get; set;}
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 加入时间
|
|
|
|
+ /// </summary>
|
|
|
|
+ public long join { get; set; }
|
|
|
|
+ }
|
|
|
|
+}
|