kyy
2025-10-08 4fae819b7e89a751699b44474f4dedca5bcecc6a
1、采购验退
已修改4个文件
306 ■■■■■ 文件已修改
Controllers/Warehouse/MesInvItemInCDetailsController.cs 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Dto/service/WarehouseQuery.cs 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
entity/Base/ItemInBaseModel.cs 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/Warehouse/MesInvItemInCDetailsManager.cs 242 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Controllers/Warehouse/MesInvItemInCDetailsController.cs
@@ -287,4 +287,53 @@
            return ResponseResult.ResponseError(ex);
        }
    }
    /// <summary>
    ///   验退扫码
    /// </summary>
    /// <returns></returns>
    [HttpPost("GetReturnBarcodeInfo")]
    public ResponseResult GetReturnBarcodeInfo(WarehouseQuery entity)
    {
        try
        {
            dynamic resultInfos = new ExpandoObject();
            resultInfos.tbBillList = m.GetReturnBarcodeInfo(entity);
            return new ResponseResult
            {
                status = 0,
                message = resultInfos.tbBillList.Message,
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return ResponseResult.ResponseError(ex);
        }
    }
    /// <summary>
    ///   验退
    /// </summary>
    /// <returns></returns>
    [HttpPost("ConfirmReturn")]
    public ResponseResult ConfirmReturn(WarehouseQuery entity)
    {
        try
        {
            dynamic resultInfos = new ExpandoObject();
            resultInfos.tbBillList = m.ConfirmReturn(entity);
            return new ResponseResult
            {
                status = 0,
                message = resultInfos.tbBillList.Message,
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return ResponseResult.ResponseError(ex);
        }
    }
}
Dto/service/WarehouseQuery.cs
@@ -15,6 +15,11 @@
    public DateTime? date { get; set; }
    public string? Type { get; set; }
    //验退条码、数量
    public string? returnBarcode { get; set; }
    public decimal? returnQuantity { get; set; }
    //库位编码
    public string? DepotCode { get; set; }
@@ -35,4 +40,6 @@
    public string? itemNo { get; set; }
    public string? message { get; set; }
}
entity/Base/ItemInBaseModel.cs
@@ -5,12 +5,16 @@
//调拨出入库单通用类
public class ItemInBaseModel
{
    //物料信息
    public MesItems itemDetail { get; set; }
    //条码明细
    //入库条码明细
    public List<dynamic>? ItemBarCDetails { get; set; }
    //物料汇总明细
    public List<dynamic>? ItemInDetails { get; set; }
    public MesInvItemBarcodes itemBarcodeDetails { get; set; }
    //出/入库单信息
    public List<dynamic>? MesInvItem { get; set; }
@@ -19,4 +23,6 @@
    //入库总数
    public decimal? SumQuantity { get; set; }
}
service/Warehouse/MesInvItemInCDetailsManager.cs
@@ -7,6 +7,9 @@
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;
@@ -1140,4 +1143,243 @@
        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;
        }
    }
}