using SqlSugar.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using System.Transactions; using WIDESEA_Core; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Enums; using WIDESEA_DTO.Inbound; using WIDESEA_IStockRepository; using WIDESEA_IStockService; using WIDESEA_Model.Models; namespace WIDESEA_StockService { public partial class StockInfoDetailService : ServiceBase, IStockInfoDetailService { private readonly IStockInfoDetailRepository _stockInfoDetailRepository; private readonly IStockInfoService _stockInfoService; public StockInfoDetailService(IStockInfoDetailRepository BaseDal, IStockInfoDetailRepository stockInfoDetailRepository, IStockInfoService stockInfoService) : base(BaseDal) { _stockInfoDetailRepository = stockInfoDetailRepository; _stockInfoService = stockInfoService; } public IStockInfoDetailRepository Repository => BaseDal; public override WebResponseContent DeleteData(object[] keys) { WebResponseContent content = new WebResponseContent(); try { var DelStockInfoDetails = BaseDal.QueryData(x => keys.Contains(x.Id)).ToList(); if (DelStockInfoDetails == null || DelStockInfoDetails.Count < 1) throw new Exception($"未找到信息"); if (DelStockInfoDetails.FirstOrDefault(x => x.Status > StockStatusEmun.组盘暂存.ObjToInt()) != null) throw new Exception($"库存明细状态不为组盘暂存暂不能删除"); var StockIds = DelStockInfoDetails.Select(x => x.StockId).ToList(); var StockInfoDetails = BaseDal.QueryData(x => StockIds.Contains(x.StockId)).Select(x => x.StockId).GroupBy(x => x).ToList(); List StockInfos = new List(); foreach (var item in StockInfoDetails) { if (item.Count() == DelStockInfoDetails.Where(x => x.StockId == item.Key).Count()) { var StockInfo = Db.Queryable().Where(x => item.Key == x.Id).First(); StockInfos.Add(StockInfo); } } List inboundOrders = new List(); List inboundOrderDetails = new List(); foreach (var DelStockInfoDetail in DelStockInfoDetails.GroupBy(x => x.OrderNo)) { var inboundOrder = BaseDal.Db.Queryable().Includes(x => x.Details).First(x => x.OrderNo == DelStockInfoDetail.Key); if (inboundOrder != null) { foreach (var item in DelStockInfoDetail) { var inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == item.BatchNo); if (inboundOrderDetail != null) { inboundOrderDetail.ReceiptQuantity -= 1;// item.StockQuantity; if (inboundOrderDetail.ReceiptQuantity == 0) inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(); } } inboundOrderDetails.AddRange(inboundOrder.Details); if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) == null) { inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt(); inboundOrders.Add(inboundOrder); } } } Db.Ado.BeginTran(); BaseDal.Db.Updateable(inboundOrderDetails).ExecuteCommand(); BaseDal.Db.Updateable(inboundOrders).ExecuteCommand(); _stockInfoService.DeleteData(StockInfos); base.DeleteData(keys); Db.Ado.CommitTran(); content.OK(); } catch (Exception ex) { Db.Ado.RollbackTran(); content.Error(ex.Message); } return content; } } }