89 lines
3.1 KiB
C#
89 lines
3.1 KiB
C#
![]() |
using System.Data;
|
|||
|
using ClosedXML.Excel;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace Seyounth.Hyosung.Data.Services;
|
|||
|
|
|||
|
public class ReportExportService(
|
|||
|
IYarnService yarnService,
|
|||
|
IVarietyService varietyService,
|
|||
|
ITrayService trayService,
|
|||
|
ILogger<ReportExportService> logger)
|
|||
|
: IReportExportService
|
|||
|
{
|
|||
|
public async Task ExportAsync(string trayCode)
|
|||
|
{
|
|||
|
var tray = await trayService.GetByCode(trayCode);
|
|||
|
var variety = await varietyService.GetById(tray.VarietyId);
|
|||
|
var yarns = await yarnService.GetYarnsByTrayIdAsync(tray.Id);
|
|||
|
DataTable dt = new DataTable();
|
|||
|
dt.Columns.Add("托盘号", typeof(string));
|
|||
|
dt.Columns.Add("品类编号", typeof(string));
|
|||
|
dt.Columns.Add("品类", typeof(string));
|
|||
|
dt.Columns.Add("Lot", typeof(string));
|
|||
|
dt.Columns.Add("纱二维码", typeof(string));
|
|||
|
dt.Columns.Add("DEN_FILA", typeof(string));
|
|||
|
dt.Columns.Add("DTEX_FILA", typeof(string));
|
|||
|
dt.Columns.Add("码垛时间", typeof(string));
|
|||
|
dt.Columns.Add("控制号", typeof(string));
|
|||
|
foreach (var yarn in yarns)
|
|||
|
{
|
|||
|
dt.Rows.Add(
|
|||
|
tray.TrayCode,
|
|||
|
variety.Id.ToString(),
|
|||
|
variety.Code,
|
|||
|
yarn.Lot.ToString(),
|
|||
|
yarn.QrCode.Replace("\r\n", " "),
|
|||
|
yarn.DenFila,
|
|||
|
yarn.DtexFila,
|
|||
|
yarn.StackTime?.ToString("yyyy-MM-dd HH:mm:ss"),
|
|||
|
tray.ControlNo.ToString()
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
var path = await GetDirectoryAsync(tray.FinishTime);
|
|||
|
var fileName =
|
|||
|
$"{variety.Id}-{variety.Code}-{tray.TrayCode}-{tray.FinishTime?.ToString("yyyyMMddHHmmss")}.xlsx";
|
|||
|
var filePath = Path.Combine(path, fileName);
|
|||
|
using var workbook = new XLWorkbook();
|
|||
|
var worksheet = workbook.Worksheets.Add(dt, "纱信息");
|
|||
|
worksheet.Columns().AdjustToContents();
|
|||
|
workbook.SaveAs(filePath);
|
|||
|
await trayService.ExportedAsync(tray.TrayCode);
|
|||
|
logger.LogInformation($"export report [{tray.TrayCode}]:{filePath}");
|
|||
|
}
|
|||
|
|
|||
|
public async Task ExportNoExportAsync()
|
|||
|
{
|
|||
|
var codes = await trayService.GetNoExportCodesAsync();
|
|||
|
codes.ForEach(async void (code) =>
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
await ExportAsync(code);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
logger.LogError(e, $"export report [{code}] error");
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
public async Task<string> GetDirectoryAsync(DateTime? date = null)
|
|||
|
{
|
|||
|
if (date is null)
|
|||
|
date = DateTime.Now;
|
|||
|
// 构建文件夹路径,格式为 年/月/日
|
|||
|
string directoryPath = Path.Combine(date.Value.Year.ToString(), date.Value.Month.ToString(),
|
|||
|
date.Value.Day.ToString());
|
|||
|
|
|||
|
// 检查文件夹是否存在
|
|||
|
if (!Directory.Exists(directoryPath))
|
|||
|
{
|
|||
|
// 如果不存在,则创建文件夹
|
|||
|
await Task.Run(() => Directory.CreateDirectory(directoryPath));
|
|||
|
}
|
|||
|
|
|||
|
return directoryPath;
|
|||
|
}
|
|||
|
}
|