OrderBookStoreApplicationService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Abp.Application.Services;
  7. using Abp.Auditing;
  8. using Abp.Authorization;
  9. using Abp.Domain.Repositories;
  10. using Abp.Json;
  11. using Abp.Timing;
  12. using IwbZero.IdentityFramework;
  13. using IwbZero.Session;
  14. using Microsoft.AspNet.Identity;
  15. using NPOI.POIFS.FileSystem;
  16. using ShwasherSys.BaseSysInfo;
  17. using ShwasherSys.Common;
  18. using ShwasherSys.Order.Dto.OrderBookStore;
  19. using ShwasherSys.ProductStoreInfo;
  20. namespace ShwasherSys.Order
  21. {
  22. [AbpAuthorize, DisableAuditing]
  23. public class OrderBookStoreAppService: ApplicationService,IOrderBookStoreAppService
  24. {
  25. public OrderBookStoreAppService(IRepository<OrderBookStore,long> repository, IRepository<CurrentProductStoreHouse> currentStoreRepository,
  26. IRepository<BusinessLog> businessLogRepository)
  27. {
  28. _repository = repository;
  29. _currentStoreRepository = currentStoreRepository;
  30. BusinessLogRepository = businessLogRepository;
  31. }
  32. protected IRepository<OrderBookStore, long> _repository { get; }
  33. protected IRepository<CurrentProductStoreHouse> _currentStoreRepository { get; }
  34. protected IRepository<BusinessLog> BusinessLogRepository { get; }
  35. public new IIwbSession AbpSession { get; set; }
  36. public async Task<List<OrderBookStore>> GetDataByOrderItemId(int id)
  37. {
  38. return await _repository.GetAllListAsync(i=>i.OrderItemId == id);
  39. }
  40. //public async Task UnLockStoreByOrderItemId(int id)
  41. //{
  42. // var orderBookStores = await _repository.GetAllListAsync(i=>i.OrderItemId == id);
  43. // foreach (var orderBookStore in orderBookStores)
  44. // {
  45. // var currentProductStore = await _currentStoreRepository.FirstOrDefaultAsync(i=>i.CurrentProductStoreHouseNo ==
  46. // orderBookStore.CurrentProductStoreHouseNo);
  47. // if (currentProductStore.FreezeQuantity < (orderBookStore.Quantity ?? 0))
  48. // {
  49. // CheckErrors(IdentityResult.Failed("解除仓库冻结库存异常!解除冻结后结果小于0"));
  50. // }
  51. // currentProductStore.FreezeQuantity -= orderBookStore.Quantity ?? 0;
  52. // currentProductStore.TimeLastMod = Clock.Now;
  53. // currentProductStore.UserIDLastMod = AbpSession.UserName;
  54. // await _currentStoreRepository.UpdateAsync(currentProductStore);
  55. // orderBookStore.Status = OrderBookStoreStatusEnum.UnLock.ToInt();
  56. // await _repository.UpdateAsync(orderBookStore);
  57. // }
  58. // BusinessLogTypeEnum.OrderLog.WriteLog(BusinessLogRepository, "订单明细解除冻结仓库库存", $"仓库库存解除冻结成功!orderItem:{id},UserName:{AbpSession.UserName}", logExt1: $"{orderBookStores.ToJsonString()}");
  59. //}
  60. //冻结锁定库存
  61. public async Task LockStore(BookStoreAddsDto input)
  62. {
  63. if (input.OrderItemId <= 0 || !input.AddItems.Any())
  64. {
  65. CheckErrors(IdentityResult.Failed("请输入需要锁定的库存记录!"));
  66. }
  67. var currentNoList = input.AddItems.Select(i=>i.CurrentProductStoreHouseNo).ToList();
  68. var currentProductStoreHouses = await _currentStoreRepository.GetAllListAsync(i=> currentNoList.Contains(i.CurrentProductStoreHouseNo));
  69. foreach (var addItem in input.AddItems)
  70. {
  71. var currentProductStore = currentProductStoreHouses.Find(i =>
  72. i.CurrentProductStoreHouseNo == addItem.CurrentProductStoreHouseNo);
  73. decimal canUse = currentProductStore.Quantity - currentProductStore.FreezeQuantity;
  74. if (canUse < addItem.Quantity)
  75. {
  76. CheckErrors(IdentityResult.Failed($"批次号{currentProductStore.ProductionOrderNo}库存不足,请重新选择!"));
  77. }
  78. currentProductStore.FreezeQuantity += addItem.Quantity ?? 0;
  79. currentProductStore.TimeLastMod = Clock.Now;
  80. currentProductStore.UserIDLastMod = AbpSession.UserName;
  81. await _currentStoreRepository.UpdateAsync(currentProductStore);
  82. var avgQuantity = addItem.QuantityPerPack == 0 ? addItem.Quantity : addItem.QuantityPerPack;
  83. decimal packageCount = addItem.Quantity <= avgQuantity
  84. ? 1
  85. : Math.Ceiling(addItem.Quantity ?? 0 / avgQuantity ?? 0);
  86. ;
  87. await _repository.InsertAsync(new OrderBookStore()
  88. {
  89. OrderItemId = input.OrderItemId,
  90. CurrentProductStoreHouseNo = addItem.CurrentProductStoreHouseNo,
  91. ProductNo = addItem.ProductNo,
  92. Quantity = addItem.Quantity,
  93. QuantityPerPack = addItem.QuantityPerPack,
  94. PackageCount = packageCount,
  95. ProductBatchNum = addItem.ProductBatchNum,
  96. StoreLocationNo = addItem.StoreLocationNo,
  97. Status = OrderBookStoreStatusEnum.Locking.ToInt(),
  98. CustomerId = addItem.CustomerId
  99. });
  100. }
  101. BusinessLogTypeEnum.OrderLog.WriteLog(BusinessLogRepository, "订单明细锁定仓库库存", $"仓库库存冻结创建成功!orderItem:{input.OrderItemId},UserName:{AbpSession.UserName}",logExt1:$"{input.AddItems.ToJsonString()}");
  102. }
  103. /**
  104. * 删除已经锁定的记录
  105. */
  106. public async Task UnLockItem(int id)
  107. {
  108. var entity = await _repository.GetAsync(id);
  109. if (entity == null)
  110. {
  111. CheckErrors(IdentityResult.Failed("为查询到相应的锁定明细!"));
  112. }
  113. await _repository.DeleteAsync(entity);
  114. CurrentProductStoreHouse currentProduct =
  115. await _currentStoreRepository.FirstOrDefaultAsync(i =>
  116. i.CurrentProductStoreHouseNo == entity.CurrentProductStoreHouseNo);
  117. currentProduct.FreezeQuantity -= entity.Quantity ?? 0;
  118. await _currentStoreRepository.UpdateAsync(currentProduct);
  119. }
  120. //查看当前订单明细是否存在冻结的库存
  121. public async Task<bool> CheckExistLockStore(int id)
  122. {
  123. return (await _repository.CountAsync(i => i.OrderItemId == id ))>0;
  124. }
  125. //查看当前订单明细是否存在已经冻结并且未解锁的库存
  126. public async Task<bool> CheckExistLockingStore(int id)
  127. {
  128. int status = OrderBookStoreStatusEnum.Locking.ToInt();
  129. return (await _repository.CountAsync(i => i.OrderItemId == id && i.Status == status)) > 0;
  130. }
  131. #region MyRegion
  132. protected virtual void CheckErrors(IdentityResult identityResult)
  133. {
  134. identityResult.CheckErrors(LocalizationManager);
  135. }
  136. #endregion
  137. }
  138. }