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.
220 lines
6.9 KiB
220 lines
6.9 KiB
|
|
|
|
using Magicodes.ExporterAndImporter.Excel;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TaskManager.Controllers;
|
|
using TaskManager.Entity;
|
|
using TaskManager.EntityFramework;
|
|
using TaskManager.EntityFramework.Repository;
|
|
using Wood.Service.Controllers;
|
|
|
|
namespace TaskManager.Controllers
|
|
{
|
|
|
|
public class LogController:NormalBaseController<TaskLog>
|
|
{
|
|
public LogController(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<TaskLog> repository) : base(context, builder, configuration, repository)
|
|
{
|
|
}
|
|
|
|
public async Task<List<TaskLog>> GetAll()
|
|
{
|
|
var log = await _context.TaskLogs.ToListAsync();
|
|
return log;
|
|
}
|
|
//[HttpGet("AddError")]
|
|
//public async Task<bool> AddError(string message,string taskname)
|
|
//{
|
|
// _context.TaskLogs.Add(new TaskLog() { Info = message, Type = "错误",TaskName=taskname ,CreationTime=DateTime.Now});
|
|
// var result =await _context.SaveChangesAsync();
|
|
// if (result > 0)
|
|
// {
|
|
// return true;
|
|
// }
|
|
// return false;
|
|
//}
|
|
//[HttpGet("AddInfo")]
|
|
//public async Task<bool> AddInfo(string message, string taskname)
|
|
//{
|
|
// _context.TaskLogs.Add(new TaskLog() { Info = message, Type = "记录", TaskName = taskname, CreationTime = DateTime.Now });
|
|
// var result = await _context.SaveChangesAsync();
|
|
// if (result > 0)
|
|
// {
|
|
// return true;
|
|
// }
|
|
// return false;
|
|
//}
|
|
|
|
|
|
|
|
[HttpGet("AddError")]
|
|
public async Task<bool> AddError(string message, string taskname, Guid taskid, string version)
|
|
{
|
|
var log = new TaskLog
|
|
{
|
|
Info = message,
|
|
Type = "错误",
|
|
TaskName = taskname,
|
|
CreationTime = DateTime.Now,
|
|
TaskId = taskid,
|
|
Version = version?.ToString()
|
|
};
|
|
|
|
_context.TaskLogs.Add(log);
|
|
var result = await _context.SaveChangesAsync();
|
|
|
|
return true; // 日志已入队,视为成功
|
|
}
|
|
|
|
[HttpGet("AddInfo")]
|
|
public async Task<bool> AddInfo(string message, string taskname, Guid taskid, string version)
|
|
{
|
|
var log = new TaskLog
|
|
{
|
|
Info = message,
|
|
Type = "信息",
|
|
TaskName = taskname,
|
|
CreationTime = DateTime.Now,
|
|
TaskId = taskid,
|
|
Version = version?.ToString()
|
|
};
|
|
|
|
_context.TaskLogs.Add(log);
|
|
var result = await _context.SaveChangesAsync();
|
|
|
|
return true;
|
|
}
|
|
[HttpGet("AddPostRequest")]
|
|
public async Task<bool> AddPostRequest(string message, string taskname, Guid taskid, string version,string remark)
|
|
{
|
|
var log = new TaskLog
|
|
{
|
|
Info = message,
|
|
Type = "请求",
|
|
TaskName = taskname,
|
|
CreationTime = DateTime.Now,
|
|
TaskId = taskid,
|
|
Version = version?.ToString(),
|
|
Remark = remark
|
|
};
|
|
_context.TaskLogs.Add(log);
|
|
var result = await _context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
[HttpGet("AddPostResponse")]
|
|
public async Task<bool> AddPostResponse(string message, string taskname, Guid taskid, string version,string remaek)
|
|
{
|
|
var log = new TaskLog
|
|
{
|
|
Info = message,
|
|
Type = "应答",
|
|
TaskName = taskname,
|
|
CreationTime = DateTime.Now,
|
|
TaskId = taskid,
|
|
Version = version?.ToString(),
|
|
Remark = remaek
|
|
};
|
|
_context.TaskLogs.Add(log);
|
|
var result = await _context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[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;
|
|
}
|
|
|
|
|
|
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 };
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/// 导出
|
|
/// </summary>
|
|
/// <param name="pageNumber">第几页</param>
|
|
/// <param name="pageSize">每页条数</param>
|
|
/// <param name="sortBy">排序列</param>
|
|
/// <param name="isAscending">是否升序</param>
|
|
/// <param name="filters">查询条件</param>
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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");
|
|
//}
|
|
|
|
|
|
|
|
}
|
|
|