如洲 陈
昨天 caed1e3200578fae1e2ee6c8bf24826707d7596a
MES.Service/service/QC/XJService.cs
@@ -3,6 +3,8 @@
using MES.Service.Modes;
using MES.Service.util;
using SqlSugar;
using System.Data;
using System.Data.Common;
namespace MES.Service.service.QC;
@@ -140,7 +142,7 @@
    }
    public List<QsQaItemXj01> setJYItem(string itemNo)
    public List<QsQaItemXj01> setJYItem(string itemNo, decimal? workQty = null)
    {
        var db = SqlSugarHelper.GetInstance();
@@ -149,32 +151,70 @@
        if (count <= 0) return new List<QsQaItemXj01>();
        return db
        // 如果没有传递工单数量,尝试从工单表中获取
        if (workQty == null || workQty <= 0)
        {
            // 通过物料编码(Daa002)匹配工单
            var workOrder = db.Queryable<Womdaa>()
                .Where(w => w.Daa002 == itemNo)
                .OrderByDescending(w => w.Id)
                .First();
            if (workOrder != null)
            {
                workQty = workOrder.Daa008; // 工单数量
            }
        }
        // 先获取基础数据
        var qualityStandards = db
            .Queryable<MesQualityStandard>()
            .Where(b => b.QsType == "2"
                        && b.ItemNo == itemNo).Select(
                b => new QsQaItemXj01
                {
                    ProjName = b.ProjName,
                    ItemMod = b.ItemMod,
                    InspectionMethod = b.InspectionMethod,
                    UsingInstruments = b.UsingInstruments,
                    LevelNum = SqlFunc.IsNull(
                        SqlFunc.IsNull(b.LevelNum * b.InspectionLevel, 1),
                        b.InspectionLevel),
                    MaxValue = b.MaxValue,
                    StandardValue = b.StandardValue,
                    MinValue = b.MinValue,
                    Notes = b.Notes,
                    FcheckLevel = b.FcheckLevel,
                    FacLevel = b.FacLevel,
                    QsCode = b.QsCode,
                    QsName = b.QsName,
                    result = "未检测",
                    isCheck = 0,
                    Picture = b.Picture,
                    Picturename = b.Picturename
                }).ToList();
            .Where(b => b.QsType == "2" && b.ItemNo == itemNo)
            .ToList();
        // 在内存中计算检验数量
        return qualityStandards.Select(b => {
            var calculatedLevelNum = CalculateInspectionQuantity(b.InspectionLevel, workQty);
            return new QsQaItemXj01
            {
                ProjName = b.ProjName,
                ItemMod = b.ItemMod,
                InspectionMethod = b.InspectionMethod,
                UsingInstruments = b.UsingInstruments,
                LevelNum = calculatedLevelNum,
                MaxValue = b.MaxValue,
                StandardValue = b.StandardValue,
                MinValue = b.MinValue,
                Notes = b.Notes,
                FcheckLevel = b.FcheckLevel,
                FacLevel = b.FacLevel,
                QsCode = b.QsCode,
                QsName = b.QsName,
                result = "未检测",
                isCheck = 0,
                Picture = b.Picture,
                Picturename = b.Picturename
            };
        }).ToList();
    }
    /// <summary>
    /// 计算检验数量:如果抽检数量大于工单数量,则检验数量为工单数量,否则为抽检数量
    /// </summary>
    /// <param name="samplingQuantity">抽检数量</param>
    /// <param name="workQty">工单数量</param>
    /// <returns>检验数量</returns>
    private decimal? CalculateInspectionQuantity(decimal? samplingQuantity, decimal? workQty)
    {
        // 如果没有工单数量,使用抽检数量
        if (workQty == null || workQty <= 0)
        {
            return samplingQuantity;
        }
        // 如果抽检数量大于工单数量,则检验数量为工单数量,否则为抽检数量
        return samplingQuantity > workQty ? workQty : samplingQuantity;
    }
    public List<QsQaItemXj01> getJYItem(decimal? pid, decimal? id)
@@ -496,11 +536,75 @@
                    .Where(s => s.Id == detail.Gid)
                    .ExecuteCommand();
            });
            // 新增:如果判定结果为不合格,调用存储过程生成异常处置单
            if (FcheckResu.Equals("不合格"))
            {
                CallXJUnqualifiedStoredProcedure(detail.Gid, detail.CreateBy);
            }
        }
        return useTransactionWithOracle;
    }
    /// <summary>
    /// 调用巡检不合格处理存储过程生成异常处置单
    /// </summary>
    /// <param name="gid">巡检主表ID</param>
    /// <param name="createBy">操作人</param>
    private void CallXJUnqualifiedStoredProcedure(decimal? gid, string createBy)
    {
        try
        {
            var db = SqlSugarHelper.GetInstance();
            // 获取巡检单信息
            var xjInfo = db.Queryable<QsQaItemXj>()
                .Where(s => s.Id == gid)
                .First();
            if (xjInfo == null) return;
            // 定义输出参数
            var outputResult = new SugarParameter("o_Result", null,
                System.Data.DbType.Int32, ParameterDirection.Output, 4000);
            var outputMessage = new SugarParameter("o_Msg", null,
                System.Data.DbType.String, ParameterDirection.Output, 4000);
            // 定义输入参数
            var parameters = new List<SugarParameter>
            {
                new("p_Gid", gid, System.Data.DbType.Decimal, ParameterDirection.Input),
                new("p_Bill_No", xjInfo.BillNo, System.Data.DbType.String, ParameterDirection.Input),
                new("p_User", createBy, System.Data.DbType.String, ParameterDirection.Input),
                outputResult,
                outputMessage
            };
            // 执行存储过程生成异常处置单
            db.Ado.ExecuteCommand(
                "BEGIN PRC_XJ_EXCEPTION_ORDER(:p_Gid, :p_Bill_No, :p_User, :o_Result, :o_Msg); END;",
                parameters.ToArray());
            // 获取输出参数的值
            var resultValue = outputResult.Value?.ToString();
            var messageValue = outputMessage.Value?.ToString();
            if ("1".Equals(resultValue))
            {
                throw new Exception($"巡检异常处置单生成失败: {messageValue}");
            }
        }
        catch (Exception ex)
        {
            // 记录日志但不影响主流程
            Console.WriteLine($"巡检异常处置单生成存储过程调用失败: {ex.Message}");
            // 可以根据需要决定是否抛出异常
            // throw new Exception($"巡检异常处置单生成失败: {ex.Message}");
        }
    }
    public int UpdateQSItemDetail(QsQaItemXj02 detail)
    {
        var withOracle = SqlSugarHelper.UseTransactionWithOracle(db =>