You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.1 KiB

2 weeks ago
using Azure.Core;
using Dapper;
2 weeks ago
using Microsoft.EntityFrameworkCore;
using System.Drawing.Printing;
2 weeks ago
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
2 weeks ago
using TaskManager.Contracts.Dtos;
using TaskManager.Entity;
using TaskManager.EntityFramework;
namespace TaskManager.Controllers
3 weeks ago
{
2 weeks ago
public class CheryRecurringJobInputPageController<T,OUTPUT> : CheryRecurringJobBaseController where T : BaseEntity
2 weeks ago
{
2 weeks ago
public CheryRecurringJobInputPageController(HttpClient httpClient, JobDbContext jobDbContext, LogController log, IRepository<T> repository) : base(httpClient, jobDbContext, log)
2 weeks ago
{
}
2 weeks ago
protected override async Task DoExecutingAsync(string url, string path, string takName, string client)
2 weeks ago
{
2 weeks ago
Url = url;
Path = path;
TaskName = takName;
Client = client;
await SyncTaskSubTable(TaskName,Client);
2 weeks ago
}
2 weeks ago
private async Task<QRReturnInfo> PostPageAsync(PagedRequest<T> t)
2 weeks ago
{
2 weeks ago
try
2 weeks ago
{
2 weeks ago
var inputjson = JsonSerializer.Serialize(t,
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true // 可选,用于格式化输出
}
);
inputjson = RemoveWhitespace(inputjson);
var content = await Post(Url, Path, inputjson);
if (!string.IsNullOrEmpty(content))
2 weeks ago
{
2 weeks ago
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = {
new JsonStringEnumConverter(), // 枚举转换
new CustomDateTimeConverter("yyyy-MM-dd HH:mm:ss") // 日期转换
}
};
return JsonSerializer.Deserialize<QRReturnInfo>(content, options);
}
else
{
await _logger.AddError($"调用接口无返回值{Url}", TaskName);
return null;
2 weeks ago
}
}
2 weeks ago
catch (Exception ex)
{
await _logger.AddError($"调用接口无返回值错误{ex.Message}", TaskName);
return null;
}
2 weeks ago
2 weeks ago
}
2 weeks ago
2 weeks ago
public static string GenerateRandomStringWith8EG()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var sb = new StringBuilder(32);
2 weeks ago
2 weeks ago
// 随机生成 "8EG" 的插入位置 (0-29)
int position = random.Next(0, 30);
2 weeks ago
2 weeks ago
// 生成前半部分随机字符串
for (int i = 0; i < position; i++)
2 weeks ago
{
2 weeks ago
sb.Append(chars[random.Next(chars.Length)]);
2 weeks ago
}
2 weeks ago
// 插入 "8EG"
sb.Append("8EG");
// 生成后半部分随机字符串,补足32位
int remainingLength = 32 - sb.Length;
for (int i = 0; i < remainingLength; i++)
2 weeks ago
{
2 weeks ago
sb.Append(chars[random.Next(chars.Length)]);
2 weeks ago
}
2 weeks ago
return sb.ToString();
}
2 weeks ago
2 weeks ago
private async Task SyncTaskSubTable(string taskName, string client)
2 weeks ago
{
2 weeks ago
if (string.IsNullOrEmpty(taskName) || string.IsNullOrEmpty(client))
{
await _logger.AddError("任务名称或客户端不能为空",taskName);
return;
}
2 weeks ago
var sublist = _jobDbContext.TaskSub.Where(p => p.TaskName == taskName && p.WriteState == false && p.Subscriber == client).ToList();
2 weeks ago
int pageSize = 1000;
if (!sublist.Any())
{
foreach (var sub in sublist)
{
2 weeks ago
string querystr = $"select * from {sub.TableName} where TaskId='{sub.TaskId}' and WriteState=0 order by uid";//任务表
2 weeks ago
2 weeks ago
var entites = _jobDbContext.Database.GetDbConnection().Query<T>(querystr);//明细表
if (entites.Any())
2 weeks ago
{
2 weeks ago
var total = entites.Count();
int totalPages = (int)Math.Ceiling((double)total / pageSize);
for (int i = 1; i <= totalPages; i++)
{
var records = entites.Skip((i - 1) * pageSize)
.Take(pageSize).ToList();
PagedRequest<T> pagedRequest = new PagedRequest<T>()
{
batchNo = GenerateRandomStringWith8EG(),
total = entites.Count(),
pageSize = pageSize,
list = records,
pageNum = i
};
foreach (var itm in records)
{
itm.WriteState = true;
}
var result = await PostPageAsync(pagedRequest);
if (result.code == "200")
{
_jobDbContext.BulkUpdate(records);
sub.SyncedPageCount = i;
if (result.data.totalGet == total)
{
sub.WriteState = true;
_jobDbContext.Set<TaskSub>().Update(sub);
_jobDbContext.SaveChanges();
}
}
else
{
await _logger.AddError($"第 {i} 页数据保存失败,请检查数据。", TaskName);
}
}
2 weeks ago
2 weeks ago
}
2 weeks ago
2 weeks ago
}
}
2 weeks ago
}
2 weeks ago
2 weeks ago
}
3 weeks ago
}