|
|
@ -1,8 +1,4 @@ |
|
|
|
using System.Data; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.Json.Serialization; |
|
|
|
using Magicodes.ExporterAndImporter.Core.Extension; |
|
|
|
using Magicodes.ExporterAndImporter.Core.Extension; |
|
|
|
using Magicodes.ExporterAndImporter.Excel; |
|
|
|
using Microsoft.AspNetCore.Builder; |
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
@ -11,11 +7,17 @@ using Microsoft.AspNetCore.Http.HttpResults; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore.Storage; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Omu.ValueInjecter; |
|
|
|
using System.Data; |
|
|
|
using System.Data.Common; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.Json.Serialization; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using TaskManager.Contracts.Dtos; |
|
|
|
using TaskManager.Controllers; |
|
|
|
|
|
|
|
using TaskManager.Entity; |
|
|
|
using TaskManager.EntityFramework; |
|
|
|
using TaskManager.EntityFramework.Repository; |
|
|
@ -31,10 +33,34 @@ namespace TaskManager.Controllers |
|
|
|
{ |
|
|
|
_repository = repository; |
|
|
|
} |
|
|
|
protected virtual async Task CreateTaskAsync(List<T> plist,JobDbContext dbContext) |
|
|
|
protected virtual async Task ConfirmDataInsertAsync(List<T> plist,JobDbContext dbContext, DbTransaction dbTransaction) |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
private async Task InsertDataAsync(List<T> list) |
|
|
|
{ |
|
|
|
if (list.Any()) |
|
|
|
{ |
|
|
|
using (var transaction = await _jobDbContext.Database.BeginTransactionAsync()) |
|
|
|
{ |
|
|
|
var tran = transaction.GetDbTransaction(); |
|
|
|
try |
|
|
|
{ |
|
|
|
_jobDbContext.BulkInsert(list); |
|
|
|
await ConfirmDataInsertAsync(list, _jobDbContext, tran); |
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
await transaction.CommitAsync(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
await transaction.RollbackAsync(); |
|
|
|
new JsonResult(new { Code = 400, Message = ex.Message }); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
[NonAction] |
|
|
|
public async Task<List<ToutputDetial>> FetchAllDataAsync(string inputdate) |
|
|
@ -57,11 +83,11 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
if (firstResponse.Data.Total == "0") |
|
|
|
{ |
|
|
|
await _logger.AddError("首次请求失败,Total为0是否已经全部读取过。", TaskName); |
|
|
|
await _logger.AddInfo("首次请求失败,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
|
|
|
@ -93,8 +119,10 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_jobDbContext.BulkInsert(pagefirstList); |
|
|
|
await CreateTaskAsync(pagefirstList,_jobDbContext); |
|
|
|
if (pagefirstList.Any()) |
|
|
|
{ |
|
|
|
await InsertDataAsync(pagefirstList); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 计算总页数
|
|
|
@ -110,7 +138,6 @@ namespace TaskManager.Controllers |
|
|
|
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)); |
|
|
@ -121,12 +148,10 @@ namespace TaskManager.Controllers |
|
|
|
entity.CreationTime = DateTime.Now; |
|
|
|
pageList.Add(entity); |
|
|
|
allData.Add(itm); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var itm in pageResponse.Data.Rows) |
|
|
|
{ |
|
|
|
T entity = new T(); |
|
|
@ -136,25 +161,22 @@ namespace TaskManager.Controllers |
|
|
|
allData.Add(itm); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_jobDbContext.BulkInsert(pageList); |
|
|
|
await CreateTaskAsync(pageList, _jobDbContext); |
|
|
|
await _logger.AddInfo($"成功获取 {pageResponse.Data.Rows.Count} 条记录", TaskName); |
|
|
|
if (pageList.Any()) |
|
|
|
{ |
|
|
|
await InsertDataAsync(pageList); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
await _logger.AddError($"第 {currentPage} 页未返回数据", TaskName); |
|
|
|
await _logger.AddInfo($"第 {currentPage} 页未返回数据", TaskName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 简单的请求间隔,避免过于频繁
|
|
|
|
await Task.Delay(200); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await _logger.AddInfo($"所有数据获取完成,总共获取了 {allData.Count} 条记录", TaskName); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@ -191,7 +213,6 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var itm in firstResponse.Data.Rows) |
|
|
|
{ |
|
|
|
T entity = new T(); |
|
|
@ -201,8 +222,10 @@ namespace TaskManager.Controllers |
|
|
|
allData.Add(itm); |
|
|
|
} |
|
|
|
} |
|
|
|
_jobDbContext.BulkInsert(pagefirstList); |
|
|
|
await CreateTaskAsync(pagefirstList, _jobDbContext); |
|
|
|
if (pagefirstList.Any()) |
|
|
|
{ |
|
|
|
await InsertDataAsync(pagefirstList); |
|
|
|
} |
|
|
|
//Console.WriteLine($"总记录数: {totalItems}, 每页大小: {pageSize}");
|
|
|
|
// 计算总页数
|
|
|
|
int totalPages = (int)Math.Ceiling((double)totalItems / pageSize); |
|
|
@ -231,7 +254,6 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var itm in pageResponse.Data.Rows) |
|
|
|
{ |
|
|
|
T entity = new T(); |
|
|
@ -241,9 +263,12 @@ namespace TaskManager.Controllers |
|
|
|
allData.Add(itm); |
|
|
|
} |
|
|
|
} |
|
|
|
_jobDbContext.BulkInsert(pageList); |
|
|
|
await CreateTaskAsync(pageList, _jobDbContext); |
|
|
|
await _logger.AddInfo($"成功获取 {pageResponse.Data.Rows.Count} 条记录", TaskName); |
|
|
|
if (pageList.Any()) |
|
|
|
{ |
|
|
|
await InsertDataAsync(pageList); |
|
|
|
} |
|
|
|
|
|
|
|
//await _logger.AddInfo($"成功获取 {pageResponse.Data.Rows.Count} 条记录", TaskName);
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@ -256,11 +281,8 @@ namespace TaskManager.Controllers |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 首次请求获取总条数和分页信息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return allData; |
|
|
|
} |
|
|
|
|
|
|
|