diff --git a/Seyounth.Hyosung.Data/Entities/TrayEntity.cs b/Seyounth.Hyosung.Data/Entities/TrayEntity.cs index ba90e54..9d430fb 100644 --- a/Seyounth.Hyosung.Data/Entities/TrayEntity.cs +++ b/Seyounth.Hyosung.Data/Entities/TrayEntity.cs @@ -14,7 +14,7 @@ public class TrayEntity [SugarColumn(ColumnDescription = "所属的品类信息")] public int VarietyId { get; set; } - [SugarColumn(ColumnDescription = "垛高",IsNullable =true)] + [SugarColumn(ColumnDescription = "垛高", IsNullable = true)] public int? StackHeight { get; set; } [SugarColumn(ColumnDescription = "是否为双号")] @@ -40,8 +40,10 @@ public class TrayEntity [SugarColumn(IsNullable = true)] public double GrossWeight { get; set; } [SugarColumn(IsNullable = true)] public int? AgvBinId { get; set; } - + [SugarColumn(IsNullable = true)] public string? Barcode { get; set; } [SugarColumn(IsNullable = true)] public bool? IsPacking { get; set; } = false; + + [SugarColumn(IsNullable = true)] public bool? Exported { get; set; } } \ No newline at end of file diff --git a/Seyounth.Hyosung.Data/Models/Tray.cs b/Seyounth.Hyosung.Data/Models/Tray.cs index 77e7c7b..909183a 100644 --- a/Seyounth.Hyosung.Data/Models/Tray.cs +++ b/Seyounth.Hyosung.Data/Models/Tray.cs @@ -40,6 +40,8 @@ public class Tray public bool? IsPacking { get; set; } + public bool? Exported { get; set; } + public TrayEntity ToEntity() { return new TrayEntity() @@ -61,7 +63,8 @@ public class Tray Unit = Unit, AgvBinId = AgvBinId, Barcode = Barcode, - IsPacking= IsPacking + IsPacking = IsPacking, + Exported = Exported }; } @@ -86,7 +89,8 @@ public class Tray Unit = entity.Unit, AgvBinId = entity.AgvBinId, Barcode = entity.Barcode, - IsPacking=entity.IsPacking + IsPacking = entity.IsPacking, + Exported = entity.Exported }; } } \ No newline at end of file diff --git a/Seyounth.Hyosung.Data/ServiceExtensions.cs b/Seyounth.Hyosung.Data/ServiceExtensions.cs index b4285d2..439ce32 100644 --- a/Seyounth.Hyosung.Data/ServiceExtensions.cs +++ b/Seyounth.Hyosung.Data/ServiceExtensions.cs @@ -44,6 +44,7 @@ public static class ServiceExtensions services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); return services; } diff --git a/Seyounth.Hyosung.Data/Services/IReportExportService.cs b/Seyounth.Hyosung.Data/Services/IReportExportService.cs new file mode 100644 index 0000000..73dc58f --- /dev/null +++ b/Seyounth.Hyosung.Data/Services/IReportExportService.cs @@ -0,0 +1,9 @@ +namespace Seyounth.Hyosung.Data.Services; + +public interface IReportExportService +{ + Task ExportAsync(string trayCode); + + + Task ExportNoExportAsync(); +} \ No newline at end of file diff --git a/Seyounth.Hyosung.Data/Services/ITrayService.cs b/Seyounth.Hyosung.Data/Services/ITrayService.cs index 4174a0f..4175b37 100644 --- a/Seyounth.Hyosung.Data/Services/ITrayService.cs +++ b/Seyounth.Hyosung.Data/Services/ITrayService.cs @@ -44,7 +44,14 @@ public interface ITrayService /// /// /// - Task PrintTrayAsync(string trayCode, int controlNo, MST_ITEM_2240_V itemInfo); + Task PrintTrayAsync(string trayCode, MST_ITEM_2240_V itemInfo); Task UpdateHeightAsync(string trayCode, int height); + + + Task ExportedAsync(string trayCode); + + Task> GetNoExportCodesAsync(); + + Task SetControlNoAsync(string trayCode, int controlNo); } \ No newline at end of file diff --git a/Seyounth.Hyosung.Data/Services/ReportExportService.cs b/Seyounth.Hyosung.Data/Services/ReportExportService.cs new file mode 100644 index 0000000..78ba304 --- /dev/null +++ b/Seyounth.Hyosung.Data/Services/ReportExportService.cs @@ -0,0 +1,89 @@ +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 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 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; + } +} \ No newline at end of file diff --git a/Seyounth.Hyosung.Data/Services/TrayService.cs b/Seyounth.Hyosung.Data/Services/TrayService.cs index c1df37a..fba7ae7 100644 --- a/Seyounth.Hyosung.Data/Services/TrayService.cs +++ b/Seyounth.Hyosung.Data/Services/TrayService.cs @@ -13,17 +13,17 @@ namespace Seyounth.Hyosung.Data.Services; public class TrayService : ITrayService { //private readonly ConcurrentDictionary _cache = new(); - // private readonly _repository; + // private readonly _repository; private readonly IHyosungWmsService _hyosungWmsService; private readonly ISqlSugarClient _db; - public TrayService(IServiceProvider provider, IHyosungWmsService hyosungWmsService,ISqlSugarClient db) + public TrayService(IServiceProvider provider, IHyosungWmsService hyosungWmsService, ISqlSugarClient db) { _db = db; // _repository = provider.CreateScope().ServiceProvider.GetRequiredService>(); //_repository = provider.GetService>(); - // var trays = _db.Queryable().Where(t => t.ControlNo == null).ToListAsync().Result; + // var trays = _db.Queryable().Where(t => t.ControlNo == null).ToListAsync().Result; _hyosungWmsService = hyosungWmsService; //foreach (var tray in trays) //{ @@ -43,7 +43,7 @@ public class TrayService : ITrayService tray.IsEven = count % 2 == 0; var identity = await _db.CopyNew().Insertable(tray.ToEntity()).ExecuteReturnIdentityAsync(); tray.Id = identity; - // _cache.TryAdd(tray.TrayCode, tray); + // _cache.TryAdd(tray.TrayCode, tray); return tray; } @@ -55,8 +55,8 @@ public class TrayService : ITrayService //} //catch //{ - return (await _db.CopyNew().Queryable().Where(d => d.TrayCode == code).FirstAsync()).Id; - // } + return (await _db.CopyNew().Queryable().Where(d => d.TrayCode == code).FirstAsync()).Id; + // } } public async Task GetByCode(string code) @@ -69,7 +69,7 @@ public class TrayService : ITrayService throw new NotImplementedException(); } - public async Task PrintTrayAsync(string trayCode, int controlNo, MST_ITEM_2240_V itemInfo) + public async Task PrintTrayAsync(string trayCode, MST_ITEM_2240_V itemInfo) { Tray tray; //try @@ -78,10 +78,9 @@ public class TrayService : ITrayService //} //catch //{ - tray = await GetByCode(trayCode); + tray = await GetByCode(trayCode); //} - tray.ControlNo = controlNo; tray.Grade = itemInfo.GRADE; tray.Type = itemInfo.TYPE; tray.DenFila = itemInfo.DEN_FILA; @@ -90,19 +89,19 @@ public class TrayService : ITrayService tray.NetWeight = itemInfo.NET_WEIGHT; tray.GrossWeight = itemInfo.GROSS_WEIGHT; tray.Barcode = - $"{itemInfo.ITEM_CODE} {DateTime.Now:yyMMdd}00{itemInfo.LOTNO.PadLeft(4, '0')}{controlNo.ToString().PadLeft(4, '0')}0"; + $"{itemInfo.ITEM_CODE} {DateTime.Now:yyMMdd}00{itemInfo.LOTNO.PadLeft(4, '0')}{tray.ControlNo?.ToString().PadLeft(4, '0')}0"; await _db.Updateable(tray.ToEntity()).ExecuteCommandAsync(); - // _cache.Remove(tray.TrayCode, out _); + // _cache.Remove(tray.TrayCode, out _); return tray; } - public async Task StorageAsync(string trayCode) + public async Task StorageAsync(string trayCode) { var tray = await _db.CopyNew().Queryable().Where(d => d.TrayCode == trayCode).FirstAsync(); tray.FinishTime = DateTime.Now; tray.IsPacking = true; await _db.CopyNew().Updateable(tray).ExecuteCommandAsync(); - // _cache.TryRemove(tray.TrayCode, out _); + // _cache.TryRemove(tray.TrayCode, out _); } public async Task UpdateHeightAsync(string trayCode, int height) @@ -114,9 +113,32 @@ public class TrayService : ITrayService .ExecuteCommandAsync(); } + public Task ExportedAsync(string trayCode) + { + var tray = _db.CopyNew().Queryable().Where(d => d.TrayCode == trayCode).First(); + tray.Exported = true; + return _db.CopyNew().Updateable(tray).ExecuteCommandAsync(); + } + + public Task> GetNoExportCodesAsync() + { + var tray = _db.CopyNew().Queryable().Where(x => x.Exported != true && x.FinishTime != null) + .ToList(); + var trayCodes = tray.Select(x => x.TrayCode).ToList(); + return Task.FromResult(trayCodes); + } + + public Task SetControlNoAsync(string trayCode, int controlNo) + { + var tray = _db.CopyNew().Queryable().Where(d => d.TrayCode == trayCode).First(); + tray.ControlNo = controlNo; + return _db.CopyNew().Updateable(tray).ExecuteCommandAsync(); + } + public async Task GetIsPacking() { - var tray= await _db.CopyNew().Queryable().Where(x => x.IsPacking!=null&&x.IsPacking.Value).FirstAsync(); + var tray = await _db.CopyNew().Queryable().Where(x => x.IsPacking != null && x.IsPacking.Value) + .FirstAsync(); return Tray.FromEntity(tray); } } \ No newline at end of file diff --git a/Seyounth.Hyosung.Data/Seyounth.Hyosung.Data.csproj b/Seyounth.Hyosung.Data/Seyounth.Hyosung.Data.csproj index 0e05839..a4c365b 100644 --- a/Seyounth.Hyosung.Data/Seyounth.Hyosung.Data.csproj +++ b/Seyounth.Hyosung.Data/Seyounth.Hyosung.Data.csproj @@ -7,10 +7,12 @@ + + diff --git a/Seyounth.Hyosung.Runtime/HyosungRuntime.cs b/Seyounth.Hyosung.Runtime/HyosungRuntime.cs index abb3ac7..6c54b4b 100644 --- a/Seyounth.Hyosung.Runtime/HyosungRuntime.cs +++ b/Seyounth.Hyosung.Runtime/HyosungRuntime.cs @@ -25,7 +25,8 @@ public class HyosungRuntime( IVarietyService varietyService, IHyosungAgvService hyosungAgvService, IHyosungWmsService hyosungWmsService, - IDictService dictService) : IHyosungRuntime + IDictService dictService, + IReportExportService reportExportService) : IHyosungRuntime { public PackLineOption PackLineOption { get; private set; } public StackStationModel Stack1 { get; private set; } = new(); @@ -37,6 +38,7 @@ public class HyosungRuntime( public async Task StartAsync(CancellationToken token) { + //reportExportService.ExportNoExportAsync(); //启动扫码服务 await hyosungScannerService.StartAsync(token); await printer.StartAsync(token); @@ -192,8 +194,8 @@ public class HyosungRuntime( try { await trayService.StorageAsync(info.TrayCode); + reportExportService.ExportAsync(info.TrayCode); await hyosungAgvService.StorageAsync(info.TrayCode); - _packingQueue?.TryDequeue(out _); //标志下线已完成 await hyosungPlcService.LeaveCompletedAsync(); logger.LogInformation($"plc leaving production line success"); @@ -300,8 +302,6 @@ public class HyosungRuntime( { var tray = await trayService.GetByCode(arg); var variety = await varietyService.GetById(tray.VarietyId); - if (_packingQueue is null) - _packingQueue = new ConcurrentQueue(); _packingQueue.Enqueue(variety.Id); var mod = await hyosungWmsService.GetItemInfoByItemCode(variety.Code); var grade = "1"; @@ -311,8 +311,10 @@ public class HyosungRuntime( if (control is null) control = await hyosungWmsService.GetControlNo(variety, grade); else - control = control + count; + control = control + 1; var isEven = control % 2 == 0; + await varietyService.SetLastNo(variety.Id, control.Value); + await trayService.SetControlNoAsync(arg, control.Value); PackLineOption = new PackLineOption() { HeadCount = variety.StackHeadCount ?? 0, @@ -355,19 +357,19 @@ public class HyosungRuntime( var tray = await trayService.GetByCode(arg); var variety = await varietyService.GetById(tray.VarietyId); var mod = await hyosungWmsService.GetItemInfoByItemCode(variety.Code); - var grade = "1"; - if (mod.GRADE != "AA") grade = mod.GRADE; - int? controlNo; - if (tray.ControlNo is null || tray.ControlNo == 0) - { - controlNo = await varietyService.GetLastNo(variety.Id); - if (controlNo is null) - controlNo = await hyosungWmsService.GetControlNo(variety, grade); - else - controlNo += 1; - tray = await trayService.PrintTrayAsync(arg, controlNo.Value, mod); - } - + // var grade = "1"; + // if (mod.GRADE != "AA") grade = mod.GRADE; + // int? controlNo; + // if (tray.ControlNo is null || tray.ControlNo == 0) + // { + // controlNo = await varietyService.GetLastNo(variety.Id); + // if (controlNo is null) + // controlNo = await hyosungWmsService.GetControlNo(variety, grade); + // else + // controlNo += 1; + // + // } + tray = await trayService.PrintTrayAsync(arg, mod); await dictService.SetValue("System", "CurrentPackingTrayCode", arg); await hyosungPlcService.WritePrintLableOptionsAsync(variety.MasterLabelCount); } @@ -398,12 +400,11 @@ public class HyosungRuntime( { await printer.PrintAsync(2, tray.TrayCode); await hyosungPlcService.WritePrintLabelResultAsync(arg1, true); - await varietyService.SetLastNo(variety.Id, tray.ControlNo.Value); + // await varietyService.SetLastNo(variety.Id, tray.ControlNo.Value); //await hyosungWmsService.UpdateControlNo(variety, tray.ControlNo.Value); await hyosungWmsService.AddLabelResult(new LabelResult(tray, variety)); } - logger.LogInformation($"plc request print label success"); } catch (Exception e)