diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index 356fd6a0..cef7ad07 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -114,6 +114,24 @@ + + + 下载模板 + + + + + + 导出数据 + + + + + + 导入数据 + + + Json To Sql 类比模式,通用模型 @@ -465,18 +483,6 @@ 用户管理 - - - 下载模板 - - - - - - 导出数据 - - - 动态条件分页查询 diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseCrudController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseCrudController.cs index 17f005ce..524c0d4d 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseCrudController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseCrudController.cs @@ -16,16 +16,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// [ApiController] - public class BaseCrudController : ControllerBase where T : class,new() + public class BaseCrudController : BaseExcelController where T : class,new() { - private readonly ILogger _logger; - private IBaseService _baseService; - private IRepository _repository; - public BaseCrudController(ILogger logger, IBaseService iBaseService) + protected readonly ILogger _logger; + protected IBaseService _baseService; + public BaseCrudController(ILogger logger, IBaseService iBaseService):base(iBaseService._repository) { _logger = logger; _baseService = iBaseService; - _repository = iBaseService._repository; } /// diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseExcelController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseExcelController.cs new file mode 100644 index 00000000..81bdee27 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseExcelController.cs @@ -0,0 +1,76 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Localization; +using Yi.Framework.Common.Const; +using Yi.Framework.Common.Enum; +using Yi.Framework.Common.Helper; +using Yi.Framework.Common.Models; +using Yi.Framework.Interface; +using Yi.Framework.Language; +using Yi.Framework.Model.Models; +using Yi.Framework.Model.Query; +using Yi.Framework.Repository; +using Yi.Framework.WebCore.AttributeExtend; + +namespace Yi.Framework.ApiMicroservice.Controllers +{ + [ApiController] + public class BaseExcelController : ControllerBase where T : class, new() + { + protected IRepository _repository; + public BaseExcelController(IRepository repository) + { + _repository = repository; + } + /// + /// 下载模板 + /// + /// + [HttpGet] + [AllowAnonymous] + public IActionResult Template() + { + List users = new(); + var fileName = nameof(T) + PathConst.DataTemplate; + var path = ExcelHelper.DownloadImportTemplate(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Excel.ToString())); + var file = System.IO.File.OpenRead(path); + return File(file, "text/plain", $"{DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileName }.xlsx"); + } + + + /// + /// 导出数据 + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task Export() + { + var users = await _repository.GetListAsync(); + var fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + nameof(T) + PathConst.DataExport; + var path = ExcelHelper.ExportExcel(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Temp.ToString())); + var file = System.IO.File.OpenRead(path); + return File(file, "text/plain", $"{ fileName }.xlsx"); + } + + + /// + /// 导入数据 + /// + /// + [HttpPost] + [AllowAnonymous] + public async Task Import(IFormFile formFile) + { + List datas = ExcelHelper.ImportData(formFile.OpenReadStream()); + + //全量删除在重新插入 + var res = await _repository.UseTranAsync(async () => + { + await _repository.DeleteAsync(u => true); + await _repository.InsertRangeAsync(datas); + }); + return Result.Success().SetStatus(res); + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleCrudController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleCrudController.cs index c59f9265..c3969036 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleCrudController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleCrudController.cs @@ -16,16 +16,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// [ApiController] - public class BaseSimpleCrudController : ControllerBase where T : class, new() + public class BaseSimpleCrudController : BaseExcelController where T : class, new() { - private readonly ILogger _logger; - private IBaseService _baseService; - private IRepository _repository; - public BaseSimpleCrudController(ILogger logger, IBaseService iBaseService) + protected readonly ILogger _logger; + protected IBaseService _baseService; + public BaseSimpleCrudController(ILogger logger, IBaseService iBaseService):base(iBaseService._repository) { _logger = logger; _baseService = iBaseService; - _repository = iBaseService._repository; } /// diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleRdController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleRdController.cs index fbd01fef..1f5be22b 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleRdController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseController/BaseSimpleRdController.cs @@ -16,16 +16,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// [ApiController] - public class BaseSimpleRdController : ControllerBase where T : class, new() + public class BaseSimpleRdController : BaseExcelController where T : class, new() { - private readonly ILogger _logger; - private IBaseService _baseService; - private IRepository _repository; - public BaseSimpleRdController(ILogger logger, IBaseService iBaseService) + protected readonly ILogger _logger; + protected IBaseService _baseService; + public BaseSimpleRdController(ILogger logger, IBaseService iBaseService):base(iBaseService._repository) { _logger = logger; _baseService = iBaseService; - _repository = iBaseService._repository; } /// diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs index abd465df..583e750d 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs @@ -37,58 +37,58 @@ namespace Yi.Framework.ApiMicroservice.Controllers //数据导入导出将会被写入到基类 - /// - /// 下载模板 - /// - /// - [HttpGet] - [AllowAnonymous] - public IActionResult Template() - { - List users = new(); - var fileName = nameof(UserEntity) + PathConst.DataTemplate; - var path = ExcelHelper.DownloadImportTemplate(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Excel.ToString())); - var file = System.IO.File.OpenRead(path); - return File(file, "text/plain", $"{DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileName }.xlsx"); - } + ///// + ///// 下载模板 + ///// + ///// + //[HttpGet] + //[AllowAnonymous] + //public IActionResult Template() + //{ + // List users = new(); + // var fileName = nameof(UserEntity) + PathConst.DataTemplate; + // var path = ExcelHelper.DownloadImportTemplate(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Excel.ToString())); + // var file = System.IO.File.OpenRead(path); + // return File(file, "text/plain", $"{DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileName }.xlsx"); + //} - /// - /// 导出数据 - /// - /// - [HttpGet] - [AllowAnonymous] - public async Task Export() - { - var users = await _iUserService._repository.GetListAsync(); - var fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + nameof(UserEntity) + PathConst.DataExport; - var path = ExcelHelper.ExportExcel(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Temp.ToString())); - var file = System.IO.File.OpenRead(path); - return File(file, "text/plain", $"{ fileName }.xlsx"); - } + ///// + ///// 导出数据 + ///// + ///// + //[HttpGet] + //[AllowAnonymous] + //public async Task Export() + //{ + // var users = await _iUserService._repository.GetListAsync(); + // var fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + nameof(UserEntity) + PathConst.DataExport; + // var path = ExcelHelper.ExportExcel(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Temp.ToString())); + // var file = System.IO.File.OpenRead(path); + // return File(file, "text/plain", $"{ fileName }.xlsx"); + //} - /// - /// 导入数据 - /// - /// - [HttpGet] - [AllowAnonymous] - public async Task Import([FromForm(Name = "file")] IFormFile formFile) - { - List users = ExcelHelper.ImportData(formFile.OpenReadStream()); + ///// + ///// 导入数据 + ///// + ///// + //[HttpPost] + //[AllowAnonymous] + //public async Task Import(IFormFile formFile) + //{ + // List users = ExcelHelper.ImportData(formFile.OpenReadStream()); - var _repository = _iUserService._repository; + // var _repository = _iUserService._repository; - //全量删除在重新插入 - var res = await _repository.UseTranAsync(async () => - { - await _repository.DeleteAsync(u => true); - await _repository.InsertRangeAsync(users); - }); - return Result.Success().SetStatus(res); - } + // //全量删除在重新插入 + // var res = await _repository.UseTranAsync(async () => + // { + // await _repository.DeleteAsync(u => true); + // await _repository.InsertRangeAsync(users); + // }); + // return Result.Success().SetStatus(res); + //} /// /// 动态条件分页查询 @@ -114,8 +114,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers [Permission("system:user:edit")] public async Task UpdateStatus(long userId, bool isDel) { - return Result.Success().SetData(await _iUserService._repository.UpdateIgnoreNullAsync(new UserEntity() { Id = userId, IsDeleted = isDel })); - + return Result.Success().SetData(await _repository.UpdateIgnoreNullAsync(new UserEntity() { Id = userId, IsDeleted = isDel })); } /// @@ -153,7 +152,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers [Permission("system:user:edit")] public async Task Update(UserInfoDto userDto) { - if (await _iUserService._repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName) && !userDto.User.Id.Equals(u.Id))) + if (await _repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName) && !userDto.User.Id.Equals(u.Id))) { return Result.Error("用户名已存在,修改失败!"); } @@ -182,7 +181,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers [Permission("system:user:add2")] public async Task Add(UserInfoDto userDto) { - if (await _iUserService._repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName))) + if (await _repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName))) { return Result.Error("用户已经存在,添加失败!"); } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281710231548UserEntity_DataExport.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281710231548UserEntity_DataExport.xlsx new file mode 100644 index 00000000..33fa2a86 Binary files /dev/null and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281710231548UserEntity_DataExport.xlsx differ diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281714190694UserEntity_DataExport.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281714190694UserEntity_DataExport.xlsx new file mode 100644 index 00000000..8867cbdf Binary files /dev/null and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281714190694UserEntity_DataExport.xlsx differ diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281714277080UserEntity_DataExport.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281714277080UserEntity_DataExport.xlsx new file mode 100644 index 00000000..720c10d1 Binary files /dev/null and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Temp/202209281714277080UserEntity_DataExport.xlsx differ diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/excel/T_DataTemplate.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/excel/T_DataTemplate.xlsx new file mode 100644 index 00000000..e7eee9a3 Binary files /dev/null and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/excel/T_DataTemplate.xlsx differ