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 { public List Save(PrintBarcode barcode) { int ftype= UseTransaction(db => { return SaveOrUpdateData(db, barcode) ? 1 : 0; }); var barcodes = new List(); 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 barcode) { var result = barcode.Select(SaveBatch).ToList(); List barcodes = GetMesShdBarcodeBatch(barcode); return barcodes; } public dynamic DeleteBarcode(ErpBarcode barcode) { return UseTransaction(db => { return DeleteData(db, barcode) ? 1 : 0; }) > 0; } public dynamic DeleteBarcodeList(List barcode) { var result = barcode.Select(DeleteBarcode).ToList(); return result.All(b => b); } // 删除数据的方法 private bool DeleteData(SqlSugarScope db, ErpBarcode barcode) { var del = db.Deleteable().Where(s => s.SmallBarcode == barcode.SmallBarcode).ExecuteCommand() > 0; if (del) { return true; } throw new NotImplementedException("删除失败"); } //返回条码 public List GetMesShdBarcode(string LineNo) { var db = SqlSugarHelper.GetInstance(); return db.Queryable() .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 GetMesShdBarcodeBatch(List dtoList) { var barcodeList = new List(); var db = SqlSugarHelper.GetInstance(); foreach (var dto in dtoList) { // 确保查询返回的是 List var barcode = db.Queryable() .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; } }