2025-03-23 13:09:36 +08:00

122 lines
4.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;
using SqlSugar;
namespace Seyounth.Hyosung.Data.Services;
public class TrayService : ITrayService
{
//private readonly ConcurrentDictionary<string, Tray> _cache = new();
// private readonly <TrayEntity> _repository;
private readonly IHyosungWmsService _hyosungWmsService;
private readonly ISqlSugarClient _db;
public TrayService(IServiceProvider provider, IHyosungWmsService hyosungWmsService,ISqlSugarClient db)
{
_db = db;
// _repository = provider.CreateScope().ServiceProvider.GetRequiredService<IRepository<TrayEntity>>();
//_repository = provider.GetService<IRepository<TrayEntity>>();
// var trays = _db.Queryable<TrayEntity>().Where(t => t.ControlNo == null).ToListAsync().Result;
_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 _db.CopyNew().Queryable<TrayEntity>().CountAsync(t => t.VarietyId == tray.VarietyId);
tray.IsEven = count % 2 == 0;
var identity = await _db.CopyNew().Insertable<TrayEntity>(tray.ToEntity()).ExecuteReturnIdentityAsync();
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 _db.CopyNew().Queryable<TrayEntity>().Where(d => d.TrayCode == code).FirstAsync()).Id;
// }
}
public async Task<Tray> GetByCode(string code)
{
return Tray.FromEntity(await _db.CopyNew().Queryable<TrayEntity>().Where(t => t.TrayCode == code).FirstAsync());
}
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 controlNo, MST_ITEM_2240_V itemInfo)
{
Tray tray;
//try
//{
// tray = _cache[trayCode];
//}
//catch
//{
tray = await GetByCode(trayCode);
//}
tray.ControlNo = controlNo;
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 _db.Updateable<TrayEntity>(tray.ToEntity()).ExecuteCommandAsync();
// _cache.Remove(tray.TrayCode, out _);
return tray;
}
public async Task StorageAsync(string trayCode)
{
var tray = await _db.CopyNew().Queryable<TrayEntity>().Where(d => d.TrayCode == trayCode).FirstAsync();
tray.FinishTime = DateTime.Now;
tray.IsPacking = true;
await _db.CopyNew().Updateable<TrayEntity>(tray).ExecuteCommandAsync();
// _cache.TryRemove(tray.TrayCode, out _);
}
public async Task UpdateHeightAsync(string trayCode, int height)
{
await _db.CopyNew().Updateable<TrayEntity>()
.Where(x => x.TrayCode == trayCode)
.SetColumns(x => x.StackHeight, height)
.SetColumns(x => x.IsPacking, false)
.ExecuteCommandAsync();
}
public async Task<Tray> GetIsPacking()
{
var tray= await _db.CopyNew().Queryable<TrayEntity>().Where(x => x.IsPacking!=null&&x.IsPacking.Value).FirstAsync();
return Tray.FromEntity(tray);
}
}