dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
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
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
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_Core.Utilities;
using WIDESEA_DTO;
using WIDESEA_DTO.Inbound;
using WIDESEA_IBasicService;
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model.Models;
 
namespace WIDESEA_InboundService
{
    public partial class InboundOrderService : ServiceBase<Dt_InboundOrder, IInboundOrderRepository>, IInboundOrderService
    {
 
        /// <summary>
        /// 根据入库单号获取入库单(含明细)
        /// </summary>
        /// <param name="inboundOrderNo"></param>
        /// <returns></returns>
        public Dt_InboundOrder GetInboundOrder(string inboundOrderNo)
        {
            return BaseDal.Db.Queryable<Dt_InboundOrder>().Includes(x => x.Details).First(x => x.OrderNo == inboundOrderNo); ;
        }
 
        public WebResponseContent MaterielGroup(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var palletCode = saveModel.MainData["palletCode"].ToString();
                if (string.IsNullOrEmpty(palletCode)) throw new Exception("托盘号不可为空");
                var orderNo = saveModel.MainData["orderNo"].ToString();
                if (string.IsNullOrEmpty(orderNo)) throw new Exception("单号不可为空");
                var batchNo = saveModel.MainData["batchNo"].ToString();
                if (string.IsNullOrEmpty(batchNo)) throw new Exception("批号不可为空");
                //var StationCode = saveModel.MainData["address"].ToString();
                //if (string.IsNullOrEmpty(StationCode)) throw new Exception("地址不可为空");
 
                //Dt_RoadwayInfo roadwayInfo = _basicService.RoadwayInfoService.Repository.QueryFirst(x => x.InStationCode == StationCode) ?? throw new Exception("未找到该入库站台,请检查基础配置信息!");
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(palletCode);
                if (stockInfo != null) throw new Exception($"托盘【{palletCode}】已存在库存信息");
                stockInfo = new Dt_StockInfo();
                stockInfo.Details = new List<Dt_StockInfoDetail>();
                Dt_InboundOrder? inboundOrder = GetInboundOrder(orderNo);
                if (inboundOrder == null) throw new Exception($"未找到单号为【{orderNo}】的入库单");
                Dt_InboundOrderDetail? inboundOrderDetail = inboundOrder.Details.Where(x => x.BatchNo == batchNo).FirstOrDefault();
                if (inboundOrderDetail == null) throw new Exception($"单号【{orderNo}】的入库单未找到批号【{batchNo}】");
                if (inboundOrderDetail.OrderQuantity - inboundOrderDetail.ReceiptQuantity < 1) throw new Exception($"单号【{orderNo}】的批号【{batchNo}】可组盘数量不足");
 
                #region 处理入库单
                inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt();
                inboundOrderDetail.ReceiptQuantity++;
                inboundOrderDetail.OrderDetailStatus = inboundOrderDetail.OverInQuantity == inboundOrderDetail.OrderQuantity ? OrderDetailStatusEnum.Over.ObjToInt() : OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                #endregion
 
                #region 添加库存
                if (stockInfo.Id == 0)
                {
                    stockInfo.PalletCode = palletCode;
                    stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                    stockInfo.Creater = "System";
                    stockInfo.Remark = "人工组盘入库";
                }
                Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                {
                    Status = StockStatusEmun.组盘暂存.ObjToInt(),
                    OrderNo = inboundOrder.OrderNo,
                    StockId = stockInfo.Id != 0 ? stockInfo.Id : 0,
                    MaterielName = inboundOrderDetail.MaterielName,
                    MaterielCode = inboundOrderDetail.MaterielCode,
                    BatchNo = inboundOrderDetail.BatchNo,
                    StockQuantity = 1,
                    SerialNumber = "",
                    Creater = "System"
                };
                stockInfo.Details.Add(stockInfoDetail);
                #endregion
                Db.Ado.BeginTran();
                BaseDal.UpdateData(inboundOrder);
                _inboundOrderDetailService.UpdateData(inboundOrderDetail);
                _stockService.StockInfoService.AddMaterielGroup(stockInfo);
                Db.Ado.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        public WebResponseContent QueryOrderDetailInfo(int pageNo, string orderNo)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_InboundOrder inboundOrder = GetInboundOrder(orderNo);
            List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details;
            content.OK(data: inboundOrderDetails);
            return content;
        }
        /// <summary>
        /// 组盘
        /// </summary>
        /// <param name="materielGroupDTO"></param>
        /// <returns></returns>
        public WebResponseContent MaterielGroup(MaterielGroupDTO materielGroupDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<MatSerialNumberDTO> matSerialNumberDTOs = CodeAnalysisHelper.CodeAnalysis<MatSerialNumberDTO>(AnalysisCode.InnerCode, materielGroupDTO.SerialNumbers);
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(matSerialNumberDTOs);
                if (!result2.Item1) return content = WebResponseContent.Instance.Error(result2.Item2);
 
                List<string> materielCodes = matSerialNumberDTOs.GroupBy(x => x.MaterielCode).Select(x => x.Key).ToList();
 
                List<Dt_MaterielInfo> materielInfos = _basicService.MaterielInfoService.GetMaterielInfos(materielCodes);
 
                Dt_InboundOrder inboundOrder = GetInboundOrder(materielGroupDTO.OrderNo);
 
 
                Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode);
                //if (stockInfo != null) throw new Exception($"托盘号【{materielGroupDTO.PalletCode}】已存在");
 
                (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, matSerialNumberDTOs, materielInfos, materielCodes, inboundOrder, stockInfo);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
 
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo();
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
 
                List<Dt_StockInfoDetail> stockInfoDetails = _mapper.Map<List<Dt_StockInfoDetail>>(matSerialNumberDTOs);
                stockInfoDetails.ForEach(x =>
                {
                    //x.Status = 0;
                    x.Status = StockStatusEmun.组盘暂存.ObjToInt();
                    x.OrderNo = inboundOrder.OrderNo;
                    x.MaterielName = materielInfos.FirstOrDefault(v => v.MaterielCode == x.MaterielCode)?.MaterielName ?? "";
                    x.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0;
                });
                if (stockInfo.Id == 0)
                {
                    stockInfo.PalletCode = materielGroupDTO.PalletCode;
                    stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                }
                stockInfo.Details.AddRange(stockInfoDetails);
 
                List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>();
                for (int i = 0; i < materielCodes.Count; i++)
                {
                    decimal stockQuantity = stockInfoDetails.Where(x => x.MaterielCode == materielCodes[i]).Sum(x => x.StockQuantity);
                    List<Dt_InboundOrderDetail> orderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCodes[i]).ToList();
                    if (orderDetails.Count > 0)
                    {
                        var OrderQuantity = orderDetails.Sum(x => x.OrderQuantity);
                        if (OrderQuantity < stockQuantity) throw new Exception("组盘数量超出单据数量");
                        inboundOrderDetails.AddRange(_inboundOrderDetailService.UpdateReceiptQuantity(orderDetails, stockQuantity));
                    }
                }
                List<int> updateDetailIds = inboundOrderDetails.Select(x => x.Id).ToList();
                if (inboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null && inboundOrder.Details.FirstOrDefault(x => !updateDetailIds.Contains(x.Id) && x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null)
                {
                    inboundOrder.OrderStatus = InboundStatusEnum.入库完成.ObjToInt();
                    BaseDal.DeleteAndMoveIntoHty(inboundOrder, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                    for (int i = 0; i < inboundOrderDetails.Count; i++)
                    {
                        _inboundOrderDetailService.Repository.DeleteAndMoveIntoHty(inboundOrderDetails[i], App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                    }
                }
                else if (inboundOrder.OrderStatus == InboundStatusEnum.未开始.ObjToInt())
                {
                    inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt();
                }
 
                content = MaterielGroupUpdateData(inboundOrder, inboundOrderDetails, stockInfo);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
 
        /// <summary>
        /// 组盘数据更新
        /// </summary>
        /// <param name="inboundOrder">入库单</param>
        /// <param name="inboundOrderDetails">入库单明细</param>
        /// <param name="stockInfo">组盘数据</param>
        /// <returns></returns>
        public WebResponseContent MaterielGroupUpdateData(Dt_InboundOrder inboundOrder, List<Dt_InboundOrderDetail> inboundOrderDetails, Dt_StockInfo stockInfo)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                UpdateData(inboundOrder);
                _inboundOrderDetailService.UpdateData(inboundOrderDetails);
                _stockService.StockInfoService.AddMaterielGroup(stockInfo);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 验证组盘数据
        /// </summary>
        /// <param name="materielGroupDTO">物料组盘DTO</param>
        /// <param name="matSerialNumberDTOs">扫码序列号</param>
        /// <param name="materielInfos">物料信息</param>
        /// <param name="materielCodes">物料编号</param>
        /// <param name="inboundOrder">入库单据</param>
        /// <param name="stockInfo">组盘信息</param>
        /// <returns></returns>
        public (bool, string, object?) CheckMaterielGroupParam(MaterielGroupDTO materielGroupDTO, List<MatSerialNumberDTO> matSerialNumberDTOs, List<Dt_MaterielInfo> materielInfos, List<string> materielCodes, Dt_InboundOrder inboundOrder, Dt_StockInfo stockInfo)
        {
            (bool, string, object?) result = ModelValidate.ValidateModelData(materielGroupDTO);
            if (!result.Item1) return result;
 
            if (_taskRepository.QueryFirst(x => x.PalletCode == materielGroupDTO.PalletCode) != null)
            {
                return (false, "该托盘号已有任务", materielGroupDTO);
            }
 
            if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
            {
                return (false, "已上架的托盘不能再次组盘", materielGroupDTO);
            }
 
            if (_stockService.StockInfoDetailService.ExistSerialNumbers(materielGroupDTO.SerialNumbers))
            {
                return (false, "有序列号在库存中已存在", materielGroupDTO);
            }
 
            if (materielInfos.Count != materielCodes.Count)
            {
                return (false, "有物料信息未录入,请录入物料信息", materielGroupDTO);
            }
 
            if (materielCodes.Count > 1 && materielInfos.FirstOrDefault(x => !x.IsMixMateriel) != null)
            {
                return (false, "有物料不可混料组盘", materielGroupDTO);
            }
 
            List<string> batchs = matSerialNumberDTOs.GroupBy(x => x.BatchNo).Select(x => x.Key).ToList();
            if (batchs.Count > 1 && materielInfos.FirstOrDefault(x => !x.IsMixMateriel) != null)
            {
                return (false, "有物料不可混批组盘", materielGroupDTO);
            }
 
            if (inboundOrder == null)
            {
                return (false, "单据不存在", materielGroupDTO);
            }
 
            if (inboundOrder.Details == null || inboundOrder.Details.Count == 0)
            {
                return (false, "无单据明细信息", materielGroupDTO);
            }
 
            if (inboundOrder.OrderStatus != InboundStatusEnum.未开始.ObjToInt() && inboundOrder.OrderStatus != InboundStatusEnum.入库中.ObjToInt())
            {
                return (false, "该单据不可再组盘", materielGroupDTO);
            }
 
            List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => materielCodes.Contains(x.MaterielCode)).ToList();
 
            if (inboundOrderDetails.GroupBy(x => x.MaterielCode).Count() != materielCodes.Count)
            {
                return (false, "有物料不在单据内", materielGroupDTO);
            }
 
            IGrouping<string, Dt_InboundOrderDetail>? temp = inboundOrder.Details.Where(x => materielCodes.Contains(x.MaterielCode)).GroupBy(x => x.MaterielCode).FirstOrDefault(x => x.Sum(v => v.OverInQuantity) >= x.Sum(v => v.OrderQuantity) || x.Sum(v => v.ReceiptQuantity) >= x.Sum(v => v.OrderQuantity));
            if (temp != null)
            {
                return (false, "有物料超出单据数量", materielGroupDTO);
            }
 
            return (true, "成功", materielGroupDTO);
        }
 
        /// <summary>
        /// PDA分页查询数据
        /// </summary>
        /// <param name="pageNo">页码</param>
        /// <param name="orderNo">订单号</param>
        /// <returns></returns>
        public WebResponseContent QueryOrderInfo(int pageNo, string orderNo)
        {
            if (string.IsNullOrEmpty(orderNo))
            {
                object obj = Repository.QueryPage(x => true, pageNo, 10, new Dictionary<string, OrderByType> { { nameof(Dt_InboundOrder.CreateDate), OrderByType.Desc } }).Rows.Select(x => new
                {
                    x.OrderNo,
                    x.UpperOrderNo,
                    x.CreateDate,
                    x.Creater
                });
                return WebResponseContent.Instance.OK(data: obj);
            }
            else
            {
                object obj = Repository.QueryPage(x => x.OrderNo == orderNo, pageNo, 10, new Dictionary<string, OrderByType> { { nameof(Dt_InboundOrder.CreateDate), OrderByType.Desc } }).Rows.Select(x => new
                {
                    x.OrderNo,
                    x.UpperOrderNo,
                    x.CreateDate,
                    x.Creater
                });
                return WebResponseContent.Instance.OK(data: obj);
            }
        }
    }
}