You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.5 KiB

2 weeks ago
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Authorization;
2 weeks ago
using Microsoft.AspNetCore.Mvc;
2 weeks ago
using Microsoft.Extensions.Configuration;
2 weeks ago
using Microsoft.Extensions.DependencyInjection;
2 weeks ago
using System;
using System.Collections.Generic;
2 weeks ago
using System.Data;
2 weeks ago
using System.Linq;
using System.Linq.Expressions;
2 weeks ago
using System.Reflection;
2 weeks ago
using System.Text;
using System.Threading.Tasks;
using TaskManager.Entity;
using TaskManager.EntityFramework;
using TaskManager.EntityFramework.Repository;
2 weeks ago
using Wood.Util;
2 weeks ago
using Magicodes.ExporterAndImporter.Core.Extension;
2 weeks ago
namespace Wood.Service.Controllers
{
2 weeks ago
[AllowAnonymous]
2 weeks ago
public class NormalBaseController<T>:ControllerBase where T:BaseEntity
2 weeks ago
{
protected readonly JobDbContext _context;
protected readonly IServiceProvider _builder;
protected readonly IConfiguration _configuration;
protected readonly IRepository<T> _repository;
2 weeks ago
2 weeks ago
public NormalBaseController(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<T> repository)
{
_builder = builder;
_context = context;
_configuration = configuration;
_repository = repository;
}
/// <summary>
/// 通过UID获得实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<ActionResult<T>> GetById(int id)
{
var entity = await _repository.GetByIdAsync(id);
if (entity == null) return NotFound();
return entity;
}
/// <summary>
/// 创建实体
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<T>> Create(T entity)
{
entity.CreationTime = DateTime.Now;
var createdEntity = await _repository.AddAsync(entity);
return new JsonResult(new { Code = 200, Message = "创建成功!" });
}
/// <summary>
/// 更新实体UID
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task<IActionResult> Update(T entity)
{
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 = "修改成功!" });
}
/// <summary>
/// 删除实体通过UID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _repository.DeleteAsync(id);
return new JsonResult(new { Code = 200, Message = "删除成功!" }); ;
}
2 weeks ago
[HttpGet]
2 weeks ago
public async Task<ActionResult<TaskManager.EntityFramework.Repository.PagedResult<T>>> GetPaged(
2 weeks ago
[FromQuery] int pageNumber = 1,
[FromQuery] int pageSize = 10,
[FromQuery] string sortBy = "",
[FromQuery] bool isAscending = true,
[FromQuery] Dictionary<string, string> filters = null)
{
var pagingParams = new PagingParams
{
PageNumber = pageNumber,
PageSize = pageSize,
SortBy = sortBy,
IsAscending = isAscending,
Filters = filters
};
// 可以在这里构建表达式树过滤条件
Expression<Func<T, bool>> filter = null;
var pagedResult = await _repository.GetPagedAsync(filter, pagingParams);
return Ok(pagedResult);
}
2 weeks ago
[HttpGet]
public async Task<FileStreamResult> Export([FromQuery] int pageNumber = 1,
[FromQuery] int pageSize = 10,
[FromQuery] string sortBy = "",
[FromQuery] bool isAscending = true,
2 weeks ago
[FromQuery] Dictionary<string, string> filters = null)
2 weeks ago
{
var pagingParams = new PagingParams
{
PageNumber = pageNumber,
PageSize = pageSize,
SortBy = sortBy,
IsAscending = isAscending,
Filters = filters
};
// 可以在这里构建表达式树过滤条件
Expression<Func<T, bool>> filter = null;
var pagedResult = await _repository.GetPagedAsync(null, pagingParams);
2 weeks ago
2 weeks ago
var dataTable = pagedResult.Data.ToDataTable();
2 weeks ago
return await ExportFile(dataTable, Guid.NewGuid().ToString() + ".xlsx");
2 weeks ago
}
2 weeks ago
protected async Task<FileStreamResult> ExportFile(DataTable dtos, string fileName)
2 weeks ago
{
var excelExporter = HttpContext.RequestServices.GetRequiredService<IExcelExporter>();
2 weeks ago
var res = await excelExporter.ExportAsByteArray(dtos);
2 weeks ago
return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_" + fileName };
}
2 weeks ago
//[HttpGet]
//public async Task<IActionResult> GetImportTemplate()
//{
// try
// {
// // 创建导入模板生成器
// var importer = new ExcelImporter();
// // 生成导入模板流(这里假设使用 YourModel 作为导入模型)
// byte[] by = await importer.GenerateTemplate<T>;
// using var stream = new MemoryStream(by);
// stream.Seek(0, SeekOrigin.Begin);
// // 设置友好的文件名,例如:"导入模板_20250530.xlsx"
// var fileName = $"导入模板_{DateTime.Now:yyyyMMdd}.xlsx";
// // 返回文件流结果
// return File(
// fileStream: stream,
// contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// fileDownloadName: fileName);
// }
// catch (Exception ex)
// {
// // 记录异常日志
// Console.WriteLine($"生成导入模板时出错: {ex.Message}");
// // 返回错误响应
// return StatusCode(500, "生成导入模板时发生错误");
// }
//}
}
2 weeks ago
2 weeks ago
}