| | |
| | | using NewPdaSqlServer.util; |
| | | using Newtonsoft.Json; |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Data; |
| | | using System.Data.SqlClient; |
| | | using static Microsoft.EntityFrameworkCore.DbLoggerCategory; |
| | | |
| | | namespace NewPdaSqlServer.service.Warehouse; |
| | |
| | | |
| | | return encodedUrl; |
| | | } |
| | | |
| | | // 验退扫码 |
| | | public ItemInBaseModel GetReturnBarcodeInfo(WarehouseQuery entity) |
| | | { |
| | | ItemInBaseModel res = new ItemInBaseModel(); |
| | | |
| | | // 参数验证 |
| | | if (string.IsNullOrEmpty(entity.userName)) throw new ArgumentNullException(nameof(entity.userName), "用户名不允许为空"); |
| | | if (string.IsNullOrEmpty(entity.sectionCode)) throw new ArgumentNullException(nameof(entity.sectionCode), "库位编号不允许为空"); |
| | | if (string.IsNullOrEmpty(entity.returnBarcode)) throw new ArgumentNullException(nameof(entity.returnBarcode), "条码不允许为空"); |
| | | |
| | | try |
| | | { |
| | | // 检查条码是否已入库 |
| | | var isInStock = Db.Queryable<MesInvItemInCDetails>() |
| | | .Where(it => it.ItemBarcode == entity.returnBarcode) |
| | | .Any(); |
| | | |
| | | if (isInStock) |
| | | throw new Exception($"该条码物料[{entity.returnBarcode}]已入库,不能再次扫描!"); |
| | | |
| | | // 检查条码是否已扫描验退 |
| | | var sqlParams = new List<SugarParameter> { new("@barcode", entity.returnBarcode) }; |
| | | var isReturnScanned = Db.Ado.SqlQuery<dynamic>( |
| | | "SELECT 1 FROM MES_INV_ITEM_CGYT_C_DETAILS WHERE ITEM_BARCODE = @barcode", |
| | | sqlParams); |
| | | |
| | | |
| | | if (isReturnScanned.Count > 0) |
| | | throw new Exception($"该条码物料[{entity.returnBarcode}]已扫描验退,请核对!"); |
| | | // 使用参数化查询并忽略大小写(适用于支持的数据库) |
| | | var itemBarcode = Db.Queryable<MesInvItemBarcodes>() |
| | | .Where(it => it.ItemBarcode.Trim().ToLower() == entity.returnBarcode.Trim().ToLower()) |
| | | .First() ?? throw new InvalidOperationException($"未找到条码 '{entity.returnBarcode}' 的匹配信息"); |
| | | |
| | | // 将查询到的条码信息赋值给返回模型 |
| | | res.itemBarcodeDetails = itemBarcode; |
| | | |
| | | |
| | | res.Message = "查询成功"; |
| | | |
| | | |
| | | return res; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 记录异常日志(建议补充具体日志记录代码) |
| | | res.Message = $"操作失败: {ex.Message}"; |
| | | res.SumQuantity = -1; |
| | | return res; |
| | | } |
| | | } |
| | | |
| | | // 验退 |
| | | public ItemInBaseModel ConfirmReturn(WarehouseQuery entity) |
| | | { |
| | | ItemInBaseModel res = new ItemInBaseModel(); |
| | | |
| | | // 参数验证 |
| | | if (string.IsNullOrEmpty(entity.userName)) throw new ArgumentNullException(nameof(entity.userName), "用户名不允许为空"); |
| | | if (string.IsNullOrEmpty(entity.sectionCode)) throw new ArgumentNullException(nameof(entity.sectionCode), "库位编号不允许为空"); |
| | | if (string.IsNullOrEmpty(entity.returnBarcode)) throw new ArgumentNullException(nameof(entity.returnBarcode), "条码不允许为空"); |
| | | if (entity.returnQuantity <= 0) throw new ArgumentException("验退数量必须大于0", nameof(entity.returnQuantity)); |
| | | |
| | | try |
| | | { |
| | | // 检查条码是否已入库 |
| | | var isInStock = Db.Queryable<MesInvItemInCDetails>() |
| | | .Where(it => it.ItemBarcode == entity.returnBarcode) |
| | | .Any(); |
| | | |
| | | if (isInStock) |
| | | throw new Exception($"该条码物料[{entity.returnBarcode}]已入库,不能再次扫描!"); |
| | | |
| | | // 检查条码是否已扫描验退 |
| | | var sqlParams = new List<SugarParameter> { new("@barcode", entity.returnBarcode) }; |
| | | var isReturnScanned = Db.Ado.SqlQuery<dynamic>( |
| | | "SELECT 1 FROM MES_INV_ITEM_CGYT_C_DETAILS WHERE ITEM_BARCODE = @barcode", |
| | | sqlParams); |
| | | |
| | | |
| | | // 调用存储过程 prc_pda_bar_cf_before |
| | | string strMsg = ""; |
| | | string intSum = ""; |
| | | string cfBar = ""; |
| | | |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | using (var cmd = new SqlCommand("[prc_pda_bar_cf_before]", conn)) |
| | | { |
| | | conn.Open(); |
| | | cmd.CommandType = CommandType.StoredProcedure; |
| | | SqlParameter[] parameters = |
| | | { |
| | | new("@outMsg", SqlDbType.NVarChar, 300), |
| | | new("@outSum", SqlDbType.NVarChar, 300), |
| | | new("@barcode_new", SqlDbType.NVarChar, 300), |
| | | new("@c_user", entity.userName), |
| | | new("@p_old_barcode", entity.returnBarcode), |
| | | new("@p_qty", entity.returnQuantity), |
| | | }; |
| | | parameters[0].Direction = ParameterDirection.Output; |
| | | parameters[1].Direction = ParameterDirection.Output; |
| | | parameters[2].Direction = ParameterDirection.Output; |
| | | |
| | | foreach (var parameter in parameters) |
| | | cmd.Parameters.Add(parameter); |
| | | cmd.ExecuteNonQuery(); |
| | | strMsg = parameters[0].Value.ToString(); |
| | | intSum = parameters[1].Value.ToString(); |
| | | cfBar = parameters[2].Value.ToString(); |
| | | |
| | | var result = Convert.ToInt32(intSum); |
| | | if (result <= 0) throw new Exception(strMsg); |
| | | } |
| | | } |
| | | |
| | | // 调用验退存储过程 prc_pda_scan_CGYT,使用拆分后的新条码 |
| | | string ytMsg = ""; |
| | | string ytSum = ""; |
| | | string ytdh = ""; |
| | | |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | using (var cmd = new SqlCommand("prc_pda_scan_CGYT", conn)) |
| | | { |
| | | conn.Open(); |
| | | cmd.CommandType = CommandType.StoredProcedure; |
| | | SqlParameter[] ytParameters = |
| | | { |
| | | new("@pi_user", SqlDbType.NVarChar, 100) { Value = entity.userName }, |
| | | new("@pi_barcode", SqlDbType.NVarChar, 100) { Value = cfBar }, // 使用拆分后的新条码 |
| | | new("@po_outMsg", SqlDbType.NVarChar, 2000) { Direction = ParameterDirection.Output }, |
| | | new("@po_outSum", SqlDbType.Int) { Direction = ParameterDirection.Output }, |
| | | new("@po_ygdh", SqlDbType.NVarChar, 200) { Direction = ParameterDirection.Output } |
| | | }; |
| | | |
| | | cmd.Parameters.AddRange(ytParameters); |
| | | cmd.ExecuteNonQuery(); |
| | | |
| | | ytMsg = ytParameters[2].Value.ToString(); |
| | | ytSum = ytParameters[3].Value.ToString(); |
| | | ytdh = ytParameters[4].Value.ToString(); |
| | | |
| | | if (ytSum == "-1") throw new Exception(ytMsg); |
| | | } |
| | | } |
| | | |
| | | // 调用入库存储过程 prc_pda_inv_cgrk,使用原始条码 |
| | | string rkMsg = ""; |
| | | string rkSum = ""; |
| | | |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | using (var cmd = new SqlCommand("prc_pda_inv_cgrk", conn)) |
| | | { |
| | | conn.Open(); |
| | | cmd.CommandType = CommandType.StoredProcedure; |
| | | SqlParameter[] rkParameters = |
| | | { |
| | | new("@pi_user", SqlDbType.NVarChar, 100) { Value = entity.userName }, |
| | | new("@pi_barcode", SqlDbType.NVarChar, 100) { Value = entity.returnBarcode }, // 使用原始条码 |
| | | new("@pi_sectionCode", SqlDbType.NVarChar, 100) { Value = entity.sectionCode }, |
| | | new("@po_outMsg", SqlDbType.NVarChar, 2000) { Direction = ParameterDirection.Output }, |
| | | new("@po_outSum", SqlDbType.Int) { Direction = ParameterDirection.Output } |
| | | }; |
| | | |
| | | cmd.Parameters.AddRange(rkParameters); |
| | | cmd.ExecuteNonQuery(); |
| | | |
| | | rkMsg = rkParameters[3].Value.ToString(); |
| | | rkSum = rkParameters[4].Value.ToString(); |
| | | |
| | | if (rkSum == "-1") throw new Exception(rkMsg); |
| | | } |
| | | } |
| | | |
| | | // 使用参数化查询并忽略大小写(适用于支持的数据库) |
| | | var itemBarcode = Db.Queryable<MesInvItemBarcodes>() |
| | | .Where(it => it.ItemBarcode.Trim().ToLower() == entity.returnBarcode.Trim().ToLower()) |
| | | .First() ?? throw new InvalidOperationException($"未找到条码 '{entity.returnBarcode}' 的匹配信息"); |
| | | |
| | | // 将查询到的条码信息赋值给返回模型 |
| | | res.itemBarcodeDetails = itemBarcode; |
| | | |
| | | // 优化查询(去除空格并忽略大小写) |
| | | var mesInvItemInCDetails = Db.Queryable<MesInvItemInCDetails>() |
| | | .Where(it => it.ItemBarcode.Trim().ToLower() == entity.returnBarcode.Trim().ToLower()) |
| | | .First() ?? throw new InvalidOperationException($"未找到条码 '{entity.returnBarcode}' 的匹配的入库信息"); |
| | | |
| | | // 从入库条码中获取有效的ItemInId |
| | | var cId = mesInvItemInCDetails.ItemInId; // 假设存在ItemInId属性 |
| | | |
| | | // 查询物料汇总明细(使用参数化查询防止SQL注入) |
| | | var sql = @" |
| | | SELECT |
| | | b.Item_No AS ItemNo, |
| | | b.Item_Name AS ItemName, |
| | | b.Item_Model AS ItemModel, |
| | | a.Quantity AS FQty, |
| | | b.Item_Id AS FMaterialId, |
| | | CONVERT(VARCHAR(36), b.Guid) AS Id |
| | | FROM MES_INV_ITEM_IN_C_ITEMS a |
| | | LEFT JOIN Mes_Items b ON a.Item_Id = b.Item_Id |
| | | WHERE a.Item_In_Id = @cId AND a.Item_Id = @itemId"; |
| | | |
| | | res.ItemInDetails = Db.Ado.SqlQuery<dynamic>(sql, new { cId, itemId = itemBarcode.ItemId }); |
| | | |
| | | // 查询入库条码明细(使用参数化查询防止SQL注入) |
| | | var sql2 = @" |
| | | SELECT |
| | | b.Item_No AS ItemNo, |
| | | b.Item_Name AS ItemName, |
| | | b.Item_Model AS ItemModel, |
| | | a.Quantity AS FQty, |
| | | b.Item_Id AS FMaterialId, |
| | | CONVERT(VARCHAR(36), a.Guid) AS Id, |
| | | a.Depot_Section_Code AS kw, |
| | | a.Item_Barcode AS barcode |
| | | FROM Mes_Inv_Item_In_C_Details a |
| | | LEFT JOIN Mes_Items b ON a.Item_Id = b.Item_Id |
| | | WHERE a.Item_In_Id = @cId AND a.Item_Id = @itemId"; |
| | | |
| | | res.ItemBarCDetails = Db.Ado.SqlQuery<dynamic>(sql2, new { cId, itemId = itemBarcode.ItemId }); |
| | | |
| | | res.Message = $"验退操作成功,拆分后条码:{cfBar},验退单号:{ytdh}"; |
| | | res.SumQuantity = res.ItemBarCDetails?.Count ?? 0; |
| | | |
| | | return res; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 记录异常日志(建议补充具体日志记录代码) |
| | | res.Message = $"操作失败: {ex.Message}"; |
| | | res.SumQuantity = -1; |
| | | return res; |
| | | } |
| | | } |
| | | |
| | | } |