132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
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<HyosungPrinter> logger,
|
|
IVarietyService varietyService,
|
|
ITrayService trayService,
|
|
IDictService dictService,
|
|
IYarnService yarnService,
|
|
IHyosungWmsService hyosungWmsService,
|
|
IOptions<PrintTemp> options) : IHyosungPrinter
|
|
{
|
|
private readonly AveryPrinter _masterPrinter = new("192.168.3.60", 9100);
|
|
private readonly AveryPrinter _slavePrinter = new("192.168.3.61", 9100);
|
|
|
|
public async Task StartAsync(CancellationToken token)
|
|
{
|
|
List<Task> ls = new List<Task>();
|
|
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, long 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 GetPrint1Content(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<string> GetPrint1Content(Tray tray)
|
|
{
|
|
var variety = await varietyService.GetById(tray.VarietyId);
|
|
var yarns = await yarnService.GetYarnsByTrayIdAsync(tray.VarietyId);
|
|
var dic = new Dictionary<string, string>();
|
|
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.MasterLabelCount == 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<string> GetPrint2Content(Tray tray)
|
|
{
|
|
var variety = await varietyService.GetById(tray.VarietyId);
|
|
var labelResult =
|
|
await hyosungWmsService.GetLabelResult(variety.Code, variety.Lot.ToString(), tray.ControlNo!.Value);
|
|
var dic = new Dictionary<string, string>
|
|
{
|
|
{ "$1153", $"\"{variety.Lot}\"" },
|
|
{ "1670dtex -144F", $"\"{tray.DtexFila}\"" },
|
|
{ "1500D - 144F", $"\"{tray.DenFila}\"" },
|
|
{ "$SA", $"\"{tray.Grade}\"" },
|
|
{ "$900", $"\"{labelResult.NET_WEIGHT.ToString("0.0")}\"" },
|
|
{ "25-01-01", $"\"{DateTime.Now:yy-MM-dd}\"" },
|
|
{ "$90", $"\"{variety.TotalCount}\"" },
|
|
{ "$965", $"\"{labelResult.GROSS_WEIGHT}\"" },
|
|
{
|
|
"250101J068",
|
|
$"\"{DateTime.Now.ToString("yyMMdd" + "J") + tray.ControlNo.ToString()?.PadLeft(3, '0')}\""
|
|
},
|
|
{ "DTYY-JPA11530SFG250101000900115305480", $"\"{labelResult.BAR_CODE}\"" }
|
|
};
|
|
string content = options.Value.DefaultTemp;
|
|
foreach (var (key, value) in dic)
|
|
{
|
|
content = content.Replace(key, value);
|
|
}
|
|
|
|
return content;
|
|
}
|
|
} |