| | |
| | | |
| | | /// <summary> |
| | | /// 更新机器时间并处理首检 |
| | | /// 变更说明: |
| | | /// 1) 最新首检结果为“不合格”时:只清空 MA_SHOUT_TIME(送检呼叫时间),不重建首检单 |
| | | /// 2) 若不存在首检单或最新首检结果为“不合格”时:允许继续创建(重建)首检单 |
| | | /// 3) 首检合格且送检时间 >= 调机开始时间时:写入调机完成时间与开工时间(沿用原逻辑) |
| | | /// </summary> |
| | | /// <param name="entity">工单状态实体,包含机器时间信息</param> |
| | | /// <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("工单不存在"); |
| | | |
| | | MesQaItemsDetect02 latestFirst = null; |
| | | |
| | | if (entity.Flag == 1) |
| | | { |
| | | // 幂等:先查是否已有首检 |
| | | var existsFirst = Db.Queryable<MesQaItemsDetect02>() |
| | | .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "首检" && (s.Fcancel == null || s.Fcancel != "Y")) |
| | | .Any(); |
| | | // 行级锁防并发 |
| | | Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001)); |
| | | |
| | | if (!existsFirst) |
| | | { |
| | | // 行级锁防并发:锁工单行 |
| | | Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001)); |
| | | |
| | | // 再次确认 |
| | | 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>() |
| | | // 当前最新首检 |
| | | 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 needCreate = latestFirst == null || latestFirst.FcheckResu == "不合格"; |
| | | |
| | | if (needCreate) |
| | | { |
| | | 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(); |
| | | // 不存在 或 “不合格” => 重新创建首检单 |
| | | Db.Ado.ExecuteCommand( |
| | | "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String)); |
| | | |
| | | // 重新获取最新首检单 |
| | | 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 trigger = latestFirst.FcheckResu == "不合格" ? "不合格重建" : "首次创建"; |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == $"工控机于{ts}自动创建的首检单({trigger})") |
| | | .Where(s => s.Id == latestFirst.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // 最新结果为“不合格” => 仅清空送检时间,不重建 |
| | | if (latestFirst != null && latestFirst.FcheckResu == "不合格") |
| | | { |
| | | Db.Updateable<MesOrderSta>() |
| | | .SetColumns(s => s.MaShoutTime == null) |
| | | .Where(s => s.Id == entity.Id) |
| | | .ExecuteCommand(); |
| | | |
| | | entity.MaShoutTime = null; |
| | | entity.remark = $"最新首检结果“不合格”,已清空送检时间,待再次送检"; |
| | | } |
| | | else if (latestFirst != null) |
| | | { |
| | | // 其它结果仅更新备注 |
| | | var ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == $"工控机于{ts}首检结果:{latestFirst.FcheckResu}") |
| | | .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/RefreshDevBycl", |
| | | 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, |
| | |
| | | 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 (DateTime.TryParse(entity.MaShoutTime, out var sjTime) && DateTime.TryParse(entity.MaStartTime, out var startTime)) |
| | | if (DateTime.TryParse(entity.MaShoutTime, out var sjTime) && |
| | | DateTime.TryParse(entity.MaStartTime, out var startTime) && |
| | | sjTime >= startTime) |
| | | { |
| | | if (sjTime >= startTime) |
| | | { |
| | | // 查找该工单号下最新的首检单 |
| | | var sjRecord = Db.Queryable<MesQaItemsDetect02>() |
| | | .Where(x => x.Aufnr == womdaa.Daa001 && x.Ftype == "首检") |
| | | .OrderBy(x => x.CreateDate, OrderByType.Desc) |
| | | .First(); |
| | | var sjRecord = latestFirst ?? |
| | | Db.Queryable<MesQaItemsDetect02>() |
| | | .Where(x => x.Aufnr == womdaa.Daa001 && x.Ftype == "首检" && (x.Fcancel == null || x.Fcancel != "Y")) |
| | | .OrderBy(x => x.CreateDate, OrderByType.Desc) |
| | | .First(); |
| | | |
| | | if (sjRecord != null && sjRecord.FcheckResu == "合格") |
| | | if (sjRecord != null && sjRecord.FcheckResu == "合格") |
| | | { |
| | | QualifiedInspection(new OrderMachineDto |
| | | { |
| | | // 使用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"); |
| | | } |
| | | 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; |
| | | } |
| | | |