lg_backend/langguanApi/Service/DetectionService.cs

48 lines
1.9 KiB
C#

using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using langguanApi.Model.Entity;
using Npoi.Mapper;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace langguanApi.Service
{
[ServiceInjection(InjectionType.Transient)]
public class DetectionService : BaseService<Detection>
{
public DetectionService(IConfiguration config) : base(config, nameof(Detection))
{
}
/// <summary>
/// //获取最新的数据
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public async Task<object> GetIndexData(int num = 50)
{
Expression<Func<Detection, bool>> exp = filter => true;
var result = (await GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).Take(num).ToList();
return result;
}
/// <summary>
/// 导出数据
/// </summary>
/// <returns></returns>
public async Task<byte[]> Export(DateTime start, DateTime end)
{
Expression<Func<Detection, bool>> exp = filter => filter.CreateDateTime >= start && filter.CreateDateTime <= end;
var result = (await GetListWithExp(exp)).OrderBy(s => s.CreateDateTime).ToList();
var mapper = new Mapper();
mapper.Map<Detection>("时间", s => s.CreateDateTime.ToString("yyyy-MM-dd HH:mm:ss"))
.Map<Detection>("工序", s => s.OrganizedName)
.Map<Detection>("设备名称", s => s.DeviceName)
.Map<Detection>("项目", s => s.keyword)
.Map<Detection>("检测值", s => s.deviceValue)
.Format<Detection>("yyyy-MM-dd HH:mm:ss", s => s.CreateDateTime);
MemoryStream stream = new MemoryStream();
mapper.Save(stream, result.ToList(), sheetName: "sheet1", leaveOpen: true);
return stream.ToArray();
}
}
}