sjz
2 天以前 bf780b6c83012b5beb3d7515b8fb61753c5fb736
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
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Dto.webApi.DeliveryNote;
using MES.Service.Modes;
using MES.Service.Modes.DeliveryNote;
using SqlSugar;
 
public class MesDeliveryNoteBarcodeManager : Repository<MesDeliveryNoteBarcode>
{
    public List<ReturnBarcode> Save(PrintBarcode barcode)
    {
        int ftype= UseTransaction(db =>
        {
            return SaveOrUpdateData(db, barcode) ? 1 : 0;
        });
        var barcodes = new List<ReturnBarcode>();
 
        if (ftype >0)
        {
            barcodes = GetMesShdBarcode(barcode.LineNo);
        }
 
        return barcodes;
    }
 
    public int SaveBatch(PrintBarcode barcode)
    {
        int ftype = UseTransaction(db =>
        {
            return SaveOrUpdateData(db, barcode) ? 1 : 0;
        });
 
        return ftype;
    }
 
    private bool SaveOrUpdateData(SqlSugarScope db, PrintBarcode barcode)
    {
        //定义输入参数
        var inputParam1 = new SugarParameter("PI_ID", barcode.LineNo);
        var inputParam2 = new SugarParameter("PI_QTY", barcode.IncludeQty);
        var inputParam3 = new SugarParameter("PI_COUNT", barcode.FCount);
        var inputParam4 = new SugarParameter("PI_BOXQTY", barcode.PackageQty);
        // 定义输出参数
        var outParam1 = new SugarParameter("PO_RESULT", null, true);
        var outParam2 = new SugarParameter("PO_MSG", null, true);
        // 使用 SqlSugar 执行存储过程
        Db.Ado.ExecuteCommand("BEGIN PRC_MES_CREATE_SHD_BARCODE(:PI_ID,:PI_QTY,:PI_COUNT,:PI_BOXQTY,:PO_RESULT,:PO_MSG); END;", inputParam1,inputParam2,inputParam3,inputParam4,outParam1,outParam2);
        // 获取输出参数的值
        int result = int.Parse((string)outParam1.Value);
        string message = outParam2.Value == DBNull.Value ? string.Empty : (string)outParam2.Value;
        if (result == 1)
        {
            //存储过程失败则事务进行回滚
            db.Ado.RollbackTran();
            throw new Exception(message);
        }
        // 提交事务
        db.Ado.CommitTran();
        return true;
        throw new NotImplementedException("生成失败");
    }
 
 
    public dynamic SaveList(List<PrintBarcode> barcode)
    {
        var result = barcode.Select(SaveBatch).ToList();
 
        List<ReturnBarcode> barcodes = GetMesShdBarcodeBatch(barcode);
 
        return barcodes;
    }
    public dynamic DeleteBarcode(ErpBarcode barcode)
    {
        return UseTransaction(db =>
        {
            return DeleteData(db, barcode) ? 1 : 0;
        }) > 0;
    }
    public dynamic DeleteBarcodeList(List<ErpBarcode> barcode)
    {
        var result = barcode.Select(DeleteBarcode).ToList();
        return result.All(b => b);
    }
 
    // 删除数据的方法
    private bool DeleteData(SqlSugarScope db, ErpBarcode barcode)
    {
        var del = db.Deleteable<MesDeliveryNoteBarcode>().Where(s => s.SmallBarcode == barcode.SmallBarcode).ExecuteCommand() > 0;
 
        if (del) { 
            return true;
        } 
        throw new NotImplementedException("删除失败");
    }
 
    //返回条码
    public List<ReturnBarcode> GetMesShdBarcode(string LineNo)
    {
        var db = SqlSugarHelper.GetInstance();
        return db.Queryable<MesDeliveryNoteBarcode>()
            .Where(t => t.LineNo == LineNo)
            .OrderBy(t => t.Id)
            .Select(t => new ReturnBarcode
            {
                Barcodes = t.SmallBarcode,
                LineNo = t.LineNo,
                IncludeQty = t.IncludeQty
            })
            .ToList();
    }
 
    //返回条码(批量)
    public List<ReturnBarcode> GetMesShdBarcodeBatch(List<PrintBarcode> dtoList)
    {
        var barcodeList = new List<ReturnBarcode>();
        var db = SqlSugarHelper.GetInstance();
 
        foreach (var dto in dtoList)
        {
            // 确保查询返回的是 List<ReturnBarcode>
            var barcode = db.Queryable<MesDeliveryNoteBarcode>()
                            .Where(t => t.LineNo == dto.LineNo)
                            .OrderBy(t => t.Id)
                            .Select(t => new ReturnBarcode
                            {
                                Barcodes = t.SmallBarcode,
                                LineNo = t.LineNo,
                                IncludeQty = t.IncludeQty
                            })
                            .ToList();
 
            // 使用 AddRange 将 barcode 中的所有对象添加到 barcodeList
            barcodeList.AddRange(barcode);
        }
 
        return barcodeList;
    }
 
}