| | |
| | | // ================== 不合格后重新送检(Flag == 2)================== |
| | | else if (entity.Flag == 2) |
| | | { |
| | | // 仅当最新有效首检为“不合格”时才允许重新生成 |
| | | // 加锁防并发(行级锁) |
| | | 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 == "首检" |
| | |
| | | .OrderBy(s => s.CreateDate, OrderByType.Desc) |
| | | .First(); |
| | | |
| | | if (latestFirst != null && latestFirst.FcheckResu == "不合格") |
| | | { |
| | | // 行级锁 + 二次确认防并发重复生成 |
| | | Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001)); |
| | | bool needReCreate = false; |
| | | |
| | | latestFirst = Db.Queryable<MesQaItemsDetect02>() |
| | | if (latestFirst != null) |
| | | { |
| | | var resu = latestFirst.FcheckResu?.Trim(); |
| | | if (!string.IsNullOrEmpty(resu) && |
| | | (resu == "不合格" || resu.Equals("NG", StringComparison.OrdinalIgnoreCase))) |
| | | { |
| | | needReCreate = true; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // 没有活动首检单,判断是否存在已作废的不合格首检记录 |
| | | var existsCanceledNg = Db.Queryable<MesQaItemsDetect02>() |
| | | .Where(s => s.Aufnr == womdaa.Daa001 |
| | | && s.Ftype == "首检" |
| | | && s.Fcancel == "Y" |
| | | && (s.FcheckResu == "不合格" || s.FcheckResu == "NG")) |
| | | .Any(); |
| | | if (existsCanceledNg) |
| | | { |
| | | needReCreate = true; |
| | | Console.WriteLine("[重送检] 无活动首检单但存在作废的不合格记录,允许重建"); |
| | | } |
| | | } |
| | | |
| | | if (needReCreate) |
| | | { |
| | | // 作废当前活动不合格单 |
| | | if (latestFirst != null) |
| | | { |
| | | Db.Updateable<MesQaItemsDetect02>() |
| | | .SetColumns(s => s.Fcancel == "Y") |
| | | .Where(s => s.Id == latestFirst.Id |
| | | && (s.Fcancel == null || s.Fcancel != "Y")) |
| | | .ExecuteCommand(); |
| | | } |
| | | |
| | | bool procOk = true; |
| | | string procErr = ""; |
| | | try |
| | | { |
| | | // 调用存储过程生成新首检单 |
| | | Db.Ado.ExecuteCommand( |
| | | "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String)); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | procOk = false; |
| | | procErr = ex.Message; |
| | | Console.WriteLine($"[重送检] 存储过程异常: {procErr}"); |
| | | } |
| | | |
| | | // 再次查询新生成的首检单 |
| | | var newLatest = 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 && latestFirst.FcheckResu == "不合格") |
| | | // 存储过程未生成 → 兜底 C# 自动生成 |
| | | if (newLatest == null) |
| | | { |
| | | // 生成新的首检单(重新送检) |
| | | Db.Ado.ExecuteCommand( |
| | | "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;", |
| | | new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String)); |
| | | |
| | | // 更新新生成首检单备注 |
| | | var newLatest = 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 (newLatest != null) |
| | | Console.WriteLine("[重送检] 存储过程未生成,执行 C# 兜底逻辑"); |
| | | var fallback = mesQaItemsDetect02Manager.AutomaticIpqcFirstCheck(womdaa.Daa001); |
| | | if (fallback) |
| | | { |
| | | 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 == newLatest.Id) |
| | | .ExecuteCommand(); |
| | | newLatest = 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 (newLatest != 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 == newLatest.Id) |
| | | .ExecuteCommand(); |
| | | } |
| | | else |
| | | { |
| | | Console.WriteLine("[重送检] 仍未生成新首检单,请检查存储过程计数是否排除作废单。"); |
| | | } |
| | | } |
| | | } |
| | | |