using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Abp.Application.Services; using Abp.Auditing; using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Json; using Abp.Timing; using IwbZero.IdentityFramework; using IwbZero.Session; using Microsoft.AspNet.Identity; using NPOI.POIFS.FileSystem; using ShwasherSys.BaseSysInfo; using ShwasherSys.Common; using ShwasherSys.Order.Dto.OrderBookStore; using ShwasherSys.ProductStoreInfo; namespace ShwasherSys.Order { [AbpAuthorize, DisableAuditing] public class OrderBookStoreAppService: ApplicationService,IOrderBookStoreAppService { public OrderBookStoreAppService(IRepository repository, IRepository currentStoreRepository, IRepository businessLogRepository) { _repository = repository; _currentStoreRepository = currentStoreRepository; BusinessLogRepository = businessLogRepository; } protected IRepository _repository { get; } protected IRepository _currentStoreRepository { get; } protected IRepository BusinessLogRepository { get; } public new IIwbSession AbpSession { get; set; } public async Task> GetDataByOrderItemId(int id) { return await _repository.GetAllListAsync(i=>i.OrderItemId == id); } //public async Task UnLockStoreByOrderItemId(int id) //{ // var orderBookStores = await _repository.GetAllListAsync(i=>i.OrderItemId == id); // foreach (var orderBookStore in orderBookStores) // { // var currentProductStore = await _currentStoreRepository.FirstOrDefaultAsync(i=>i.CurrentProductStoreHouseNo == // orderBookStore.CurrentProductStoreHouseNo); // if (currentProductStore.FreezeQuantity < (orderBookStore.Quantity ?? 0)) // { // CheckErrors(IdentityResult.Failed("解除仓库冻结库存异常!解除冻结后结果小于0")); // } // currentProductStore.FreezeQuantity -= orderBookStore.Quantity ?? 0; // currentProductStore.TimeLastMod = Clock.Now; // currentProductStore.UserIDLastMod = AbpSession.UserName; // await _currentStoreRepository.UpdateAsync(currentProductStore); // orderBookStore.Status = OrderBookStoreStatusEnum.UnLock.ToInt(); // await _repository.UpdateAsync(orderBookStore); // } // BusinessLogTypeEnum.OrderLog.WriteLog(BusinessLogRepository, "订单明细解除冻结仓库库存", $"仓库库存解除冻结成功!orderItem:{id},UserName:{AbpSession.UserName}", logExt1: $"{orderBookStores.ToJsonString()}"); //} //冻结锁定库存 public async Task LockStore(BookStoreAddsDto input) { if (input.OrderItemId <= 0 || !input.AddItems.Any()) { CheckErrors(IdentityResult.Failed("请输入需要锁定的库存记录!")); } var currentNoList = input.AddItems.Select(i=>i.CurrentProductStoreHouseNo).ToList(); var currentProductStoreHouses = await _currentStoreRepository.GetAllListAsync(i=> currentNoList.Contains(i.CurrentProductStoreHouseNo)); foreach (var addItem in input.AddItems) { var currentProductStore = currentProductStoreHouses.Find(i => i.CurrentProductStoreHouseNo == addItem.CurrentProductStoreHouseNo); decimal canUse = currentProductStore.Quantity - currentProductStore.FreezeQuantity; if (canUse < addItem.Quantity) { CheckErrors(IdentityResult.Failed($"批次号{currentProductStore.ProductionOrderNo}库存不足,请重新选择!")); } currentProductStore.FreezeQuantity += addItem.Quantity ?? 0; currentProductStore.TimeLastMod = Clock.Now; currentProductStore.UserIDLastMod = AbpSession.UserName; await _currentStoreRepository.UpdateAsync(currentProductStore); var avgQuantity = addItem.QuantityPerPack == 0 ? addItem.Quantity : addItem.QuantityPerPack; decimal packageCount = addItem.Quantity <= avgQuantity ? 1 : Math.Ceiling(addItem.Quantity ?? 0 / avgQuantity ?? 0); ; await _repository.InsertAsync(new OrderBookStore() { OrderItemId = input.OrderItemId, CurrentProductStoreHouseNo = addItem.CurrentProductStoreHouseNo, ProductNo = addItem.ProductNo, Quantity = addItem.Quantity, QuantityPerPack = addItem.QuantityPerPack, PackageCount = packageCount, ProductBatchNum = addItem.ProductBatchNum, StoreLocationNo = addItem.StoreLocationNo, Status = OrderBookStoreStatusEnum.Locking.ToInt(), CustomerId = addItem.CustomerId }); } BusinessLogTypeEnum.OrderLog.WriteLog(BusinessLogRepository, "订单明细锁定仓库库存", $"仓库库存冻结创建成功!orderItem:{input.OrderItemId},UserName:{AbpSession.UserName}",logExt1:$"{input.AddItems.ToJsonString()}"); } /** * 删除已经锁定的记录 */ public async Task UnLockItem(int id) { var entity = await _repository.GetAsync(id); if (entity == null) { CheckErrors(IdentityResult.Failed("为查询到相应的锁定明细!")); } await _repository.DeleteAsync(entity); CurrentProductStoreHouse currentProduct = await _currentStoreRepository.FirstOrDefaultAsync(i => i.CurrentProductStoreHouseNo == entity.CurrentProductStoreHouseNo); currentProduct.FreezeQuantity -= entity.Quantity ?? 0; await _currentStoreRepository.UpdateAsync(currentProduct); } //查看当前订单明细是否存在冻结的库存 public async Task CheckExistLockStore(int id) { return (await _repository.CountAsync(i => i.OrderItemId == id ))>0; } //查看当前订单明细是否存在已经冻结并且未解锁的库存 public async Task CheckExistLockingStore(int id) { int status = OrderBookStoreStatusEnum.Locking.ToInt(); return (await _repository.CountAsync(i => i.OrderItemId == id && i.Status == status)) > 0; } #region MyRegion protected virtual void CheckErrors(IdentityResult identityResult) { identityResult.CheckErrors(LocalizationManager); } #endregion } }