2025-03-21 09:07:39 +08:00

102 lines
3.3 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Seyounth.Core.Extensions;
using Seyounth.Hyosung.Data.Entities;
using Seyounth.Hyosung.Data.Models;
using Seyounth.Hyosung.Data.Repositories;
using Seyounth.Hyosung.Data.Services.Hyosung;
using Seyounth.Hyosung.Data.Services.Hyosung.Entities;
namespace Seyounth.Hyosung.Data.Services;
public class TrayService : ITrayService
{
private readonly ConcurrentDictionary<string, Tray> _cache = new();
private readonly IRepository<TrayEntity> _repository;
private readonly IHyosungWmsService _hyosungWmsService;
public TrayService(IServiceProvider provider, IHyosungWmsService hyosungWmsService)
{
_repository = provider.CreateScope().ServiceProvider.GetRequiredService<IRepository<TrayEntity>>();
var trays = _repository.GetList(t => t.ControlNo == null);
_hyosungWmsService = hyosungWmsService;
foreach (var tray in trays)
{
_cache.TryAdd(tray.TrayCode, Tray.FromEntity(tray));
}
}
public async Task<Tray> GeneraNewTray(int varietyId)
{
var tray = new Tray()
{
TrayCode = DateTime.Now.ToTimestamp().ToString(),
VarietyId = varietyId,
CreateTime = DateTime.Now
};
var count = await _repository.CountAsync(t => t.VarietyId == tray.VarietyId);
tray.IsEven = count % 2 == 0;
var identity = await _repository.InsertReturnIdentityAsync(tray.ToEntity());
tray.Id = identity;
_cache.TryAdd(tray.TrayCode, tray);
return tray;
}
public async Task<int> GetIdByCode(string code)
{
try
{
return _cache[code].Id;
}
catch
{
return (await _repository.GetFirstAsync(d => d.TrayCode == code)).Id;
}
}
public async Task<Tray> GetByCode(string code)
{
return Tray.FromEntity(await _repository.GetSingleAsync(t => t.TrayCode == code));
}
public Task StorageAsync(string trayCode, int stackHeight, int controlNo, MST_ITEM_2240_V itemInfo)
{
throw new NotImplementedException();
}
public async Task<Tray> PrintTrayAsync(string trayCode, int stackHeight, int controlNo, MST_ITEM_2240_V itemInfo)
{
Tray tray;
try
{
tray = _cache[trayCode];
}
catch
{
tray = await GetByCode(trayCode);
}
tray.ControlNo = controlNo;
tray.StackHeight = stackHeight;
tray.Grade = itemInfo.GRADE;
tray.Type = itemInfo.TYPE;
tray.DenFila = itemInfo.DEN_FILA;
tray.DtexFila = itemInfo.DTEX_FILA;
tray.Unit = itemInfo.UNIT;
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";
await _repository.UpdateAsync(tray.ToEntity());
_cache.Remove(tray.TrayCode, out _);
return tray;
}
public async Task StorageAsync(string trayCode)
{
var tray = await _repository.GetFirstAsync(d => d.TrayCode == trayCode);
tray.FinishTime = DateTime.Now;
await _repository.UpdateAsync(tray);
_cache.TryRemove(tray.TrayCode, out _);
}
}