using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; 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.FriendlyException; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Interfaces; using Mapster; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Consts; using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Dto.Inputs; using Tnb.WarehouseMgr.Entities.Entity; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr { /// /// 调拨出库单(原材料) /// [OverideVisualDev(ModuleConsts.MODULE_WmsMaterialSignH_ID)] public class WmsMaterialSignHService : BaseWareHouseService { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; private readonly IBillRullService _billRullService; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; private readonly IWmsPDAScanInStockService _wmsPDAScanInStock; private readonly IWmsCarryUnbindService _wmsCarryUnbindService; private readonly IWareHouseService _wareHouseService; public WmsMaterialSignHService( ISqlSugarRepository repository, IUserManager userManager, IBillRullService billRullService, IRunService runService, IVisualDevService visualDevService, IWmsPDAScanInStockService wmsPDAScanInStock, IWmsCarryUnbindService wmsCarryUnbindService, IWareHouseService wareHouseService) { _db = repository.AsSugarClient(); _userManager = userManager; _billRullService = billRullService; _runService = runService; _visualDevService = visualDevService; _wmsPDAScanInStock = wmsPDAScanInStock; _wmsCarryUnbindService = wmsCarryUnbindService; _wareHouseService = wareHouseService; } /// /// 物料签收 /// /// /// /// /// [HttpPost] public async Task MaterialSign(MaterialSignInput input) { try { if (input == null) { throw new ArgumentNullException(nameof(input)); } WmsCarryH wmsCarryH = await _db.Queryable().Where(r => r.carry_code == input.carry_code).FirstAsync(); if (wmsCarryH == null) { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}不存在", 500); } if (wmsCarryH.is_lock == 1) { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}已锁定", 500); } if (wmsCarryH.carry_status != "1") { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}未绑定物料", 500); } if (string.IsNullOrEmpty(wmsCarryH.location_id)) { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}当前库位为空,无法签收", 500); } List wmsCarryCodes = _db.Queryable().Where(r => r.carry_id == wmsCarryH.id).ToList(); WmsMaterialSignH wmsMaterialSignH = new WmsMaterialSignH(); wmsMaterialSignH.create_id = input.create_id; wmsMaterialSignH.create_time = DateTime.Now; wmsMaterialSignH.carry_id = wmsCarryH.id; wmsMaterialSignH.carry_code = wmsCarryH.carry_code; List wmsMaterialSignDs = new List(); await _db.Ado.BeginTranAsync(); foreach (var item in input.details) { if (item.sign_qty < 0) { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}签收数量不能为空", 500); } WmsCarryCode wmsCarryCode = wmsCarryCodes.Where(r => r.id == item.carry_code_id).First(); if (wmsCarryCode == null) { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code} 载具物料明细id {item.carry_code_id}不存在", 500); } if (item.sign_qty > wmsCarryCode.codeqty) { throw new AppFriendlyException($"【MaterialSign】载具{input.carry_code}签收数量{item.sign_qty}超过了可签收数量{wmsCarryCode.codeqty}", 500); } WmsMaterialSignD wmsMaterialSignD = new WmsMaterialSignD(); wmsMaterialSignD.bill_id = wmsMaterialSignH.id; wmsMaterialSignD.create_id = input.create_id; wmsMaterialSignD.create_time = DateTime.Now; wmsMaterialSignD.barcode = wmsCarryCode.barcode; wmsMaterialSignD.material_id = wmsCarryCode.material_id; BasMaterial basMaterial = await _db.Queryable().Where(r => r.code == wmsCarryCode.material_code).FirstAsync(); if (basMaterial == null) { throw new AppFriendlyException($"【MaterialSign】物料{wmsCarryCode.material_code} 基础资料不存在", 500); } wmsMaterialSignD.material_code = basMaterial.code; wmsMaterialSignD.material_name = basMaterial.name; wmsMaterialSignD.material_specification = basMaterial.material_specification; wmsMaterialSignD.code_batch = wmsCarryCode.code_batch; wmsMaterialSignD.qty = wmsCarryCode.codeqty; wmsMaterialSignD.sign_qty = item.sign_qty; wmsMaterialSignDs.Add(wmsMaterialSignD); wmsCarryCode.codeqty = wmsCarryCode.codeqty - item.sign_qty; if (wmsCarryCode.codeqty == 0) { CarryCodeUnbindCodeInput carryCodeUnbindCodeInput = new CarryCodeUnbindCodeInput(); await _wmsCarryUnbindService.CarryCodeUnbindCode(carryCodeUnbindCodeInput, _db); } await _db.Updateable().SetColumns(r => r.codeqty == wmsCarryCode.codeqty) .Where(r => r.barcode == wmsCarryCode.barcode).ExecuteCommandAsync(); } BasLocation carryLoc = await _db.Queryable().Where(r => r.id == wmsCarryH.location_id).FirstAsync(); if (carryLoc != null && carryLoc.is_type == "0") { throw new Exception($"托盘{wmsCarryH.carry_code}在存储库位中,不能签收!"); } switch (carryLoc.wh_id) { case WmsWareHouseConst.WAREHOUSE_YCL_ID: { await _db.Updateable(wmsCarryCodes).ExecuteCommandAsync(); await _wareHouseService.s_taskExecuteSemaphore_YCLInstock.WaitAsync(); wmsMaterialSignH.warehouse_sign_id = WmsWareHouseConst.WAREHOUSE_YCL_ID; wmsMaterialSignH.warehouse_instock_id = WmsWareHouseConst.WAREHOUSE_YCL_ID; try { InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = "1", Size = 1, AvoidBusyPassage = true, Region_id = WmsWareHouseConst.REGION_YCLCache_ID }; List endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput); if (endLocations.Count == 0) { throw new AppFriendlyException("没有可以回库的库位", 500); } CommonCreatePretaskInput commonCreatePretaskInput = new CommonCreatePretaskInput(); commonCreatePretaskInput.startlocation_id = carryLoc.id; commonCreatePretaskInput.endlocation_id = endLocations[0].id; commonCreatePretaskInput.carry_id = wmsCarryH.id; commonCreatePretaskInput.carry_code = wmsCarryH.carry_code; commonCreatePretaskInput.task_type = WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID; commonCreatePretaskInput.biz_type = WmsWareHouseConst.BIZTYPE_WmsMaterialSign_ID; Logger.LogInformation($"【MaterialSign】 开始生成原材料仓回库任务 起点{carryLoc.location_code} 终点{endLocations[0].location_code} 托盘 {wmsCarryH.carry_code}"); Entities.Dto.Outputs.Result res = await _wareHouseService.CommonCreatePretask(commonCreatePretaskInput, _db); if (res.code != HttpStatusCode.OK) { throw new AppFriendlyException(res.msg, 500); } } catch (Exception ex) { throw; } finally { _wareHouseService.s_taskExecuteSemaphore_YCLInstock.Release(); } break; } } await _db.Insertable(wmsMaterialSignH).ExecuteCommandAsync(); await _db.Insertable(wmsMaterialSignDs).ExecuteCommandAsync(); await _db.Ado.CommitTranAsync(); } catch (Exception ex) { Logger.LogError($"【MaterialSign】 {ex.Message}"); Logger.LogError($"【MaterialSign】 {ex.StackTrace}"); await _db.Ado.RollbackTranAsync(); throw new AppFriendlyException($"【MaterialSign】物料签收失败 {ex.Message}", 500); } } } }