using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 NPOI.SS.Formula; 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.Enums; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr { /// /// 载具绑定 /// [OverideVisualDev(ModuleConsts.MODULE_WMSCARRYBIND_ID)] public class WmsCarryBindService : BaseWareHouseService, IWmsCarryBindService { private readonly ISqlSugarClient _db; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; private readonly IUserManager _userManager; private readonly IBillRullService _billRullService; public WmsCarryBindService( ISqlSugarRepository repository, IRunService runService, IVisualDevService visualDevService, IUserManager userManager, IBillRullService billRullService) { _db = repository.AsSugarClient(); _runService = runService; _visualDevService = visualDevService; _userManager = userManager; _billRullService = billRullService; OverideFuncs.CreateAsync = CarryBind; } [NonAction] public async Task CarryBind(VisualDevModelDataCrInput input) { var isOk = false; try { await _db.Ado.BeginTranAsync(); VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSCARRYBIND_ID, true); await _runService.Create(templateEntity, input); if (input == null) throw new ArgumentNullException(nameof(input)); var carryId = input.data.ContainsKey("carry_id") ? input.data["carry_id"]?.ToString() : ""; var subCarryId = input.data.ContainsKey("membercarry_id") ? input.data["membercarry_id"]?.ToString() : ""; var carry = await _db.Queryable().SingleAsync(it => it.id == carryId); var subCarry = await _db.Queryable().SingleAsync(it => it.id == subCarryId); if (carry != null && subCarry != null) { carry.carry_status = ((int)EnumCarryStatus.占用).ToString(); var row = await _db.Updateable(carry).ExecuteCommandAsync(); subCarry.carry_status = ((int)EnumCarryStatus.占用).ToString(); row = await _db.Updateable(subCarry).ExecuteCommandAsync(); //更新载具明细表 WmsCarryD wmsCarryD = new() { id = SnowflakeIdHelper.NextId(), carry_id = carry.id, org_id = carry?.org_id!, membercarry_id = subCarry.id, membercarry_code = subCarry.carry_code, loc = input.data[nameof(WmsCarrybindH.loc)].ParseToInt(1), create_id = _userManager.UserId, create_time = DateTime.Now }; row = await _db.Insertable(wmsCarryD).ExecuteCommandAsync(); var items = await _db.Queryable().Where(it => it.carry_id == subCarryId).ToListAsync(); List wmsCarrybindCodes = new(); //更新载具绑定条码表 for (int i = 0; i < items.Count; i++) { WmsCarrybindCode wmsCarrybindCode = new(); wmsCarrybindCode.id = SnowflakeIdHelper.NextId(); wmsCarrybindCode.org_id = (subCarry != null && subCarry.org_id != null) ? subCarry.org_id : _userManager.User.OrganizeId; wmsCarrybindCode.carrybind_id = input.data["ReturnIdentity"]?.ToString()!; wmsCarrybindCode.material_id = items[i].material_id; wmsCarrybindCode.material_code = items[i].material_code; wmsCarrybindCode.barcode = items[i].barcode; wmsCarrybindCode.code_batch = items[i].code_batch; wmsCarrybindCode.codeqty = items[i].codeqty; wmsCarrybindCode.membercarry_id = subCarry?.id; wmsCarrybindCode.membercarry_code = subCarry?.carry_code; wmsCarrybindCode.unit_id = items[i].unit_id; wmsCarrybindCode.unit_code = items[i].unit_code; wmsCarrybindCode.create_id = _userManager.UserId; wmsCarrybindCode.create_time = DateTime.Now; wmsCarrybindCodes.Add(wmsCarrybindCode); } row = await _db.Insertable(wmsCarrybindCodes).ExecuteCommandAsync(); isOk = (row > 0); if (!isOk) throw Oops.Oh(ErrorCode.COM1001); } else { if (carry == null || subCarry == null) { throw new AppFriendlyException("没有可用的主载具", 500); } } await _db.Ado.CommitTranAsync(); } catch (Exception) { await _db.Ado.RollbackTranAsync(); throw; } return Task.FromResult(true); } /* public override async Task ModifyAsync(WareHouseUpInput input) { if (input == null) throw new ArgumentNullException(nameof(input)); var isOk = await _db.Updateable().SetColumns(it => new WmsCarryReplaceH { status = input.bizTypeId }).Where(it => it.id == input.requireId).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1001); }*/ } }