diff --git a/API/TaskManager.Entity/Entity.cs b/API/TaskManager.Entity/Entity.cs index 186038f..fe931b7 100644 --- a/API/TaskManager.Entity/Entity.cs +++ b/API/TaskManager.Entity/Entity.cs @@ -1,6 +1,4 @@ -using Magicodes.ExporterAndImporter.Core; -using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -8,6 +6,9 @@ using System.Linq; using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; +using System.Xml.Linq; +using Magicodes.ExporterAndImporter.Core; +using Newtonsoft.Json; namespace TaskManager.Entity { @@ -19,30 +20,31 @@ namespace TaskManager.Entity } public class TaskConifgure:BaseEntity { - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - [ExporterHeader(IsIgnore = true)] - public long UId { get; set; } + /// /// API路径 /// [ExporterHeader(DisplayName = "API路径")] + [ImporterHeader(Name = "API路径")] public string? Api { get; set; } /// /// 任务周期设置 /// [ExporterHeader(DisplayName = "任务周期设置")] + [ImporterHeader(Name = "任务周期设置")] public string? Corn { get; set; } /// /// 模块 /// [ExporterHeader(IsIgnore = true)] + [ImporterHeader(IsIgnore = true)] public string? Module { get; set; } /// /// 模块 /// - [ExporterHeader(IsIgnore = true)] + [ExporterHeader(DisplayName = "客户")] + [ImporterHeader(Name = "客户")] public string? Client { get; set; } @@ -50,11 +52,14 @@ namespace TaskManager.Entity /// 备注 /// [ExporterHeader(DisplayName = "备注")] + [ImporterHeader(Name = "备注")] public string? Remark { get; set; } /// /// 表名 /// - [ExporterHeader(IsIgnore =true)] + [ExporterHeader(DisplayName = "表名")] + [ImporterHeader(Name = "表名")] + public string? TableName { get; set; } /// /// 任务名称 @@ -64,12 +69,14 @@ namespace TaskManager.Entity /// /// 全路径 /// - [ExporterHeader(DisplayName = "全路径")] + [ExporterHeader(DisplayName = "调用全路径")] + [ImporterHeader(Name = "调用全路径")] public string? Url { get; set; } /// /// 是否自动执行 /// [ExporterHeader(DisplayName = "是否自动执行")] + [ImporterHeader(Name = "是否自动执行")] public bool IsAuto { get; set; } public TaskConifgure() { } @@ -77,39 +84,42 @@ namespace TaskManager.Entity } - public class TEA_SUBSCRIBE + public class TaskAllocation:BaseEntity { - [Key] - [Required] - public Guid GUID { get; set; } + /// + /// 订阅任务名 + /// [MaxLength(50)] - public string TableName { get; set; } + [ExporterHeader(DisplayName = "订阅任务名")] + [ImporterHeader(Name = "订阅任务名")] + public string TaskName { get; set; } = ""; + [ExporterHeader(DisplayName = "订阅表名")] + [ImporterHeader(Name = "订阅表名")] - [MaxLength(50)] - public string Creator { get; set; } + /// + /// 订阅表名 + /// [MaxLength(50)] - public string Subscriber { get; set; } - - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int UID { get; set; } - + public string TableName { get; set; } + /// + /// 创建系统 + /// + [ExporterHeader(DisplayName = "创建系统")] + [ImporterHeader(Name = "创建系统")] [MaxLength(50)] - public string CreateUser { get; set; } - - [Required] - public DateTime CreateTime { get; set; } - - public string Remark { get; set; } + public string Creator { get; set; } + /// + /// 订阅客户 + /// + [ExporterHeader(DisplayName = "订阅客户")] + [ImporterHeader(Name = "订阅客户")] [MaxLength(50)] - public string UpdateUser { get; set; } - - public DateTime? UpdateTime { get; set; } + public string Subscriber { get; set; } - [Required] - public bool Enable { get; set; } + } @@ -162,18 +172,25 @@ namespace TaskManager.Entity [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [ExporterHeader(IsIgnore=true)] + [ImporterHeader(IsIgnore = true)] public long UId { get; set; } /// /// 写状态 /// + [ExporterHeader(IsIgnore = true)] + [ImporterHeader(IsIgnore = true)] public bool WriteState { get; set; } [ExporterHeader(IsIgnore = true)] + [ImporterHeader(IsIgnore = true)] public bool ReadState { get; set; } [ExporterHeader(IsIgnore = true)] + [ImporterHeader(IsIgnore = true)] public DateTime CreationTime { get; set; } [ExporterHeader(IsIgnore = true)] + [ImporterHeader(IsIgnore = true)] public string? Remark { get; set; } [ExporterHeader(IsIgnore = true)] + [ImporterHeader(IsIgnore = true)] public Guid TaskId { get; set; } diff --git a/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs b/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs index e106cb0..cabf5ca 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs @@ -1,16 +1,17 @@ -using Azure.Core; +using System.Data; +using System.Drawing.Printing; +using System.Linq.Expressions; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using Azure.Core; using Dapper; using Magicodes.ExporterAndImporter.Core.Extension; using Magicodes.ExporterAndImporter.Excel; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using System.Data; -using System.Drawing.Printing; -using System.Linq.Expressions; -using System.Text; -using System.Text.Json; -using System.Text.Json.Serialization; using TaskManager.Contracts.Dtos; using TaskManager.Entity; using TaskManager.EntityFramework; @@ -312,6 +313,65 @@ namespace TaskManager.Controllers return StatusCode(500, "生成导入模板时发生错误"); } } + [HttpPost("Import")] + public async virtual Task Import(IFormFile file) + { + if (file == null || file.Length <= 0) + { + return BadRequest("No file uploaded."); + } + + try + { + var excelImporter = HttpContext.RequestServices.GetRequiredService(); + var importResult = await excelImporter.Import(file.OpenReadStream()); + //if (importResult.HasError) + //{ + // return BadRequest(importResult.ErrorMessage); + //} + + // 处理导入的数据 + List list = new List(); + 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 p_list) + { + + } + + /// + /// 重写插入后进行操作如 + /// + /// + /// + protected virtual async Task ImportAfter(List p_list) + { + + } diff --git a/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs b/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs index 2e5509b..719c98d 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs @@ -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,12 +26,12 @@ namespace TaskManager.Controllers public class CheryRecurringJobOutPageController : RecurringJobBaseController where T : CherryReadBaseEntity, new() where ToutputDetial : CherryReadBaseEntityDto { protected readonly IRepository _repository; - public CheryRecurringJobOutPageController(HttpClient httpClient, JobDbContext jobDbContext, LogController log,IRepository repository) : base(httpClient, jobDbContext, log) + public CheryRecurringJobOutPageController(HttpClient httpClient, JobDbContext jobDbContext, LogController log, IRepository repository) : base(httpClient, jobDbContext, log) { _repository = repository; } - + [NonAction] public async Task> FetchAllDataAsync(string inputdate) @@ -42,12 +45,23 @@ namespace TaskManager.Controllers var readedcount = _jobDbContext.Set().Where(p => p.RequestDate == inputdate).Count(); if (readedcount == 0)//第一次请求用false,接口人胡启名要求 { - PagedResponse firstResponse = await GetPageAsync(new PAGE_DTO() { Date = date, IsForce = false }); + PagedResponse 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().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 pageResponse = await GetPageAsync(pageinput); if (pageResponse?.Data.Rows != null && pageResponse.Data.Rows.Count > 0) { List pageList = new List(); + 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); } @@ -227,8 +256,8 @@ namespace TaskManager.Controllers } - // 首次请求获取总条数和分页信息 - + // 首次请求获取总条数和分页信息 + return allData; @@ -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); } + /// + /// 获取所有记录 + /// + /// [HttpGet] public async Task>> GetAll() { return await _repository.GetAllAsync() as List; } - + /// + /// 获取实体通过ID + /// + /// + /// [HttpGet("{id}")] public async Task> GetById(int id) { @@ -307,24 +344,33 @@ namespace TaskManager.Controllers [HttpPost] public async Task> 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); } - + /// + /// 修改 + /// + /// + /// [HttpPut("{id}")] public async Task 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 = "修改失败!" }); } - + await _repository.UpdateAsync(entity); - return new JsonResult(new { Code = 200, Message = "修改成功!" }); + return new JsonResult(new { Code = 200, Message = "修改成功!" }); } + /// + /// 删除 + /// + /// + /// [HttpDelete("{id}")] public async Task Delete(int id) @@ -332,6 +378,15 @@ namespace TaskManager.Controllers await _repository.DeleteAsync(id); return new JsonResult(new { Code = 200, Message = "删除成功!" }); ; } + /// + /// 分页 + /// + /// 第几页 + /// 每页条数 + /// 排序字段 + /// 正序还是倒序 + /// 过滤条件 + /// [HttpGet] public async Task>> GetPaged( [FromQuery] int pageNumber = 1, @@ -356,7 +411,15 @@ namespace TaskManager.Controllers return Ok(pagedResult); } - + /// + /// 导出 + /// + /// + /// 每页条数 + /// 排序字段 + /// 正序还是倒序 + /// 过滤条件 + /// [HttpGet] public async Task Export([FromQuery] int pageNumber = 1, @@ -390,7 +453,10 @@ namespace TaskManager.Controllers } - + /// + /// 获取导入模板 + /// + /// [HttpGet] @@ -431,9 +497,62 @@ namespace TaskManager.Controllers return StatusCode(500, "生成导入模板时发生错误"); } } + /// + /// 导入 + /// + /// 选择文件 + /// + [HttpPost("Import")] + public async virtual Task Import(IFormFile file) + { + if (file == null || file.Length <= 0) + { + return BadRequest("No file uploaded."); + } + + try + { + var excelImporter = HttpContext.RequestServices.GetRequiredService(); + var importResult = await excelImporter.Import(file.OpenReadStream()); + //if (importResult.HasError) + //{ + // return BadRequest(importResult.ErrorMessage); + //} + + // 处理导入的数据 + List list = new List(); + 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 p_list) + { + } + + + protected virtual async Task ImportAfter(List p_list) + { + + } @@ -457,4 +576,8 @@ namespace TaskManager.Controllers writer.WriteStringValue(value.ToString(_format)); } } + + + + } diff --git a/API/Wood.Service/Controllers/NormalBaseController.cs b/API/Wood.Service/Controllers/NormalBaseController.cs index c8bd625..a7b514f 100644 --- a/API/Wood.Service/Controllers/NormalBaseController.cs +++ b/API/Wood.Service/Controllers/NormalBaseController.cs @@ -1,10 +1,4 @@ -using Magicodes.ExporterAndImporter.Core; -using Magicodes.ExporterAndImporter.Excel; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using System; +using System; using System.Collections.Generic; using System.Data; using System.Linq; @@ -12,11 +6,18 @@ using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; +using Magicodes.ExporterAndImporter.Core; +using Magicodes.ExporterAndImporter.Core.Extension; +using Magicodes.ExporterAndImporter.Excel; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using TaskManager.Entity; using TaskManager.EntityFramework; using TaskManager.EntityFramework.Repository; using Wood.Util; -using Magicodes.ExporterAndImporter.Core.Extension; namespace Wood.Service.Controllers @@ -29,8 +30,9 @@ namespace Wood.Service.Controllers protected readonly IServiceProvider _builder; protected readonly IConfiguration _configuration; protected readonly IRepository _repository; - + + public NormalBaseController(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) { _builder = builder; @@ -94,8 +96,19 @@ namespace Wood.Service.Controllers await _repository.DeleteAsync(id); return new JsonResult(new { Code = 200, Message = "删除成功!" }); ; } + + + /// + /// 分页 + /// + /// + /// + /// + /// + /// + /// [HttpGet] public async Task>> GetPaged( [FromQuery] int pageNumber = 1, @@ -120,7 +133,15 @@ namespace Wood.Service.Controllers return Ok(pagedResult); } - + /// + /// 导出 + /// + /// + /// + /// + /// + /// + /// [HttpGet] public async Task Export([FromQuery] int pageNumber = 1, @@ -157,7 +178,10 @@ namespace Wood.Service.Controllers } - + /// + /// 导入模板 + /// + /// [HttpGet] @@ -198,6 +222,62 @@ namespace Wood.Service.Controllers return StatusCode(500, "生成导入模板时发生错误"); } } + /// + /// 导入 + /// + /// + /// + [HttpPost("Import")] + public async virtual Task Import(IFormFile file) + { + if (file == null || file.Length <= 0) + { + return BadRequest("No file uploaded."); + } + + try + { + var excelImporter = HttpContext.RequestServices.GetRequiredService(); + var importResult = await excelImporter.Import(file.OpenReadStream()); + //if (importResult.HasError) + //{ + // return BadRequest(importResult.ErrorMessage); + //} + + // 处理导入的数据 + List list = new List(); + foreach (var item in importResult.Data) + { + list.Add(item); + + } + await ImportBefore(list); + + await _context.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 p_list) + { + + } + + + protected virtual async Task ImportAfter(List p_list) + { + + } diff --git a/API/Wood.Service/Controllers/TaskAllocationService.cs b/API/Wood.Service/Controllers/TaskAllocationService.cs new file mode 100644 index 0000000..b729112 --- /dev/null +++ b/API/Wood.Service/Controllers/TaskAllocationService.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using TaskManager.Entity; +using TaskManager.EntityFramework; + +namespace Wood.Service.Controllers +{ + public class TaskAllocationService : NormalBaseController + { + public TaskAllocationService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) : base(context, builder, configuration, repository) + { + } + } +} diff --git a/API/Wood.Service/Controllers/TaskSubService.cs b/API/Wood.Service/Controllers/TaskSubService.cs new file mode 100644 index 0000000..46d914c --- /dev/null +++ b/API/Wood.Service/Controllers/TaskSubService.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using TaskManager.Entity; +using TaskManager.EntityFramework; + +namespace Wood.Service.Controllers +{ + public class TaskSubService : NormalBaseController + { + public TaskSubService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) : base(context, builder, configuration, repository) + { + } + } +} diff --git a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs index fc4414e..12c6c1b 100644 --- a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs +++ b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs @@ -27,7 +27,7 @@ namespace Wood.Service.Datas [HttpPost] [Route("import")] - public async Task Import(IFormFile file) + public override async Task Import(IFormFile file) { if (file == null || file.Length == 0) { diff --git a/API/Wood.Service/Datas/SupplierInfoDtService.cs b/API/Wood.Service/Datas/SupplierInfoDtService.cs index c5a0944..7678632 100644 --- a/API/Wood.Service/Datas/SupplierInfoDtService.cs +++ b/API/Wood.Service/Datas/SupplierInfoDtService.cs @@ -27,7 +27,7 @@ namespace Wood.Service.Datas [HttpPost] [Route("import")] - public async Task Import(IFormFile file) + public override async Task Import(IFormFile file) { if (file == null || file.Length == 0) { diff --git a/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs b/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs index 76c8604..8d498cc 100644 --- a/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs +++ b/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs @@ -28,7 +28,7 @@ namespace Wood.Service.Datas [HttpPost] [Route("import")] - public async Task Import(IFormFile file) + public override async Task Import(IFormFile file) { if (file == null || file.Length == 0) { diff --git a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs index 0b93ffd..b30084d 100644 --- a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs +++ b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs @@ -28,7 +28,7 @@ namespace Wood.Service.Datas [HttpPost] [Route("import")] - public async Task Import(IFormFile file) + public override async Task Import(IFormFile file) { if (file == null || file.Length == 0) {