kyy
2025-10-11 2dbdf56a285a9272425a02bb4acf28236c5c5dce
1、采购入库退货新增克重
已修改2个文件
124 ■■■■■ 文件已修改
Dto/service/WarehouseQuery.cs 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/Warehouse/MesInvItemInCDetailsManager.cs 121 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Dto/service/WarehouseQuery.cs
@@ -41,5 +41,8 @@
    public string? itemNo { get; set; }
    public string? message { get; set; }
    //克重
    public decimal? weight { get; set; }
}
service/Warehouse/MesInvItemInCDetailsManager.cs
@@ -18,7 +18,125 @@
{
    //当前类已经继承了 Repository 增、删、查、改的方法
    public ItemInBaseModel SaveBarCodes(WarehouseQuery entity)
    {
        ItemInBaseModel res = new ItemInBaseModel();
        string connectionString = DbHelperSQL.strConn;
        // 参数验证
        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.barcode)) throw new ArgumentNullException(nameof(entity.barcode), "条码不允许为空");
        using (var conn = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand("[prc_pda_inv_cgrk]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    // 设置存储过程参数
                    SqlParameter[] parameters =
                    {
                        new SqlParameter("@pi_user", SqlDbType.NVarChar, 100) { Value = entity.userName },
                        new SqlParameter("@pi_barcode", SqlDbType.NVarChar, 100) { Value =  entity.barcode.Trim().ToLower() },
                        new SqlParameter("@pi_sectionCode", SqlDbType.NVarChar, 30) { Value = entity.sectionCode },
                        new SqlParameter("@pi_weight", SqlDbType.Decimal) { Value = entity.weight },
                        new SqlParameter("@po_outMsg", SqlDbType.NVarChar, 300) { Direction = ParameterDirection.Output },
                        new SqlParameter("@po_outSum", SqlDbType.NVarChar, 300) { Direction = ParameterDirection.Output }
                    };
                    cmd.Parameters.AddRange(parameters);
                    cmd.ExecuteNonQuery();
                    // 获取输出参数
                    string? outMessage = parameters[4].Value?.ToString();
                    string? outSum = parameters[5].Value?.ToString();
                    if (string.IsNullOrEmpty(outMessage) || string.IsNullOrEmpty(outSum))
                        throw new InvalidOperationException("存储过程返回参数异常");
                    // 验证结果
                    if (!int.TryParse(outSum, out int result) || result <= 0)
                        throw new InvalidOperationException(outMessage);
                    // 调试输出
                    Console.WriteLine($"查询的条码值: '{entity.barcode}' (长度: {entity.barcode.Length})");
                    // 使用参数化查询并忽略大小写(适用于支持的数据库)
                    var itemBarcodeDetails = Db.Queryable<MesInvItemBarcodes>()
                        .Where(it => it.ItemBarcode.Trim().ToLower() == entity.barcode.Trim().ToLower())
                        .First() ?? throw new InvalidOperationException($"未找到条码 '{entity.barcode}' 的匹配信息");
                    // 调试输出(检查值和长度)
                    Console.WriteLine($"查询的条码值: '{entity.barcode}' (长度: {entity.barcode.Length})");
                    // 优化查询(去除空格并忽略大小写)
                    var MesInvItemInCDetails = Db.Queryable<MesInvItemInCDetails>()
                        .Where(it => it.ItemBarcode.Trim().ToLower() == entity.barcode.Trim().ToLower())
                        .First() ?? throw new InvalidOperationException($"未找到条码 '{entity.barcode}' 的匹配的入库信息");
                    // 从入库条码中获取有效的ItemInId
                    var cId = MesInvItemInCDetails.ItemInId; // 假设存在ItemInId属性
                    // 查询物料汇总明细
                    var sql = string.Format(@"
                    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 = '{0}' AND a.Item_Id = '{1}'", cId, itemBarcodeDetails.ItemId);
                    res.ItemInDetails = Db.Ado.SqlQuery<dynamic>(sql);
                    // 查询入库条码明细
                    var sql2 = string.Format(@"
                    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 = '{0}' AND a.Item_Id = '{1}'", cId, itemBarcodeDetails.ItemId);
                    res.ItemBarCDetails = Db.Ado.SqlQuery<dynamic>(sql2);
                    // 设置返回信息
                    res.Message = outMessage;
                    res.SumQuantity = result;
                    return res;
                }
                catch (Exception ex)
                {
                    // 记录异常日志(建议添加日志记录)
                    res.Message = $"操作失败: {ex.Message}";
                    res.SumQuantity = -1;
                    return res;
                }
            }
        }
    }
    /*  public ItemInBaseModel SaveBarCodes(WarehouseQuery entity)
    {
        string functionName = "采购入库", fieldName = null, inFieldVal = null;
        string user = entity.userName,
@@ -881,7 +999,7 @@
        return result;
    }
  */
    public ItemInBaseModel getPurchaseInventory(WarehouseQuery query)
    {
        return new ItemInBaseModel
@@ -1318,6 +1436,7 @@
                        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("@pi_weight", SqlDbType.Decimal) { Value = entity.weight },
                        new("@po_outMsg", SqlDbType.NVarChar, 2000) { Direction = ParameterDirection.Output },
                        new("@po_outSum", SqlDbType.Int) { Direction = ParameterDirection.Output }
                    };