using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Seyounth.Hyosung.Data.Models; using Seyounth.Hyosung.Data.Services; using Seyounth.Hyosung.Data.Services.Hyosung; namespace Seyounth.Hyosung.Core.Printer; public class HyosungPrinter( ILogger logger, IVarietyService varietyService, ITrayService trayService, IDictService dictService, IYarnService yarnService, IHyosungWmsService hyosungWmsService, IOptions options) : IHyosungPrinter { private readonly AveryPrinter _masterPrinter = new("192.168.3.61", 9100); private readonly AveryPrinter _slavePrinter = new("192.168.3.60", 9100); public async Task StartAsync(CancellationToken token) { List ls = new List(); ls.Add(_masterPrinter.ConnectAsync(token)); ls.Add(_slavePrinter.ConnectAsync(token)); await Task.WhenAll(ls); logger.LogInformation("printer connected"); } public async Task StopAsync(CancellationToken token) { await _masterPrinter.CloseAsync(token); await _slavePrinter.CloseAsync(token); logger.LogInformation("printer disconnected"); } public async Task PrintAsync(int index, string trayCode) { var tray = await trayService.GetByCode(trayCode); if (index == 1) { await PrintLabel1Async(tray); } else { await PrintLabel2Async(tray); } } private async Task PrintLabel1Async(Tray tray) { await _masterPrinter.CleanContextAsync(); var content = await GetPrint2Content(tray); await _masterPrinter.SendContentAsync(content); await _masterPrinter.PrintAsync(); } private async Task PrintLabel2Async(Tray tray) { await _slavePrinter.CleanContextAsync(); var content = await GetPrint2Content(tray); await _slavePrinter.SendContentAsync(content); await _slavePrinter.PrintAsync(); } private async Task GetPrint1Content(Tray tray) { var variety = await varietyService.GetById(tray.VarietyId); var yarns = await yarnService.GetYarnsByTrayIdAsync(tray.Id); var dic = new Dictionary(); if (yarns.Count > 0) { var machineNoList = yarns .Where(item => item.Machine.Contains('-')) .Select(item => item.Machine.Split('-')[0]) .Distinct() .ToList(); var machineStr = string.Join(" ", machineNoList); dic.Add("1569", yarns[0].Lot.ToString()); dic.Add("T1 T2 T3 T4 T5", machineStr); } string temp; if (variety.SubLabelCount == 1) temp = options.Value.Partition; else { var tempName = options.Value.Sides.First(t => t.ItemCode == variety.Code).TempName; temp = options.Value.Temps.First(t => t.Name == tempName).Code; } foreach (var (key, value) in dic) { temp = temp.Replace(key, value); } return temp; } private async Task GetPrint2Content(Tray tray) { var variety = await varietyService.GetById(tray.VarietyId); var netWeight = tray.NetWeight.ToString("0.0"); if (netWeight.EndsWith(".0")) netWeight = netWeight.Replace(".0", ""); var dic = new Dictionary { { "$1153", $"\"{variety.Lot}\"" }, { "1670dtex -144F", $"\"{tray.DtexFila}\"" }, { "1500D - 144F", $"\"{tray.DenFila}\"" }, { "$SA", $"\"{tray.Grade}\"" }, { "$900", $"\"{netWeight}\"" }, { "25-01-01", $"\"{DateTime.Now:yy-MM-dd}\"" }, { "$90", $"\"{variety.TotalCount}\"" }, { "$965", $"\"{tray.GrossWeight}\"" }, { "250101J068", $"\"{DateTime.Now.ToString("yyMMdd" + "J") + tray.ControlNo.ToString()?.PadLeft(3, '0')}\"" }, { "DTYY-JPA11530SFG250101000900115305480", $"\"{tray.Barcode}\"" } }; string content = options.Value.DefaultTemp; foreach (var (key, value) in dic) { content = content.Replace(key, value); } return content; } }