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.

132 lines
4.5 KiB

3 weeks ago

2 weeks ago
using Magicodes.ExporterAndImporter.Excel;
3 weeks ago
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2 weeks ago
using Microsoft.Extensions.Configuration;
3 weeks ago
using Microsoft.Extensions.DependencyInjection;
2 weeks ago
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
3 weeks ago
using System;
using System.Collections.Generic;
using System.Linq;
2 weeks ago
using System.Linq.Expressions;
3 weeks ago
using System.Text;
using System.Threading.Tasks;
using TaskManager.Controllers;
using TaskManager.Entity;
using TaskManager.EntityFramework;
2 weeks ago
using TaskManager.EntityFramework.Repository;
using Wood.Service.Controllers;
3 weeks ago
namespace TaskManager.Controllers
{
2 weeks ago
2 weeks ago
public class LogController:NormalBaseController<TaskLog>
3 weeks ago
{
2 weeks ago
public LogController(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<TaskLog> repository) : base(context, builder, configuration, repository)
3 weeks ago
{
}
2 weeks ago
3 weeks ago
public async Task<List<TaskLog>> GetAll()
{
2 weeks ago
var log = await _context.TaskLogs.ToListAsync();
3 weeks ago
return log;
}
[HttpGet("AddError")]
public async Task<bool> AddError(string message,string taskname)
{
2 weeks ago
_context.TaskLogs.Add(new TaskLog() { Info = message, Type = "错误",TaskName=taskname ,CreationTime=DateTime.Now});
var result =await _context.SaveChangesAsync();
3 weeks ago
if (result > 0)
{
return true;
}
return false;
}
[HttpGet("AddInfo")]
public async Task<bool> AddInfo(string message, string taskname)
{
2 weeks ago
_context.TaskLogs.Add(new TaskLog() { Info = message, Type = "记录", TaskName = taskname, CreationTime = DateTime.Now });
var result = await _context.SaveChangesAsync();
3 weeks ago
if (result > 0)
{
return true;
}
return false;
}
2 weeks ago
[HttpGet("AddInfoRemark")]
public async Task<bool> AddInfoRemark(string message, string taskname,string remark)
{
_context.TaskLogs.Add(new TaskLog() { Info = message, Type = "记录", TaskName = taskname, CreationTime = DateTime.Now,Remark=remark });
var result = await _context.SaveChangesAsync();
if (result > 0)
{
return true;
}
return false;
}
2 weeks ago
public async Task<FileStreamResult> Export([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(null, pagingParams);
return await ExportFile<TaskLog>(pagedResult.Data, Guid.NewGuid().ToString() + ".xlsx");
}
protected async Task<FileStreamResult> ExportFile<T>(ICollection<T> dtos, string fileName) where T : class, new()
{
var excelExporter = HttpContext.RequestServices.GetRequiredService<IExcelExporter>();
var res = await excelExporter.ExportAsByteArray(dtos);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_" + fileName };
}
3 weeks ago
}
2 weeks ago
/// 导出
/// </summary>
/// <param name="pageNumber">第几页</param>
/// <param name="pageSize">每页条数</param>
/// <param name="sortBy">排序列</param>
/// <param name="isAscending">是否升序</param>
/// <param name="filters">查询条件</param>
/// <returns></returns>
3 weeks ago
//private readonly IServiceProvider _serviceProvider;
//public LogController(IServiceProvider serviceProvider)
//{
// _serviceProvider = serviceProvider;
//}
//public async Task<IEnumerable<Logs>> GetLogs()
//{
// var dbcontext= _serviceProvider.GetRequiredService<JobDbContext>();
// var connection=dbcontext.Database.GetDbConnection();
// connection.Query<Logs>("select top 10 * from logs");
//}
2 weeks ago
}