pan
2025-12-02 61d8f975a9f02a4e1d3bd0eb234851e6da39fff4
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
using Microsoft.AspNetCore.Http;
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.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Outbound;
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
 
namespace WIDESEA_IOutboundService
{
    public partial class OutStockLockInfoService : ServiceBase<Dt_OutStockLockInfo, IRepository<Dt_OutStockLockInfo>>, IOutStockLockInfoService
    {
        public IRepository<Dt_OutStockLockInfo> Repository => BaseDal;
        public IUnitOfWorkManage _unitOfWorkManage;
 
        private readonly IStockService _stockService;
        private readonly IRecordService _recordService;
 
        public OutStockLockInfoService(IRepository<Dt_OutStockLockInfo> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IRecordService recordService) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _stockService = stockService;
            _recordService = recordService;
        }
 
 
        /// <summary>
        /// 创建出库锁定
        /// </summary>
        public Dt_OutStockLockInfo GetOutStockLockInfo(
            Dt_OutboundOrder outboundOrder,
            Dt_OutboundOrderDetail outboundOrderDetail,
            Dt_StockInfo outStock,
            decimal assignQuantity,
            string barcode = null,string outboundBatchNo = "")
        {
            // 获取库存明细信息
            var stockDetails = outStock.Details
                .Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode)
                .ToList();
 
            if (!stockDetails.Any())
            {
                throw new Exception($"未找到物料[{outboundOrderDetail.MaterielCode}]的库存明细");
            }
 
            // 确定条码
            string targetBarcode;
            var  firstAvailableDetail=new Dt_StockInfoDetail();  
            if (!string.IsNullOrEmpty(barcode))
            {
                // 验证指定的条码是否存在
                firstAvailableDetail = stockDetails.FirstOrDefault(x => x.Barcode == barcode);
                if (firstAvailableDetail == null)
                {
                    throw new Exception($"指定的条码[{barcode}]在库存中不存在");
                }
 
                targetBarcode = barcode;
            }
            else
            {
                // 使用第一个可用条码
                  firstAvailableDetail = stockDetails
                    .Where(x => x.StockQuantity > x.OutboundQuantity)
                    .OrderByDescending(x => x.CreateDate)
                    .FirstOrDefault();
 
                if (firstAvailableDetail == null)
                {
                    throw new Exception($"物料[{outboundOrderDetail.MaterielCode}]没有可用库存");
                }
                targetBarcode = firstAvailableDetail.Barcode;
            }
 
            return new Dt_OutStockLockInfo()
            {                      
                PalletCode = outStock.PalletCode,
                AssignQuantity = assignQuantity,              
                LocationCode = outStock.LocationCode,
                MaterielName = outboundOrderDetail.MaterielName,
                OrderDetailId = outboundOrderDetail.Id,
                OrderNo = outboundOrder.OrderNo,
                OrderQuantity = outboundOrderDetail.OrderQuantity,
                OriginalQuantity = outStock.Details
                    .Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode)
                    .Sum(x => x.StockQuantity),
                Status = (int)OutLockStockStatusEnum.已分配,
                StockId = outStock.Id,     
                OrderType=outboundOrder.OrderType,
                SupplyCode = firstAvailableDetail.SupplyCode,                
                WarehouseCode = firstAvailableDetail.WarehouseCode,
                // 新增字段
                CurrentBarcode = targetBarcode,
                OriginalLockQuantity = assignQuantity,
                IsSplitted = 0,
                MaterielCode = outboundOrderDetail.MaterielCode,
                BatchNo = firstAvailableDetail.BatchNo,
                Unit = firstAvailableDetail.Unit,
                BarcodeQty = firstAvailableDetail.BarcodeQty,
                BarcodeUnit = firstAvailableDetail.BarcodeUnit,
                FactoryArea = firstAvailableDetail.FactoryArea,
                lineNo = outboundOrderDetail.lineNo,
                OutboundBatchNo= outboundBatchNo
            };
        }
 
 
        public List<Dt_OutStockLockInfo> GetOutStockLockInfos(Dt_OutboundOrder outboundOrder, Dt_OutboundOrderDetail outboundOrderDetail, List<Dt_StockInfo> outStocks, int? taskNum = null)
        {
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
 
            foreach (var item in outStocks)
            {
                Dt_OutStockLockInfo outStockLockInfo = new Dt_OutStockLockInfo()
                {
                    PalletCode = item.PalletCode,
                    AssignQuantity = item.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.OutboundQuantity),
                    MaterielCode = outboundOrderDetail.MaterielCode,
                    BatchNo = outboundOrderDetail.BatchNo,
                    LocationCode = item.LocationCode,
                    MaterielName = outboundOrderDetail.MaterielName,
                    OrderDetailId = outboundOrderDetail.Id,
                    OrderNo = outboundOrder.OrderNo,
                    OrderType = outboundOrder.OrderType,
                    OriginalQuantity = item.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity),
                    Status = taskNum == null ? OutLockStockStatusEnum.已分配.ObjToInt() : OutLockStockStatusEnum.出库中.ObjToInt(),
                    StockId = item.Id,
                    TaskNum = taskNum,
                  
 
                };
                outStockLockInfos.Add(outStockLockInfo);
            }
 
            return outStockLockInfos;
        }
 
        /// <summary>
        /// 根据订单明细ID获取出库锁定信息
        /// </summary>
        public async Task<List<Dt_OutStockLockInfo>> GetByOrderDetailId(int orderDetailId)
        {
            return await Db.Queryable<Dt_OutStockLockInfo>()
                .Where(x => x.OrderDetailId == orderDetailId)
                .OrderBy(x => x.Id)
                .ToListAsync();
        }
 
        public async Task<LockInfoDetailDto> GetLockInfoDetail(int lockInfoId)
        {
            var lockInfo = await Db.Queryable<Dt_OutStockLockInfo>()
                .LeftJoin<Dt_OutboundOrderDetail>((lockInfo, detail) => lockInfo.OrderDetailId == detail.Id)
                .Where((lockInfo, detail) => lockInfo.Id == lockInfoId)
                .Select((lockInfo, detail) => new LockInfoDetailDto
                {
                    Id = lockInfo.Id,
                    OrderNo = lockInfo.OrderNo,
                    OrderDetailId = lockInfo.OrderDetailId,
                    BatchNo = lockInfo.BatchNo,
                    MaterielCode = lockInfo.MaterielCode,
                    StockId = lockInfo.StockId,
                    OrderQuantity = lockInfo.OrderQuantity,
                    OriginalQuantity = lockInfo.OriginalQuantity,
                    AssignQuantity = lockInfo.AssignQuantity,
                    PickedQty = lockInfo.PickedQty,
                    LocationCode = lockInfo.LocationCode,
                    PalletCode = lockInfo.PalletCode,
                    Status = lockInfo.Status,
                    IsSplitted = lockInfo.IsSplitted,
                    ParentLockId = lockInfo.ParentLockId,
 
                    MaterielName = detail.MaterielName,
                    Unit = detail.Unit
                })
                .FirstAsync();
 
            return lockInfo;
        }
 
        /// <summary>
        /// 根据托盘编号获取出库锁定信息
        /// </summary>
        public async Task<List<Dt_OutStockLockInfo>> GetByPalletCode(string palletCode, int? status = null)
        {
            var query = Db.Queryable<Dt_OutStockLockInfo>()
                .Where(x => x.PalletCode == palletCode);
 
            if (status.HasValue)
            {
                query = query.Where(x => x.Status == status.Value);
            }
 
            return await query.OrderBy(x => x.Id).ToListAsync();
        }
 
        /// <summary>
        /// 获取托盘的锁定信息
        /// </summary>
        public async Task<List<Dt_OutStockLockInfo>> GetPalletLockInfos(string palletCode)
        {
            return await Db.Queryable<Dt_OutStockLockInfo>()
                .Where(x => x.PalletCode == palletCode && x.Status == (int)OutLockStockStatusEnum.出库中)
                .ToListAsync();
        }
 
        /// <summary>
        /// 更新出库锁定信息的条码(用于拆包操作)
        /// </summary>
        public async Task<WebResponseContent> UpdateLockInfoBarcode(int lockInfoId, string newBarcode)
        {
            try
            {
                var lockInfo = await Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.Id == lockInfoId)
                    .FirstAsync();
 
                if (lockInfo == null)
                    return WebResponseContent.Instance.Error("未找到出库锁定信息");
 
                // 验证新条码是否存在
                var stockDetail = await Db.Queryable<Dt_StockInfoDetail>()
                    .Where(x => x.Barcode == newBarcode &&
                               x.StockId == lockInfo.StockId &&
                               x.MaterielCode == lockInfo.MaterielCode)
                    .FirstAsync();
 
                if (stockDetail == null)
                    return WebResponseContent.Instance.Error("新条码在库存中不存在");
 
                // 更新条码和拆包状态
                lockInfo.CurrentBarcode = newBarcode;
                lockInfo.IsSplitted = 1;
 
                await Db.Updateable(lockInfo).ExecuteCommandAsync();
 
                return WebResponseContent.Instance.OK("更新条码成功");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error($"更新条码失败: {ex.Message}");
            }
        }
 
        public List<Dt_OutStockLockInfo> GetByOrderDetailId(int orderDetailId, OutLockStockStatusEnum? outStockStatus)
        {
            return BaseDal.QueryData(x => x.OrderDetailId == orderDetailId );
        }
 
    }
}