using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Enums;
using JNPF.FriendlyException;
using JNPF.Logging;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using SqlSugar;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
///
/// PDA投料记录
///
[OverideVisualDev(ModuleConsts.MODULE_WMSFEEDINGRECORDPDA_ID)]
public class WmsPDAFeedingService : BaseWareHouseService, IPdaStroage
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
private readonly IBillRullService _billRullService;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IWmsCarryService _carryService;
public WmsPDAFeedingService(
ISqlSugarRepository repository,
IUserManager userManager,
IBillRullService billRullService,
IRunService runService,
IVisualDevService visualDevService,
IWmsCarryService carryService)
{
_db = repository.AsSugarClient();
_userManager = userManager;
_billRullService = billRullService;
_runService = runService;
_visualDevService = visualDevService;
_carryService = carryService;
OverideFuncs.CreateAsync = WmsPDAFeedingRecord;
}
///
/// PDA投料记录
///
///
/// 输入参数:
///
{
///
old_carry_id: 料箱ID
///
new_carry_id:新载具ID
///
}
///
///
///
private async Task WmsPDAFeedingRecord(VisualDevModelDataCrInput input)
{
bool isOk = false;
try
{
await _db.Ado.BeginTranAsync();
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSFEEDINGRECORDPDA_ID, true);
await _runService.Create(templateEntity, input);
string? carryId = input.data.ContainsKey("carry_id") ? input.data["carry_id"]?.ToString() : "";
string? feedBoxCode = input.data.ContainsKey("feedbox_code") ? input.data["feedbox_code"]?.ToString() : "";
WmsCarryH carry = await _db.Queryable().SingleAsync(it => it.id == carryId);
WmsFeedbox? feedBox = await _db.Queryable().SingleAsync(it => it.feedbox_code == feedBoxCode);
WmsCarryMat? carryMaterial = await _db.Queryable().FirstAsync(it => it.carry_id == carryId);
List carryCodes = await _db.Queryable().Where(it => it.carry_id == carryId).ToListAsync();
if (carryMaterial != null && feedBox != null && carry != null)
{
//更新投料箱
feedBox.material_id = carryMaterial.material_id;
feedBox.material_code = carryMaterial.material_code;
feedBox.qty = carryMaterial.qty;
feedBox.batch = carryMaterial?.code_batch!;
feedBox.modify_id = _userManager.UserId;
feedBox.modify_time = DateTime.Now;
int row = await _db.Updateable(feedBox)
.UpdateColumns(it => new
{
it.material_id,
it.material_code,
it.qty,
it.batch,
it.modify_id,
it.modify_time
}).ExecuteCommandAsync();
//更新载具
row = await _carryService.UpdateNullCarry(carry).Unwrap();
isOk = row > 0;
if (!isOk)
{
throw Oops.Oh(ErrorCode.COM1001);
}
}
else
{
if (carryMaterial == null)
{
throw new AppFriendlyException("没有可用的载具", 500);
}
if (feedBox == null)
{
throw new AppFriendlyException("没有可用的投料箱", 500);
}
}
await _db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
Log.Error("投料失败", ex);
await _db.Ado.RollbackTranAsync();
throw;
}
return isOk;
}
}
}