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 Dictionary _printers = new Dictionary() { { 1001, new AveryPrinter("192.168.3.61", 9100) }, { 2, new AveryPrinter("192.168.3.62", 9100) }, { 1, new AveryPrinter("192.168.3.60", 9100) } }; public async Task StartAsync(CancellationToken token) { List ls = new List(); foreach (var (_, printer) in _printers) { ls.Add(printer.ConnectAsync(token)); } await Task.WhenAll(ls); logger.LogInformation("printer connected"); } public async Task StopAsync(CancellationToken token) { foreach (var (_, printer) in _printers) { printer.CloseAsync(token); } logger.LogInformation("printer disconnected"); } public async Task PrintMasterLabelAsync(int color, string trayCode) { var tray = await trayService.GetByCode(trayCode); var content = await GetMasterLabelContent(tray); await _printers[color].CleanContextAsync(); await _printers[color].SendContentAsync(content); await _printers[color].PrintAsync(); } public async Task PrintSubLabelAsync(string trayCode) { var tray = await trayService.GetByCode(trayCode); var content = await GetSubLabelContentAsync(tray); await _printers[1001].CleanContextAsync(); await _printers[1001].SendContentAsync(content); await _printers[1001].PrintAsync(); } private async Task GetSubLabelContentAsync(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 GetMasterLabelContent(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); } logger.LogInformation($"print content: {content}"); return content; } }