1
huangxiaoqiang
2025-12-17 c4fa404692ed09b476d03fb7860766c072784469
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
using LogLibrary.Log;
using MailKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Inbound;
using WIDESEA_IInboundService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
 
namespace WIDESEA_InboundService
{
    public class InboundService : IInboundService
    {
        LogFactory LogFactory = new LogFactory();
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        public IInboundOrderDetailService InboundOrderDetailService { get; }
 
        public IInboundOrderService InbounOrderService { get; }
        private readonly IRepository<Dt_InboundOrder> _inboundOrderRepository;
        private readonly IRepository<Dt_WarehouseArea> _warehouseAreaRepository;
        private readonly IRepository<Dt_LocationType> _locationTypeRepository;
        private readonly IRepository<Dt_StockInfo> _stockInfoRepository;
        private readonly IRepository<Dt_InboundOrderDetail> _inboundOrderDetailRepository;
        private readonly IRepository<Dt_Task> _taskRepository;
        private IStockService _stockService;
 
        public InboundService(IUnitOfWorkManage unitOfWorkManage, IInboundOrderDetailService inboundOrderDetailService, IInboundOrderService inbounOrderService, IRepository<Dt_InboundOrder> inboundOrderRepository, IRepository<Dt_WarehouseArea> warehouseAreaRepository, IRepository<Dt_LocationType> locationTypeRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IStockService stockService, IRepository<Dt_Task> taskRepository)
        {
            _unitOfWorkManage = unitOfWorkManage;
            InboundOrderDetailService = inboundOrderDetailService;
            InbounOrderService = inbounOrderService;
            _inboundOrderRepository = inboundOrderRepository;
            _warehouseAreaRepository = warehouseAreaRepository;
            _locationTypeRepository = locationTypeRepository;
            _stockInfoRepository = stockInfoRepository;
            _inboundOrderDetailRepository = inboundOrderDetailRepository;
            _stockService = stockService;
            _taskRepository = taskRepository;
        }
 
        public async Task<WebResponseContent> GroupPallet(GroupPalletDto palletDto)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(palletDto);
                if (!result2.Item1) return content.Error(result2.Item2);
 
                var code = _warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>().Where(x => x.Code == palletDto.WarehouseType).Select(x => x.Code).First();
                if (string.IsNullOrEmpty(code))
                {
                    return content.Error($"仓库中没有该{palletDto.WarehouseType}编号。");
                }
 
                Dt_InboundOrder inboundOrder = new Dt_InboundOrder();
                var details = _inboundOrderDetailRepository.QueryData(x => (x.OutBoxbarcodes == palletDto.Barcode|| x.Barcode == palletDto.Barcode) && x.OrderDetailStatus == (int)InOrderStatusEnum.未开始);
 
                if (details.Count() <= 0)
                {
                    return content.Error("未找到该条码单据信息请确认是否已经组盘完成");
                }
                inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Includes(x=>x.Details).Where(x => x.Id == details.First().OrderId).First();
 
                if (inboundOrder == null)
                {
                    return content.Error("未找到该条码主单信息");
                }
                Dt_StockInfo? stockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
 
                List<string?> materielCodes = details.GroupBy(x => x.Barcode).Select(x => x.Key).ToList();
 
                (bool, string, object?) result = CheckMaterielGroupParam(palletDto, materielCodes, inboundOrder, stockInfo);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
 
 
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None, LocationType = Convert.ToInt32(palletDto.locationType) };
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
 
                if (stockInfo != null && stockInfo.Details.Count>0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
                {
                    return content.Error($"该托盘组盘仓库为{stockInfo.Details.FirstOrDefault()?.WarehouseCode}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘");
                }
 
                foreach (var item in details)
                {
                    stockInfo.Details.Add(new Dt_StockInfoDetail
                    {
                        StockId = stockInfo == null ? 0 : stockInfo.Id,
                        Barcode = item.Barcode,
                        MaterielCode = item.MaterielCode,
                        BatchNo = item.BatchNo,
                        Unit = item.Unit,
                        InboundOrderRowNo = item.lineNo,
                        SupplyCode = item.SupplyCode,
                        WarehouseCode = palletDto.WarehouseType,
                        StockQuantity = item.OrderQuantity,
                        BarcodeQty = item.BarcodeQty,
                        BarcodeUnit = item.BarcodeUnit,
                        FactoryArea = inboundOrder.FactoryArea,
                        Status = 0,
                        OrderNo = inboundOrder.InboundOrderNo,
                        BusinessType = inboundOrder.BusinessType,
 
                    });
 
                    item.ReceiptQuantity = item.BarcodeQty;
                    item.OrderDetailStatus = (int)OrderDetailStatusEnum.GroupAndInbound;
                    item.WarehouseCode = palletDto.WarehouseType;
                    item.ReturnToMESStatus = 0;
                }
 
                if (stockInfo.Id == 0)
                {
                    stockInfo.PalletCode = palletDto.PalletCode;
                    stockInfo.StockStatus = (int)StockStatusEmun.组盘暂存;
                }
                stockInfo.PalletType = (int)PalletTypeEnum.None;
 
                List<int> updateDetailIds = details.Select(x => x.Id).ToList();
                if (inboundOrder.OrderStatus == (int)InOrderStatusEnum.未开始)
                {
                    inboundOrder.OrderStatus = (int)InOrderStatusEnum.入库中;
                }
                inboundOrder.Operator = App.User.UserName;
                content = MaterielGroupUpdateData(inboundOrder, details, stockInfo).Result;
                if (content.Status)
                {
                    Dt_StockInfo? NewstockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
 
                    return WebResponseContent.Instance.OK(data: NewstockInfo.Details.OrderByDescending(x => x.Id));
                }
                else
                {
                    content = WebResponseContent.Instance.Error(content.Message);
                }
            }
            catch (Exception ex)
            {
                LogFactory.GetLog($"组盘信息").Info(true, $"【异常】:【{ex.Message}】{Environment.NewLine}【{ex.StackTrace}】{Environment.NewLine}{Environment.NewLine}");
                return content.Error(ex.Message);
            }
            return content;
        }
 
        /// <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(GroupPalletDto materielGroupDTO, List<string> barcodeCodes, 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 != (int)StockStatusEmun.组盘暂存)
            {
                return (false, "已上架的托盘不能再次组盘", materielGroupDTO);
            }
 
            if (_stockService.StockInfoDetailService.ExistBarcodes(barcodeCodes))
            {
                return (false, $"{barcodeCodes[0]} 条码在库存中已存在", materielGroupDTO);
            }     
 
            if (inboundOrder == null)
            {
                return (false, "单据不存在", materielGroupDTO);
            }
 
            if (inboundOrder.Details == null || inboundOrder.Details.Count == 0)
            {
                return (false, "无单据明细信息", materielGroupDTO);
            }
 
            if (inboundOrder.OrderStatus != (int)InOrderStatusEnum.未开始 && inboundOrder.OrderStatus != (int)InOrderStatusEnum.入库中)
            {
                return (false, "该单据不可再组盘", materielGroupDTO);
            }
 
            List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => barcodeCodes.Contains(x.Barcode)).ToList();
 
            if (inboundOrderDetails.GroupBy(x => x.Barcode).Count() != barcodeCodes.Count)
            {
                return (false, "有物料不在单据内", materielGroupDTO);
            }
 
            return (true, "成功", materielGroupDTO);
        }
 
        public async Task<WebResponseContent> MaterielGroupUpdateData(Dt_InboundOrder inboundOrder, List<Dt_InboundOrderDetail> inboundOrderDetails, Dt_StockInfo stockInfo)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                //await _inboundOrderRepository.Db.UpdateNav(inboundOrder).Include(x=>x.Details).ExecuteCommandAsync();
                _inboundOrderRepository.UpdateData(inboundOrder);
                _inboundOrderDetailRepository.UpdateData(inboundOrderDetails);
                _stockService.StockInfoService.AddMaterielGroup(stockInfo);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}