1
dengjunjie
2025-05-08 092971a8ba7848f024427694c642959d0fbc8599
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_StockRepository;
 
namespace WIDESEA_OutboundService
{
    public partial class OutboundOrderDetailService : ServiceBase<Dt_OutboundOrderDetail, IOutboundOrderDetailRepository>, IOutboundOrderDetailService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
        private readonly IStockService _stockService;
        private readonly IBasicService _basicService;
        private readonly IRecordService _recordService;
        private readonly IOutboundRepository _outboundRepository;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
 
        public IOutboundOrderDetailRepository Repository => BaseDal;
 
        public OutboundOrderDetailService(IOutboundOrderDetailRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IBasicService basicService, IRecordService recordService, IOutboundRepository outboundRepository, IOutStockLockInfoService outStockLockInfoService) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _stockService = stockService;
            _basicService = basicService;
            _recordService = recordService;
            _outboundRepository = outboundRepository;
            _outStockLockInfoService = outStockLockInfoService;
        }
 
        public (List<Dt_StockInfo>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>, Dt_OutboundOrder) AssignStockOutbound(Dt_OutboundOrderDetail outboundOrderDetail)
        {
            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId);
            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
            List<string> LocationCodes = new List<string>();
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
            return (outStocks, outStockLockInfos, locationInfos, outboundOrder);
        }
 
        public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>, Dt_OutboundOrder) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
        {
            if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1)
            {
                throw new Exception($"请勿同时操作多个单据明细");
            }
            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId);
            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
            List<string> LocationCodes = new List<string>();
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
            List<int> keys = new List<int>();
            foreach (var item in outboundOrderDetails)
            {
                var StockSelectViews = _stockService.StockInfoService.GetStockSelectViews(item.MaterielCode);
                if (StockSelectViews.Count < 1) throw new Exception($"未找到可分配库存");
                StockSelectViews = StockSelectViews.Where(x => !keys.Contains(x.Id)).OrderBy(x => x.InDate).ToList();
                if (StockSelectViews.Count < 1) throw new Exception($"可分配库存不足");
                foreach (var StockSelectView in StockSelectViews.GroupBy(x => x.LocationCode))
                {
                    if (item.OrderQuantity - item.LockQuantity <= 0) break;
                    decimal StockQuantity = StockSelectView.Sum(x => x.UseableQuantity);
                    if (StockQuantity <= item.OrderQuantity - item.LockQuantity)
                    {
                        foreach (var Stock in StockSelectView.OrderByDescending(x => x.InDate))
                        {
                            keys.Add(Stock.Id);
                            item.LockQuantity += Stock.UseableQuantity;
                            item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                            List<Dt_StockInfo> dt_StockInfos = _stockService.StockInfoService.Repository.QueryData(x => Stock.PalletCode.Contains(x.PalletCode));
                            dt_StockInfos.ForEach(x =>
                            {
                                x.StockStatus = StockStatusEmun.出库中.ObjToInt();
                            });
                            outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
                            outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, dt_StockInfos, BatchNo: Stock.BatchNo));
                            outStocks.AddRange(dt_StockInfos);
                        }
                        LocationCodes.Add(StockSelectView.Key);
                    }
                    else
                    {
                        foreach (var Stock in StockSelectView.OrderByDescending(x => x.InDate))
                        {
                            if (item.OrderQuantity - item.LockQuantity - Stock.UseableQuantity >= 0)
                            {
                                keys.Add(Stock.Id);
                                item.LockQuantity += Stock.UseableQuantity;
                                item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                                List<Dt_StockInfo> StockInfos = _stockService.StockInfoService.Repository.QueryData(x => Stock.PalletCode.Contains(x.PalletCode));
                                StockInfos.ForEach(x =>
                                {
                                    x.StockStatus = StockStatusEmun.出库中.ObjToInt();
                                });
                                outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
                                outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, StockInfos, BatchNo: Stock.BatchNo));
                                outStocks.AddRange(StockInfos);
                                LocationCodes.Add(Stock.LocationCode);
                            }
                        }
                    }
                }
                #region MyRegion
                //StockSelectViewDTO? stockSelectView = StockSelectViews.Where(x => x.UseableQuantity == item.OrderQuantity).FirstOrDefault();
                //if (stockSelectView == null)
                //{
                //    foreach (var StockSelectView in StockSelectViews)
                //    {
                //        if (item.OrderQuantity - item.LockQuantity - StockSelectView.UseableQuantity >= 0)
                //        {
                //            keys.Add(StockSelectView.Id);
                //            item.LockQuantity += StockSelectView.UseableQuantity;
                //            item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                //            List<Dt_StockInfo> StockInfos = _stockService.StockInfoService.Repository.QueryData(x => StockSelectView.PalletCode.Contains(x.PalletCode));
                //            StockInfos.ForEach(x =>
                //            {
                //                x.StockStatus = StockStatusEmun.出库中.ObjToInt();
                //            });
                //            outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
                //            outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, StockInfos, BatchNo: StockSelectView.BatchNo));
                //            outStocks.AddRange(StockInfos);
                //            LocationCodes.Add(StockSelectView.LocationCode);
                //        }
                //    }
                //    if (keys.Count < 1) throw new Exception($"未找到匹配出库单数量库存");
                //}
                //else
                //{
                //    keys.Add(stockSelectView.Id);
                //    item.LockQuantity = stockSelectView.UseableQuantity;
                //    item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                //    List<Dt_StockInfo> dt_StockInfos = _stockService.StockInfoService.Repository.QueryData(x => stockSelectView.PalletCode.Contains(x.PalletCode));
                //    dt_StockInfos.ForEach(x =>
                //    {
                //        x.StockStatus = StockStatusEmun.出库中.ObjToInt();
                //    });
                //    outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
                //    outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, dt_StockInfos, BatchNo: stockSelectView.BatchNo));
                //    outStocks.AddRange(dt_StockInfos);
                //    LocationCodes.Add(stockSelectView.LocationCode);
                //} 
                #endregion
            }
            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(LocationCodes));
            locationInfos.ForEach(x =>
            {
                x.LocationStatus = LocationStatusEnum.Outbounding.ObjToInt();
            });
            return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos, outboundOrder);
        }
    }
}