通用数据控制器
This commit is contained in:
@@ -114,6 +114,24 @@
|
||||
<param name="ids"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseExcelController`1.Template">
|
||||
<summary>
|
||||
下载模板
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseExcelController`1.Export">
|
||||
<summary>
|
||||
导出数据
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseExcelController`1.Import(Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<summary>
|
||||
导入数据
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1">
|
||||
<summary>
|
||||
Json To Sql 类比模式,通用模型
|
||||
@@ -465,18 +483,6 @@
|
||||
用户管理
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.Template">
|
||||
<summary>
|
||||
下载模板
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.Export">
|
||||
<summary>
|
||||
导出数据
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.PageList(Yi.Framework.Model.Models.UserEntity,Yi.Framework.Common.Models.PageParModel,System.Nullable{System.Int64})">
|
||||
<summary>
|
||||
动态条件分页查询
|
||||
|
||||
@@ -16,16 +16,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[ApiController]
|
||||
public class BaseCrudController<T> : ControllerBase where T : class,new()
|
||||
public class BaseCrudController<T> : BaseExcelController<T> where T : class,new()
|
||||
{
|
||||
private readonly ILogger<T> _logger;
|
||||
private IBaseService<T> _baseService;
|
||||
private IRepository<T> _repository;
|
||||
public BaseCrudController(ILogger<T> logger, IBaseService<T> iBaseService)
|
||||
protected readonly ILogger<T> _logger;
|
||||
protected IBaseService<T> _baseService;
|
||||
public BaseCrudController(ILogger<T> logger, IBaseService<T> iBaseService):base(iBaseService._repository)
|
||||
{
|
||||
_logger = logger;
|
||||
_baseService = iBaseService;
|
||||
_repository = iBaseService._repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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<T> : ControllerBase where T : class, new()
|
||||
{
|
||||
protected IRepository<T> _repository;
|
||||
public BaseExcelController(IRepository<T> repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
/// <summary>
|
||||
/// 下载模板
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Template()
|
||||
{
|
||||
List<T> 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");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导出数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> 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");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导入数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
public async Task<Result> Import(IFormFile formFile)
|
||||
{
|
||||
List<T> datas = ExcelHelper.ImportData<T>(formFile.OpenReadStream());
|
||||
|
||||
//全量删除在重新插入
|
||||
var res = await _repository.UseTranAsync(async () =>
|
||||
{
|
||||
await _repository.DeleteAsync(u => true);
|
||||
await _repository.InsertRangeAsync(datas);
|
||||
});
|
||||
return Result.Success().SetStatus(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,16 +16,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[ApiController]
|
||||
public class BaseSimpleCrudController<T> : ControllerBase where T : class, new()
|
||||
public class BaseSimpleCrudController<T> : BaseExcelController<T> where T : class, new()
|
||||
{
|
||||
private readonly ILogger<T> _logger;
|
||||
private IBaseService<T> _baseService;
|
||||
private IRepository<T> _repository;
|
||||
public BaseSimpleCrudController(ILogger<T> logger, IBaseService<T> iBaseService)
|
||||
protected readonly ILogger<T> _logger;
|
||||
protected IBaseService<T> _baseService;
|
||||
public BaseSimpleCrudController(ILogger<T> logger, IBaseService<T> iBaseService):base(iBaseService._repository)
|
||||
{
|
||||
_logger = logger;
|
||||
_baseService = iBaseService;
|
||||
_repository = iBaseService._repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -16,16 +16,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[ApiController]
|
||||
public class BaseSimpleRdController<T> : ControllerBase where T : class, new()
|
||||
public class BaseSimpleRdController<T> : BaseExcelController<T> where T : class, new()
|
||||
{
|
||||
private readonly ILogger<T> _logger;
|
||||
private IBaseService<T> _baseService;
|
||||
private IRepository<T> _repository;
|
||||
public BaseSimpleRdController(ILogger<T> logger, IBaseService<T> iBaseService)
|
||||
protected readonly ILogger<T> _logger;
|
||||
protected IBaseService<T> _baseService;
|
||||
public BaseSimpleRdController(ILogger<T> logger, IBaseService<T> iBaseService):base(iBaseService._repository)
|
||||
{
|
||||
_logger = logger;
|
||||
_baseService = iBaseService;
|
||||
_repository = iBaseService._repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -37,58 +37,58 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
|
||||
//数据导入导出将会被写入到基类
|
||||
|
||||
/// <summary>
|
||||
/// 下载模板
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Template()
|
||||
{
|
||||
List<UserEntity> 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");
|
||||
}
|
||||
///// <summary>
|
||||
///// 下载模板
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpGet]
|
||||
//[AllowAnonymous]
|
||||
//public IActionResult Template()
|
||||
//{
|
||||
// List<UserEntity> 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");
|
||||
//}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导出数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> 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");
|
||||
}
|
||||
///// <summary>
|
||||
///// 导出数据
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpGet]
|
||||
//[AllowAnonymous]
|
||||
//public async Task<IActionResult> 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");
|
||||
//}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导入数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<Result> Import([FromForm(Name = "file")] IFormFile formFile)
|
||||
{
|
||||
List<UserEntity> users = ExcelHelper.ImportData<UserEntity>(formFile.OpenReadStream());
|
||||
///// <summary>
|
||||
///// 导入数据
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpPost]
|
||||
//[AllowAnonymous]
|
||||
//public async Task<Result> Import(IFormFile formFile)
|
||||
//{
|
||||
// List<UserEntity> users = ExcelHelper.ImportData<UserEntity>(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);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 动态条件分页查询
|
||||
@@ -114,8 +114,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
[Permission("system:user:edit")]
|
||||
public async Task<Result> 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 }));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -153,7 +152,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
[Permission("system:user:edit")]
|
||||
public async Task<Result> 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<Result> 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("用户已经存在,添加失败!");
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user