kyy
2025-12-04 aa6c2ce07e6a8a99d3d861ce5fae8fa76f62d36f
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.service.@base;
using NewPdaSqlServer.util;
using SqlSugar;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
 
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 
namespace NewPdaSqlServer.service.Warehouse;
 
// 期初条码入库
public class InventoryManager : RepositoryNoEntity
{
    // 这个方法用于扫描库位条码
    public string ScanDepotNo(WarehouseQuery query)
    {
        var p_section_code = query.DepotCode;
 
        // 检查库位代码是否为空
        if (string.IsNullOrEmpty(p_section_code))
            throw new Exception("002[请扫库位条码!");
 
        // 查询库区库位信息
        var depotSection = Db.Queryable<MesDepotSections, MesDepots>((a, b) =>
                new JoinQueryInfos(
                    JoinType.Inner, a.DepotGuid == b.Guid
                ))
            .Where((a, b) => a.DepotSectionCode == p_section_code)
            .Select((a, b) => new
                { a.DepotSectionName, b.DepotCode, b.DepotName })
            .Single();
 
        // 如果库位代码不存在,则返回错误信息
        if (depotSection == null)
            throw new Exception("库位编码 " + p_section_code + " 不存在,请确认!");
 
        // 返回成功信息
        return "仓库:" + depotSection.DepotName + "  库位:" +
               depotSection.DepotSectionName;
    }
 
    // 这个方法用于扫描条码
    public WarehouseQuery ScanBarcode(WarehouseQuery query)
    {
        var p_item_barcode = query.barcode;
        var p_section_code = query.DepotCode;
        var c_user = query.userName;
        var p_bill_type_id = 100;
        var p_transction_no = 601;
 
 
 
 
        // 查询库区库位信息
        /* var depotSection = Db.Queryable<MesDepotSections, MesDepots>((a, b) =>
                 new JoinQueryInfos(
                     JoinType.Inner, a.DepotGuid == b.Guid
                 ))
             .Where((a, b) => a.DepotSectionCode == p_section_code)
             .Select((a, b) => new
             {
                 a.DepotSectionName,
                 b.DepotCode,
                 b.DepotName,
                 b.DepotId,
                 b.FSubsidiary
             })
             .Single();*/
 
        // ============================
        // 替换:使用原生 SqlConnection + SqlCommand 调用存储过程(参考 SaveBarCodes 方法风格)
        // ============================
        string connectionString = DbHelperSQL.strConn; // 复用参考代码中的连接字符串获取方式
        int po_depotId = 0;
        string po_depotNo = string.Empty;
        string po_depotName = string.Empty;
        string po_fSubsidiary = string.Empty;
        string po_outMsg = string.Empty;
        int po_outSum = -1;
 
 
   
 
        // 查询条码详细信息
        var c_mes_inv_item_barcodes = Db.Queryable<MesInvItemBarcodes>()
            .Where(t => t.ItemBarcode == p_item_barcode)
            .Single();
 
        if (c_mes_inv_item_barcodes.Memo != "初期物料")
            throw new Exception("002[条码不是期初条码,无法用期初入库!");
 
        using (var conn = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand("prc_pda_select_depot", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
 
                    // 设置存储过程参数(输入+输出)
                    SqlParameter[] parameters =
                    {
                    // 输入参数
                    new SqlParameter("@pi_code", SqlDbType.NVarChar, 100) { Value = p_item_barcode },
                    new SqlParameter("@pi_sectionCode", SqlDbType.NVarChar, 100) { Value = p_section_code },
                    // 输出参数
                    new SqlParameter("@po_depotId", SqlDbType.Int) { Direction = ParameterDirection.Output },
                    new SqlParameter("@po_depotNo", SqlDbType.NVarChar, 80) { Direction = ParameterDirection.Output },
                    new SqlParameter("@po_depotName", SqlDbType.NVarChar, 80) { Direction = ParameterDirection.Output },
                    new SqlParameter("@po_fSubsidiary", SqlDbType.NVarChar, 80) { Direction = ParameterDirection.Output },
                    new SqlParameter("@po_outMsg", SqlDbType.NVarChar, 2000) { Direction = ParameterDirection.Output },
                    new SqlParameter("@po_outSum", SqlDbType.Int) { Direction = ParameterDirection.Output }
                };
 
                    cmd.Parameters.AddRange(parameters);
                    cmd.ExecuteNonQuery();
 
                    // 获取输出参数值(注意:需判断 DBNull,避免空引用异常)
                    po_depotId = parameters[2].Value != DBNull.Value ? Convert.ToInt32(parameters[2].Value) : 0;
                    po_depotNo = parameters[3].Value != DBNull.Value ? parameters[3].Value.ToString() : string.Empty;
                    po_depotName = parameters[4].Value != DBNull.Value ? parameters[4].Value.ToString() : string.Empty;
                    po_fSubsidiary = parameters[5].Value != DBNull.Value ? parameters[5].Value.ToString() : string.Empty;
                    po_outMsg = parameters[6].Value != DBNull.Value ? parameters[6].Value.ToString() : string.Empty;
                    po_outSum = parameters[7].Value != DBNull.Value ? Convert.ToInt32(parameters[7].Value) : -1;
                }
                catch (Exception ex)
                {
                    throw new Exception($"调用存储过程 prc_pda_select_depot 失败:{ex.Message}");
                }
            }
        }
 
        // 验证存储过程执行结果
        if (po_outSum < 0 || string.IsNullOrEmpty(po_depotNo) || po_depotId <= 0)
        {
            throw new Exception(string.IsNullOrEmpty(po_outMsg) ? "获取仓库信息失败,请检查条码和库位是否正确" : po_outMsg);
        }
 
        // 构造与原 depotSection 结构一致的对象,确保后续代码兼容
        var depotSection = new
        {
            // DepotSectionName = string.Empty, // 存储过程未返回库位名称,如需使用可后续补充
            DepotCode = po_depotNo,          // 对应存储过程的 po_depotNo(仓库编码)
            DepotName = po_depotName,        // 对应存储过程的 po_depotName(仓库名称)
            DepotId = po_depotId,            // 对应存储过程的 po_depotId(仓库ID)
            FSubsidiary = po_fSubsidiary     // 对应存储过程的 po_fSubsidiary(组织)
        };
 
 
        //由于龙巍入库可不按仓位对应仓库来所以特此取消此校验
/*        var wmsManager = new WmsBaseMangeer();
        // 新增入库校验(调用存储过程)
        var checkResult = wmsManager.pdaInvJY(
            db: Db,
            edtUserNo: query.userName,
            barcode: p_item_barcode,
            sectionCode: p_section_code,
            stockId: depotSection.DepotId.ToString(),
            stockOrgId: depotSection.FSubsidiary,
            billNo: "", // 根据实际单据号传值
            transactionNo: "601" // 事务类型与存储过程匹配
        );
        
        if ( Convert.ToInt32(checkResult.result) < 1) 
            throw new Exception($"入库校验失败:{checkResult.strMsg}");*/
 
        UseTransaction(db =>
        {
            // 自动生成入库单
            var c_id = db.Queryable<MesInvItemIns>()
                .Where(d =>
                    d.Sapstatus == 0 &&
                    d.InsDate.Value.ToString("yyyy-MM-dd") ==
                    DateTime.Now.ToString("yyyy-MM-dd") &&
                    d.Status == 0 &&
                    d.TransctionNo == p_transction_no.ToString() &&
                    d.CbillNo == c_mes_inv_item_barcodes.BillNo &&
                    (d.SuppId ?? "0") ==
                    (c_mes_inv_item_barcodes.SuppId ?? "0") &&
                    d.DepotsCode == depotSection.DepotCode)
                .Single();
 
            if (c_id == null)
            {
                var billNo = BillNo.GetBillNo("IN(入库单)");
                // 生成入库单号
                var newMesInvItemIns = new MesInvItemIns
                {
                    Guid = Guid.NewGuid(),
                    TransctionNo = p_transction_no.ToString(),
                    DepotsCode = depotSection.DepotCode,
                    DepotsId = depotSection.DepotId,
                    BillTypeId = p_bill_type_id,
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    BillNo = billNo,
                    SuppId = c_mes_inv_item_barcodes.SuppId,
                    SuppNo = c_mes_inv_item_barcodes.SuppNo,
                    UrgentFlag = c_mes_inv_item_barcodes.UrgentFlag == null
                        ?
                        null
                        : c_mes_inv_item_barcodes.UrgentFlag.Value
                            ? "1"
                            : "0",
                    CbillNo = c_mes_inv_item_barcodes.BillNo,
                    Fstatus = 0,
                    ReceiveOrgId = depotSection.FSubsidiary,
                    InType = "期初入库",
                };
                db.Insertable(newMesInvItemIns)
                    .IgnoreColumns(true)
                    .ExecuteCommand();
                c_id = newMesInvItemIns;
            }
 
            // 插入入库单明细
            db.Insertable(new MesInvItemInCDetails
            {
                Guid = Guid.NewGuid(),
                ItemInId = c_id.Guid,
                BillNo = c_id.BillNo,
                ItemBarcode = p_item_barcode,
                Quantity = c_mes_inv_item_barcodes.Quantity,
                BarcodeFlag = true,
                EpFlag = true,
                WorkType = 1,
                ItemNo = c_mes_inv_item_barcodes.ItemNo,
                LotNo = c_mes_inv_item_barcodes.LotNo,
                SuppNo = c_mes_inv_item_barcodes.SuppNo,
                DepotId = depotSection.DepotId,
                DepotCode = depotSection.DepotCode,
                DepotSectionCode = p_section_code,
                ItemSname = c_mes_inv_item_barcodes.ItemSname,
                Unit = c_mes_inv_item_barcodes.Unit,
                CreateBy = c_user,
                CreateDate = DateTime.Now,
                LastupdateBy = c_user,
                LastupdateDate = DateTime.Now,
                Remark = c_mes_inv_item_barcodes.Memo,
                Ebeln = c_mes_inv_item_barcodes.Mblnr,
                EbelnLineNo = c_mes_inv_item_barcodes.Zeile,
                WorkNo = c_mes_inv_item_barcodes.WorkNo,
                WorkLine = c_mes_inv_item_barcodes.WorkLine,
                CbillNo = c_mes_inv_item_barcodes.BillNo,
                UrgentFlag = c_mes_inv_item_barcodes.UrgentFlag,
                BoardStyle = c_mes_inv_item_barcodes.BoardStyle,
                TaskNo = c_mes_inv_item_barcodes.TaskNo,
                ItemId = c_mes_inv_item_barcodes.ItemId,
                ReceiveOrgId = depotSection.FSubsidiary,
            }).IgnoreColumns(true).ExecuteCommand();
 
 
            db.Insertable(new MesInvBusiness2
            {
                Guid = Guid.NewGuid(),
                Status = 1,
                BillTypeId = p_bill_type_id,
                TransactionCode = p_transction_no.ToString(),
                BusinessType = 1,
                ItemBarcode = p_item_barcode,
                ItemNo = c_mes_inv_item_barcodes.ItemNo,
                LotNo = c_mes_inv_item_barcodes.LotNo,
                EpFlag = true,
                Quantity = c_mes_inv_item_barcodes.Quantity,
                InvDepotId = depotSection.DepotId,
                ToInvDepotsCode = depotSection.DepotCode,
                ToInvDepotSectionsCode = p_section_code,
                CreateBy = c_user,
                CreateDate = DateTime.Now,
                LastupdateBy = c_user,
                LastupdateDate = DateTime.Now,
                TaskNo = c_mes_inv_item_barcodes.TaskNo,
                BillNo = c_id.BillNo,
                WorkNo = c_mes_inv_item_barcodes.WorkNo,
                WorkLine = c_mes_inv_item_barcodes.WorkLine,
                SuppNo = c_mes_inv_item_barcodes.SuppNo,
                ItemId = c_mes_inv_item_barcodes.ItemId
            }).IgnoreColumns(true).ExecuteCommand();
 
            // 插入库存信息
            db.Insertable(new MesInvItemStocks
            {
                Guid = Guid.NewGuid(),
                TaskNo = c_mes_inv_item_barcodes.TaskNo,
                ItemBarcode = p_item_barcode,
                ItemNo = c_mes_inv_item_barcodes.ItemNo,
                LotNo = c_mes_inv_item_barcodes.LotNo,
                Quantity = c_mes_inv_item_barcodes.Quantity,
                EpFlag = c_mes_inv_item_barcodes.EpFlag == null
                    ? null
                    : (byte)(c_mes_inv_item_barcodes.EpFlag.Value ? 1 : 0),
                DepotsCode = depotSection.DepotCode,
                DepotId = depotSection.DepotId,
                DepotsId = depotSection.DepotId,
                DepotSectionsCode = p_section_code,
                CheckDate = c_mes_inv_item_barcodes.CreateDate,
                IndepDate = c_mes_inv_item_barcodes.CreateDate,
                BoardStyle = c_mes_inv_item_barcodes.BoardStyle,
                WorkNo = c_mes_inv_item_barcodes.WorkNo,
                WorkLine = c_mes_inv_item_barcodes.WorkLine,
                // ItemUnit = c_mes_inv_item_barcodes.Unit,
                SuppNo = c_mes_inv_item_barcodes.SuppNo,
                ItemId = c_mes_inv_item_barcodes.ItemId,
                IndepUserCode = c_user,
                StockOrgId = depotSection.FSubsidiary,
                // Fsubsidiary = c_organize
            }).IgnoreColumns(true).ExecuteCommand();
 
            return 1;
        });
 
        var mesItems = Db.Queryable<MesItems>()
            .Where(s => s.Id == c_mes_inv_item_barcodes.ItemId)
            .First();
 
        query.itemNo = mesItems.ItemNo;
        query.Num = c_mes_inv_item_barcodes.Quantity;
        query.message = "物料 " +
                        c_mes_inv_item_barcodes.ItemNo + " 本次收货总数: " +
                        c_mes_inv_item_barcodes.Quantity;
        // 返回成功信息
        return query;
    }
 
 
    public dynamic GetDepoptsInfo(dynamic unity)
    {
 
        //// 使用参数化查询防止SQL注入
        var sqlParams = new List<SugarParameter> { new("@sectionCode", unity.sectionCode) };
 
        var sql2 = @"        SELECT TOP 1  B.depot_code + '('+B.depot_name+')' depotsInfo,
                     B.FSubsidiary,
                     C.FNumber + '('+C.NAME+')' orgInfo
        FROM MES_DEPOT_SECTIONS A
                 LEFT JOIN
             MES_DEPOTS B ON A.depot_guid = B.Guid
            LEFT JOIN SYS_ORGANIZATION C ON C.FID = B.FSubsidiary
        WHERE a.depot_section_code = @sectionCode;";
 
        var XcslItem = Db.Ado.SqlQuery<dynamic>(sql2, sqlParams).FirstOrDefault();
 
        // 检查结果有效性
        if (XcslItem == null)
        {
            throw new Exception($"库位 [{unity.sectionCode}] 不存在,或所属仓库状态异常(可能被禁用或删除)。");
        }
 
        return XcslItem; // 返回第一行数据,如果没有则返回 null
    }
 
 
 
    public dynamic GetItemsList(dynamic unity)
    {
        var sqlParams = new List<SugarParameter> { new("@orgId", unity.orgId) };
 
        var sql2 = new StringBuilder(@"
        SELECT TOP 20 item_id, item_no, item_name, item_model,item_no+'---'+item_name AS wlInfo
        FROM MES_ITEMS
        WHERE FSubsidiary = @orgId");
 
        if (!string.IsNullOrWhiteSpace(unity.selectKey?.ToString()))
        {
            sqlParams.Add(new("@selectKey", unity.selectKey));
            sql2.Append(@"
            AND (item_no LIKE '%' + @selectKey + '%' 
            OR item_name LIKE '%' + @selectKey + '%' 
            OR item_model LIKE '%' + @selectKey + '%')");
        }
 
        var XcslItem = Db.Ado.SqlQuery<dynamic>(sql2.ToString(), sqlParams);
 
        if (XcslItem == null)
        {
            throw new Exception("该条件下无对应物料信息,请重新输入!");
        }
 
        return XcslItem;
    }
 
    public List<dynamic> ExecuteBeginBar(dynamic query)
    {
        if (query == null)
            throw new ArgumentNullException(nameof(query), "参数对象不能为null");
 
        // 增强参数校验
        if (string.IsNullOrEmpty(query.userName?.ToString()))
            throw new ArgumentException("用户账号不能为空", nameof(query.userName));
 
        if (string.IsNullOrEmpty(query.itemid?.ToString()))
            throw new ArgumentException("物料ID不能为空", nameof(query.itemid));
 
        var resultList = new List<dynamic>();
 
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        using (var cmd = new SqlCommand("rpt_BeginBar", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(new[]
            {
                new SqlParameter("@inUser", SqlDbType.NVarChar, 50) { Value = query.userName },
                new SqlParameter("@inItemId", SqlDbType.NVarChar, 50) { Value = query.itemid },
                new SqlParameter("@isDesign", SqlDbType.Int) { Value = 0 },
                new SqlParameter("@in4", SqlDbType.NVarChar, 20) { Value = query.in4 ?? DBNull.Value },
                new SqlParameter("@in5", SqlDbType.NVarChar, 20) { Value = query.num ?? DBNull.Value }
            });
 
            try
            {
                conn.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var barcode = reader.GetString(0);
                        
                        // 构建符合方法签名的参数对象
                        var scanQuery = new WarehouseQuery 
                        {
                            barcode = barcode,
                            DepotCode = query.sectionCode, // 从原始query获取库位编码
                            userName = query.userName // 从原始query获取用户账号
                        };
                        
                        try
                        {
                            // 调用正式签名方法
                            ScanBarcode(scanQuery);
                            var printData = GetPrintBar(barcode); 
                            resultList.Add(printData);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception($"期初条码打印入库存在异常{ex.Message},请重新打印!");
                        }
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                throw new Exception($"生成期初条码失败:{ex.Message}");
            }
        }
    }
 
    public dynamic GetPrintBar(string barcode)
    {
        var sqlParams = new List<SugarParameter> { new("@barcode", barcode) };
 
        var sql1 = @"    SELECT TOP 1 C.ITEM_NO,C.item_name,C.item_model,B.OLDQTY as QUANTITY,B.CREATE_DATE, '期初条码' AS BarType,A.ITEM_BARCODE,GETDATE() as print_date
            FROM MES_INV_ITEM_STOCKS A
            LEFT JOIN MES_INV_ITEM_BARCODES B ON A.ITEM_BARCODE = B.ITEM_BARCODE
            LEFT JOIN MES_ITEMS C ON C.item_id = B.ITEM_ID
            WHERE B.ITEM_BARCODE = @barcode";
 
        var XcslItem = Db.Ado.SqlQuery<dynamic>(sql1, sqlParams).First();
 
        return XcslItem;
    }
}