kyy
2025-08-29 bdfcb2760a617d0fb95ee934696a3dde477d7e68
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
using System.Data;
using Masuit.Tools;
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
using MES.Service.util;
using SqlSugar;
using DbType = System.Data.DbType;
 
namespace MES.Service.service.QC;
 
public class MesOqcItemsDetect02Manager : Repository<MesOqcItemsDetect02>
{
    public (List<MesOqcItemsDetect02> item, int TotalCount) GetPage(
        XJPageResult queryObj)
    {
        if (queryObj == null)
            throw new ArgumentNullException(nameof(queryObj));
 
        if (string.IsNullOrEmpty(queryObj.createUser))
            return (new List<MesOqcItemsDetect02>(), 0);
 
        if (queryObj.createUser.IsNullOrEmpty()) return ([], 0);
 
        var db = Db;
 
        //安全的类型转换
        if (!decimal.TryParse(queryObj.id, out var id))
            id = 0;
 
        var totalCount = 0;
 
        var pageList = Db.Queryable<MesOqcItemsDetect02, MesItems
                , SysUser, SysUser, MesDepots>((a, b, c, d, e) =>
                new JoinQueryInfos(JoinType.Left, a.ItemId == b.Id,
                    JoinType.Left, a.CreateBy == c.Fcode,
                    JoinType.Left, a.FcheckBy == d.Fcode,
                    JoinType.Left, a.DepotId == e.DepotId
                ))
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                "未完成".Equals(queryObj.result),
                (a, b, c, d, e) => a.FcheckResu == null || a.Fsubmit == 0)
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                !"未完成".Equals(queryObj.result),
                (a, b, c, d, e) => a.FcheckResu != null && a.Fsubmit == 1)
            .WhereIF(id > 0, (a, b, c, d, e) => a.Id == id)
            .WhereIF(StringUtil.IsNotNullOrEmpty(queryObj.createUser) && queryObj.SearchIndex != null, 
                (a, b, c, d, e) => c.Fname.Contains(queryObj.createUser))
            .WhereIF(StringUtil.IsNotNullOrEmpty(queryObj.ItemNo), 
                (a, b, c, d, e) => b.ItemNo.Contains(queryObj.ItemNo))
            .WhereIF(StringUtil.IsNotNullOrEmpty(queryObj.ItemName), 
                (a, b, c, d, e) => b.ItemName.Contains(queryObj.ItemName))
            .WhereIF(StringUtil.IsNotNullOrEmpty(queryObj.ReleaseNo), 
                (a, b, c, d, e) => a.ReleaseNo.Contains(queryObj.ReleaseNo))
            .OrderByDescending((a, b, c, d, e) => a.Id)
            .Select((a, b, c, d, e) => new MesOqcItemsDetect02()
            {
                ItemNo = b.ItemNo,
                ItemName = b.ItemName,
                ItemModel = b.ItemModel,
                CreateUser = c.Fname,
                FcheckUser = d.Fname,
                DepotName = e.DepotName
            }, true)
            .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount);
        return (pageList, totalCount);
    }
 
    public List<MesOqcItemsDetectDetail5> GetDetail5(OQCDto dto)
    {
        return Db.Queryable<MesOqcItemsDetectDetail5>()
            .Where(s => s.ReleaseNo == dto.ReleaseNo)
            .ToList();
    }
 
    public List<MesOqcItemsDetectDetail12> GetDetail12(OQCDto dto)
    {
        return Db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.ReleaseNo == dto.ReleaseNo
                        && s.MainId == dto.MainId)
            .ToList();
    }
 
    public MesInvItemStocks GetItemBarCode(XJPageResult queryObj)
    {
        if (queryObj.ItemCode.IsNullOrEmpty())
        {
            throw new NotImplementedException("请扫描条码");
        }
 
        var mesInvItemStocks = Db.Queryable<MesInvItemStocks>()
            .Where(a => a.ItemBarcode == queryObj.ItemCode)
            .First();
 
        if (mesInvItemStocks == null)
        {
            throw new NotImplementedException("条码不存在于库存中");
        }
 
        // 检查条码是否已经存在于MesOqcBarcode表中(已检验过)
        var existingBarcode = Db.Queryable<MesOqcBarcode>()
            .Where(a => a.ItemBarcode == queryObj.ItemCode)
            .First();
 
        if (existingBarcode != null)
        {
            throw new NotImplementedException("该条码已经检验过,不能重复检验");
        }
 
        var mesDepots = Db.Queryable<MesDepots>()
            .Where(s => s.DepotId == 121163).First();
 
        if (!mesDepots.DepotCode.Equals(mesInvItemStocks.DepotsCode))
        {
            throw new NotImplementedException("只能扫描" + mesDepots.DepotCode +
                                              "仓库的码");
        }
 
        if (mesInvItemStocks.ItemId is null or 0)
        {
            throw new NotImplementedException("物料是非法的");
        }
 
        var mesItems = Db.Queryable<MesItems>()
            .Where(b => b.Id == mesInvItemStocks.ItemId)
            .Select<MesItems>(b => new MesItems
            {
                ItemName = b.ItemName,
                ItemModel = b.ItemModel,
            })
            .First();
 
        mesInvItemStocks.ItemName = mesItems.ItemName;
        mesInvItemStocks.ItemModel = mesItems.ItemModel;
 
        return mesInvItemStocks;
    }
 
    //OQCDto
    public int ItemBarCodeSubmit(OQCDto dto)
    {
        //getbillcode1
        var billNo = Db.Ado.GetString(
            "SELECT getbillcode1('1000', '1000', 'OQC') FROM DUAL");
 
        var dtoItemBarCodeData = dto.ItemBarCodeData;
 
        var sum = dtoItemBarCodeData.Sum(s => s.Quantity);
 
        MesOqcItemsDetect02 from = new MesOqcItemsDetect02
        {
            CreateBy = dto.CreateUser,
            CreateDate = DateTime.Now,
            ReleaseNo = billNo,
            Fsubmit = 0,
            Ismodify1 = 0,
            FcheckBy = dto.CreateUser,
            FcheckDate = DateTime.Now,
            ItemNo = dtoItemBarCodeData[0].ItemNo,
            ItemId = dtoItemBarCodeData[0].ItemId,
            PlanQty = sum,
            SaleOrderNo = dtoItemBarCodeData[0].TaskNo,
            DepotId = 121163
        };
 
        var id = Db.Insertable(from)
            .IgnoreColumns(ignoreNullColumn: true)
            .ExecuteReturnIdentity();
 
        //记录条码
        List<MesOqcBarcode> oqcBarcodes = new List<MesOqcBarcode>();
 
        foreach (var mesInvItemStockse in dtoItemBarCodeData)
        {
            MesOqcBarcode entity = new MesOqcBarcode()
            {
                CreateBy = dto.CreateUser,
                CreateDate = DateTime.Now,
                ItemBarcode = mesInvItemStockse.ItemBarcode,
                ItemId = mesInvItemStockse.ItemId,
                ItemNo = mesInvItemStockse.ItemNo,
                Qty = mesInvItemStockse.Quantity,
                Pid = id,
                SaleOrderNo = mesInvItemStockse.TaskNo,
            };
            oqcBarcodes.Add(entity);
        }
 
        var executeCommand = Db.Insertable(oqcBarcodes).PageSize(1)
            .IgnoreColumnsNull()
            .ExecuteCommand();
 
        //PRC_OQC_ITEM_INSERT_BTN
        if (executeCommand > 0 && id > 0)
        {
            // 定义输出参数
            var outputResult = new SugarParameter("PO_RESULT", null,
                DbType.Int32, ParameterDirection.Output, 4000);
            var outputMessage = new SugarParameter("PO_TEXT", null,
                DbType.String,
                ParameterDirection.Output, 4000);
 
            // 定义输入参数
            var parameters = new List<SugarParameter>
            {
                new("P_RELEASE_NO", from.ReleaseNo, DbType.String,
                    ParameterDirection.Input),
                new("P_ITEM_ID", from.ItemId, DbType.Int32,
                    ParameterDirection.Input),
                new("P_BILL_NO  ", "", DbType.String,
                    ParameterDirection.Input),
                outputResult,
                outputMessage
            };
 
            var db = SqlSugarHelper.GetInstance();
 
            // 使用 SqlSugar 执行存储过程
            db.Ado.ExecuteCommand(
                "BEGIN PRC_OQC_ITEM_INSERT_BTN(:P_RELEASE_NO,:P_ITEM_ID,:P_BILL_NO,:PO_RESULT, :PO_TEXT); END;",
                parameters.ToArray());
 
            // 获取输出参数的值
            var resultValue = outputResult.Value?.ToString();
            var messageValue = outputMessage.Value?.ToString();
 
            if ("1".Equals(resultValue))
                // 处理失败情况,返回错误信息
                throw new Exception($"操作失败: {messageValue}");
 
            // 当 resultValue 为 "0" 时返回成功状态
            return id;
        }
 
        return 0;
    }
 
    public bool Regenerate(MesOqcItemsDetect02 from)
    {
        // 定义输出参数
        var outputResult = new SugarParameter("PO_RESULT", null,
            DbType.Int32, ParameterDirection.Output, 4000);
        var outputMessage = new SugarParameter("PO_TEXT", null,
            DbType.String,
            ParameterDirection.Output, 4000);
 
        // 定义输入参数
        var parameters = new List<SugarParameter>
        {
            new("P_RELEASE_NO", from.ReleaseNo, DbType.String,
                ParameterDirection.Input),
            new("P_ITEM_ID", from.ItemId, DbType.Int32,
                ParameterDirection.Input),
            new("P_BILL_NO  ", "", DbType.String,
                ParameterDirection.Input),
            outputResult,
            outputMessage
        };
 
        var db = SqlSugarHelper.GetInstance();
 
        // 使用 SqlSugar 执行存储过程
        db.Ado.ExecuteCommand(
            "BEGIN PRC_OQC_ITEM_INSERT_BTN(:P_RELEASE_NO,:P_ITEM_ID,:P_BILL_NO,:PO_RESULT, :PO_TEXT); END;",
            parameters.ToArray());
 
        // 获取输出参数的值
        var resultValue = outputResult.Value?.ToString();
        var messageValue = outputMessage.Value?.ToString();
 
        if ("1".Equals(resultValue))
            // 处理失败情况,返回错误信息
            throw new Exception($"操作失败: {messageValue}");
 
        // 当 resultValue 为 "0" 时返回成功状态
        return true;
    }
 
    public OQCDto getXjDetail02ById(decimal? id)
    {
        var rkjDto = new OQCDto();
 
        var qsItemOqcItem =
            Db.Queryable<MesOqcItemsDetectDetail5>().Single(s => s.Id == id);
 
        /* if (qsItemOqcItem.IsPass == 0)
             qsItemOqcItem.Result = "不合格";
         else if (qsItemOqcItem.IsPass == 1)
             qsItemOqcItem.Result = "合格";
         else
             qsItemOqcItem.Result = "未完成";
 
         if (qsItemOqcItem.Picture is { Length: > 0 })
             qsItemOqcItem.imageData =
                 Convert.ToBase64String(qsItemOqcItem.Picture);
 */
        //获取不合格数
        var count = Db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.Fstand == "×" && s.MainId == id).Count();
 
        qsItemOqcItem.Unqualified = count;
 
        rkjDto.ItemXj01 = qsItemOqcItem;
 
 
        rkjDto.ItemXj02s = Db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.MainId == id)
            .ToList();
 
        return rkjDto;
    }
 
    public int UpdateQSItemDetail(MesOqcItemsDetectDetail12 detail)
    {
        var withOracle = SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<MesOqcItemsDetectDetail12>()
                .SetColumns(s => s.LastupdateBy == detail.LastupdateBy)
                // .SetColumns(s => s.LastupdateDate == DateTime.Now)
                .SetColumnsIF(StringUtil.IsNotNullOrEmpty(detail.Fstand),
                    s => s.Fstand == detail.Fstand)
                .SetColumnsIF(StringUtil.IsNotNullOrEmpty(detail.FcheckResu),
                    s => s.FcheckResu == detail.FcheckResu)
                .Where(s => s.MainId == detail.MainId && s.Id == detail.Id)
                .ExecuteCommand();
        });
 
        detail.CreateBy = detail.LastupdateBy;
 
        withOracle += autoResult(detail);
 
        return withOracle;
    }
 
    private int autoResult(MesOqcItemsDetectDetail12 detail)
    {
        var db = SqlSugarHelper.GetInstance();
 
        // Single 没有查询到结果时返回Null,如果结果大于1条会抛出错误
        var QsItemOqcItem = db.Queryable<MesOqcItemsDetectDetail5>()
            .Single(s => s.Id == detail.MainId);
 
        if (QsItemOqcItem == null) return 0;
 
        //查询这个检验项目下的检验结果
        var count = db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.MainId == detail.MainId).Count();
 
        updateDetail5(detail);
        var result = "";
 
        //检验实际结果不等于应该检验的个数时直接推出
        if (QsItemOqcItem.CheckQyt != count) return 0;
 
        //合格的有多少个
        var passCount = db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.MainId == detail.MainId && s.Fstand == "√").Count();
        //不合格的有多少个
        var noCount = db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.MainId == detail.MainId && s.Fstand == "×").Count();
 
        if (count == passCount)
            result = "合格";
        //else if (count - passCount < QsItemOqcItem.FreQty) 
        //    result = "不合格";
        else if (noCount >= QsItemOqcItem.FreQty)
            result = "不合格";
        var useTransactionWithOracle =
            SqlSugarHelper.UseTransactionWithOracle(db =>
            {
                var commit = 0;
                commit += db.Updateable<MesOqcItemsDetectDetail5>()
                    .SetColumns(s => s.FcheckResu == result)
                    .SetColumns(s => s.FenterQty == count)
                    .Where(s => s.Id == detail.MainId)
                    .ExecuteCommand();
 
                return commit;
            });
 
        var isNull = db.Queryable<MesOqcItemsDetectDetail5>()
            .Where(s => s.ReleaseNo == detail.ReleaseNo && s.FcheckResu == null)
            .Count();
 
        if (isNull > 0) return 1;
        //获取检验单的检验项目的个数
        var sum = db.Queryable<MesOqcItemsDetectDetail5>()
            .Where(s => s.ReleaseNo == detail.ReleaseNo).Count();
 
        if (sum == 0) return 1;
 
        //获取检验单下的合格的检验项目个数
        var icount = db.Queryable<MesOqcItemsDetectDetail5>()
            .Where(s => s.ReleaseNo == detail.ReleaseNo && s.FcheckResu == "合格")
            .Count();
 
        var FcheckResu = "不合格";
 
        //实际个数等于理论个数时对检验单进行判定
        if (sum == icount)
            //合格的检验结果等于总检验数视为合格
            FcheckResu = "合格";
        var sysUser = db.Queryable<SysUser>()
            .Where(s => s.Fcode == detail.CreateBy).First();
 
        SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<MesOqcItemsDetect02>()
                .SetColumns(s => s.FcheckResu == FcheckResu)
                .SetColumns(s => s.FcheckDate == DateTime.Now)
                //.SetColumns(s => s.FcheckBy == sysUser.Fname)
                .SetColumns(s => s.LastupdateBy == detail.CreateBy)
                .SetColumns(s => s.LastupdateDate == DateTime.Now)
                .Where(s => s.ReleaseNo == detail.ReleaseNo)
                .ExecuteCommand();
        });
        //  if (FcheckResu.Equals("不合格"))
        //自动生成入库检异常对策
        ///     saveDetect02(detail.Id, detail.CreateBy);
 
        return useTransactionWithOracle;
    }
 
    private int updateDetail5(MesOqcItemsDetectDetail12 detail)
    {
        var db = SqlSugarHelper.GetInstance();
 
        //查询这个检验项目下的检验数量
        var count = db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.MainId == detail.MainId).Count();
 
        //获取不合格数
        var countNo = db.Queryable<MesOqcItemsDetectDetail12>()
            .Where(s => s.MainId == detail.MainId && s.Fstand == "×").Count();
        //更新检验明细已检数量  
        var withOracle = SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<MesOqcItemsDetectDetail5>()
                .SetColumns(s => s.FenterQty == count)
                .SetColumns(s => s.FngQty == countNo)
                .Where(s => s.Id == detail.MainId)
                .ExecuteCommand();
        });
 
        return withOracle;
    }
 
    public int saveRemarksPid(LLJDto dto)
    {
        return SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<MesOqcItemsDetectDetail5>()
                .SetColumns(it =>
                    it.Remarks == dto.Remarks) //SetColumns是可以叠加的 写2个就2个字段赋值
                .Where(it => it.Id == dto.pid)
                .ExecuteCommand();
        });
    }
 
    public bool IqcQaSubmit(LLJDto dto)
    {
        try
        {
            // 定义输出参数
            var outputResult = new SugarParameter("o_Result", null,
                DbType.Int32, ParameterDirection.Output,
                4000);
 
            var outputMessage = new SugarParameter("o_Msg", null,
                DbType.String,
                ParameterDirection.Output, 4000);
 
            // 定义输入参数
            var parameters = new List<SugarParameter>
            {
                new("p_Release_No", dto.releaseNo, DbType.String,
                    ParameterDirection.Input),
                new("p_User", dto.userNo, DbType.String,
                    ParameterDirection.Input),
                outputResult,
                outputMessage
            };
 
            var db = SqlSugarHelper.GetInstance();
 
            // 使用 SqlSugar 执行存储过程
            db.Ado.ExecuteCommand(
                "BEGIN PRC_MES_OQC_QA_SUBMIT82(:p_Release_No, :p_User, :o_Result, :o_Msg); END;",
                parameters.ToArray());
 
            // 获取输出参数的值
            var resultValue = outputResult.Value?.ToString();
            var messageValue = outputMessage.Value?.ToString();
 
            if ("1".Equals(resultValue)) throw new Exception(messageValue);
 
            return true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
 
    public int saveRemarksGid(LLJDto dto)
    {
        return SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<MesOqcItemsDetect02>()
                .SetColumns(it =>
                    it.Remeke == dto.Remarks) //SetColumns是可以叠加的 写2个就2个字段赋值
                .Where(it => it.ReleaseNo == dto.releaseNo)
                .ExecuteCommand();
        });
    }
 
    public int SetQSItemDetail(MesOqcItemsDetectDetail12 detail)
    {
        var oracle = SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            List<MesOqcItemsDetectDetail12> result = new();
            for (var i = 0; i < detail.count; i++)
            {
                var item = new MesOqcItemsDetectDetail12();
                item.MainId = detail.MainId;
                item.ReleaseNo = detail.ReleaseNo;
                item.Fstand = detail.Fstand;
                item.FcheckResu = detail.FcheckResu;
                item.CreateBy = detail.LastupdateBy;
                item.CreateDate = DateTime.Now;
                item.Factory = "1000";
                item.Company = "1000";
                result.Add(item);
            }
 
            return db.Insertable(result).ExecuteCommand();
        });
 
        detail.CreateBy = detail.LastupdateBy;
 
        autoResult(detail);
 
        return oracle;
    }
}