using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aop.Api.Domain; using JNPF.Common.Core.Manager; using JNPF.Common.Dtos.VisualDev; using JNPF.Common.Enums; using JNPF.Common.Extension; using JNPF.Common.Security; using JNPF.FriendlyException; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Entitys; using JNPF.VisualDev.Interfaces; using Mapster; using Microsoft.AspNetCore.Mvc; 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.Entities.Dto.Inputs; using Tnb.WarehouseMgr.Entities.Entity; using Tnb.WarehouseMgr.Entities.Enums; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr { /// /// 空载具出库 /// [OverideVisualDev(ModuleConsts.MODULE_WMSEMPTYOUTSTK_ID)] [ServiceModule(BizTypeId)] public class WmsEmptyOutstockService : BaseWareHouseService, IWmsEmptyOutstockService { private const string BizTypeId = "26122265173285"; 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 WmsEmptyOutstockService( ISqlSugarRepository repository, IRunService runService, IVisualDevService visualDevService, IWareHouseService wareHouseService, IUserManager userManager, IBillRullService billRullService, ITaskMessageNotify taskMessageNotify ) : base(taskMessageNotify.Writer) { _db = repository.AsSugarClient(); _runService = runService; _visualDevService = visualDevService; _wareHouseService = wareHouseService; _userManager = userManager; _billRullService = billRullService; OverideFuncs.CreateAsync = WmsEmptyOut; } private async Task WmsEmptyOut(VisualDevModelDataCrInput input) { try { await _db.Ado.BeginTranAsync(); /*//入库取终点 var inStockStrategyInput = new InStockStrategyQuery { warehouse_id = input.data[nameof(InStockStrategyQuery.warehouse_id)].ToString(), Size = 1 }; var endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);*/ //判断目标库位是否自动签收 var loc = await _db.Queryable().SingleAsync(it => it.id == input.data[nameof(WmsPointH.location_id)].ToString()); //出库取起点,获取所有符合输入的载具规格的载具 var OutStockStrategyInput = new OutStockStrategyQuery { carrystd_id = input.data[nameof(OutStockStrategyQuery.carrystd_id)].ToString(), warehouse_id = input.data[nameof(OutStockStrategyQuery.warehouse_id)].ToString(), }; var carrys = await _wareHouseService.OutStockStrategy(OutStockStrategyInput); //var carrys = await _db.Queryable().LeftJoin((a, b) => a.location_id == b.id) // .Where((a, b) => a.carrystd_id == input.data[nameof(WmsEmptyOutstockH.carrystd_id)].ToString() // && a.carry_status == ((int)EnumCarryStatus.空闲).ToString() && a.is_lock == 0 && b.is_lock == 0 && b.is_type == ((int)EnumLocationType.存储库位).ToString()) // .ToListAsync(); // 判断最终目标库位是否可以放置当前载具 if (carrys?.Count > 0) { var curCarry = carrys[^carrys.Count]; var isMatch = await IsCarryAndLocationMatchByCarryStd(curCarry, loc); if (!isMatch) throw new AppFriendlyException("该载具无法放置到目标库位", 500); VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSEMPTYOUTSTK_ID, true); await _runService.Create(templateEntity, input); } var setQty = await _db.Queryable().FirstAsync(it => it.bill_code == input.data[nameof(WmsEmptyOutstockH.bill_code)].ToString()); 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() && it.is_lock == 0); } //根据每个载具的起始库位做路径运算 if (carrys?.Count > 0) { int min = (carrys.Count > setQty.qty) ? setQty.qty : carrys.Count; for (int i = 0; i < min; i++) { sPoint = await _db.Queryable().FirstAsync(it => it.location_id == carrys[i].location_id); var isOk = false; if (sPoint != null && ePoint != null) { var points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id); if (points.Count <= 2) throw new AppFriendlyException("该路径不存在", 500); //根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序) else { var preTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it => { var sPoint = it.FirstOrDefault(); var 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_WMSEPTYOUTSTK_ID, task_type = WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID, carry_id = carrys![i].id, carry_code = carrys![i].carry_code, area_id = sPoint?.area_id!, area_code = it.Key, require_id = input.data["ReturnIdentity"].ToString(), require_code = input.data[nameof(preTask.bill_code)]?.ToString()!, create_id = _userManager.UserId, create_time = DateTime.Now }; return preTask; }).ToList(); //更新页面 //赋值签收状态 if (loc.is_sign == 0) { preTasks[^1].is_sign = 0; // 修改最后一个元素的是否签收值 } isOk = await _wareHouseService.GenPreTask(preTasks, null!); } if (isOk) { GenPreTaskUpInput preTaskUpInput = new() { RquireId = input.data["ReturnIdentity"].ToString()!, CarryId = carrys![i].id, CarryStartLocationId = points!.FirstOrDefault()!.location_id!, CarryStartLocationCode = points!.FirstOrDefault()!.location_code!, LocationIds = points!.Select(x => x.location_id).ToList()! }; //更新明细表 WmsEmptyOutstockD wmsEmptyOutstockD = new() { id = SnowflakeIdHelper.NextId(), bill_id = preTaskUpInput.RquireId, biz_type = WmsWareHouseConst.BIZTYPE_WMSEPTYOUTSTK_ID, location_id = ePoint.location_id!, status = WmsWareHouseConst.BILLSTATUS_ON_ID, carry_id = carrys[i].id, carry_code = carrys[i].carry_code, create_id = _userManager.UserId, create_time = DateTime.Now }; await _db.Insertable(wmsEmptyOutstockD) .ExecuteCommandAsync(); //根据空载具出库Id,回更单据状态 await _db.Updateable().SetColumns(it => new WmsEmptyOutstockH { 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 }); } } else { throw new AppFriendlyException("无可出库的空载具或目标库位不存在或者目标库位被锁", 500); } } } await _db.Ado.CommitTranAsync(); } catch (Exception) { await _db.Ado.RollbackTranAsync(); throw; } finally { await Publish(nameof(IWareHouseService.GenTaskExecute)); } return Task.FromResult(true); } public async override Task ModifyAsync(WareHouseUpInput input) { if (input == null) throw new ArgumentNullException(nameof(input)); try { await _db.Ado.BeginTranAsync(); //根据载具更新明细表状态 var isOk = await _db.Updateable().SetColumns(it => new WmsEmptyOutstockD { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID }).Where(it => it.bill_id == input.requireId && input.carryIds.Contains(it.carry_id)).ExecuteCommandHasChangeAsync(); var emptyCarrys = await _db.Queryable().Where(it => it.bill_id == input.requireId).ToListAsync(); // 判断所有明细是否都完成 if (emptyCarrys?.Count > 0 && emptyCarrys.All(x => x.status == WmsWareHouseConst.BILLSTATUS_COMPLETE_ID)) { isOk = await _db.Updateable().SetColumns(it => new WmsEmptyOutstockH { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID }).Where(it => it.id == input.requireId).ExecuteCommandHasChangeAsync(); } if (!isOk) throw Oops.Oh(ErrorCode.COM1001); await _db.Ado.CommitTranAsync(); } catch (Exception) { await _db.Ado.RollbackTranAsync(); throw; } } /// /// MES空载具出库接口 /// /// /// [HttpPost] public async Task MESEmptyCarryOutStk(MESEmptyCarryOutStkInput input) { if (input.IsNull()) throw new ArgumentNullException("input"); try { var location = await _db.Queryable().SingleAsync(it => it.location_code == input.location_code && it.is_type != EnumLocationType.存储库位.ToString()) ?? throw new AppFriendlyException("无此库位或为存储库位", 500); Dictionary dic = new() { [nameof(WmsEmptyOutstockH.id)] = SnowflakeIdHelper.NextId(), [nameof(WmsEmptyOutstockH.org_id)] = input.org_id, [nameof(WmsEmptyOutstockH.location_id)] = location.id, [nameof(WmsEmptyOutstockH.carrystd_id)] = input.carrystd_id, [nameof(WmsEmptyOutstockH.bill_code)] = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_EMPTYOUTSTK_ENCODE).GetAwaiter().GetResult(), [nameof(WmsEmptyOutstockH.status)] = WmsWareHouseConst.BILLSTATUS_ADD_ID, [nameof(WmsEmptyOutstockH.qty)] = input.qty, [nameof(WmsEmptyOutstockH.biz_type)] = WmsWareHouseConst.BIZTYPE_WMSEPTYOUTSTK_ID, [nameof(WmsEmptyOutstockH.create_id)] = input.create_id, [nameof(WmsEmptyOutstockH.create_time)] = DateTime.Now }; VisualDevModelDataCrInput visualDevModelDataCrInput = new VisualDevModelDataCrInput { data = dic, }; await WmsEmptyOut(visualDevModelDataCrInput); } catch (Exception ex) { await _db.Ado.RollbackTranAsync(); return await ToApiResult(JNPF.Common.Enums.HttpStatusCode.InternalServerError, ex.Message); } return ToApiResult(); } } }