using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.EventBus;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Mapster;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Attributes;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
///
/// 载具移出
///
[OverideVisualDev(ModuleConsts.MODULE_WMSCARRYMOOUTSTK_ID)]
[ServiceModule(BizTypeId)]
public class WmsCarryMoveOutStockService : BaseWareHouseService
{
private const string BizTypeId = "26122271183141";
private readonly ISqlSugarClient _db;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IWareHouseService _wareHouseService;
private readonly IBillRullService _billRullService;
private readonly IUserManager _userManager;
public WmsCarryMoveOutStockService(
ISqlSugarRepository repository,
IRunService runService,
IVisualDevService visualDevService,
IWareHouseService wareHouseService,
IUserManager userManager,
IBillRullService billRullService,
IEventPublisher eventPublisher
)
{
_db = repository.AsSugarClient();
_runService = runService;
_visualDevService = visualDevService;
_wareHouseService = wareHouseService;
_userManager = userManager;
_billRullService = billRullService;
OverideFuncs.CreateAsync = CarryMoveOut;
}
private async Task CarryMoveOut(VisualDevModelDataCrInput input)
{
try
{
await _db.Ado.BeginTranAsync();
//判断目标库位是否自动签收
BasLocation loc = await _db.Queryable().SingleAsync(it => it.id == input.data[nameof(WmsPointH.location_id)].ToString());
WmsCarryH carry = await _db.Queryable().FirstAsync(it => it.id == input.data[nameof(WmsMoveOutstock.carry_id)].ToString());
bool isMatch = await IsCarryAndLocationMatchByCarryStd(carry, loc);
if (!isMatch)
{
throw new AppFriendlyException("该载具无法放置到目标库位", 500);
}
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSCARRYMOOUTSTK_ID, true);
await _runService.Create(templateEntity, input);
//入库取终点
//var OutStockStrategyInput = new OutStockStrategyQuery { carry_id = input.data[nameof(OutStockStrategyQuery.carry_id)].ToString(), Size = 1 };
//var outStkCarry = await _wareHouseService.OutStockStrategy(OutStockStrategyInput);
WmsPointH? sPoint = null;
WmsPointH? ePoint = null;
if (input.data.ContainsKey(nameof(WmsPointH.location_id)))
{
ePoint = await _db.Queryable().FirstAsync(it => it.location_id == input.data[nameof(WmsPointH.location_id)].ToString());
}
if (carry != null)
{
sPoint = await _db.Queryable().FirstAsync(it => it.location_id == carry.location_id);
}
if (sPoint != null && ePoint != null)
{
List points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id);
//根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序)
if (points?.Count > 0)
{
if (points.Count <= 2)
{
throw new AppFriendlyException($"sPoint {sPoint.point_code} ePoint{ePoint.point_code}该路径不存在", 500);
}
List preTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it =>
{
WmsPointH? sPoint = it.FirstOrDefault();
WmsPointH? ePoint = it.LastOrDefault();
WmsPretaskH preTask = new()
{
org_id = _userManager.User.OrganizeId,
startlocation_id = sPoint?.location_id!,
startlocation_code = sPoint?.location_code!,
endlocation_id = ePoint?.location_id!,
endlocation_code = ePoint?.location_code!,
start_floor = sPoint?.floor.ToString(),
end_floor = ePoint?.floor.ToString(),
startpoint_id = sPoint?.id!,
startpoint_code = sPoint?.point_code!,
endpoint_id = ePoint?.id!,
endpoint_code = ePoint?.point_code!,
bill_code = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(),
status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID,
biz_type = WmsWareHouseConst.BIZTYPE_WMSMOOUTSTK_ID,
task_type = WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID
};
preTask.carry_id = input.data[nameof(preTask.carry_id)]?.ToString()!;
preTask.carry_code = input.data[nameof(preTask.carry_code)]?.ToString()!;
preTask.area_id = sPoint?.area_id!;
preTask.area_code = it.Key;
preTask.require_id = input.data["ReturnIdentity"].ToString();
preTask.require_code = input.data[nameof(preTask.bill_code)]?.ToString()!;
preTask.create_id = _userManager.UserId;
preTask.create_time = DateTime.Now;
return preTask;
}).ToList();
if (loc.is_sign == 0)
{
preTasks[^1].is_sign = 0; // 修改最后一个元素的是否签收值
}
bool isOk = await _wareHouseService.GenPreTask(preTasks, null!);
if (isOk)
{
GenPreTaskUpInput preTaskUpInput = new()
{
RquireId = input.data["ReturnIdentity"].ToString()!,
CarryId = input.data[nameof(WmsCarryD.carry_id)]?.ToString()!,
CarryStartLocationId = points.FirstOrDefault()!.location_id!,
CarryStartLocationCode = points.FirstOrDefault()!.location_code!,
LocationIds = points.Select(x => x.location_id).ToList()!,
PreTaskRecords = preTasks.Adapt>()
};
preTaskUpInput.PreTaskRecords.ForEach(x => x.id = SnowflakeIdHelper.NextId());
//根据载具移出Id,回更单据状态
_ = await _db.Updateable().SetColumns(it => new WmsMoveOutstock { status = WmsWareHouseConst.BILLSTATUS_ON_ID }).Where(it => it.id == preTaskUpInput.RquireId).ExecuteCommandAsync();
await _wareHouseService.GenInStockTaskHandleAfter(preTaskUpInput,
it => new WmsCarryH { is_lock = 1 },
it => new BasLocation { is_lock = 1 });
}
}
}
await _db.Ado.CommitTranAsync();
}
catch (Exception)
{
await _db.Ado.RollbackTranAsync();
throw;
}
finally
{
await InvokeGenPretaskExcute();
}
return Task.FromResult(true);
}
public override async Task ModifyAsync(WareHouseUpInput input)
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
bool isOk = await _db.Updateable().SetColumns(it => new WmsMoveOutstock { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID }).Where(it => it.id == input.requireId).ExecuteCommandHasChangeAsync();
if (!isOk)
{
throw Oops.Oh(ErrorCode.COM1001);
}
}
}
}