|
|
@ -1,19 +1,22 @@ |
|
|
|
using Magicodes.ExporterAndImporter.Core.Extension; |
|
|
|
using System.Data; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.Json.Serialization; |
|
|
|
using Magicodes.ExporterAndImporter.Core.Extension; |
|
|
|
using Magicodes.ExporterAndImporter.Excel; |
|
|
|
using Microsoft.AspNetCore.Builder; |
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
|
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 |
|
|
@ -23,7 +26,7 @@ namespace TaskManager.Controllers |
|
|
|
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) |
|
|
|
public CheryRecurringJobOutPageController(HttpClient httpClient, JobDbContext jobDbContext, LogController log, IRepository<T> repository) : base(httpClient, jobDbContext, log) |
|
|
|
{ |
|
|
|
_repository = repository; |
|
|
|
} |
|
|
@ -42,12 +45,23 @@ namespace TaskManager.Controllers |
|
|
|
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 }); |
|
|
|
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 (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
|
|
|
@ -68,6 +82,7 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var itm in firstResponse.Data.Rows) |
|
|
|
{ |
|
|
|
T entity = new T(); |
|
|
@ -77,20 +92,25 @@ namespace TaskManager.Controllers |
|
|
|
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 }; |
|
|
|
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)); |
|
|
@ -105,6 +125,7 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var itm in pageResponse.Data.Rows) |
|
|
|
{ |
|
|
|
T entity = new T(); |
|
|
@ -114,16 +135,24 @@ namespace TaskManager.Controllers |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
@ -281,7 +310,7 @@ namespace TaskManager.Controllers |
|
|
|
TaskName = taskName; |
|
|
|
await FetchAllDataAsync(inputdate); |
|
|
|
} |
|
|
|
protected override async Task DoExecutingAsync(string url, string path, string takName,string client) |
|
|
|
protected override async Task DoExecutingAsync(string url, string path, string takName, string client) |
|
|
|
{ |
|
|
|
Url = url; |
|
|
|
Path = path; |
|
|
@ -290,12 +319,20 @@ namespace TaskManager.Controllers |
|
|
|
await FetchAllDataAsync(string.Empty); |
|
|
|
|
|
|
|
} |
|
|
|
/// <summary>
|
|
|
|
/// 获取所有记录
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet] |
|
|
|
public async Task<ActionResult<IEnumerable<T>>> GetAll() |
|
|
|
{ |
|
|
|
return await _repository.GetAllAsync() as List<T>; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取实体通过ID
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet("{id}")] |
|
|
|
public async Task<ActionResult<T>> GetById(int id) |
|
|
|
{ |
|
|
@ -307,15 +344,19 @@ namespace TaskManager.Controllers |
|
|
|
[HttpPost] |
|
|
|
public async Task<ActionResult<T>> Create(T entity) |
|
|
|
{ |
|
|
|
entity.CreationTime=DateTime.Now; |
|
|
|
entity.CreationTime = DateTime.Now; |
|
|
|
var createdEntity = await _repository.AddAsync(entity); |
|
|
|
return CreatedAtAction(nameof(GetById), new { id = createdEntity.Id }, createdEntity); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 修改
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="entity"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPut("{id}")] |
|
|
|
public async Task<IActionResult> Update(T entity) |
|
|
|
{ |
|
|
|
var _first=await _repository.GetByIdAsync(entity.UId); |
|
|
|
var _first = await _repository.GetByIdAsync(entity.UId); |
|
|
|
if (_first == null) |
|
|
|
{ |
|
|
|
return new JsonResult(new { Code = 400, Message = "修改失败!" }); |
|
|
@ -325,6 +366,11 @@ namespace TaskManager.Controllers |
|
|
|
await _repository.UpdateAsync(entity); |
|
|
|
return new JsonResult(new { Code = 200, Message = "修改成功!" }); |
|
|
|
} |
|
|
|
/// <summary>
|
|
|
|
/// 删除
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
[HttpDelete("{id}")] |
|
|
|
public async Task<IActionResult> Delete(int id) |
|
|
@ -332,6 +378,15 @@ namespace TaskManager.Controllers |
|
|
|
await _repository.DeleteAsync(id); |
|
|
|
return new JsonResult(new { Code = 200, Message = "删除成功!" }); ; |
|
|
|
} |
|
|
|
/// <summary>
|
|
|
|
/// 分页
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pageNumber">第几页</param>
|
|
|
|
/// <param name="pageSize">每页条数</param>
|
|
|
|
/// <param name="sortBy">排序字段</param>
|
|
|
|
/// <param name="isAscending">正序还是倒序</param>
|
|
|
|
/// <param name="filters">过滤条件</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet] |
|
|
|
public async Task<ActionResult<PagedResult<T>>> GetPaged( |
|
|
|
[FromQuery] int pageNumber = 1, |
|
|
@ -356,7 +411,15 @@ namespace TaskManager.Controllers |
|
|
|
return Ok(pagedResult); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 导出
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pageNumber"></param>
|
|
|
|
/// <param name="pageSize">每页条数</param>
|
|
|
|
/// <param name="sortBy">排序字段</param>
|
|
|
|
/// <param name="isAscending">正序还是倒序</param>
|
|
|
|
/// <param name="filters">过滤条件</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
[HttpGet] |
|
|
|
public async Task<FileStreamResult> Export([FromQuery] int pageNumber = 1, |
|
|
@ -390,7 +453,10 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取导入模板
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
[HttpGet] |
|
|
|
|
|
|
@ -431,9 +497,62 @@ namespace TaskManager.Controllers |
|
|
|
return StatusCode(500, "生成导入模板时发生错误"); |
|
|
|
} |
|
|
|
} |
|
|
|
/// <summary>
|
|
|
|
/// 导入
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="file">选择文件</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("Import")] |
|
|
|
public async virtual Task<IActionResult> Import(IFormFile file) |
|
|
|
{ |
|
|
|
if (file == null || file.Length <= 0) |
|
|
|
{ |
|
|
|
return BadRequest("No file uploaded."); |
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var excelImporter = HttpContext.RequestServices.GetRequiredService<IExcelImporter>(); |
|
|
|
var importResult = await excelImporter.Import<T>(file.OpenReadStream()); |
|
|
|
//if (importResult.HasError)
|
|
|
|
//{
|
|
|
|
// return BadRequest(importResult.ErrorMessage);
|
|
|
|
//}
|
|
|
|
|
|
|
|
// 处理导入的数据
|
|
|
|
List<T> list = new List<T>(); |
|
|
|
foreach (var item in importResult.Data) |
|
|
|
{ |
|
|
|
list.Add(item); |
|
|
|
|
|
|
|
} |
|
|
|
await ImportBefore(list); |
|
|
|
|
|
|
|
await _jobDbContext.BulkInsertAsync(list); |
|
|
|
|
|
|
|
await ImportAfter(list); |
|
|
|
|
|
|
|
|
|
|
|
return new JsonResult(new { Code = 200, Message = "修改成功!" }); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
await _logger.AddError(ex.Message, TaskName); |
|
|
|
return new JsonResult(new { Code = 400, Message = "导入失败!" }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual async Task ImportBefore(List<T> p_list) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual async Task ImportAfter(List<T> p_list) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -457,4 +576,8 @@ namespace TaskManager.Controllers |
|
|
|
writer.WriteStringValue(value.ToString(_format)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|