|
|
|
using Magicodes.ExporterAndImporter.Core.Extension;
|
|
|
|
using Magicodes.ExporterAndImporter.Excel;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Omu.ValueInjecter;
|
|
|
|
using System.Data;
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
using TaskManager.Contracts.Dtos;
|
|
|
|
using TaskManager.Controllers;
|
|
|
|
using TaskManager.Entity;
|
|
|
|
using TaskManager.EntityFramework;
|
|
|
|
|
|
|
|
using TaskManager.EntityFramework.Repository;
|
|
|
|
|
|
|
|
namespace TaskManager.Controllers
|
|
|
|
{
|
|
|
|
//[ApiController]
|
|
|
|
//[Route("api/[controller]")]
|
|
|
|
public class CheryRecurringJobOutPageController<T, ToutputDetial> : RecurringJobBaseController where T : CherryReadBaseEntity, new() where ToutputDetial : CherryReadBaseEntityDto
|
|
|
|
{
|
|
|
|
protected readonly IRepository<T> _repository;
|
|
|
|
public CheryRecurringJobOutPageController(HttpClient httpClient, JobDbContext jobDbContext, LogController log,IRepository<T> repository) : base(httpClient, jobDbContext, log)
|
|
|
|
{
|
|
|
|
_repository = repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[NonAction]
|
|
|
|
public async Task<List<ToutputDetial>> FetchAllDataAsync(string inputdate)
|
|
|
|
{
|
|
|
|
var allData = new List<ToutputDetial>();
|
|
|
|
int totalItems = 0;
|
|
|
|
int pageSize = 0;
|
|
|
|
int currentPage = 1;
|
|
|
|
string date = !string.IsNullOrEmpty(inputdate) ? inputdate : DateTime.Now.ToString("yyyy-MM-dd");
|
|
|
|
|
|
|
|
var readedcount = _jobDbContext.Set<T>().Where(p => p.RequestDate == inputdate).Count();
|
|
|
|
if (readedcount == 0)//第一次请求用false,接口人胡启名要求
|
|
|
|
{
|
|
|
|
PagedResponse<ToutputDetial> firstResponse = await GetPageAsync(new PAGE_DTO() { Date = date, IsForce = false });
|
|
|
|
if (firstResponse == null || firstResponse.Code != 200)
|
|
|
|
{
|
|
|
|
await _logger.AddError("首次请求失败,无法获取分页信息。", TaskName);
|
|
|
|
return allData;
|
|
|
|
}
|
|
|
|
if (readedcount != int.Parse(firstResponse.Data.Total))//记录数不相等
|
|
|
|
{
|
|
|
|
var ids = _jobDbContext.Set<T>().Where(p => p.RequestDate == inputdate).Select(p => p.Id).ToList();//已经同步的ID
|
|
|
|
totalItems = int.Parse(firstResponse.Data.Total);
|
|
|
|
pageSize = int.Parse(firstResponse.Data.PageSize);
|
|
|
|
List<T> pagefirstList = new List<T>();
|
|
|
|
if (readedcount > 0)
|
|
|
|
{
|
|
|
|
var listrows = firstResponse.Data.Rows.Where(p => !ids.Contains(p.Id));
|
|
|
|
foreach (var itm in firstResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pagefirstList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (var itm in firstResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pagefirstList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_jobDbContext.BulkInsert(pagefirstList);
|
|
|
|
//Console.WriteLine($"总记录数: {totalItems}, 每页大小: {pageSize}");
|
|
|
|
// 计算总页数
|
|
|
|
int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
|
|
|
|
//Console.WriteLine($"总共需要请求 {totalPages} 页数据");
|
|
|
|
// 循环请求剩余页面
|
|
|
|
for (currentPage = 2; currentPage <= totalPages; currentPage++)
|
|
|
|
{
|
|
|
|
PAGE_DTO pageinput = new PAGE_DTO() { Date = date, PageNum = currentPage, IsForce =false };
|
|
|
|
Console.WriteLine($"正在请求第 {currentPage} 页...");
|
|
|
|
PagedResponse<ToutputDetial> pageResponse = await GetPageAsync(pageinput);
|
|
|
|
if (pageResponse?.Data.Rows != null && pageResponse.Data.Rows.Count > 0)
|
|
|
|
{
|
|
|
|
List<T> pageList = new List<T>();
|
|
|
|
if (readedcount > 0)
|
|
|
|
{
|
|
|
|
var listrows = pageResponse.Data.Rows.Where(p => !ids.Contains(p.Id));
|
|
|
|
foreach (var itm in pageResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pageList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (var itm in pageResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pageList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_jobDbContext.BulkInsert(pageList);
|
|
|
|
await _logger.AddInfo($"成功获取 {pageResponse.Data.Rows.Count} 条记录", TaskName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await _logger.AddError($"第 {currentPage} 页未返回数据", TaskName);
|
|
|
|
}
|
|
|
|
// 简单的请求间隔,避免过于频繁
|
|
|
|
await Task.Delay(200);
|
|
|
|
}
|
|
|
|
await _logger.AddInfo($"所有数据获取完成,总共获取了 {allData.Count} 条记录", TaskName);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
PagedResponse<ToutputDetial> firstResponse = await GetPageAsync(new PAGE_DTO() { Date = date, IsForce = true });
|
|
|
|
if (firstResponse == null || firstResponse.Code != 200)
|
|
|
|
{
|
|
|
|
await _logger.AddInfo("首次请求失败,无法获取分页信息。", TaskName);
|
|
|
|
return allData;
|
|
|
|
}
|
|
|
|
if (firstResponse.Data.Total == "0")
|
|
|
|
{
|
|
|
|
await _logger.AddError("首次请求失败,Total为0是否已经全部读取过。", TaskName);
|
|
|
|
return allData;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (readedcount != int.Parse(firstResponse.Data.Total))//记录数不相等
|
|
|
|
{
|
|
|
|
var ids = _jobDbContext.Set<T>().Where(p => p.RequestDate == inputdate).Select(p => p.Id).ToList();//已经同步的ID
|
|
|
|
totalItems = int.Parse(firstResponse.Data.Total);
|
|
|
|
pageSize = int.Parse(firstResponse.Data.PageSize);
|
|
|
|
List<T> pagefirstList = new List<T>();
|
|
|
|
if (readedcount > 0)
|
|
|
|
{
|
|
|
|
var listrows = firstResponse.Data.Rows.Where(p => !ids.Contains(p.Id));
|
|
|
|
foreach (var itm in firstResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pagefirstList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (var itm in firstResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pagefirstList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_jobDbContext.BulkInsert(pagefirstList);
|
|
|
|
//Console.WriteLine($"总记录数: {totalItems}, 每页大小: {pageSize}");
|
|
|
|
// 计算总页数
|
|
|
|
int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
|
|
|
|
//Console.WriteLine($"总共需要请求 {totalPages} 页数据");
|
|
|
|
|
|
|
|
// 循环请求剩余页面
|
|
|
|
for (currentPage = 2; currentPage <= totalPages; currentPage++)
|
|
|
|
{
|
|
|
|
PAGE_DTO pageinput = new PAGE_DTO() { Date = date, PageNum = currentPage, IsForce = true };
|
|
|
|
Console.WriteLine($"正在请求第 {currentPage} 页...");
|
|
|
|
PagedResponse<ToutputDetial> pageResponse = await GetPageAsync(pageinput);
|
|
|
|
if (pageResponse?.Data.Rows != null && pageResponse.Data.Rows.Count > 0)
|
|
|
|
{
|
|
|
|
List<T> pageList = new List<T>();
|
|
|
|
if (readedcount > 0)
|
|
|
|
{
|
|
|
|
var listrows = pageResponse.Data.Rows.Where(p => !ids.Contains(p.Id));
|
|
|
|
foreach (var itm in pageResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pageList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (var itm in pageResponse.Data.Rows)
|
|
|
|
{
|
|
|
|
T entity = new T();
|
|
|
|
entity.InjectFrom(itm);
|
|
|
|
entity.CreationTime = DateTime.Now;
|
|
|
|
pageList.Add(entity);
|
|
|
|
allData.Add(itm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_jobDbContext.BulkInsert(pageList);
|
|
|
|
await _logger.AddInfo($"成功获取 {pageResponse.Data.Rows.Count} 条记录", TaskName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await _logger.AddInfo($"第 {currentPage} 页未返回数据", TaskName);
|
|
|
|
}
|
|
|
|
// 简单的请求间隔,避免过于频繁
|
|
|
|
await Task.Delay(200);
|
|
|
|
}
|
|
|
|
await _logger.AddInfo($"所有数据获取完成,总共获取了 {allData.Count} 条记录", TaskName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 首次请求获取总条数和分页信息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return allData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<PagedResponse<ToutputDetial>> GetPageAsync(PAGE_DTO input)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var inputjson = JsonSerializer.Serialize(input,
|
|
|
|
new JsonSerializerOptions
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
WriteIndented = true // 可选,用于格式化输出
|
|
|
|
}
|
|
|
|
);
|
|
|
|
inputjson = RemoveWhitespace(inputjson);
|
|
|
|
var content = await Post(Url, Path, inputjson);
|
|
|
|
if (!string.IsNullOrEmpty(content))
|
|
|
|
{
|
|
|
|
|
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
Converters = {
|
|
|
|
new JsonStringEnumConverter(), // 枚举转换
|
|
|
|
new CustomDateTimeConverter("yyyy-MM-dd HH:mm:ss") // 日期转换
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return JsonSerializer.Deserialize<PagedResponse<ToutputDetial>>(content, options);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await _logger.AddError($"调用接口无返回值{Url}", TaskName);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await _logger.AddError($"调用接口无返回值错误{ex.Message}", TaskName);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
[NonAction]
|
|
|
|
public virtual async Task TestAsync(string url, string path, string taskName, string inputdate)
|
|
|
|
{
|
|
|
|
Url = url;
|
|
|
|
Path = path;
|
|
|
|
TaskName = taskName;
|
|
|
|
await FetchAllDataAsync(inputdate);
|
|
|
|
}
|
|
|
|
protected override async Task DoExecutingAsync(string url, string path, string takName,string client)
|
|
|
|
{
|
|
|
|
Url = url;
|
|
|
|
Path = path;
|
|
|
|
TaskName = takName;
|
|
|
|
Client = client;
|
|
|
|
await FetchAllDataAsync(string.Empty);
|
|
|
|
|
|
|
|
}
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<ActionResult<IEnumerable<T>>> GetAll()
|
|
|
|
{
|
|
|
|
return await _repository.GetAllAsync() as List<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
public async Task<ActionResult<T>> GetById(int id)
|
|
|
|
{
|
|
|
|
var entity = await _repository.GetByIdAsync(id);
|
|
|
|
if (entity == null) return NotFound();
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public async Task<ActionResult<T>> Create(T entity)
|
|
|
|
{
|
|
|
|
entity.CreationTime=DateTime.Now;
|
|
|
|
var createdEntity = await _repository.AddAsync(entity);
|
|
|
|
return CreatedAtAction(nameof(GetById), new { id = createdEntity.Id }, createdEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
public async Task<IActionResult> Update(T entity)
|
|
|
|
{
|
|
|
|
var _first=await _repository.GetByIdAsync(entity.UId);
|
|
|
|
if (_first == null)
|
|
|
|
{
|
|
|
|
return new JsonResult(new { Code = 400, Message = "修改失败!" });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _repository.UpdateAsync(entity);
|
|
|
|
return new JsonResult(new { Code = 200, Message = "修改成功!" });
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
public async Task<IActionResult> Delete(int id)
|
|
|
|
{
|
|
|
|
await _repository.DeleteAsync(id);
|
|
|
|
return new JsonResult(new { Code = 200, Message = "删除成功!" }); ;
|
|
|
|
}
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<ActionResult<PagedResult<T>>> GetPaged(
|
|
|
|
[FromQuery] int pageNumber = 1,
|
|
|
|
[FromQuery] int pageSize = 10,
|
|
|
|
[FromQuery] string sortBy = "",
|
|
|
|
[FromQuery] bool isAscending = true,
|
|
|
|
[FromQuery] Dictionary<string, string> filters = null)
|
|
|
|
{
|
|
|
|
var pagingParams = new PagingParams
|
|
|
|
{
|
|
|
|
PageNumber = pageNumber,
|
|
|
|
PageSize = pageSize,
|
|
|
|
SortBy = sortBy,
|
|
|
|
IsAscending = isAscending,
|
|
|
|
Filters = filters
|
|
|
|
};
|
|
|
|
|
|
|
|
// 可以在这里构建表达式树过滤条件
|
|
|
|
Expression<Func<T, bool>> filter = null;
|
|
|
|
|
|
|
|
var pagedResult = await _repository.GetPagedAsync(filter, pagingParams);
|
|
|
|
return Ok(pagedResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<FileStreamResult> Export([FromQuery] int pageNumber = 1,
|
|
|
|
[FromQuery] int pageSize = 10,
|
|
|
|
[FromQuery] string sortBy = "",
|
|
|
|
[FromQuery] bool isAscending = true,
|
|
|
|
[FromQuery] Dictionary<string, string> filters = null)
|
|
|
|
{
|
|
|
|
var pagingParams = new PagingParams
|
|
|
|
{
|
|
|
|
PageNumber = pageNumber,
|
|
|
|
PageSize = pageSize,
|
|
|
|
SortBy = sortBy,
|
|
|
|
IsAscending = isAscending,
|
|
|
|
Filters = filters
|
|
|
|
};
|
|
|
|
|
|
|
|
// 可以在这里构建表达式树过滤条件
|
|
|
|
//Expression<Func<T, bool>> filter = null;
|
|
|
|
|
|
|
|
var pagedResult = await _repository.GetPagedAsync(null, pagingParams);
|
|
|
|
return await ExportFile(pagedResult.Data, Guid.NewGuid().ToString() + ".xlsx");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
protected async Task<FileStreamResult> ExportFile(ICollection<T> dtos, string fileName)
|
|
|
|
{
|
|
|
|
var excelExporter = HttpContext.RequestServices.GetRequiredService<IExcelExporter>();
|
|
|
|
var res = await excelExporter.ExportAsByteArray(dtos);
|
|
|
|
return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_" + fileName };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
|
|
public async Task<IActionResult> GetImportTemplate()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 创建导入模板生成器
|
|
|
|
var importer = new ExcelImporter();
|
|
|
|
|
|
|
|
// 生成导入模板流(这里假设使用 YourModel 作为导入模型)
|
|
|
|
var bytes = await importer.GenerateTemplateBytes<T>();
|
|
|
|
|
|
|
|
|
|
|
|
using var stream = new MemoryStream(bytes);
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
// 设置友好的文件名,例如:"导入模板_20250530.xlsx"
|
|
|
|
var fileName = $"导入模板_{DateTime.Now:yyyyMMdd}.xlsx";
|
|
|
|
|
|
|
|
// 返回文件流结果
|
|
|
|
return File(
|
|
|
|
fileStream: stream,
|
|
|
|
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
|
|
fileDownloadName: fileName);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
// 记录异常日志
|
|
|
|
Console.WriteLine($"生成导入模板时出错: {ex.Message}");
|
|
|
|
|
|
|
|
// 返回错误响应
|
|
|
|
return StatusCode(500, "生成导入模板时发生错误");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public class CustomDateTimeConverter : JsonConverter<DateTime>
|
|
|
|
{
|
|
|
|
private readonly string _format;
|
|
|
|
|
|
|
|
public CustomDateTimeConverter(string format)
|
|
|
|
{
|
|
|
|
_format = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
{
|
|
|
|
return DateTime.ParseExact(reader.GetString(), _format, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
|
|
{
|
|
|
|
writer.WriteStringValue(value.ToString(_format));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|