using JNPF.Common.Contracts; using JNPF.Common.Core.Manager; using JNPF.Common.Dtos.VisualDev; using JNPF.Common.Enums; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using Mapster; using Microsoft.AspNetCore.Mvc; using Senparc.Weixin.Work.AdvancedAPIs.OaDataOpen; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.Common.Utils; using Tnb.WarehouseMgr.Entities; 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_WMSFEEDINGRECORDPDA_ID)] public class WmsPDAFeedingService : BaseWareHouseService, IWmsCarryService, IWmsFeedingService { //private const string ModuleId = "26496913096981"; private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; private readonly IBillRullService _billRullService; private readonly WmsCarryService _wmsCarryService; private static Dictionary dicMaterial = new Dictionary(); public WmsPDAFeedingService(ISqlSugarRepository repository, IUserManager userManager, IBillRullService billRullService, WmsCarryService wmsCarryService) { _db = repository.AsSugarClient(); _userManager = userManager; _billRullService = billRullService; _wmsCarryService = wmsCarryService; OverideFuncs.CreateAsync = WmsPDAFeeding; } private async Task WmsPDAFeeding(VisualDevModelDataCrInput input) { var isOk = false; try { await _db.Ado.BeginTranAsync(); var carryId = input.data.ContainsKey("carry_id") ? input.data["carry_id"]?.ToString() : ""; var feedBoxCode = input.data.ContainsKey("feedbox_code") ? input.data["feedbox_code"]?.ToString() : ""; var carry = await _db.Queryable().FirstAsync(it => it.id == carryId); var feedBox = await _db.Queryable().FirstAsync(it => it.feedbox_code == feedBoxCode); var citem = await _db.Queryable().FirstAsync(it => it.carry_id == carryId); if (carry != null && feedBox != null) { feedBox.material_id = citem.material_id; feedBox.material_code = citem.material_code; feedBox.qty = citem.qty; feedBox.batch = citem?.code_batch!; var row = await _db.Updateable(feedBox).UpdateColumns(it => new { it.material_id, it.material_code, it.qty, it.batch }).ExecuteCommandAsync(); /* var row = await _db.Updateable(feedBox).SetColumns(it => new { it.material_id == item.material_id,it.material_code } )*/ var items = await _db.Queryable().Where(it => it.carry_id == carryId).ToListAsync(); foreach (var item in items) { WmsFeedingrecordCode wmsFeedingRecordCode = new(); wmsFeedingRecordCode.id = SnowflakeIdHelper.NextId(); wmsFeedingRecordCode.org_id = carry?.org_id!; wmsFeedingRecordCode.record_id = input.data["ReturnIdentity"]?.ToString()!; wmsFeedingRecordCode.material_id = item.material_id; wmsFeedingRecordCode.material_code = item.material_code; wmsFeedingRecordCode.barcode = item.barcode; wmsFeedingRecordCode.code_batch = item.code_batch; wmsFeedingRecordCode.codeqty = item.codeqty; wmsFeedingRecordCode.unit_id = item.unit_id; wmsFeedingRecordCode.unit_code = item.unit_code; wmsFeedingRecordCode.create_id = _userManager.UserId; wmsFeedingRecordCode.create_time = DateTime.Now; row = await _db.Insertable(wmsFeedingRecordCode).ExecuteCommandAsync(); } row = await UpdateNullCarry(carry); isOk = (row > 0); if (!isOk) throw Oops.Oh(ErrorCode.COM1001); } else { if (carry == null) { throw new AppFriendlyException("没有可用的旧载具", 500); } if (feedBox == null) { throw new AppFriendlyException("没有可用的新载具", 500); } } await _db.Ado.CommitTranAsync(); } catch (Exception ex) { Log.Error("载具更换失败", ex); await _db.Ado.RollbackTranAsync(); throw; } return isOk; } public async Task UpdateNullCarry(WmsCarryH carryObj) { var row = -1; try { carryObj.status = 0; carryObj.carry_status = "0"; carryObj.location_id = null; carryObj.location_code = null; carryObj.out_status = "0"; carryObj.is_check = 0; 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; row = await _db.Updateable(carryObj).ExecuteCommandAsync(); //删除载具明细 await _db.Deleteable().Where(it => it.carry_id == carryObj.id).ExecuteCommandHasChangeAsync(); //删除载具分拣物料明细 await _db.Deleteable().Where(it => it.carry_id == carryObj.id).ExecuteCommandHasChangeAsync(); //删除载具条码 await _db.Deleteable().Where(it => it.carry_id == carryObj.id).ExecuteCommandHasChangeAsync(); } catch (Exception ex) { } return row; } private async Task _updateSubCarry(ExChangeCarryInput input) where T : BaseEntity, IWmsCarryEntity, new() { var row = -1; var 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); } /// /// 根据载具Id获取载具条码记录 /// /// /// [HttpGet] public async Task GetCarryCodeList([FromRoute] string carryId) { if (dicMaterial.Count < 1) { dicMaterial = await _db.Queryable().ToDictionaryAsync(x => x.id, x => x.name); } var items = await _db.Queryable().Where(a => a.carry_id == carryId) .Select(a => new CarryCodeDetailOutput { barcode = a.barcode, code_batch = a.code_batch, codeqty = a.codeqty, material_code = a.material_code, material_id = a.material_id, unit_id = a.unit_id, }) .Mapper(it => it.material_name = dicMaterial.ContainsKey(it.material_id) ? dicMaterial[it.material_id].ToString()! : "") .ToListAsync(); return items; } } }