xwt
2025-10-17 707e8f07abb295629f73557759cf0e708ca6b5fa
StandardInterface/MES.Service/service/QC/LljService.cs
@@ -26,78 +26,153 @@
        var totalCount = 0;
        // 使用数据库分页查询,一次查询50条,提升性能
        var pageList = db.Queryable<LtsLlj, V_LljUser>((a, v) =>
                new JoinQueryInfos(JoinType.Left, a.ItemNo == v.ItemNo))
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                "未完成".Equals(queryObj.result),
                (a, v) => a.FcheckResu == null)
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                !"未完成".Equals(queryObj.result),
                (a, v) => a.FcheckResu != null)
            .WhereIF(id > 0, (a, v) => a.Id == id)
            // 权限控制:PL017、HMLYY、HMCS 可以看到所有单据,其他用户需要根据物料维护情况判断
            .WhereIF(queryObj.createUser != "PL017" && queryObj.createUser != "HMLYY" && queryObj.createUser != "HMCS" && queryObj.UserIndex == "0",
                (a, v) =>
                    // 如果物料被维护,只有维护人员可以看到
                    (v.Fcode != null && v.Fcode == queryObj.createUser) ||
                    // 如果物料未被维护,所有人都可以看到
                    (v.Fcode == null))
            //加筛选条件,根据选择的搜索字段进行精确搜索
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 1, // 物料编号搜索
                (a, v) => a.ItemNo != null && a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 2, // 物料名称搜索
                (a, v) => a.ItemName != null && a.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 3, // 供应商搜索
                (a, v) => a.SuppName != null && a.SuppName.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 4, // 到货单号搜索
                (a, v) => a.LotNo != null && a.LotNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 5, // 检验单号搜索
                (a, v) => a.ReleaseNo != null && a.ReleaseNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 6, // 物料规格搜索
                (a, v) => a.ItemModel != null && a.ItemModel.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .Select((a, v) => new LtsLlj
            {
                Id = a.Id,
                ItemNo = a.ItemNo,
                ItemId = a.ItemId,
                ItemName = a.ItemName,
                ItemModel = a.ItemModel,
                SuppName = a.SuppName,
                LotNo = a.LotNo,
                ReleaseNo = a.ReleaseNo,
                FcheckResu = a.FcheckResu,
                CreateDate = a.CreateDate,
                FcovertQty = a.FcovertQty,
                DEPARTMENTNAME = a.DEPARTMENTNAME,
                FngDesc = a.FngDesc,
                UrgentFlag = a.UrgentFlag,
                Ftype = a.Ftype,
                LotNo1 = a.LotNo1,
                EMERGENCY = a.EMERGENCY,
                Status = a.Status,
                IqcDate = a.IqcDate,
                // 添加维护人员信息
                Fcode = v.Fcode,
                // 添加破坏实验数量
                PHSY = a.PHSY,
                // 添加不良原因
                BLYY = a.BLYY,
                // 添加所属车间
                SSCJ = a.SSCJ,
                // 添加评审状态
                PSZT = a.PSZT,
                // 添加检验项目维护状态
                Jyxm = a.Jyxm
            })
            .OrderBy("CASE WHEN EMERGENCY = 1 THEN 0 ELSE 1 END")
            .OrderBy("CASE WHEN JYXM = 0 THEN 0 ELSE 1 END")
            .OrderBy("CASE WHEN STATUS = '已提交' THEN 1 ELSE 0 END")
            .OrderBy("IQC_DATE DESC")
            .OrderBy("CREATE_DATE")
            .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount);
        // 判断是否为管理员账号
        bool isAdmin = queryObj.createUser == "PL017" || queryObj.createUser == "HMLYY" || queryObj.createUser == "HMCS";
        List<LtsLlj> pageList;
        if (isAdmin)
        {
            // 管理员账号:直接查询主表,避免JOIN导致的重复记录
            pageList = db.Queryable<LtsLlj>()
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    "未完成".Equals(queryObj.result),
                    a => a.FcheckResu == null)
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    !"未完成".Equals(queryObj.result),
                    a => a.FcheckResu != null)
                .WhereIF(id > 0, a => a.Id == id)
                //加筛选条件,根据选择的搜索字段进行精确搜索
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 1, // 物料编号搜索
                    a => a.ItemNo != null && a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 2, // 物料名称搜索
                    a => a.ItemName != null && a.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 3, // 供应商搜索
                    a => a.SuppName != null && a.SuppName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 4, // 到货单号搜索
                    a => a.LotNo != null && a.LotNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 5, // 检验单号搜索
                    a => a.ReleaseNo != null && a.ReleaseNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 6, // 物料规格搜索
                    a => a.ItemModel != null && a.ItemModel.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .Select(a => new LtsLlj
                {
                    Id = a.Id,
                    ItemNo = a.ItemNo,
                    ItemId = a.ItemId,
                    ItemName = a.ItemName,
                    ItemModel = a.ItemModel,
                    SuppName = a.SuppName,
                    LotNo = a.LotNo,
                    ReleaseNo = a.ReleaseNo,
                    FcheckResu = a.FcheckResu,
                    CreateDate = a.CreateDate,
                    FcovertQty = a.FcovertQty,
                    DEPARTMENTNAME = a.DEPARTMENTNAME,
                    FngDesc = a.FngDesc,
                    UrgentFlag = a.UrgentFlag,
                    Ftype = a.Ftype,
                    LotNo1 = a.LotNo1,
                    EMERGENCY = a.EMERGENCY,
                    Status = a.Status,
                    IqcDate = a.IqcDate,
                    // 添加维护人员信息(管理员不需要此字段)
                    Fcode = null,
                    // 添加破坏实验数量
                    PHSY = a.PHSY,
                    // 添加不良原因
                    BLYY = a.BLYY,
                    // 添加所属车间
                    SSCJ = a.SSCJ,
                    // 添加评审状态
                    PSZT = a.PSZT,
                    // 添加检验项目维护状态
                    Jyxm = a.Jyxm
                })
                .OrderBy("IQC_DATE DESC")
                .OrderBy("CASE WHEN EMERGENCY = 1 THEN 0 ELSE 1 END")
                .OrderBy("CASE WHEN JYXM = 0 THEN 0 ELSE 1 END")
                .OrderBy("CASE WHEN STATUS = '已提交' THEN 1 ELSE 0 END")
                .OrderBy("CREATE_DATE")
                .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount);
        }
        else
        {
            // 普通用户:使用JOIN查询,根据维护情况判断权限
            pageList = db.Queryable<LtsLlj, V_LljUser>((a, v) =>
                    new JoinQueryInfos(JoinType.Left, a.ItemNo == v.ItemNo))
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    "未完成".Equals(queryObj.result),
                    (a, v) => a.FcheckResu == null)
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    !"未完成".Equals(queryObj.result),
                    (a, v) => a.FcheckResu != null)
                .WhereIF(id > 0, (a, v) => a.Id == id)
                // 权限控制:普通用户需要根据物料维护情况判断
                .WhereIF(queryObj.UserIndex == "0",
                    (a, v) =>
                        // 如果物料被维护,只有维护人员可以看到
                        (v.Fcode != null && v.Fcode == queryObj.createUser) ||
                        // 如果物料未被维护,所有人都可以看到
                        (v.Fcode == null))
                //加筛选条件,根据选择的搜索字段进行精确搜索
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 1, // 物料编号搜索
                    (a, v) => a.ItemNo != null && a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 2, // 物料名称搜索
                    (a, v) => a.ItemName != null && a.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 3, // 供应商搜索
                    (a, v) => a.SuppName != null && a.SuppName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 4, // 到货单号搜索
                    (a, v) => a.LotNo != null && a.LotNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 5, // 检验单号搜索
                    (a, v) => a.ReleaseNo != null && a.ReleaseNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 6, // 物料规格搜索
                    (a, v) => a.ItemModel != null && a.ItemModel.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .Select((a, v) => new LtsLlj
                {
                    Id = a.Id,
                    ItemNo = a.ItemNo,
                    ItemId = a.ItemId,
                    ItemName = a.ItemName,
                    ItemModel = a.ItemModel,
                    SuppName = a.SuppName,
                    LotNo = a.LotNo,
                    ReleaseNo = a.ReleaseNo,
                    FcheckResu = a.FcheckResu,
                    CreateDate = a.CreateDate,
                    FcovertQty = a.FcovertQty,
                    DEPARTMENTNAME = a.DEPARTMENTNAME,
                    FngDesc = a.FngDesc,
                    UrgentFlag = a.UrgentFlag,
                    Ftype = a.Ftype,
                    LotNo1 = a.LotNo1,
                    EMERGENCY = a.EMERGENCY,
                    Status = a.Status,
                    IqcDate = a.IqcDate,
                    // 添加维护人员信息
                    Fcode = v.Fcode,
                    // 添加破坏实验数量
                    PHSY = a.PHSY,
                    // 添加不良原因
                    BLYY = a.BLYY,
                    // 添加所属车间
                    SSCJ = a.SSCJ,
                    // 添加评审状态
                    PSZT = a.PSZT,
                    // 添加检验项目维护状态
                    Jyxm = a.Jyxm
                })
                .OrderBy("IQC_DATE DESC")
                .OrderBy("CASE WHEN EMERGENCY = 1 THEN 0 ELSE 1 END")
                .OrderBy("CASE WHEN JYXM = 0 THEN 0 ELSE 1 END")
                .OrderBy("CASE WHEN STATUS = '已提交' THEN 1 ELSE 0 END")
                .OrderBy("CREATE_DATE")
                .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount);
        }
        // 批量获取检验项目数量,避免N+1查询问题
        if (pageList.Any())
@@ -117,40 +192,76 @@
        }
        
        // 计算所有数据的去重总数(不是当前页的去重数)
        var allDataQuery = db.Queryable<LtsLlj, V_LljUser>((a, v) =>
                new JoinQueryInfos(JoinType.Left, a.ItemNo == v.ItemNo))
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                "未完成".Equals(queryObj.result),
                (a, v) => a.FcheckResu == null)
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                !"未完成".Equals(queryObj.result),
                (a, v) => a.FcheckResu != null)
            .WhereIF(id > 0, (a, v) => a.Id == id)
            // 权限控制:PL017、HMLYY、HMCS 可以看到所有单据,其他用户需要根据物料维护情况判断
            .WhereIF(queryObj.createUser != "PL017" && queryObj.createUser != "HMLYY" && queryObj.createUser != "HMCS" && queryObj.UserIndex == "0",
                (a, v) =>
                    // 如果物料被维护,只有维护人员可以看到
                    (v.Fcode != null && v.Fcode == queryObj.createUser) ||
                    // 如果物料未被维护,所有人都可以看到
                    (v.Fcode == null))
            //加筛选条件,根据选择的搜索字段进行精确搜索
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 1, // 物料编号搜索
                (a, v) => a.ItemNo != null && a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 2, // 物料名称搜索
                (a, v) => a.ItemName != null && a.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 3, // 供应商搜索
                (a, v) => a.SuppName != null && a.SuppName.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 4, // 到货单号搜索
                (a, v) => a.LotNo != null && a.LotNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 5, // 检验单号搜索
                (a, v) => a.ReleaseNo != null && a.ReleaseNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 6, // 物料规格搜索
                (a, v) => a.ItemModel != null && a.ItemModel.ToLower().Contains(queryObj.SearchValue.ToLower()))
            .Select((a, v) => a.ReleaseNo)
            .Distinct()
            .Count();
        int allDataQuery;
        if (isAdmin)
        {
            // 管理员账号:直接查询主表
            allDataQuery = db.Queryable<LtsLlj>()
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    "未完成".Equals(queryObj.result),
                    a => a.FcheckResu == null)
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    !"未完成".Equals(queryObj.result),
                    a => a.FcheckResu != null)
                .WhereIF(id > 0, a => a.Id == id)
                //加筛选条件,根据选择的搜索字段进行精确搜索
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 1, // 物料编号搜索
                    a => a.ItemNo != null && a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 2, // 物料名称搜索
                    a => a.ItemName != null && a.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 3, // 供应商搜索
                    a => a.SuppName != null && a.SuppName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 4, // 到货单号搜索
                    a => a.LotNo != null && a.LotNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 5, // 检验单号搜索
                    a => a.ReleaseNo != null && a.ReleaseNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 6, // 物料规格搜索
                    a => a.ItemModel != null && a.ItemModel.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .Select(a => a.ReleaseNo)
                .Distinct()
                .Count();
        }
        else
        {
            // 普通用户:使用JOIN查询
            allDataQuery = db.Queryable<LtsLlj, V_LljUser>((a, v) =>
                    new JoinQueryInfos(JoinType.Left, a.ItemNo == v.ItemNo))
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    "未完成".Equals(queryObj.result),
                    (a, v) => a.FcheckResu == null)
                .WhereIF(
                    StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                    !"未完成".Equals(queryObj.result),
                    (a, v) => a.FcheckResu != null)
                .WhereIF(id > 0, (a, v) => a.Id == id)
                // 权限控制:普通用户需要根据物料维护情况判断
                .WhereIF(queryObj.UserIndex == "0",
                    (a, v) =>
                        // 如果物料被维护,只有维护人员可以看到
                        (v.Fcode != null && v.Fcode == queryObj.createUser) ||
                        // 如果物料未被维护,所有人都可以看到
                        (v.Fcode == null))
                //加筛选条件,根据选择的搜索字段进行精确搜索
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 1, // 物料编号搜索
                    (a, v) => a.ItemNo != null && a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 2, // 物料名称搜索
                    (a, v) => a.ItemName != null && a.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 3, // 供应商搜索
                    (a, v) => a.SuppName != null && a.SuppName.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 4, // 到货单号搜索
                    (a, v) => a.LotNo != null && a.LotNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 5, // 检验单号搜索
                    (a, v) => a.ReleaseNo != null && a.ReleaseNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && queryObj.selectedIndex == 6, // 物料规格搜索
                    (a, v) => a.ItemModel != null && a.ItemModel.ToLower().Contains(queryObj.SearchValue.ToLower()))
                .Select((a, v) => a.ReleaseNo)
                .Distinct()
                .Count();
        }
        
        var emergencyValues = pageList.Select(item => item.EMERGENCY).ToList(); 
        return (pageList, allDataQuery);
@@ -1095,4 +1206,293 @@
            throw new Exception(ex.Message);
        }
    }
    public List<MesItems> GetWomdab(string daa001)
    {
        //if (string.IsNullOrEmpty(ItemNo))
        //{
        // throw new ArgumentException("检验单号不能为空");
        //}
        var db = SqlSugarHelper.GetInstance();
        try
        {
            return db.Queryable<Womdab>()
                    .LeftJoin<MesItems>((w, m) => w.Dab003 == m.ItemId.ToString()) // 需要替换为实际的关联字段
                    .Where((w, m) => w.Dab001 == daa001)
                    .OrderBy((w, m) => w.Dab003, OrderByType.Desc)
                    // .ThenBy((w, m) => w.CreateDate, OrderByType.Desc)
                    .Select((w, m) => new MesItems
                    {
                           ItemNo = m.ItemNo,
                           ItemName = m.ItemName,
                           ItemModel = m.ItemModel
                    }).ToList();
        }
        catch (Exception ex)
        {
           throw new Exception($"查询附件信息失败: {ex.Message}");
        }
    }
    public List<MesItems> GetWomdabById(string daa001,string ItemNo)
    {
        //if (string.IsNullOrEmpty(ItemNo))
        //{
        // throw new ArgumentException("检验单号不能为空");
        //}
        var db = SqlSugarHelper.GetInstance();
        try
        {
            return db.Queryable<Womdab>()
                    .LeftJoin<MesItems>((w, m) => w.Dab003 == m.ItemId.ToString()) // 需要替换为实际的关联字段
                    .Where((w, m) => w.Dab001 == daa001 && m.ItemNo.Contains(ItemNo))
                    .OrderBy((w, m) => w.Dab003, OrderByType.Desc)
                    // .ThenBy((w, m) => w.CreateDate, OrderByType.Desc)
                    .Select((w, m) => new MesItems
                    {
                        ItemNo = m.ItemNo,
                        ItemName = m.ItemName,
                        ItemModel = m.ItemModel
                    }).ToList();
        }
        catch (Exception ex)
        {
            throw new Exception($"查询附件信息失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 根据二维码查询物料信息
    /// </summary>
    /// <param name="itemBarcode">二维码内容</param>
    /// <param name="currentBillNo">当前检验单的到货单号(已废弃,保留参数兼容性)</param>
    /// <returns>物料信息</returns>
    public List<MaterialInfoDto> GetMaterialByBarcode(string itemBarcode, string currentBillNo)
    {
        var db = SqlSugarHelper.GetInstance();
        try
        {
            // 使用JOIN查询MES_INV_ITEM_BARCODES和MES_ITEMS表
            var materialInfo = db.Queryable<MesInvItemBarcodes>()
                .LeftJoin<MesItems>((b, m) => b.ItemId == m.Id)
                .Where((b, m) => b.ItemBarcode == itemBarcode && b.ComeFlg == 1)
                .Select((b, m) => new MaterialInfoDto
                {
                    ItemNo = b.ItemNo,        // 物料编码
                    OldQty = b.Oldqty,        // 数量
                    ItemId = b.ItemId,        // 物料ID
                    ItemName = m.ItemName,    // 物料名称
                    ItemModel = m.ItemModel,  // 规格型号
                    BillNo = b.BillNo         // 到货单号
                })
                .ToList();
            // 移除到货单号校验,直接返回查询结果
            return materialInfo;
        }
        catch (Exception ex)
        {
            throw new Exception($"查询物料信息失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 查询破坏实验记录是否存在
    /// </summary>
    /// <param name="billNo">到货单号</param>
    /// <param name="releaseNo">检验单号</param>
    /// <returns>是否存在记录</returns>
    public bool CheckPhsyRecordExists(string billNo, string releaseNo)
    {
        var db = SqlSugarHelper.GetInstance();
        try
        {
            var count = db.Queryable<MesInvPhsy>()
                .Where(x => x.BillNo == billNo && x.ReleaseNo == releaseNo)
                .Count();
            return count > 0;
        }
        catch (Exception ex)
        {
            throw new Exception($"查询破坏实验记录失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 获取破坏实验记录详细信息
    /// </summary>
    /// <param name="billNo">到货单号</param>
    /// <param name="releaseNo">检验单号</param>
    /// <returns>破坏实验记录信息</returns>
    public List<PhsyRecordInfoDto> GetPhsyRecordInfo(string billNo, string releaseNo)
    {
        var db = SqlSugarHelper.GetInstance();
        try
        {
            // 先查询破坏实验记录
            var phsyRecords = db.Queryable<MesInvPhsy>()
                .Where(x => x.BillNo == billNo && x.ReleaseNo == releaseNo)
                .ToList();
            var result = new List<PhsyRecordInfoDto>();
            foreach (var record in phsyRecords)
            {
                // 尝试通过条码查询物料信息
                var materialInfo = db.Queryable<MesInvItemBarcodes>()
                    .LeftJoin<MesItems>((b, m) => b.ItemId == m.Id)
                    .Where((b, m) => b.ItemBarcode == record.ItemBarcode)
                    .Select((b, m) => new {
                        ItemNo = b.ItemNo,
                        ItemName = m.ItemName,
                        ItemModel = m.ItemModel
                    })
                    .First();
                var dto = new PhsyRecordInfoDto
                {
                    ItemBarcode = record.ItemBarcode,
                    ItemNo = materialInfo?.ItemNo ?? record.ItemBarcode,
                    ItemName = materialInfo?.ItemName ?? "已记录物料",
                    ItemModel = materialInfo?.ItemModel ?? "已记录规格",
                    BillNo = record.BillNo,
                    Yqty = record.Yqty,
                    Cqty = record.Cqty,
                    CreateDate = record.CreateDate
                };
                result.Add(dto);
            }
            return result;
        }
        catch (Exception ex)
        {
            throw new Exception($"获取破坏实验记录信息失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 调用破坏实验存储过程
    /// </summary>
    /// <param name="itemBarcode">扫描的条码值</param>
    /// <param name="yqty">扫码查询出来的条码数量</param>
    /// <param name="cqty">填写的破坏实验数量</param>
    /// <param name="billNo">查询到的到货单号</param>
    /// <param name="lx">操作类型:1新增,2修改,3删除</param>
    /// <param name="releaseNo">检验单号</param>
    /// <param name="itemId">物料ID</param>
    /// <returns>执行结果</returns>
    public (int result, string message) CallPhsyUpdateProcedure(string itemBarcode, decimal yqty, decimal cqty, string billNo, int lx, string releaseNo, decimal? itemId = null)
    {
        var db = SqlSugarHelper.GetInstance();
        try
        {
            // 定义输入参数
            var inputParam1 = new SugarParameter("P_ITEM_BARCODE", itemBarcode, DbType.String, ParameterDirection.Input);
            var inputParam2 = new SugarParameter("P_YQTY", yqty, DbType.Decimal, ParameterDirection.Input);
            var inputParam3 = new SugarParameter("P_CQTY", cqty, DbType.Decimal, ParameterDirection.Input);
            var inputParam4 = new SugarParameter("P_BILL_NO", billNo, DbType.String, ParameterDirection.Input);
            var inputParam5 = new SugarParameter("P_LX", lx, DbType.Int32, ParameterDirection.Input);
            var inputParam6 = new SugarParameter("ITEM_ID", itemId ?? 0, DbType.Decimal, ParameterDirection.Input);
            var inputParam7 = new SugarParameter("P_RELEASE_NO", releaseNo, DbType.String, ParameterDirection.Input);
            // 定义输出参数
            var outputResult = new SugarParameter("PO_RESULT", null, DbType.Int32, ParameterDirection.Output);
            var outputMessage = new SugarParameter("PO_MSG", null, DbType.String, ParameterDirection.Output, 4000);
            // 使用SqlSugar执行存储过程
            db.Ado.ExecuteCommand("BEGIN PRC_INV_PHSYUPDATE(:P_ITEM_BARCODE, :P_YQTY, :P_CQTY, :P_BILL_NO, :P_LX, :ITEM_ID, :P_RELEASE_NO, :PO_RESULT, :PO_MSG); END;",
                inputParam1, inputParam2, inputParam3, inputParam4, inputParam5, inputParam6, inputParam7, outputResult, outputMessage);
            // 获取输出参数的值
            var result = outputResult.Value == null ? 1 : Convert.ToInt32(outputResult.Value);
            var message = outputMessage.Value?.ToString() ?? "";
            return (result, message);
        }
        catch (Exception ex)
        {
            return (1, $"调用存储过程失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 设置堵穴信息
    /// </summary>
    /// <param name="releaseNo">检验单号</param>
    /// <param name="blockedHoles">堵穴信息,格式如"1,2,3"</param>
    /// <param name="itemId">检验项目ID</param>
    /// <returns>执行结果</returns>
    public (int result, string message) SetBlockedHoles(string releaseNo, string blockedHoles, decimal itemId)
    {
        var db = SqlSugarHelper.GetInstance();
        try
        {
            // 解析堵穴信息
            var blockedHolesList = blockedHoles.Split(',', ',')
                .Select(s => s.Trim())
                .Where(s => !string.IsNullOrEmpty(s))
                .Select(s => int.Parse(s))
                .ToList();
            // 获取检验项目信息
            var item = db.Queryable<MesQaItemsDetectDetail5>()
                .Where(x => x.Id == itemId && x.ReleaseNo == releaseNo)
                .First();
            if (item == null)
            {
                return (1, "检验项目不存在");
            }
            // 解析开穴数
            var holeCount = ParseHoleCount(item.FcheckItem);
            if (holeCount == 0)
            {
                return (1, "该检验项目没有穴数信息");
            }
            // 验证堵穴数量不能大于等于开穴数
            if (blockedHolesList.Count >= holeCount)
            {
                return (1, $"堵穴数量不能大于等于开穴数({holeCount})");
            }
            // 验证堵穴号是否在有效范围内
            foreach (var hole in blockedHolesList)
            {
                if (hole < 1 || hole > holeCount)
                {
                    return (1, $"堵穴号必须在1-{holeCount}之间");
                }
            }
            // 计算新的检验数量
            var newCheckQyt = item.CheckQyt - (item.CheckQyt / holeCount) * blockedHolesList.Count;
            // 更新数据库
            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
            {
                return db.Updateable<MesQaItemsDetectDetail5>()
                    .SetColumns(x => x.Dnum == blockedHoles)
                    .SetColumns(x => x.CheckQyt == newCheckQyt)
                    .Where(x => x.Id == itemId && x.ReleaseNo == releaseNo)
                    .ExecuteCommand();
            });
            if (result > 0)
            {
                return (0, "堵穴设置成功");
            }
            else
            {
                return (1, "堵穴设置失败");
            }
        }
        catch (Exception ex)
        {
            return (1, $"设置堵穴失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 解析检验项目名称中的穴数
    /// </summary>
    /// <param name="checkItemName">检验项目名称</param>
    /// <returns>穴数,如果没有穴数信息返回0</returns>
    private int ParseHoleCount(string checkItemName)
    {
        if (string.IsNullOrEmpty(checkItemName))
            return 0;
        // 匹配格式:尺寸检查(5穴)或 尺寸检查(5穴)
        var match = System.Text.RegularExpressions.Regex.Match(checkItemName, @"[((](\d+)穴[))]");
        return match.Success ? int.Parse(match.Groups[1].Value) : 0;
    }
}