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 _cache = new(); private readonly IRepository _repository; private readonly IHyosungWmsService _hyosungWmsService; public TrayService(IServiceProvider provider, IHyosungWmsService hyosungWmsService) { _repository = provider.CreateScope().ServiceProvider.GetRequiredService>(); 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 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 GetIdByCode(string code) { try { return _cache[code].Id; } catch { return (await _repository.GetFirstAsync(d => d.TrayCode == code)).Id; } } public async Task 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 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; await _repository.UpdateAsync(tray.ToEntity()); _cache.Remove(tray.TrayCode, out _); } public async Task StorageAsync(string trayCode) { } }