pan
2025-11-17 f5df33d51a8555d709a4e8369fa98ce70759ddfc
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
using Microsoft.AspNetCore.Http;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_DTO.Outbound;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_OutboundService
{
    internal class SplitPackageService : ServiceBase<Dt_SplitPackageRecord, IRepository<Dt_SplitPackageRecord>>, ISplitPackageService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        public IRepository<Dt_SplitPackageRecord> Repository => BaseDal;
 
        private readonly IStockInfoService _stockInfoService;
        private readonly IStockInfoDetailService _stockInfoDetailService;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
        private readonly IDailySequenceService _dailySequenceService;
 
        public SplitPackageService(IRepository<Dt_SplitPackageRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockInfoService stockInfoService, IOutStockLockInfoService outStockLockInfoService, IStockInfoDetailService stockInfoDetailService, IDailySequenceService dailySequenceService) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _stockInfoService = stockInfoService;
            _outStockLockInfoService = outStockLockInfoService;
            _stockInfoDetailService = stockInfoDetailService;
            _dailySequenceService = dailySequenceService;
        }
 
        /// <summary>
        /// 拆包拆箱操作
        /// </summary>
        public async Task<WebResponseContent> SplitPackage(SplitPackageDto request)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
 
                // 1. 验证出库锁定信息
                var lockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                          .Where(x => x.OrderNo == request.OrderNo &&
                           x.PalletCode == request.PalletCode &&
                           x.CurrentBarcode == request.OriginalBarcode &&
                           x.Status == 1)
                    .FirstAsync();
 
                if (lockInfo == null)
                    return WebResponseContent.Instance.Error("未找到有效的出库锁定信息");
 
                // 2. 检查剩余锁定数量
                decimal remainingLockQuantity = lockInfo.AssignQuantity - lockInfo.PickedQty;
                if (request.SplitQuantity > remainingLockQuantity)
                    return WebResponseContent.Instance.Error($"拆包数量不能大于剩余锁定数量,剩余:{remainingLockQuantity}");
 
                var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
               .Where(x => x.Barcode == request.OriginalBarcode && x.StockId == lockInfo.StockId)
               .FirstAsync();
                if (stockDetail == null)
                    throw new Exception($"未找到条码{request.OriginalBarcode}对应的库存记录");
 
                var seq = await _dailySequenceService.GetNextSequenceAsync();
                // 3. 生成新条码
                string newBarcode = "WSLOT" + DateTime.Now.ToString("yyyyMMdd") + seq.ToString()?.PadLeft(5, '0');
 
                decimal remainingQty = remainingLockQuantity - request.SplitQuantity;
 
                // 为拆包产生的新条码创建库存记录
                var newStockDetail = new Dt_StockInfoDetail
                {                    
                    StockId = lockInfo.StockId,
                    MaterielCode = lockInfo.MaterielCode,
                    OrderNo = lockInfo.OrderNo,
                    BatchNo = lockInfo.BatchNo,
                    StockQuantity = remainingQty,
                    OutboundQuantity = remainingQty, // 锁定全部数量
                    Barcode = newBarcode,
                    InboundOrderRowNo = stockDetail.InboundOrderRowNo,
                  
                };
                await _outStockLockInfoService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
 
                // 4. 创建新的出库锁定信息(新条码)
                var newLockInfo = new Dt_OutStockLockInfo
                {
                    
                    OrderNo = lockInfo.OrderNo,
                    OrderDetailId = lockInfo.OrderDetailId,
                    BatchNo = lockInfo.BatchNo,
                    MaterielCode = lockInfo.MaterielCode,
                    MaterielName = lockInfo.MaterielName,
                    StockId = lockInfo.StockId,
                    OrderQuantity = remainingQty,
                    OriginalQuantity = remainingQty,
                    AssignQuantity = remainingQty, // 新条码分配数量
                    PickedQty = 0, // 新条码未拣选
                    LocationCode = lockInfo.LocationCode,
                    PalletCode = lockInfo.PalletCode,
                    TaskNum = lockInfo.TaskNum,
                    Status = (int)OutLockStockStatusEnum.出库中,
                    Unit = lockInfo.Unit,
                    SupplyCode = lockInfo.SupplyCode,
                    OrderType=lockInfo.OrderType,
                    CurrentBarcode = newBarcode, // 新条码
                    OriginalLockQuantity = request.SplitQuantity,
                    IsSplitted = 1,
                    ParentLockId = lockInfo.Id // 记录父级锁定ID
                };
                await _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
 
                // 5. 更新原锁定信息的分配数量(减少拆包数量)
                lockInfo.AssignQuantity = request.SplitQuantity;
                await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
 
                // 6. 记录拆包历史(用于追踪)
                var splitHistory = new Dt_SplitPackageRecord
                {
                    FactoryArea = lockInfo.FactoryArea,
                    TaskNum=lockInfo.TaskNum,
                    OutStockLockInfoId = lockInfo.Id,
                    StockId = stockDetail?.StockId ?? 0,
                    Operator = App.User.UserName,
                    IsReverted = false,
                    OriginalBarcode = lockInfo.CurrentBarcode,
                    NewBarcode = newBarcode,
                    SplitQty = request.SplitQuantity,
                    RemainQuantity = lockInfo.RemainQuantity - request.SplitQuantity,
                    MaterielCode = lockInfo.MaterielCode,
                    SplitTime = DateTime.Now,
                    OrderNo = request.OrderNo,
                    PalletCode = request.PalletCode,
                    Status = (int)SplitPackageStatusEnum.已拆包
                };
                await Db.Insertable(splitHistory).ExecuteCommandAsync();
 
                _unitOfWorkManage.CommitTran();
 
                // 7. 回传新条码给MES
                // await SendBarcodeToMES(newBarcode, request.MaterielCode, request.SplitQuantity);
 
                return WebResponseContent.Instance.OK("拆包成功", new
                {
                    NewBarcode = newBarcode,
                    NewLockInfoId = newLockInfo.Id
                });
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error($"拆包失败: {ex.Message}");
            }
        }
 
        // 撤销拆包
        public async Task<WebResponseContent> RevertSplitPackage(string originalBarcode)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                // 查找最近的未撤销拆包记录
                var splitPackage = await Db.Queryable<Dt_SplitPackageRecord>()
                        .Where(x => x.OriginalBarcode == originalBarcode && !x.IsReverted)
                        .OrderByDescending(x => x.CreateDate)
                        .FirstAsync();
 
                if (splitPackage == null)
                    return WebResponseContent.Instance.Error("未找到拆包记录");
 
                // 检查新条码是否已拣选
                var newOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
                    .FirstAsync();
 
                if (newOutStockInfo.Status == 2)
                    return WebResponseContent.Instance.Error("新条码已拣选,无法撤销拆包");
 
                // 还原原出库详情数量
                var originalOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.CurrentBarcode == originalBarcode)
                    .FirstAsync();
 
                originalOutStockInfo.AssignQuantity += splitPackage.SplitQty;
                await _outStockLockInfoService.Db.Updateable(originalOutStockInfo).ExecuteCommandAsync();
 
                // 删除新出库详情记录
                await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
                    .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
                    .ExecuteCommandAsync();
 
                // 标记拆包记录为已撤销
                splitPackage.IsReverted = true;
                await Db.Updateable(splitPackage).ExecuteCommandAsync();
 
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK("撤销拆包成功");
 
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error($"撤销拆包失败:{ex.Message}");
            }
        }
 
        // 获取拆包信息
        public async Task<WebResponseContent> GetSplitPackageInfo(string orderNo, string palletCode, string barcode)
        {
            var outStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                .Where(x => x.OrderNo == orderNo &&
                           x.PalletCode == palletCode &&
                           x.CurrentBarcode == barcode &&
                           x.Status == 1)
                .FirstAsync();
 
            if (outStockInfo == null)
                return WebResponseContent.Instance.Error("未找到对应的出库信息");
 
            //var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
            //    .Where(x => x.Barcode == barcode)
            //    .FirstAsync();
 
            return WebResponseContent.Instance.OK("", new
            {
                MaterielCode = outStockInfo.MaterielCode,
                RemainQuantity = outStockInfo.RemainQuantity,
                Unit = outStockInfo.Unit
            });
        }
        /// <summary>
        /// 获取可拆包的出库锁定信息
        /// </summary>
        public async Task<WebResponseContent> GetSplitableLockInfos(int orderDetailId)
        {
            var lockInfos = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                .Includes(x => x.StockInfo)
                .Where(x => x.OrderDetailId == orderDetailId &&
                           x.Status == (int)OutLockStockStatusEnum.出库中 &&
                           x.AssignQuantity > x.PickedQty) // 还有未拣选数量
                .Select(x => new
                {
                    x.Id,
                    x.PalletCode,
                    x.LocationCode,
                    x.MaterielCode,
                    LockQuantity = x.AssignQuantity - x.PickedQty,
                    x.CurrentBarcode,
                    x.IsSplitted,
                    StockDetails = x.StockInfo.Details.Where(d => d.MaterielCode == x.MaterielCode)
                        .Select(d => new
                        {
                            d.Barcode,
                            AvailableQuantity = d.StockQuantity - d.OutboundQuantity
                        })
                        .ToList()
                })
                .ToListAsync();
 
            return WebResponseContent.Instance.OK(null, lockInfos);
        }
    }
}