wangxinhui
2025-04-29 95e39ae7aecd6e1016c71cf5ae70a680d8f569bb
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_IBasicRepository;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StockService
{
    public partial class ProStockInfoService : ServiceBase<Dt_ProStockInfo, IProStockInfoRepository>, IProStockInfoService
    {
        public IProStockInfoRepository Repository => BaseDal;
        private readonly IStockRepository _stockRepository;
        private readonly IBasicRepository _basicRepository;
        public ProStockInfoService(IProStockInfoRepository BaseDal,IStockRepository stockRepository, IBasicRepository basicRepository) : base(BaseDal)
        {
            _stockRepository = stockRepository;
            _basicRepository = basicRepository;
        }
        /// <summary>
        /// 根据外包信息解绑内包信息
        /// </summary>
        public WebResponseContent UnBindStock(List<Dt_ProStockInfoDetail> proStockInfoDetails)
        {
            WebResponseContent content = new WebResponseContent();
            //根据内包号进行库存扣除
            try
            {
                //获取库存
                List<string> BagNos = proStockInfoDetails.Select(x => x.BagNo).Distinct().ToList();
                List<Dt_ProStockInfo> proStockInfos = _stockRepository.ProStockInfoRepository.Db.Queryable<Dt_ProStockInfo>()
                    .Where(x => x.StockStatus == StockStatusEmun.出库完成.ObjToInt())
                    .Includes(x => x.proStockInfoDetails)
                    .Where(x => x.proStockInfoDetails.Any(v => BagNos.Contains(v.BagNo))).Distinct().ToList();
                List<Dt_ProStockInfoDetail> delProStockDetails=new List<Dt_ProStockInfoDetail>();
                List<Dt_ProStockInfoDetail> upProStockDetails = new List<Dt_ProStockInfoDetail>();
                List<Dt_ProStockInfo> delProStockInfos = new List<Dt_ProStockInfo>();
                foreach (var item in proStockInfoDetails)
                {
                    Dt_ProStockInfo? proStockInfo = proStockInfos.Where(x => x.proStockInfoDetails.Any(v => v.BagNo == item?.BagNo)).FirstOrDefault();
                    Dt_ProStockInfoDetail? proStockInfoDetail = proStockInfo?.proStockInfoDetails.Where(x => x.BagNo == item?.BagNo).FirstOrDefault();
                    if (proStockInfo==null || proStockInfoDetail == null)
                    {
                       return content.Error($"未找到{item?.BagNo}库存");
                    }
                    
                    //判断明细是否全部扫出
                    if (proStockInfoDetail.StockPcsQty==item.StockPcsQty)
                    {
                        delProStockDetails.Add(proStockInfoDetail);
                        proStockInfo.proStockInfoDetails.Remove(proStockInfoDetail);
                        item.OutDetailId = proStockInfoDetail.OutDetailId;
                        item.OutDetailSaleNo = proStockInfoDetail.OutDetailSaleNo;
                    }
                    else
                    {
                        proStockInfoDetail.StockPcsQty -= item.StockPcsQty;
                        proStockInfoDetail.SETQty -= item.SETQty;
                        proStockInfoDetail.OutboundQuantity = 0;
                        item.OutDetailId = 0;
                        item.OutDetailSaleNo = "";
                        upProStockDetails.Add(proStockInfoDetail);
                    }
 
                    if (proStockInfo.proStockInfoDetails.Count==0)
                    {
                        delProStockInfos.Add(proStockInfo);
                    }
                }
                if (delProStockInfos.Count>0)
                {
                    BaseDal.DeleteAndMoveIntoHty(delProStockInfos, OperateTypeEnum.自动删除);
                }
                if (delProStockDetails.Count > 0)
                {
                    _stockRepository.ProStockInfoDetailRepository.DeleteAndMoveIntoHty(delProStockDetails, OperateTypeEnum.自动删除);
                }
                if (upProStockDetails.Count > 0)
                {
                    _stockRepository.ProStockInfoDetailRepository.UpdateData(upProStockDetails);
                }
                content.OK("成功", proStockInfoDetails);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        //查找可用库存
        public List<Dt_ProStockInfo> GetUseableStocks(int warehoseId,Dt_ProOutOrderDetail proOutOrderDetail)
        {
            List<string> locationCodes = _basicRepository.LocationInfoRepository.GetCanOutLocationCodes(warehoseId);
            return BaseDal.GetProStocks(proOutOrderDetail,locationCodes);
        }
        /// <summary>
        /// MES提库可用库存
        /// </summary>
        /// <returns></returns>
        public List<Dt_ProStockInfo> GetUseableStocks(Dt_MesRworkOutboundOrder mesRworkOutboundOrder)
        {
            //转换成 成品仓仓库信息获取尾数属性的货位
            Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x=>x.WarehouseCode==WarehouseEnum.HA71.ToString());
            List<string> locationCodes = _basicRepository.LocationInfoRepository.GetCanOutLocationCodes(warehouse.WarehouseId);
            return BaseDal.GetProStocks(mesRworkOutboundOrder, locationCodes);
        }
        /// <summary>
        /// 获取出库库存
        /// </summary>
        public List<Dt_ProStockInfo> GetOutboundStocks(List<Dt_ProStockInfo> stockInfos, Dt_ProOutOrderDetail outOrderDetail, float needQuantity, out float residueQuantity)
        {
            List<Dt_ProStockInfo> assignOutStocks =new List<Dt_ProStockInfo>();
            float stockTotalQuantity = stockInfos.Select(x => x.proStockInfoDetails.Sum(v => v.StockPcsQty - v.OutboundQuantity)).Sum(x => x);
            //stockInfos = stockInfos.OrderBy(x => x.Id).ToList();
            bool isCanLot = string.IsNullOrEmpty(outOrderDetail.PLot);
            bool isCanDate = string.IsNullOrEmpty(outOrderDetail.DateCode);
            if (stockTotalQuantity >= needQuantity)//库存够
            {
                int index = 0;
                while (needQuantity > 0)
                {
                    Dt_ProStockInfo stockInfo = stockInfos[index];
                    float useableStockQuantity = stockInfo.proStockInfoDetails
                        .Where(x => x.ProductCode == outOrderDetail.PCode && x.ProductVersion.StartsWith(outOrderDetail.PVer.Substring(0,1))
                            && (isCanLot ? isCanLot : x.BagNo == outOrderDetail.PLot)
                            && (isCanDate ? isCanDate : x.DateCode == outOrderDetail.DateCode))
                        .Sum(x => x.StockPcsQty - x.OutboundQuantity);
                    if (useableStockQuantity < needQuantity)
                    {
                        stockInfo.proStockInfoDetails.Where(x => x.ProductCode == outOrderDetail.PCode && x.ProductVersion.StartsWith(outOrderDetail.PVer.Substring(0, 1))
                            && (isCanLot ? isCanLot : x.BagNo == outOrderDetail.PLot)
                            && (isCanDate ? isCanDate : x.DateCode == outOrderDetail.DateCode)).ToList().ForEach(x => x.OutboundQuantity = x.StockPcsQty);
                        needQuantity -= useableStockQuantity;
                    }
                    else
                    {
                        stockInfo.proStockInfoDetails.ForEach(x =>
                        {
                            //满足条件进行分配
                            if ((x.StockPcsQty > x.OutboundQuantity)
                                && x.ProductCode == outOrderDetail.PCode && x.ProductVersion.StartsWith(outOrderDetail.PVer.Substring(0, 1)) 
                                && (isCanLot ? isCanLot : x.BagNo == outOrderDetail.PLot)
                                && (isCanDate ? isCanDate : x.DateCode == outOrderDetail.DateCode))
                            {
                                if (x.StockPcsQty - x.OutboundQuantity >= needQuantity)
                                {
                                    x.OutboundQuantity += needQuantity;
                                    needQuantity = 0;
                                    x.OutDetailSaleNo = outOrderDetail.SaleOrder;
                                    x.OutDetailId = outOrderDetail.Id;
                                }
                                else
                                {
                                    needQuantity -= (x.StockPcsQty - x.OutboundQuantity);
                                    x.OutboundQuantity = x.StockPcsQty;
                                    x.OutDetailSaleNo = outOrderDetail.SaleOrder;
                                    x.OutDetailId = outOrderDetail.Id;
                                }
                            }
                        });
                    }
                    assignOutStocks.Add(stockInfo);
                    index++;
                }
            }
            #region 成品可用库存不足不进行分配
            //else
            //{
            //    for (int i = 0; i < stockInfos.Count; i++)
            //    {
            //        Dt_ProStockInfo stockInfo = stockInfos[i];
            //        float useableStockQuantity = stockInfo.proStockInfoDetails
            //            .Where(x => x.ProductCode == outOrderDetail.PCode && x.ProductVersion == outOrderDetail.PVer
            //                && (isCanLot ? isCanLot : x.BagNo == outOrderDetail.PLot)
            //                && (isCanDate ? isCanDate : x.DateCode == outOrderDetail.DateCode))
            //            .Sum(x => x.StockPcsQty - x.OutboundQuantity);
            //        if (useableStockQuantity < needQuantity)
            //        {
            //            stockInfo.proStockInfoDetails.ForEach(x => x.OutboundQuantity = x.StockPcsQty);
            //            needQuantity -= useableStockQuantity;
            //        }
            //        else
            //        {
            //            stockInfo.proStockInfoDetails.ForEach(x =>
            //            {
            //                if (x.StockPcsQty > x.OutboundQuantity && x.ProductCode == outOrderDetail.PCode && x.ProductVersion == outOrderDetail.PVer
            //                    && (isCanLot ? isCanLot : x.BagNo == outOrderDetail.PLot)
            //                    && (isCanDate ? isCanDate : x.DateCode == outOrderDetail.DateCode))
            //                {
            //                    if (x.StockPcsQty - x.OutboundQuantity >= needQuantity)
            //                    {
            //                        x.OutboundQuantity += needQuantity;
            //                        needQuantity = 0;
            //                    }
            //                    else
            //                    {
            //                        needQuantity -= (x.StockPcsQty - x.OutboundQuantity);
            //                        x.OutboundQuantity = x.StockPcsQty;
            //                    }
            //                }
            //            });
            //        }
            //        stockInfo.proStockInfoDetails.ForEach(x =>
            //        {
            //            x.OutDetailSaleNo = outOrderDetail.SaleOrder;
            //            x.OutDetailId = outOrderDetail.Id;
            //        });
            //        assignOutStocks.Add(stockInfo);
            //    }
            //}
            #endregion
            residueQuantity = needQuantity;
            return assignOutStocks;
        }
        /// <summary>
        /// 获取MES提库库存
        /// </summary>
        public List<Dt_ProStockInfo> GetOutboundStocks(List<Dt_ProStockInfo> stockInfos, Dt_MesRworkOutboundOrder mesRworkOutboundOrder, float needQuantity, out float residueQuantity)
        {
            List<Dt_ProStockInfo> assignOutStocks = new List<Dt_ProStockInfo>();
            float stockTotalQuantity = stockInfos.Select(x => x.proStockInfoDetails.Sum(v => v.StockPcsQty - v.OutboundQuantity)).Sum(x => x);
            //stockInfos = stockInfos.OrderBy(x => x.Id).ToList();
            bool isCanDate = string.IsNullOrEmpty(mesRworkOutboundOrder.DateCode);
            if (stockTotalQuantity >= needQuantity)//库存够
            {
                int index = 0;
                while (needQuantity > 0)
                {
                    Dt_ProStockInfo stockInfo = stockInfos[index];
                    float useableStockQuantity = stockInfo.proStockInfoDetails
                        .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                            && x.ProductCode == mesRworkOutboundOrder.ProductCode 
                            && x.ProductVersion == mesRworkOutboundOrder.ProductVersion
                            && (isCanDate? isCanDate: x.DateCode == mesRworkOutboundOrder.DateCode))
                        .Sum(x => x.StockPcsQty - x.OutboundQuantity);
                    if (useableStockQuantity < needQuantity)
                    {
                        stockInfo.proStockInfoDetails.ForEach(x => x.OutboundQuantity = x.StockPcsQty);
                        needQuantity -= useableStockQuantity;
                    }
                    else
                    {
                        stockInfo.proStockInfoDetails.ForEach(x =>
                        {
                            if ((x.StockPcsQty > x.OutboundQuantity) && x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                                && x.ProductCode == mesRworkOutboundOrder.ProductCode 
                                && x.ProductVersion == mesRworkOutboundOrder.ProductVersion
                                && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode))
                            {
                                if (x.StockPcsQty - x.OutboundQuantity >= needQuantity)
                                {
                                    x.OutboundQuantity += needQuantity;
                                    needQuantity = 0;
                                }
                                else
                                {
                                    needQuantity -= (x.StockPcsQty - x.OutboundQuantity);
                                    x.OutboundQuantity = x.StockPcsQty;
                                }
                            }
                        });
                    }
                    assignOutStocks.Add(stockInfo);
                    index++;
                }
            }
            else
            {
                for (int i = 0; i < stockInfos.Count; i++)
                {
                    Dt_ProStockInfo stockInfo = stockInfos[i];
                    float useableStockQuantity = stockInfo.proStockInfoDetails
                        .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder
                            && x.ProductCode == mesRworkOutboundOrder.ProductCode
                            && x.ProductVersion == mesRworkOutboundOrder.ProductVersion
                            && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode))
                        .Sum(x => x.StockPcsQty - x.OutboundQuantity);
                    if (useableStockQuantity < needQuantity)
                    {
                        stockInfo.proStockInfoDetails.ForEach(x => x.OutboundQuantity = x.StockPcsQty);
                        needQuantity -= useableStockQuantity;
                    }
                    else
                    {
                        stockInfo.proStockInfoDetails.ForEach(x =>
                        {
                            if (x.StockPcsQty > x.OutboundQuantity && x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                                && x.ProductCode == mesRworkOutboundOrder.ProductCode 
                                && x.ProductVersion == mesRworkOutboundOrder.ProductVersion
                                && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode))
                            {
                                if (x.StockPcsQty - x.OutboundQuantity >= needQuantity)
                                {
                                    x.OutboundQuantity += needQuantity;
                                    needQuantity = 0;
                                }
                                else
                                {
                                    needQuantity -= (x.StockPcsQty - x.OutboundQuantity);
                                    x.OutboundQuantity = x.StockPcsQty;
                                }
                            }
                        });
                    }
                    assignOutStocks.Add(stockInfo);
                }
            }
            residueQuantity = needQuantity;
            return assignOutStocks;
        }
        ////处理出库库存
        //public (List<Dt_ProStockInfoDetail>?, List<Dt_ProStockInfoDetail>?) HandleOutProStock(Dt_ProStockInfo proStockInfo)
        //{
        //    List<Dt_ProStockInfoDetail>? deleteStockDetails = null;
        //    List<Dt_ProStockInfoDetail>? updateStockDetails = null;
        //    foreach (var item in proStockInfo.proStockInfoDetails)
        //    {
        //        if (item.StockPcsQty==item.OutboundQuantity)
        //        {
        //            item.ProOutDetailStatus = StockStatusEmun.出库完成.ObjToInt();
        //            deleteStockDetails.Add(item);
        //        }
        //        else if(item.StockPcsQty> item.OutboundQuantity && item.OutboundQuantity>0)
        //        {
        //            item.StockPcsQty-=item.OutboundQuantity;
        //            updateStockDetails.Add(item);
        //        }
        //    }
        //    return (deleteStockDetails, updateStockDetails);
        //}
    }
}