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.Extension; using JNPF.Common.Security; using JNPF.FriendlyException; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.ProductionMgr.Interfaces; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Attributes; using Tnb.WarehouseMgr.Entities.Consts; using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Enums; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr { /// /// PDA扫码入库模块 /// [OverideVisualDev(ModuleConsts.MODULE_WMSSCANCODEINSTOCKPDA_ID)] [ServiceModule(BizTypeId)] public class WmsPDAScanInStockService : BaseWareHouseService, IPdaStroage { private const string BizTypeId = "26191496816421"; private readonly ISqlSugarClient _db; private readonly IDictionaryDataService _dictionaryDataService; private readonly IUserManager _userManager; private readonly IWareHouseService _wareHouseService; private readonly IBillRullService _billRullService; private readonly IPrdInstockService _prdInstockService; public WmsPDAScanInStockService( ISqlSugarRepository repository, IDictionaryDataService dictionaryDataService, IUserManager userManager, IBillRullService billRullService, IWareHouseService wareHouseService, IPrdInstockService prdInstockService, ITaskMessageNotify taskMessageNotify ) : base(taskMessageNotify.Writer) { _db = repository.AsSugarClient(); _dictionaryDataService = dictionaryDataService; _userManager = userManager; _billRullService = billRullService; _wareHouseService = wareHouseService; _prdInstockService = prdInstockService; OverideFuncs.CreateAsync = ScanInStock; } public async Task ScanInStock(VisualDevModelDataCrInput input) { try { await _db.Ado.BeginTranAsync(); var item = input.data.Adapt(); if (item.codeqty == 0) throw new AppFriendlyException("请输入入库数量", 500); var carryCode = item.barcode; var carry = await _db.Queryable().FirstAsync(it => it.carry_code == carryCode); if (carry == null) throw new AppFriendlyException("载具有误", 500); var mat = await _db.Queryable().FirstAsync(it => it.code == item.material_code); if (mat == null) throw new AppFriendlyException("物料有误", 500); var loc = await _db.Queryable().FirstAsync(it => it.location_code == item.extras); if (loc == null) throw new AppFriendlyException("库位有误", 500); WmsInstockH instock = new() { id = SnowflakeIdHelper.NextId(), org_id = _userManager.UserId, carry_code = carryCode, carry_id = carry.id, location_id = loc.id, bill_code = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_INSTOCK_ENCODE).GetAwaiter().GetResult(), bill_type = WmsWareHouseConst.BILLTYPE_MATERIALINSTOCK_ID, biz_type = WmsWareHouseConst.BIZTYPE_WMSINSTOCK_ID, bill_date = DateTime.Today, warehouse_id = "26103372441637", status = WmsWareHouseConst.BILLSTATUS_ADD_ID, generate_type = "0", sync_status = WmsWareHouseConst.SYNC_STATUS_NONEEDSYNC, print_status = WmsWareHouseConst.PRINT_STATUS_PRINTCOMPLETE, is_check = 1, create_id = _userManager.UserId, create_time = DateTime.Now, }; WmsInstockD instockD = new() { id = SnowflakeIdHelper.NextId(), bill_id = instock.id, line_status = WmsWareHouseConst.BILLSTATUS_ADD_ID, material_id = mat.id, material_code = mat.code, unit_id = mat.unit_id, pr_qty = item.codeqty, qty = 0, code_batch = item.code_batch, warehouse_id = "26103372441637", print_qty = item.codeqty, scan_qty = item.codeqty, print_id = "", create_id = _userManager.UserId, create_time = DateTime.Now, }; WmsInstockCode instockCode = new() { id = SnowflakeIdHelper.NextId(), bill_id = instock.id, bill_d_id = instockD.id, line_status = WmsWareHouseConst.BILLSTATUS_ADD_ID, material_id = mat.id, material_code = mat.code, unit_id = mat.unit_id, barcode = carryCode, code_batch = item.code_batch, codeqty = item.codeqty, is_lock = 0, is_end = 0, create_id = _userManager.UserId, create_time = DateTime.Now, }; await _db.Insertable(instock).ExecuteCommandAsync(); await _db.Insertable(instockD).ExecuteCommandAsync(); await _db.Insertable(instockCode).ExecuteCommandAsync(); var inStockStrategyInput = new InStockStrategyQuery { warehouse_id = "26103372441637", Size = 1 }; var endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput); WmsPointH sPoint = new(); WmsPointH ePoint = new(); if (endLocations?.Count > 0) { var eloc = await _db.Queryable().SingleAsync(it => it.id == endLocations[0].id); var isMatch = await IsCarryAndLocationMatchByCarryStd(carry, eloc); if (!isMatch) throw new AppFriendlyException("库位与载具规格不匹配", 500); ePoint = await _db.Queryable().FirstAsync(it => it.location_id == endLocations[0].id); } sPoint = await _db.Queryable().FirstAsync(it => it.location_id == loc.id); if (sPoint != null && ePoint != null) { var points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id); if (points.Count <= 2) throw new AppFriendlyException("该路径不存在", 500); //根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序) if (points?.Count > 0) { 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(); preTask.org_id = _userManager.User.OrganizeId!; preTask.startlocation_id = sPoint?.location_id!; preTask.startlocation_code = sPoint?.location_code!; preTask.endlocation_id = ePoint?.location_id!; preTask.endlocation_code = ePoint?.location_code!; preTask.start_floor = sPoint?.floor.ToString(); preTask.end_floor = ePoint?.floor.ToString(); preTask.startpoint_id = sPoint?.id!; preTask.startpoint_code = sPoint?.point_code!; preTask.endpoint_id = ePoint?.id!; preTask.endpoint_code = ePoint?.point_code!; preTask.bill_code = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(); preTask.status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID; preTask.biz_type = WmsWareHouseConst.BIZTYPE_WMSINSTOCK_ID; preTask.task_type = WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID; preTask.carry_id = carry.id; preTask.carry_code = carry.carry_code; preTask.area_id = sPoint?.area_id!; preTask.area_code = it.Key; preTask.require_id = instock.id; preTask.require_code = instock.bill_code; preTask.create_id = _userManager.UserId; preTask.create_time = DateTime.Now; return preTask; }).ToList(); List pretaskCodes = new(); foreach (var pt in preTasks) { WmsPretaskCode ptc = pt.Adapt(); ptc.id = SnowflakeIdHelper.NextId(); ptc.bill_id = pt.id; ptc.material_id = instockCode.material_id; ptc.material_code = instockCode.material_code; ptc.barcode = instockCode.barcode; ptc.codeqty = instockCode.codeqty; ptc.unit_id = instockCode.unit_id; ptc.code_batch = instockCode.code_batch; pretaskCodes.Add(ptc); } var isOk = await _wareHouseService.GenPreTask(preTasks, pretaskCodes); if (isOk) { var preTaskUpInput = new GenPreTaskUpInput(); preTaskUpInput.RquireId = instock.id; preTaskUpInput.CarryId = carry.id; preTaskUpInput.CarryStartLocationId = points.FirstOrDefault()!.location_id!; preTaskUpInput.CarryStartLocationCode = points.FirstOrDefault()!.location_code!; preTaskUpInput.LocationIds = points.Select(x => x.location_id).ToList()!; //生成操作记录 WmsHandleH handleH = new(); handleH.org_id = _userManager.User.OrganizeId; handleH.startlocation_id = loc.id; handleH.endlocation_id = endLocations![0].id; handleH.bill_code = instock.bill_code; handleH.biz_type = instock.biz_type; handleH.carry_id = carry.id; handleH.carry_code = carry.carry_code; handleH.require_id = instock.id; handleH.require_code = instock.bill_code; handleH.create_id = _userManager.UserId; handleH.create_time = DateTime.Now; preTaskUpInput.PreTaskRecord = handleH; //生成操作记录条码表 WmsHandleCode handleCode = instockCode.Adapt(); handleCode.id = SnowflakeIdHelper.NextId(); handleCode.org_id = _userManager.User.OrganizeId; handleCode.bill_id = handleH.id; handleCode.create_id = _userManager.UserId; handleCode.create_time = DateTime.Now; preTaskUpInput.PreTaskHandleCodes.Add(handleCode); //生成载具条码表 WmsCarryCode wmsCarryCode = instockCode.Adapt(); { wmsCarryCode.id = SnowflakeIdHelper.NextId(); wmsCarryCode.carry_id = carry.id; wmsCarryCode.is_out = 0; wmsCarryCode.location_id = loc.id; wmsCarryCode.location_code = loc.location_code; wmsCarryCode.warehouse_id = instock.warehouse_id; } await _db.Insertable(wmsCarryCode).ExecuteCommandAsync(); //回更状态 await _wareHouseService.GenInStockTaskHandleAfter(preTaskUpInput, it => new WmsCarryH { carry_code = instock!.carry_code!, is_lock = 1, carry_status = ((int)EnumCarryStatus.占用).ToString(), location_id = preTaskUpInput.CarryStartLocationId, location_code = preTaskUpInput.CarryStartLocationCode }, it => new BasLocation { is_lock = 1, is_use = ((int)EnumCarryStatus.占用).ToString() }); await _db.Updateable().SetColumns(it => new WmsInstockD { line_status = WmsWareHouseConst.BILLSTATUS_ON_ID }).Where(it => instockCode.bill_d_id == it.id).ExecuteCommandAsync(); await _db.Updateable().SetColumns(it => new WmsInstockH { status = WmsWareHouseConst.BILLSTATUS_ON_ID }).Where(it => it.id == instock!.id).ExecuteCommandAsync(); } } } await _db.Ado.CommitTranAsync(); } catch (Exception) { await _db.Ado.RollbackTranAsync(); throw; } return Task.FromResult(true); } } }