using System.Text; using System.Text.RegularExpressions; using JNPF.Common.Contracts; 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.Logging; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Interfaces; using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tnb.Common.Utils; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Consts; using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Dto.Inputs; using Tnb.WarehouseMgr.Entities.Dto.Outputs; using Tnb.WarehouseMgr.Entities.Enums; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr { /// /// 载具服务 /// [OverideVisualDev(ModuleConsts.MODULE_WMSCARRYREPLACE_ID)] public class WmsCarryService : BaseWareHouseService, IWmsCarryService { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; private readonly IBillRullService _billRullService; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; public WmsCarryService( ISqlSugarRepository repository, IUserManager userManager, IBillRullService billRullService, IRunService runService, IVisualDevService visualDevService) { _db = repository.AsSugarClient(); _userManager = userManager; _billRullService = billRullService; _runService = runService; _visualDevService = visualDevService; OverideFuncs.CreateAsync = ExchangeCarry; } /// /// 根据载具ID获取载具信息 /// /// /// [HttpGet] public async Task GetCarryInfoById([FromRoute] string carryId) { CarryInfoOutput carryOutPut = await _db.Queryable().Where(it => it.id == carryId).Select().FirstAsync(); if (carryOutPut == null) { throw new AppFriendlyException("找不到载具", 500); } carryOutPut.carryDetails = await _db.Queryable().Where(it => it.carry_id == carryId).ToListAsync(); carryOutPut.carryCodes = await _db.Queryable().Where(it => it.carry_id == carryId).ToListAsync(); carryOutPut.carryMats = await _db.Queryable().Where(it => it.carry_id == carryId).ToListAsync(); return carryOutPut; } [HttpGet] public async Task GetCarryInfoByCode([FromRoute] string carrycode) { var carryOutPut = await _db.Queryable().Where(it => it.carry_code == carrycode).FirstAsync(); return carryOutPut; } /// /// 更换载具 /// /// /// 输入参数: ///
{ ///
old_carry_id:老载具id ///
new_carry_id:新载具ID ///
} /// /// /// private async Task ExchangeCarry(VisualDevModelDataCrInput input) { bool isOk = false; try { await _db.Ado.BeginTranAsync(); string? oldCarryId = input.data.ContainsKey("carry_id") ? input.data["carry_id"]?.ToString() : ""; string? newCarryId = input.data.ContainsKey("newcarry_id") ? input.data["newcarry_id"]?.ToString() : ""; WmsCarryH? oldCarry = await _db.Queryable().FirstAsync(it => it.id == oldCarryId); WmsCarryH? newCarry = await _db.Queryable().FirstAsync(it => it.id == newCarryId); if (oldCarry != null && newCarry != null) { ExChangeCarryInput carryInput = new() { old_carry_id = oldCarry.id, new_carry_id = newCarry.id }; isOk = await _updateSubCarry(carryInput); isOk = await _updateSubCarry(carryInput); isOk = await _updateSubCarry(carryInput); newCarry.status = oldCarry.status; newCarry.carry_status = oldCarry.carry_status; newCarry.location_id = oldCarry.location_id; newCarry.location_code = oldCarry.location_code; newCarry.is_lock = oldCarry.is_lock; newCarry.out_status = oldCarry.out_status; newCarry.is_check = oldCarry.is_check; newCarry.bale_num = oldCarry.bale_num; newCarry.collocation_scheme_id = oldCarry.collocation_scheme_id; newCarry.collocation_scheme_code = oldCarry.collocation_scheme_code; newCarry.source_id = oldCarry.source_id; newCarry.source_code = oldCarry.source_code; newCarry.create_id = _userManager.UserId; newCarry.create_time = DateTime.Now; int row = await _db.Updateable(newCarry).ExecuteCommandAsync(); WmsCarryReplaceH wmsCarryReplaceH = oldCarry.Adapt(); wmsCarryReplaceH.id = SnowflakeIdHelper.NextId(); wmsCarryReplaceH.org_id = _userManager.User.OrganizeId; wmsCarryReplaceH.bill_code = await _billRullService.GetBillNumber(WmsCarryConst.WMS_CARRY_REPLACE_ENCODE); wmsCarryReplaceH.carry_id = oldCarry.id; wmsCarryReplaceH.carry_code = oldCarry.carry_code; wmsCarryReplaceH.newcarry_id = newCarry.id; wmsCarryReplaceH.newcarry_code = newCarry.carry_code; wmsCarryReplaceH.create_id = _userManager.UserId; wmsCarryReplaceH.create_time = DateTime.Now; wmsCarryReplaceH.modify_id = null; wmsCarryReplaceH.modify_time = null; row = await _db.Insertable(wmsCarryReplaceH).ExecuteCommandAsync(); row = await await UpdateNullCarry(oldCarry); isOk = row > 0; if (!isOk) { throw Oops.Oh(ErrorCode.COM1001); } } else { if (oldCarry == null) { throw new AppFriendlyException("没有可用的旧载具", 500); } if (newCarry == null) { throw new AppFriendlyException("没有可用的新载具", 500); } } await _db.Ado.CommitTranAsync(); } catch (Exception ex) { Log.Error("载具更换失败", ex); await _db.Ado.RollbackTranAsync(); throw; } return isOk; } [NonAction] public async Task> UpdateNullCarry(WmsCarryH carryObj, Func>? updateTask = null) { int row = 1; Task resTask = Task.FromResult(row); try { carryObj.status = 0; carryObj.carry_status = ((int)EnumCarryStatus.空闲).ToString(); carryObj.location_id = null; carryObj.location_code = null; carryObj.out_status = "0"; carryObj.is_check = 1; carryObj.status = 1; carryObj.bale_num = null; carryObj.collocation_scheme_id = null; carryObj.collocation_scheme_code = null; carryObj.source_id = null; carryObj.source_code = null; resTask = updateTask?.Invoke(carryObj) ?? _db.CopyNew().Updateable(carryObj).ExecuteCommandAsync(); //*****删除对应明细表 //删除载具明细 _ = await _db.CopyNew().Deleteable().Where(it => it.carry_id == carryObj.id).ExecuteCommandHasChangeAsync(); //删除载具分拣物料明细 _ = await _db.CopyNew().Deleteable().Where(it => it.carry_id == carryObj.id).ExecuteCommandHasChangeAsync(); //删除载具条码 _ = await _db.CopyNew().Deleteable().Where(it => it.carry_id == carryObj.id).ExecuteCommandHasChangeAsync(); } catch (Exception ex) { row = 0; resTask = Task.FromResult(row); Log.Error("更新空载具出错", ex); throw; } return resTask; } private async Task _updateSubCarry(ExChangeCarryInput input) where T : BaseEntity, IWmsCarryEntity, new() { int row = -1; List items = await _db.Queryable().Where(it => it.carry_id == input.old_carry_id).ToListAsync(); if (items?.Count > 0) { List newItems = DeepCopyHelper.DeepCopyList(items); if (newItems?.Count > 0) { newItems.ForEach(x => { x.id = SnowflakeIdHelper.NextId(); x.carry_id = input.new_carry_id; }); row = await _db.Insertable(newItems).ExecuteCommandAsync(); } if (row > 0) { row = await _db.Deleteable(items).ExecuteCommandAsync(); } } return row > 0; } /// /// 载具批量新增 /// /// /// /// /// [HttpPost, AllowAnonymous] public async Task BatchAdd(CarryBatchAddInput input) { int r = 0, num = 0; //var nonZeroPattern = @"[1-9]+"; var nonZeroPattern = @"\d+"; var code = input.carry_code.Match(@"\D+"); if (input.carry_code.IsMatch(@"\d+")) { num = input.carry_code.Match(nonZeroPattern).ParseToInt(); } var carrys = await _db.Queryable().Where(it => it.carry_code.Contains(code)).ToListAsync(); if (carrys?.Count < 1) { num = Math.Max(num, 1); } else { var lastCarry = carrys?.OrderByDescending(o => o.carry_code).FirstOrDefault() ?? default; num = lastCarry?.carry_code.Match(nonZeroPattern).ParseToInt() ?? -1; num = num + 1; } var batchCarrys = new List(input.quantity); for (var i = 1; i <= input.quantity; i++) { if (i != 1) { num += 1; } var sb = new StringBuilder(); sb.Append(code); sb.Append(num.ToString().PadLeft(5, '0')); var carryCode = sb.ToString(); WmsCarryH carry = input.Adapt(); carry.id = SnowflakeIdHelper.NextId(); carry.carry_name = carryCode; carry.carry_code = carryCode; carry.is_check = 1; carry.status = 1; carry.create_id = "25398501929509"; carry.create_time = DateTime.Now; batchCarrys.Add(carry); } r = await _db.Insertable(batchCarrys).ExecuteCommandAsync(); return r; } /// /// 条码打印 /// /// /// [HttpPost] public new void BarCodePrint(CarryBarCodeInput input) { if (input.barCodes == null || input.barCodes.Count < 1) throw new ArgumentNullException(nameof(input.barCodes)); input.barCodes = input.barCodes.OrderBy(o => o).ToList(); base.BarCodePrint(input.barCodes, input.copies,""); } } }