| | |
| | | |
| | | /// <summary> |
| | | /// 更新机器时间并处理首检 |
| | | /// 逻辑说明: |
| | | /// 1) 首检结果“不合格”不立即重建,等待用户再次点击送检呼叫(送检时间变化)触发重建 |
| | | /// 2) 首次无首检单或“不合格”且送检时间变化 => 调用存储过程重建首检单 |
| | | /// 3) 重建后保留当前送检时间,不清空 |
| | | /// 4) 不合格但送检时间未变化 => 提示用户需要重新送检 |
| | | /// 5) 合格且送检时间 >= 调机开始时间 => 写入开工与调机完成时间 |
| | | /// 可选扩展:支持 ForceRebuild 强制重建(如果前端加字段) |
| | | /// </summary> |
| | | /// <param name="entity">工单状态实体,包含机器时间信息</param> |
| | | /// <returns>更新是否成功</returns> |
| | | public bool ChangeMachineTime(MesOrderSta entity) |
| | | { |
| | | // 查询工单主表信息 |
| | | var womdaa = Db.Queryable<Womdaa>() |
| | | .Where(s => s.Id == entity.OrderId).First(); |
| | | const string FirstCheckType = "首检"; |
| | | const string FirstCheckResultOK = "合格"; |
| | | const string FirstCheckResultNG = "不合格"; |
| | | |
| | | // 如果标记为1,需要处理首检相关逻辑 |
| | | if (entity.Flag == 1) |
| | | var womdaa = Db.Queryable<Womdaa>().Where(s => s.Id == entity.OrderId).First(); |
| | | if (womdaa == null) throw new Exception("工单不存在"); |
| | | |
| | | var dbSta = Db.Queryable<MesOrderSta>().Where(s => s.Id == entity.Id).First(); |
| | | |
| | | // 行级锁 |
| | | Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001)); |
| | | |
| | | // 最新首检 |
| | | var latestFirst = Db.Queryable<MesQaItemsDetect02>() |
| | | .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == FirstCheckType && (s.Fcancel == null || s.Fcancel != "Y")) |
| | | .OrderBy(s => s.CreateDate, OrderByType.Desc) |
| | | .First(); |
| | | var latestResult = latestFirst?.FcheckResu?.Trim(); |
| | | |
| | | // ForceRebuild 可选扩展 |
| | | var forceRebuild = (entity as dynamic)?.ForceRebuild == true; |
| | | |
| | | // 立即重建条件:首次无单 或 最新结果 == 不合格 或 强制 |
| | | var needRebuild = latestFirst == null |
| | | || string.Equals(latestResult, FirstCheckResultNG, StringComparison.OrdinalIgnoreCase) |
| | | || forceRebuild; |
| | | |
| | | if (needRebuild) |
| | | { |
| | | //调用存储过程执行自动首检 |
| | | Db.Ado.ExecuteCommand( |
| | | "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String)); |
| | | var automaticIpqcFirstCheck = |
| | | mesQaItemsDetect02Manager |
| | | .AutomaticIpqcFirstCheck(womdaa.Daa001); |
| | | if (!automaticIpqcFirstCheck) |
| | | var previousState = latestResult == null ? "首次创建" |
| | | : (latestResult == FirstCheckResultNG ? "不合格后立即重建" : "重建"); |
| | | |
| | | // 可选:保留旧记录,或标记旧记录作废(如果需要避免多条待检) |
| | | // 若要作废上一条不合格单:取消即可 |
| | | if (latestFirst != null && string.Equals(latestResult, FirstCheckResultNG, StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | Console.WriteLine($"自动首检失败,工单号:{womdaa.Daa001}"); |
| | | throw new Exception("首检生成失败"); |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Fcancel == "Y") |
| | | .Where(s => s.Id == latestFirst.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | |
| | | // 获取当前时间 |
| | | var s1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | // 生成首检备注信息 |
| | | var remeke = "工控机于" + s1 + "自动创建的首检单"; |
| | | // 调用存储过程创建新首检单 |
| | | Db.Ado.ExecuteCommand( |
| | | "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String)); |
| | | |
| | | // 更新首检单备注信息 |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == remeke) |
| | | .Where(s => s.Ftype == "首检" && s.Aufnr == womdaa.Daa001) |
| | | .ExecuteCommand(); |
| | | // 重新获取新建的首检单 |
| | | latestFirst = Db.Queryable<MesQaItemsDetect02>() |
| | | .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == FirstCheckType && (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"); |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == $"工控机于{ts}自动创建的首检单({previousState})") |
| | | .Where(s => s.Id == latestFirst.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | |
| | | if (previousState.Contains("不合格")) |
| | | entity.remark = "不合格已重建,等待检验"; |
| | | } |
| | | else |
| | | { |
| | | // 不需要重建时仅写结果备注(若已有首检) |
| | | if (latestFirst != null && !string.IsNullOrEmpty(latestResult)) |
| | | { |
| | | var ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == $"工控机于{ts}首检结果:{latestResult}") |
| | | .Where(s => s.Id == latestFirst.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | } |
| | | |
| | | // 报工锚点逻辑保持 |
| | | var mesReporting = Db.Queryable<MesReporting>() |
| | | .Where(s => s.BillNo == womdaa.Daa001) |
| | | .OrderByDescending(s => s.Id) |
| | | .First(); |
| | | |
| | | |
| | | var editDate = DateTime.Now.ToString("yyyy-MM-dd"); |
| | | |
| | | // 发送HTTP请求到数据刷新接口 |
| | | MesNumericalBycl mesNumerical = null; |
| | | try |
| | | { |
| | | using (var httpClient = new HttpClient()) |
| | | using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; |
| | | var content = new StringContent( |
| | | JsonConvert.SerializeObject(new { machineNo = entity.MachineNo }), |
| | | Encoding.UTF8, "application/json"); |
| | | var response = httpClient.PostAsync("http://192.168.0.94:9095/Numerical/RefreshDevBycl", content) |
| | | .GetAwaiter().GetResult(); |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | httpClient.Timeout = TimeSpan.FromSeconds(30); |
| | | |
| | | var content = new StringContent( |
| | | JsonConvert.SerializeObject(new |
| | | { machineNo = entity.MachineNo }), |
| | | Encoding.UTF8, |
| | | "application/json"); |
| | | |
| | | var response = httpClient |
| | | .PostAsync("http://192.168.0.94:9095/Numerical/RefreshDev", |
| | | content).GetAwaiter().GetResult(); |
| | | |
| | | if (response.IsSuccessStatusCode) |
| | | var responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); |
| | | var responseObj = JsonConvert.DeserializeObject<dynamic>(responseString); |
| | | if (responseObj != null && responseObj.code == 200) |
| | | { |
| | | var responseString = response.Content.ReadAsStringAsync() |
| | | .GetAwaiter().GetResult(); |
| | | var responseObj = |
| | | JsonConvert.DeserializeObject<dynamic>(responseString); |
| | | |
| | | if (responseObj != null && responseObj.code == 200) |
| | | { |
| | | // 请求成功,获取MesNumerical数据 |
| | | mesNumerical = Db.Queryable<MesNumericalBycl>() |
| | | .Where(s => s.EditDate == editDate |
| | | && s.MachineNo == entity.MachineNo) |
| | | .OrderByDescending(s => s.Id) |
| | | .First(); |
| | | } |
| | | mesNumerical = Db.Queryable<MesNumericalBycl>() |
| | | .Where(s => s.EditDate == editDate && s.MachineNo == entity.MachineNo) |
| | | .OrderByDescending(s => s.Id) |
| | | .First(); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 记录异常但不阻止流程 |
| | | Console.WriteLine($"发送数据刷新请求时出错: {ex.Message}"); |
| | | } |
| | | |
| | | // 如果请求失败或获取数据失败,mesNumerical将保持为null |
| | | |
| | | Db.Deleteable<MesAnchors>() |
| | | .Where(a => a.EditDate == editDate |
| | | && a.OrderId == womdaa.Id) |
| | | .Where(a => a.EditDate == editDate && a.OrderId == womdaa.Id) |
| | | .ExecuteCommand(); |
| | | |
| | | MesAnchors eAnchors = new MesAnchors |
| | | var eAnchors = new MesAnchors |
| | | { |
| | | 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(); |
| | | Db.Insertable<MesAnchors>(eAnchors).ExecuteCommand(); |
| | | |
| | | // 新增逻辑:送检时间有值时,判断最新首检单是否合格,合格则写入调机完成时间为当前时间 |
| | | if (!string.IsNullOrEmpty(entity.MaShoutTime)) |
| | | // 合格写开工/调机完成 |
| | | if (!string.IsNullOrEmpty(entity.MaShoutTime) |
| | | && DateTime.TryParse(entity.MaShoutTime, out var sjTime) |
| | | && DateTime.TryParse(entity.MaStartTime, out var startTime) |
| | | && sjTime >= startTime |
| | | && latestFirst != null |
| | | && string.Equals(latestFirst.FcheckResu?.Trim(), FirstCheckResultOK, StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | //送检呼叫时间必须大于或等于调机开始时间,以此来筛选首检单 |
| | | if (DateTime.TryParse(entity.MaShoutTime, out var sjTime) && DateTime.TryParse(entity.MaStartTime, out var startTime)) |
| | | QualifiedInspection(new OrderMachineDto |
| | | { |
| | | 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 == "合格") |
| | | { |
| | | //将送检时间写入开工时间 |
| | | entity.StartTime = entity.MaShoutTime; |
| | | entity.MaEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | } |
| | | } |
| | | } |
| | | 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>() |
| | | // 如果有送检时间则更新 |
| | | .SetColumnsIF(entity.MaShoutTime != null, |
| | | s => s.MaShoutTime == entity.MaShoutTime) |
| | | // 如果有调机开始时间则更新 |
| | | .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 + "时间有一次送检") |
| | | // 根据ID匹配记录 |
| | | .SetColumnsIF(entity.MaShoutTime != null, s => s.MaShoutTime == entity.MaShoutTime) |
| | | .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) |
| | | .SetColumnsIF(entity.Flag == 1 && entity.remark != null, s => s.remark == entity.remark) |
| | | .Where(s => s.Id == entity.Id) |
| | | // 执行更新并判断影响行数是否大于0 |
| | | .ExecuteCommand() > 0; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 初始化工单状态 |
| | |
| | | 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 |
| | | }; |
| | | |
| | | // 插入报工记录 |