| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 更新机器时间并处理首检 |
| | | /// 调机送检逻辑重写版 |
| | | /// 流程: |
| | | /// 1. 调机开始:只写入调机开始时间(MA_START_TIME) |
| | | /// 2. 送检呼叫:写入调机开始时间(若未写)与送检时间(MA_SHOUT_TIME),并创建首检单 |
| | | /// 3. 调机完成:保持原逻辑,首检合格后写入开工(START_TIME)与调机完成(MA_END_TIME) |
| | | /// 4. 若最新首检结果为“不合格”,清空送检时间(MA_SHOUT_TIME),提示重新送检,不立即重建 |
| | | /// 用户再次点击送检呼叫才创建新的首检单 |
| | | /// </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) |
| | | { |
| | | //调用存储过程执行自动首检 |
| | | 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) |
| | | { |
| | | Console.WriteLine($"自动首检失败,工单号:{womdaa.Daa001}"); |
| | | throw new Exception("首检生成失败"); |
| | | } |
| | | if (entity == null || entity.Id <= 0) throw new ArgumentException("参数错误"); |
| | | var womdaa = Db.Queryable<Womdaa>().Where(s => s.Id == entity.OrderId).First(); |
| | | if (womdaa == null) throw new Exception("工单不存在"); |
| | | |
| | | // 获取当前时间 |
| | | var s1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | // 生成首检备注信息 |
| | | var remeke = "工控机于" + s1 + "自动创建的首检单"; |
| | | // 当前数据库状态快照 |
| | | var dbSta = Db.Queryable<MesOrderSta>().Where(s => s.Id == entity.Id).First(); |
| | | if (dbSta == null) throw new Exception("工单状态不存在"); |
| | | |
| | | // 更新首检单备注信息 |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == remeke) |
| | | .Where(s => s.Ftype == "首检" && s.Aufnr == womdaa.Daa001) |
| | | .ExecuteCommand(); |
| | | } |
| | | // 行级锁,保证创建首检单的并发安全 |
| | | Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001)); |
| | | |
| | | var mesReporting = Db.Queryable<MesReporting>() |
| | | .Where(s => s.BillNo == womdaa.Daa001) |
| | | .OrderByDescending(s => s.Id) |
| | | // 最新首检单(未作废) |
| | | 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(); |
| | | |
| | | // 判定操作类型 |
| | | var hasStartTimeInput = !string.IsNullOrWhiteSpace(entity.MaStartTime); |
| | | var hasShoutTimeInput = !string.IsNullOrWhiteSpace(entity.MaShoutTime); |
| | | // 仅开始:有开始时间,送检时间为空 |
| | | var isStartOnly = hasStartTimeInput && !hasShoutTimeInput && dbSta.MaStartTime != entity.MaStartTime; |
| | | // 送检呼叫:有送检时间(可能同时第一次写入开始时间) |
| | | var isShoutCall = hasShoutTimeInput && dbSta.MaShoutTime != entity.MaShoutTime; |
| | | // 完成逻辑保持:后面通过首检合格条件判断 |
| | | |
| | | var editDate = DateTime.Now.ToString("yyyy-MM-dd"); |
| | | |
| | | // 发送HTTP请求到数据刷新接口 |
| | | MesNumericalBycl mesNumerical = null; |
| | | try |
| | | // 4) 若最新判定结果“不合格”,清空送检时间,提示需重新呼叫,不创建新单 |
| | | if (string.Equals(latestResult, FirstCheckResultNG, StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | using (var httpClient = new HttpClient()) |
| | | // 清空送检时间(数据库与返回实体) |
| | | entity.MaShoutTime = null; |
| | | entity.remark = "首检不合格,送检时间已清空,请重新送检呼叫生成新的首检单"; |
| | | return Db.Updateable<MesOrderSta>() |
| | | .SetColumns(s => s.MaShoutTime == null) |
| | | .SetColumnsIF(hasStartTimeInput && dbSta.MaStartTime != entity.MaStartTime, |
| | | s => s.MaStartTime == entity.MaStartTime) // 允许同时写入新的开始时间(如果刚修改) |
| | | .SetColumnsIF(entity.remark != null, s => s.remark == entity.remark) |
| | | .Where(s => s.Id == entity.Id) |
| | | .ExecuteCommand() > 0; |
| | | } |
| | | |
| | | // 1) 调机开始:只写开始时间 |
| | | if (isStartOnly) |
| | | { |
| | | return Db.Updateable<MesOrderSta>() |
| | | .SetColumns(s => s.MaStartTime == entity.MaStartTime) |
| | | .Where(s => s.Id == entity.Id) |
| | | .ExecuteCommand() > 0; |
| | | } |
| | | |
| | | // 2) 送检呼叫:写入开始时间(若未写) + 送检时间,并创建首检单 |
| | | if (isShoutCall) |
| | | { |
| | | // 若数据库尚无开始时间,补写 |
| | | var needWriteStart = string.IsNullOrWhiteSpace(dbSta.MaStartTime) && hasStartTimeInput; |
| | | |
| | | // 创建首检单(始终按送检呼叫生成一张新的,如果需要作废旧单可加取消逻辑) |
| | | 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 == FirstCheckType |
| | | && (s.Fcancel == null || s.Fcancel != "Y")) |
| | | .OrderBy(s => s.CreateDate, OrderByType.Desc) |
| | | .First(); |
| | | |
| | | if (latestFirst != null) |
| | | { |
| | | 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) |
| | | { |
| | | // 请求成功,获取MesNumerical数据 |
| | | mesNumerical = Db.Queryable<MesNumericalBycl>() |
| | | .Where(s => s.EditDate == editDate |
| | | && s.MachineNo == entity.MachineNo) |
| | | .OrderByDescending(s => s.Id) |
| | | .First(); |
| | | } |
| | | } |
| | | var ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Remeke == $"工控机于{ts}调机送检生成首检单") |
| | | .Where(s => s.Id == latestFirst.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 记录异常但不阻止流程 |
| | | Console.WriteLine($"发送数据刷新请求时出错: {ex.Message}"); |
| | | |
| | | entity.remark = "已生成首检单,等待检验"; |
| | | var updateCount = Db.Updateable<MesOrderSta>() |
| | | .SetColumns(s => s.MaShoutTime == entity.MaShoutTime) |
| | | .SetColumnsIF(needWriteStart, s => s.MaStartTime == entity.MaStartTime) |
| | | .SetColumns(s => s.remark == entity.remark) |
| | | .Where(s => s.Id == entity.Id) |
| | | .ExecuteCommand(); |
| | | |
| | | return updateCount > 0; |
| | | } |
| | | |
| | | // 如果请求失败或获取数据失败,mesNumerical将保持为null |
| | | |
| | | Db.Deleteable<MesAnchors>() |
| | | .Where(a => a.EditDate == editDate |
| | | && a.OrderId == womdaa.Id) |
| | | .ExecuteCommand(); |
| | | |
| | | MesAnchors eAnchors = new MesAnchors |
| | | // 3) 保留调机完成逻辑:首检合格且送检时间 >= 调机开始时间时自动写开工与调机完成 |
| | | // (此处与原逻辑一致:前端不直接点“调机完成”,而是由合格触发) |
| | | if (!string.IsNullOrEmpty(entity.MaShoutTime) |
| | | && DateTime.TryParse(entity.MaShoutTime, out var shoutTime) |
| | | && DateTime.TryParse(dbSta.MaStartTime ?? entity.MaStartTime, out var startTime) |
| | | && shoutTime >= startTime |
| | | && latestFirst != null |
| | | && string.Equals(latestFirst.FcheckResu?.Trim(), FirstCheckResultOK, StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | OrderId = womdaa.Id, |
| | | OrderNo = womdaa.Daa001, |
| | | EditDate = editDate, |
| | | Qty = mesReporting == null ? 0 : Int64.Parse(mesReporting.DyQty), |
| | | 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)) |
| | | // 写开工及调机完成 |
| | | 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(); |
| | | OrderId = entity.OrderId, |
| | | orderNo = entity.OrderNo, |
| | | machineNo = entity.MachineNo |
| | | }); |
| | | |
| | | 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"); |
| | | } |
| | | } |
| | | } |
| | | entity.StartTime = entity.MaShoutTime; |
| | | entity.MaEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | return Db.Updateable<MesOrderSta>() |
| | | .SetColumnsIF(entity.MaEndTime != null, s => s.MaEndTime == entity.MaEndTime) |
| | | .SetColumnsIF(entity.StartTime != null, s => s.StartTime == entity.StartTime) |
| | | .Where(s => s.Id == entity.Id) |
| | | .ExecuteCommand() > 0; |
| | | } |
| | | |
| | | // 更新工单状态表 |
| | | 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匹配记录 |
| | | .Where(s => s.Id == entity.Id) |
| | | // 执行更新并判断影响行数是否大于0 |
| | | .ExecuteCommand() > 0; |
| | | // 若本次调用未匹配任何操作(可能只是重复提交),保持不变 |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /// <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 |
| | | }; |
| | | |
| | | // 插入报工记录 |