快乐的昕的电脑
2025-10-22 ef0be0cf267c3b668a6559a921bdec0c0cd30865
Services/MesOrderStaManager.cs
@@ -97,35 +97,54 @@
    /// <returns>更新是否成功</returns>
    public bool ChangeMachineTime(MesOrderSta entity)
    {
        // 查询工单主表信息
        var womdaa = Db.Queryable<Womdaa>()
            .Where(s => s.Id == entity.OrderId).First();
        if (womdaa == null) throw new Exception("工单不存在");
        // 如果标记为1,需要处理首检相关逻辑
        if (entity.Flag == 1)
        {
            // 调用存储过程执行自动首检
            // Db.Ado.ExecuteCommand(
            //     "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;",
            //     new SugarParameter("BILL_NO", womdaa.Daa001,
            //         DbType.String));
            var automaticIpqcFirstCheck =
                mesQaItemsDetect02Manager
                    .AutomaticIpqcFirstCheck(womdaa.Daa001);
            if (!automaticIpqcFirstCheck) throw new Exception("首检生成失败");
            // 幂等:先查是否已有首检
            var existsFirst = Db.Queryable<MesQaItemsDetect02>()
                .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "首检" && (s.Fcancel == null || s.Fcancel != "Y"))
                .Any();
            // 获取当前时间
            var s1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            // 生成首检备注信息
            var remeke = "工控机于" + s1 + "自动创建的首检单";
            if (!existsFirst)
            {
                // 行级锁防并发:锁工单行
                Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE",
                    new SugarParameter("BILL_NO", womdaa.Daa001));
            // 更新首检单备注信息
            Db.Updateable<MesQaItemsDetect02>()
                .SetColumns(s => s.Remeke == remeke)
                .Where(s => s.Ftype == "首检" && s.Aufnr == womdaa.Daa001)
                .ExecuteCommand();
                // 再次确认
                existsFirst = Db.Queryable<MesQaItemsDetect02>()
                    .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "首检" && (s.Fcancel == null || s.Fcancel != "Y"))
                    .Any();
                if (!existsFirst)
                {
                    // 调用存储过程(单路径)
                    Db.Ado.ExecuteCommand(
                        "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;",
                        new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String));
                }
            }
            // 统一更新最新首检备注
            var latestFirst = Db.Queryable<MesQaItemsDetect02>()
                .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "首检" && (s.Fcancel == null || s.Fcancel != "Y"))
                .OrderBy(s => s.CreateDate, OrderByType.Desc)
                .First();
            if (latestFirst != null)
            {
                var ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var remark = $"工控机于{ts}自动创建的首检单";
                Db.Updateable<MesQaItemsDetect02>()
                    .SetColumns(s => s.Remeke == remark)
                    .Where(s => s.Id == latestFirst.Id)
                    .ExecuteCommand();
            }
        }
        // 原逻辑保持...
        var mesReporting = Db.Queryable<MesReporting>()
            .Where(s => s.BillNo == womdaa.Daa001)
            .OrderByDescending(s => s.Id)
@@ -135,7 +154,7 @@
        var editDate = DateTime.Now.ToString("yyyy-MM-dd");
        // 发送HTTP请求到数据刷新接口
        MesNumerical mesNumerical = null;
        MesNumericalBycl mesNumerical = null;
        try
        {
            using (var httpClient = new HttpClient())
@@ -149,7 +168,7 @@
                    "application/json");
                var response = httpClient
                    .PostAsync("http://192.168.0.94:9095/Numerical/RefreshDev",
                    .PostAsync("http://192.168.0.94:9095/Numerical/RefreshDevBycl",
                        content).GetAwaiter().GetResult();
                if (response.IsSuccessStatusCode)
@@ -162,7 +181,7 @@
                    if (responseObj != null && responseObj.code == 200)
                    {
                        // 请求成功,获取MesNumerical数据
                        mesNumerical = Db.Queryable<MesNumerical>()
                        mesNumerical = Db.Queryable<MesNumericalBycl>()
                            .Where(s => s.EditDate == editDate
                                        && s.MachineNo == entity.MachineNo)
                            .OrderByDescending(s => s.Id)
@@ -189,12 +208,44 @@
            OrderId = womdaa.Id,
            OrderNo = womdaa.Daa001,
            EditDate = editDate,
            Qty = mesReporting == null ? 0 : Int64.Parse(mesReporting.DyQty),
            Qty = mesReporting == null ? 0 : (long?)(mesReporting.DyQty ?? 0),
            InitialValue = mesNumerical == null ? 0 : mesNumerical.CjNum
        };
        Db.Insertable<MesAnchors>(eAnchors)
            .ExecuteCommand();
        // 新增逻辑:送检时间有值时,判断最新首检单是否合格,合格则写入调机完成时间为当前时间
        if (!string.IsNullOrEmpty(entity.MaShoutTime))
        {
            //送检呼叫时间必须大于或等于调机开始时间,以此来筛选首检单
            if (DateTime.TryParse(entity.MaShoutTime, out var sjTime) && DateTime.TryParse(entity.MaStartTime, out var startTime))
            {
                if (sjTime >= startTime)
                {
                    // 查找该工单号下最新的首检单
                    var sjRecord = Db.Queryable<MesQaItemsDetect02>()
                        .Where(x => x.Aufnr == womdaa.Daa001 && x.Ftype == "首检")
                        .OrderBy(x => x.CreateDate, OrderByType.Desc)
                        .First();
                    if (sjRecord != null && sjRecord.FcheckResu == "合格")
                    {
                        // 使用QualifiedInspection方法更新工单状态
                        QualifiedInspection(new OrderMachineDto
                        {
                            OrderId = entity.OrderId,
                            orderNo = entity.OrderNo,
                            machineNo = entity.MachineNo
                        });
                        //将送检时间写入开工时间
                        entity.StartTime = entity.MaShoutTime;
                        entity.MaEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
            }
        }
        // 更新工单状态表
        return Db.Updateable<MesOrderSta>()
@@ -204,6 +255,12 @@
            // 如果有调机开始时间则更新
            .SetColumnsIF(entity.MaStartTime != null,
                s => s.MaStartTime == entity.MaStartTime)
            // 如果有调机完成时间则更新
            .SetColumnsIF(entity.MaEndTime != null,
                s => s.MaEndTime == entity.MaEndTime)
            // 如果有开工时间则更新
            .SetColumnsIF(entity.StartTime != null,
                s => s.StartTime == entity.StartTime)
            // 如果标记为1则更新备注信息
            .SetColumnsIF(entity.Flag == 1,
                s => s.remark == "于" + entity.MaShoutTime + "时间有一次送检")
@@ -449,7 +506,7 @@
                        // var reportingOkQty =
                        //     Convert.ToDecimal(vOrder.todayOutput) -
                        //     reporting.OkQty;
                        var CjQty = Db.Queryable<MesNumerical>()
                        var CjQty = Db.Queryable<MesNumericalBycl>()
                            .Where(s => s.MachineNo == entity.MachineNo && s.EditDate == date)
                            .OrderByDescending(s=>s.CjTiem)
                            .Select<long?>(s=>s.CjNum).First();
@@ -467,12 +524,12 @@
                            MachineNo = womdaa.MachineNo,
                            // BfQty = reportingOkQty,
                            BfQty = 0,
                            BlQty = reportingOkQty.ToString(),
                            BlQty = reportingOkQty,
                            OkQty = 0,
                            ItemNo = womdaa.Daa002,
                            BillNo = womdaa.Daa001,
                            CjQty = CjQty.ToString(),
                            DyQty = reporting.OkQty.ToString()
                            CjQty = CjQty,
                            DyQty = reporting.OkQty
                        };
                        
                        // 插入报工记录
@@ -581,7 +638,7 @@
        if (binding)
            return Db.Updateable<MesOrderSta>()
                .SetColumns(s => s.MaEndTime == endDate)
                .SetColumns(s => s.MaEndTime == endDate) // 自动写入调机完成时间
                .SetColumns(s => s.StartTime == entity.MaShoutTime)
                .Where(s => s.OrderId == query.OrderId).ExecuteCommand() > 0;
@@ -616,18 +673,4 @@
        // 更新工单时间和状态
        return UpdateTime(entity);
    }
    /// <summary>
    /// 刀具查询(支持编号或名称模糊查询)
    /// </summary>
    /// <param name="searchKey">查询关键字</param>
    /// <returns>刀具列表</returns>
    public List<MesCutterLedger> QueryTools(string searchKey)
    {
        return Db.Queryable<MesCutterLedger>()
            .WhereIF(!string.IsNullOrEmpty(searchKey),
                t => t.CutterId.Contains(searchKey) || t.CutterName.Contains(searchKey))
            .ToList();
    }
}