wangxinhui
2025-04-08 dcecb039035f4b2e82e31c39b74db9402444c536
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
342
343
344
345
346
347
348
349
350
351
352
353
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO;
using WIDESEA_DTO.MES;
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_OutboundRepository;
using WIDESEA_Common.APIEnum;
using WIDESEA_Common.OrderEnum;
 
namespace WIDESEA_OutboundService
{
    public class MesRworkOutboundOrderService : ServiceBase<Dt_MesRworkOutboundOrder, IMesRworkOutboundOrderRepository>, IMesRworkOutboundOrderService
    {
        private readonly IBasicRepository _basicRepository;
        private readonly IStockService _stockService;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
        private readonly IBasicService _basicService;
        private readonly IRecordService _recordService;
        private readonly IOutProStockInfoService _outProStockInfoService;
        private readonly IStockRepository _stockRepository;
        private readonly IOutProStockInfoRepository _outProStockInfoRepository;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IApiInfoRepository _apiInfoRepository;
 
        public IMesRworkOutboundOrderRepository Repository => BaseDal;
 
        public MesRworkOutboundOrderService(IMesRworkOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, IOutProStockInfoService outProStockInfoService, IStockRepository stockRepository, IOutProStockInfoRepository outProStockInfoRepository,IUnitOfWorkManage unitOfWorkManage, IApiInfoRepository apiInfoRepository) : base(BaseDal)
        {
            _basicRepository = basicRepository;
            _stockService = stockService;
            _outStockLockInfoService = outStockLockInfoService;
            _basicService = basicService;
            _recordService = recordService;
            _outProStockInfoService = outProStockInfoService;
            _stockRepository = stockRepository;
            _outProStockInfoRepository = outProStockInfoRepository;
            _unitOfWorkManage = unitOfWorkManage;
            _apiInfoRepository = apiInfoRepository;
        }
        /// <summary>
        /// 提库任务分配库存
        /// </summary>
        public (List<Dt_ProStockInfo>?,Dt_MesRworkOutboundOrder?,List<Dt_OutProStockInfo>?,List<Dt_LocationInfo>) AssignMesStocks(Dt_MesRworkOutboundOrder mesRworkOutboundOrder)
        {
            List<Dt_ProStockInfo> proStockInfos = new List<Dt_ProStockInfo>();
            Dt_MesRworkOutboundOrder assignOutOrder= new Dt_MesRworkOutboundOrder();
            List<Dt_OutProStockInfo> outProStockInfos=new List<Dt_OutProStockInfo>();
            List<Dt_LocationInfo> locationInfos=new List<Dt_LocationInfo>();
            float originalNeedQuantity = mesRworkOutboundOrder.RequiredQuantity;
 
            float needQuantity = originalNeedQuantity;
 
            //查找可用库存
            List<Dt_ProStockInfo> stockInfoss = _stockService.ProStockInfoService.GetUseableStocks(mesRworkOutboundOrder);
            if (!stockInfoss.Any())
            {
                throw new Exception("未找到可分配库存");
            }
            List<Dt_ProStockInfo> autoAssignStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfoss,mesRworkOutboundOrder, needQuantity,out float residueQuantity);
            mesRworkOutboundOrder.LockQuantity += needQuantity - residueQuantity;
            autoAssignStocks.OrderBy(x => x.proStockInfoDetails.FirstOrDefault()?.StockPcsQty).ToList();
            proStockInfos.AddRange(autoAssignStocks);
            float assignQuantity = needQuantity - residueQuantity;
 
            float orderQuantity = mesRworkOutboundOrder.RequiredQuantity;
            bool isCanDate = string.IsNullOrEmpty(mesRworkOutboundOrder.DateCode);
            for (int j = 0; j < autoAssignStocks.Count; j++)
            {
                //出库订单明细已分配数量
                float detailAssignQuantity = outProStockInfos
                    .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                    && x.PCode == mesRworkOutboundOrder.ProductCode 
                    && x.PVer == mesRworkOutboundOrder.ProductVersion
                    && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode))
                    .Sum(x => x.AssignQuantity);
 
                //出库详情已分配数量
                float palletAssignQuantity = outProStockInfos
                    .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                    && x.PCode == mesRworkOutboundOrder.ProductCode 
                    && x.PVer == mesRworkOutboundOrder.ProductVersion 
                    && x.PalletCode == autoAssignStocks[j].PalletCode
                    && (isCanDate ? isCanDate : x.DateCode == mesRworkOutboundOrder.DateCode))
                    .Sum(x => x.AssignQuantity);
 
                float palletOutboundQuantity = autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity);
                if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息
                {
                    float orderDetailNeedQuantity = mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity;
                    if (orderDetailNeedQuantity > autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity)
                    {
                        mesRworkOutboundOrder.LockQuantity += autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
                        Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity);
                        outProStockInfos.Add(outStockLockInfo);
                    }
                    else
                    {
                        Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], mesRworkOutboundOrder.RequiredQuantity-mesRworkOutboundOrder.LockQuantity);
                        outProStockInfos.Add(outStockLockInfo);
                        mesRworkOutboundOrder.LockQuantity = mesRworkOutboundOrder.RequiredQuantity;
                        break;
                    }
                }
            }
            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(proStockInfos.Select(x => x.LocationCode).ToList()));
 
            return (proStockInfos, mesRworkOutboundOrder, outProStockInfos, locationInfos);
        }
        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_ProStockInfo> stockInfos, List<Dt_OutProStockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
        {
            try
            {
                stockInfos.ForEach(x => {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                });
                _stockService.ProStockInfoService.Repository.UpdateData(stockInfos);
                List<Dt_ProStockInfoDetail> stockInfoDetails = new List<Dt_ProStockInfoDetail>();
                foreach (var item in stockInfos)
                {
                    stockInfoDetails.AddRange(item.proStockInfoDetails);
                }
                _stockService.ProStockInfoDetailService.Repository.UpdateData(stockInfoDetails);
 
                List<Dt_OutProStockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList();
                if (addOutStockLockInfos != null && addOutStockLockInfos.Any())
                {
                    if (tasks != null)
                    {
                        addOutStockLockInfos.ForEach(x =>
                        {
                            x.TaskNum = tasks.FirstOrDefault(v => v.PalletCode == x.PalletCode)?.TaskNum;
                        });
                    }
 
                    _outProStockInfoService.Repository.AddData(addOutStockLockInfos);
                }
                List<Dt_OutProStockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
                {
                    _outProStockInfoService.Repository.UpdateData(updateOutStockLockInfos);
                }
 
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus, LocationChangeType.OutboundAssignLocation, "", tasks?.Select(x => x.TaskNum).ToList());
                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 成品MES提库出库完后一包一包拣选
        /// </summary>
        /// <returns></returns>
        public WebResponseContent MesRworkOutPick(int OrderId,string InrPKCode,int Count=0)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //获取当前的Mes提库单
                Dt_MesRworkOutboundOrder rworkOutboundOrder = BaseDal.QueryFirst(x => x.Id == OrderId);
                if (rworkOutboundOrder == null)
                {
                    return content.Error("未找到当前Mes提库单");
                }
                if (rworkOutboundOrder.OrderStatus>=OutOrderStatusEnum.出库完成.ObjToInt())
                {
                    return content.Error("当前Mes提库单已完成或关闭");
                }
                //根据扫描内包号获取库存
                Dt_ProStockInfoDetail? proStockInfoDetail = _stockRepository.ProStockInfoDetailRepository.QueryFirst(x => x.BagNo == InrPKCode && x.ProOutDetailStatus == StockStatusEmun.出库完成.ObjToInt());
                if (proStockInfoDetail == null)
                {
                    return content.Error($"未找到内包{InrPKCode}信息");
                }
                //获取库存信息
                Dt_ProStockInfo? proStockInfo = _stockRepository.ProStockInfoRepository.Db.Queryable<Dt_ProStockInfo>().Where(x => x.Id == proStockInfoDetail.ProStockId).Includes(x => x.proStockInfoDetails).First();
                if (proStockInfo == null)
                {
                    return content.Error($"未找到内包{InrPKCode}库存信息");
                }
                //查询该托盘是否存在该单据的出库详情
                Dt_OutProStockInfo outProStockInfo = _outProStockInfoRepository.QueryFirst(x => x.OrderNo == rworkOutboundOrder.TaskNo && x.PalletCode == proStockInfo.PalletCode);
                if (outProStockInfo == null)
                {
                    return content.Error($"内包{InrPKCode}非单据{rworkOutboundOrder.TaskNo}内容");
                }
                //判断是否整包数量
                if (Count > 0)
                {
                    if (Count> proStockInfoDetail.StockPcsQty)
                    {
                        return content.Error($"内包数量{(int)proStockInfoDetail.StockPcsQty}<出库数量{Count}");
                    }
                    int setinterval = (int)proStockInfoDetail.StockPcsQty / (int)proStockInfoDetail.SETQty;
                    if (Count % setinterval!=0)
                    {
                        return content.Error($"获取SET数量失败,1SET:{setinterval}PCS");
                    }
                    proStockInfoDetail.OutSETQty = Count / setinterval;
                    proStockInfoDetail.OutboundQuantity = Count;
                }
                else
                {
                    proStockInfoDetail.OutSETQty = proStockInfoDetail.SETQty;
                    proStockInfoDetail.OutboundQuantity = proStockInfoDetail.StockPcsQty;
                }
                rworkOutboundOrder.OverOutQuantity += proStockInfoDetail.OutboundQuantity;
                if (rworkOutboundOrder.OverOutQuantity> rworkOutboundOrder.RequiredQuantity)
                {
                    return content.Error($"单据{rworkOutboundOrder.TaskNo}溢出{rworkOutboundOrder.OverOutQuantity - rworkOutboundOrder.RequiredQuantity}");
                }
                if (rworkOutboundOrder.OverOutQuantity== rworkOutboundOrder.RequiredQuantity)
                {
                    rworkOutboundOrder.OrderStatus = OutOrderStatusEnum.出库完成.ObjToInt();
                }
 
                int SetCount = (int)proStockInfoDetail.OutSETQty;
                int EligiblePcsCount = (int)proStockInfoDetail.OutboundQuantity;
                _unitOfWorkManage.BeginTran();
                if (proStockInfoDetail.SETQty == proStockInfoDetail.OutSETQty && proStockInfoDetail.StockPcsQty== proStockInfoDetail.OutboundQuantity)
                {
                    proStockInfo.proStockInfoDetails.Remove(proStockInfoDetail);
                    if (proStockInfo.proStockInfoDetails.Count==0)
                    {
                        _stockRepository.ProStockInfoRepository.DeleteAndMoveIntoHty(proStockInfo, OperateTypeEnum.自动删除);
                    }
                    _stockRepository.ProStockInfoDetailRepository.DeleteAndMoveIntoHty(proStockInfoDetail, OperateTypeEnum.自动删除);
                }
                else
                {
                    proStockInfoDetail.SETQty -= proStockInfoDetail.OutSETQty;
                    proStockInfoDetail.StockPcsQty -= proStockInfoDetail.OutboundQuantity;
                    proStockInfoDetail.OutSETQty = 0;
                    proStockInfoDetail.OutboundQuantity = 0;
                    _stockRepository.ProStockInfoDetailRepository.UpdateData(proStockInfoDetail);
                }
                BaseDal.UpdateData(rworkOutboundOrder);
                //MES成品库存板出库同步
                WebResponseContent responseContent = ProductOutBoundSync(MesProOutBound(rworkOutboundOrder, new List<Dt_ProStockInfoDetail> { proStockInfoDetail }, SetCount, EligiblePcsCount));
                if (responseContent.Status)
                {
                    throw new Exception("同步MES库存板出库失败,错误:" + responseContent.Message);
                }
                _unitOfWorkManage.CommitTran();
                if (rworkOutboundOrder.OrderStatus==OutOrderStatusEnum.出库完成.ObjToInt())
                {
                    content.OK($"单号{rworkOutboundOrder.TaskNo}已完成");
                }
                else
                {
                    content.OK($"单号{rworkOutboundOrder.TaskNo},剩余:{rworkOutboundOrder.RequiredQuantity-rworkOutboundOrder.OverOutQuantity}PCS");
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// 库存板数据转换
        /// </summary>
        /// <returns></returns>
        public MesProductOutBound MesProOutBound(Dt_MesRworkOutboundOrder mesRworkOutboundOrder, List<Dt_ProStockInfoDetail> proStockInfoDetails,int SetCount,int EligiblePcsCount)
        {
            //获取仓库信息
            Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == mesRworkOutboundOrder.WarehouseId);
            List<MesInventoryInfo> inventoryInfos = new List<MesInventoryInfo>();
            foreach (var item in proStockInfoDetails)
            {
                MesInventoryInfo mesInventoryInfo = new MesInventoryInfo()
                {
                    Warhouseno = warehouse.WarehouseCode,
                    InternalPackageNumber = item.BagNo,
                    SetCount = SetCount,
                    EligiblePcsCount = EligiblePcsCount
                };
                inventoryInfos.Add(mesInventoryInfo);
            }
            MesProductOutBound mesProductOutBound = new MesProductOutBound()
            {
                TaskNo = mesRworkOutboundOrder.TaskNo,
                ProductCode = mesRworkOutboundOrder.ProductCode,
                ProductVersion = mesRworkOutboundOrder.ProductVersion,
                DateCode = mesRworkOutboundOrder.DateCode,
                SaleOrder = mesRworkOutboundOrder.SaleOrder,
                InventoryInfo = inventoryInfos
            };
            return mesProductOutBound;
        }
        /// <summary>
        /// 成品库存板同步接口
        /// </summary>
        public WebResponseContent ProductOutBoundSync(MesProductOutBound model)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_ApiInfo apiInfo = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMS_MES_ProductOutBound.ToString());
                MESRoot<MesProductOutBound> root = new MESRoot<MesProductOutBound>()
                {
                    From = "WMS",
                    DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Content = model
                };
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };
                string request = JsonConvert.SerializeObject(root, settings);
                string response = HttpMesHelper.Post(apiInfo.ApiAddress, request);
                MesResponseContent mesResponseContent = response.DeserializeObject<MesResponseContent>();
                //调用接口
                if (mesResponseContent.BSucc == true)
                {
                    content.OK(mesResponseContent.StrMsg);
                }
                else
                {
                    content.Error(mesResponseContent.StrMsg);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}