using System; using System.Collections.Generic; using System.Text; using System.Data; namespace DAL { public class ClsCg_CarTrajectoryBill : DBUtility.ClsXt_BaseBill { public Model.ClsCg_CarTrajectoryBillMain omodel = new Model.ClsCg_CarTrajectoryBillMain(); public List DetailColl = new List(); public ClsCg_CarTrajectoryBill() { base.MvarItemKeySub = "Cg_CarTrajectoryBillSub"; base.MvarItemKeySub2 = ""; base.MvarItemKeySub3 = ""; base.MvarItemKeySub4 = ""; base.MvarItemKey = "Cg_CarTrajectoryBillMain"; base.MvarReportTitle = "车辆轨迹单据"; base.BillType = "1119"; base.HBillSubType = "1119"; } #region 固定代码 ~ClsCg_CarTrajectoryBill() { DetailColl = null; } #endregion #region 自定义方法 // 修改单据 public override bool ModifyBill(Int64 lngBillKey, ref string sReturn) { try { oCn.BeginTran(); // 保存前控制 - 与新增保持一致 string HBillNote = ""; DataSet ds = oCn.RunProcReturn("EXEC h_p_Cg_CarTrajectoryBill_BeforeSaveCtrl " + omodel.HInterID.ToString() + ", '" + omodel.HBillNo + "','" + HBillNote + "',2 ", "h_p_Cg_CarTrajectoryBill_BeforeSaveCtrl"); if (ds == null) { sReturn = "保存前判断失败!"; return false; } if (DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBack"]) != "0") { sReturn = "保存失败!" + DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBackRemark"]); return false; } // 更新主表 string updateSql = "UPDATE Cg_CarTrajectoryBillMain SET " + "HBillNo = '" + omodel.HBillNo + "', " + "HDate = '" + omodel.HDate.ToString() + "', " + "HYear = " + omodel.HYear.ToString() + ", " + "HPeriod = " + omodel.HPeriod.ToString() + ", " + "HRemark = '" + omodel.HRemark + "', " + "HUpDater = '" + DBUtility.ClsPub.CurUserName + "', " + "HUpDateDate = GETDATE(), " + // 自定义字段 "HContractTransportBillMainID = " + (omodel.HContractTransportBillMainID.HasValue ? omodel.HContractTransportBillMainID.ToString() : "NULL") + ", " + "HContractTransportBillNo = '" + (omodel.HContractTransportBillNo ?? "") + "', " + "HCarNumber = '" + omodel.HCarNumber + "', " + "HGetTime = '" + omodel.HGetTime.ToString() + "', " + "HLongitude = " + (omodel.HLongitude.HasValue ? omodel.HLongitude.ToString() : "NULL") + ", " + "HLatitude = " + (omodel.HLatitude.HasValue ? omodel.HLatitude.ToString() : "NULL") + " " + "WHERE HInterID = " + lngBillKey.ToString(); // 调试输出 LogToFile("执行更新SQL: " + updateSql); oCn.RunProc(updateSql); // 删除关联 DeleteRelation(ref sReturn, lngBillKey); // 删除子表 DeleteBillSub(omodel.HInterID); // 插入子表数据 foreach (Model.ClsCg_CarTrajectoryBillSub oSub in DetailColl) { string insertSubSql = "INSERT INTO Cg_CarTrajectoryBillSub " + "(HInterID, HBillNo_bak, HEntryID, HCloseMan, HEntryCloseDate, HCloseType, HRemark, " + "HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType, HRelationQty, HRelationMoney) " + "VALUES (" + omodel.HInterID.ToString() + ", '" + omodel.HBillNo + "', " + oSub.HEntryID.ToString() + ", '" + oSub.HCloseMan + "', '" + oSub.HEntryCloseDate.ToString("yyyy-MM-dd") + "', " + Convert.ToInt32(oSub.HCloseType) + ", '" + oSub.HRemark + "', " + oSub.HSourceInterID.ToString() + ", " + oSub.HSourceEntryID.ToString() + ", '" + oSub.HSourceBillNo + "', '" + oSub.HSourceBillType + "', " + oSub.HRelationQty.ToString() + ", " + oSub.HRelationMoney.ToString() + ")"; oCn.RunProc(insertSubSql); } DataSet ds2 = oCn.RunProcReturn("EXEC h_p_Cg_CarTrajectoryBill_AfterSaveCtrl " + omodel.HInterID.ToString() + ", '" + omodel.HBillNo + "',2 ", "h_p_Cg_CarTrajectoryBill_AfterSaveCtrl"); if (ds2 == null) { sReturn = "保存后控制判断失败!"; oCn.RollBack(); return false; } if (DBUtility.ClsPub.isStrNull(ds2.Tables[0].Rows[0]["HBack"]) != "0") { sReturn = "保存失败!" + DBUtility.ClsPub.isStrNull(ds2.Tables[0].Rows[0]["HBackRemark"]); oCn.RollBack(); return false; } sReturn = "修改单据成功!"; oCn.Commit(); return true; } catch (Exception e) { // 记录详细的错误信息 string errorDetails = $"编辑单据失败!HInterID: {omodel.HInterID}, HBillNo: {omodel.HBillNo}\n" + $"错误信息: {e.Message}\n" + $"堆栈跟踪: {e.StackTrace}"; LogToFile(errorDetails); System.Diagnostics.Debug.WriteLine(errorDetails); sReturn = "编辑单据失败!" + e.Message; oCn.RollBack(); throw; } } // 添加日志方法 private void LogToFile(string message) { string logPath = @"D:\Logs\MES-SQL.log"; try { if (!System.IO.Directory.Exists(@"D:\Logs")) System.IO.Directory.CreateDirectory(@"D:\Logs"); System.IO.File.AppendAllText(logPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message + Environment.NewLine); } catch { } } // 新增单据 public override bool AddBill(ref string sReturn) { try { oCn.BeginTran(); // 获取新的HInterID if (omodel.HInterID == 0) { sReturn = "123"; return false; } // 保存前控制 string HBillNote = ""; DataSet ds = oCn.RunProcReturn("EXEC h_p_Cg_CarTrajectoryBill_BeforeSaveCtrl " + omodel.HInterID.ToString() + ", '" + omodel.HBillNo + "','" + HBillNote + "',1 ", "h_p_Cg_CarTrajectoryBill_BeforeSaveCtrl"); if (ds == null) { sReturn = "保存前判断失败!"; return false; } if (DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBack"]) != "0") { sReturn = "保存失败!" + DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBackRemark"]); return false; } // 插入主表 string insertMainSql = "INSERT INTO Cg_CarTrajectoryBillMain " + "(HYear, HPeriod, HBillType, HBillSubType, HInterID, HDate, HBillNo, HBillStatus, " + "HCheckItemNowID, HCheckItemNextID, HCheckFlowID, HRemark, HBacker, HBackDate, HBackRemark, " + "HChecker, HCheckDate, HMaker, HMakeDate, HUpDater, HUpDateDate, HCloseMan, HCloseDate, " + "HCloseType, HDeleteMan, HDeleteDate, HMainSourceBillType, HMainSourceInterID, " + "HMainSourceEntryID, HMainSourceBillNo, HPrintQty, HContractTransportBillMainID, " + "HContractTransportBillNo, HCarNumber, HGetTime, HLongitude, HLatitude) " + "VALUES (" + omodel.HYear.ToString() + ", " + omodel.HPeriod.ToString() + ", '" + this.BillType + "', '" + this.HBillSubType + "', " + omodel.HInterID.ToString() + ", '" + omodel.HDate.ToString() + "', '" + omodel.HBillNo + "', " + omodel.HBillStatus.ToString() + ", " + omodel.HCheckItemNowID.ToString() + ", " + omodel.HCheckItemNextID.ToString() + ", " + omodel.HCheckFlowID.ToString() + ", '" + omodel.HRemark + "', '" + omodel.HBacker + "', " + (omodel.HBackDate.HasValue ? "'" + omodel.HBackDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "'" : "NULL") + ", '" + omodel.HBackRemark + "', '" + omodel.HChecker + "', " + (omodel.HCheckDate.HasValue ? "'" + omodel.HCheckDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "'" : "NULL") + ", '" + omodel.HMaker + "', '" + omodel.HMakeDate.ToString() + "', '" + omodel.HUpDater + "', " + (omodel.HUpDateDate.HasValue ? "'" + omodel.HUpDateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "'" : "NULL") + ", '" + omodel.HCloseMan + "', " + (omodel.HCloseDate.HasValue ? "'" + omodel.HCloseDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "'" : "NULL") + ", " + Convert.ToInt32(omodel.HCloseType) + ", '" + omodel.HDeleteMan + "', " + (omodel.HDeleteDate.HasValue ? "'" + omodel.HDeleteDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "'" : "NULL") + ", '" + omodel.HMainSourceBillType + "', " + (omodel.HMainSourceInterID.HasValue ? omodel.HMainSourceInterID.ToString() : "NULL") + ", " + (omodel.HMainSourceEntryID.HasValue ? omodel.HMainSourceEntryID.ToString() : "NULL") + ", '" + omodel.HMainSourceBillNo + "', " + omodel.HPrintQty.ToString() + ", " + (omodel.HContractTransportBillMainID.HasValue ? omodel.HContractTransportBillMainID.ToString() : "NULL") + ", '" + omodel.HContractTransportBillNo + "', '" + omodel.HCarNumber + "', '" + omodel.HGetTime.ToString() + "', " + (omodel.HLongitude.HasValue ? omodel.HLongitude.ToString() : "NULL") + ", " + (omodel.HLatitude.HasValue ? omodel.HLatitude.ToString() : "NULL") + ")"; oCn.RunProc(insertMainSql); // 获取自动生成的HInterID //DataSet dsMainID = oCn.RunProcReturn("SELECT SCOPE_IDENTITY() AS HInterID", "GetMainID"); //if (dsMainID != null && dsMainID.Tables[0].Rows.Count > 0) //{ // omodel.HInterID = Convert.ToInt32(dsMainID.Tables[0].Rows[0]["HInterID"]); //} // 插入子表数据 foreach (Model.ClsCg_CarTrajectoryBillSub oSub in DetailColl) { string insertSubSql = "INSERT INTO Cg_CarTrajectoryBillSub " + "(HInterID, HBillNo_bak, HEntryID, HCloseMan, HEntryCloseDate, HCloseType, HRemark, " + "HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType, HRelationQty, HRelationMoney) " + "VALUES (" + omodel.HInterID.ToString() + ", '" + omodel.HBillNo + "', " + oSub.HEntryID.ToString() + ", '" + oSub.HCloseMan + "', '" + oSub.HEntryCloseDate.ToString("yyyy-MM-dd") + "', " + Convert.ToInt32(oSub.HCloseType) + ", '" + oSub.HRemark + "', " + oSub.HSourceInterID.ToString() + ", " + oSub.HSourceEntryID.ToString() + ", '" + oSub.HSourceBillNo + "', '" + oSub.HSourceBillType + "', " + oSub.HRelationQty.ToString() + ", " + oSub.HRelationMoney.ToString() + ")"; oCn.RunProc(insertSubSql); } // 保存后控制 DataSet ds2 = oCn.RunProcReturn("EXEC h_p_Cg_CarTrajectoryBill_AfterSaveCtrl " + omodel.HInterID.ToString() + ", '" + omodel.HBillNo + "',1 ", "h_p_Cg_CarTrajectoryBill_AfterSaveCtrl"); if (ds2 == null) { sReturn = "保存后控制判断失败!"; oCn.RollBack(); return false; } if (DBUtility.ClsPub.isStrNull(ds2.Tables[0].Rows[0]["HBack"]) != "0") { sReturn = "保存失败!" + DBUtility.ClsPub.isStrNull(ds2.Tables[0].Rows[0]["HBackRemark"]); oCn.RollBack(); return false; } sReturn = "新增单据成功!"; oCn.Commit(); return true; } catch (Exception e) { sReturn = e.Message; oCn.RollBack(); throw (e); } } // 显示单据 public override bool ShowBill(Int64 lngBillKey, ref string sReturn) { try { // 查询主表 DataSet Ds = oCn.RunProcReturn("SELECT * FROM Cg_CarTrajectoryBillMain WHERE HInterID = " + lngBillKey.ToString(), "Cg_CarTrajectoryBillMain"); if (Ds.Tables[0].Rows.Count == 0) { sReturn = "单据未找到!"; return false; } DataRow row = Ds.Tables[0].Rows[0]; // 基本字段 - 使用安全转换 omodel.HYear = SafeConvertToInt(row["HYear"]); omodel.HPeriod = SafeConvertToInt(row["HPeriod"]); omodel.HBillType = SafeConvertToString(row["HBillType"]); omodel.HBillSubType = SafeConvertToString(row["HBillSubType"]); omodel.HInterID = SafeConvertToInt(row["HInterID"]); omodel.HDate = SafeConvertToDateTime(row["HDate"], DateTime.Now); omodel.HBillNo = SafeConvertToString(row["HBillNo"]); omodel.HBillStatus = SafeConvertToInt(row["HBillStatus"]); omodel.HCheckItemNowID = SafeConvertToInt(row["HCheckItemNowID"]); omodel.HCheckItemNextID = SafeConvertToInt(row["HCheckItemNextID"]); omodel.HCheckFlowID = SafeConvertToInt(row["HCheckFlowID"]); omodel.HRemark = SafeConvertToString(row["HRemark"]); // 可空日期字段 omodel.HBackDate = SafeConvertToNullableDateTime(row["HBackDate"]); omodel.HBacker = SafeConvertToString(row["HBacker"]); omodel.HBackRemark = SafeConvertToString(row["HBackRemark"]); omodel.HCheckDate = SafeConvertToNullableDateTime(row["HCheckDate"]); omodel.HChecker = SafeConvertToString(row["HChecker"]); omodel.HMaker = SafeConvertToString(row["HMaker"]); omodel.HMakeDate = SafeConvertToDateTime(row["HMakeDate"], DateTime.Now); omodel.HUpDateDate = SafeConvertToNullableDateTime(row["HUpDateDate"]); omodel.HUpDater = SafeConvertToString(row["HUpDater"]); omodel.HCloseDate = SafeConvertToNullableDateTime(row["HCloseDate"]); omodel.HCloseMan = SafeConvertToString(row["HCloseMan"]); omodel.HCloseType = SafeConvertToBool(row["HCloseType"]); omodel.HDeleteDate = SafeConvertToNullableDateTime(row["HDeleteDate"]); omodel.HDeleteMan = SafeConvertToString(row["HDeleteMan"]); // 源单信息 omodel.HMainSourceBillType = SafeConvertToString(row["HMainSourceBillType"]); omodel.HMainSourceInterID = row["HMainSourceInterID"] == DBNull.Value ? (int?)null : SafeConvertToInt(row["HMainSourceInterID"]); omodel.HMainSourceEntryID = row["HMainSourceEntryID"] == DBNull.Value ? (int?)null : SafeConvertToInt(row["HMainSourceEntryID"]); omodel.HMainSourceBillNo = SafeConvertToString(row["HMainSourceBillNo"]); omodel.HPrintQty = SafeConvertToInt(row["HPrintQty"]); // 自定义字段 omodel.HContractTransportBillMainID = row["HContractTransportBillMainID"] == DBNull.Value ? (int?)null : SafeConvertToInt(row["HContractTransportBillMainID"]); omodel.HContractTransportBillNo = SafeConvertToString(row["HContractTransportBillNo"]); omodel.HCarNumber = SafeConvertToString(row["HCarNumber"]); omodel.HGetTime = SafeConvertToDateTime(row["HGetTime"], DateTime.Now); // 经纬度字段 omodel.HLongitude = row["HLongitude"] == DBNull.Value ? (decimal?)null : SafeConvertToDecimal(row["HLongitude"]); omodel.HLatitude = row["HLatitude"] == DBNull.Value ? (decimal?)null : SafeConvertToDecimal(row["HLatitude"]); omodel.HItemMainID = SafeConvertToInt(row["HItemMainID"]); // 查询子表 DataSet DsSub = oCn.RunProcReturn("SELECT * FROM Cg_CarTrajectoryBillSub WHERE HInterID = " + omodel.HInterID.ToString() + " ORDER BY HEntryID", "Cg_CarTrajectoryBillSub"); DetailColl.Clear(); for (int i = 0; i < DsSub.Tables[0].Rows.Count; i++) { Model.ClsCg_CarTrajectoryBillSub oSub = new Model.ClsCg_CarTrajectoryBillSub(); DataRow subRow = DsSub.Tables[0].Rows[i]; // 使用安全转换 oSub.HInterID = SafeConvertToInt(subRow["HInterID"]); oSub.HBillNo_bak = SafeConvertToString(subRow["HBillNo_bak"]); oSub.HEntryID = SafeConvertToInt(subRow["HEntryID"]); oSub.HCloseMan = SafeConvertToString(subRow["HCloseMan"]); oSub.HEntryCloseDate = SafeConvertToDateTime(subRow["HEntryCloseDate"], DateTime.Now); // 第404行修复 oSub.HCloseType = SafeConvertToBool(subRow["HCloseType"]); oSub.HRemark = SafeConvertToString(subRow["HRemark"]); oSub.HSourceInterID = SafeConvertToInt(subRow["HSourceInterID"]); oSub.HSourceEntryID = SafeConvertToInt(subRow["HSourceEntryID"]); oSub.HSourceBillNo = SafeConvertToString(subRow["HSourceBillNo"]); oSub.HSourceBillType = SafeConvertToString(subRow["HSourceBillType"]); oSub.HRelationQty = SafeConvertToDecimal(subRow["HRelationQty"]); oSub.HRelationMoney = SafeConvertToDecimal(subRow["HRelationMoney"]); oSub.HItemSubID = SafeConvertToInt(subRow["HItemSubID"]); DetailColl.Add(oSub); } sReturn = "显示单据成功!"; return true; } catch (Exception e) { sReturn = $"显示单据失败!单据ID: {lngBillKey},错误: {e.Message}"; throw; } } // 安全转换辅助方法 - 添加到 ClsCg_CarTrajectoryBill 类中 #region 安全转换方法 /// /// 安全转换为整数 /// /// 数据库值 /// 转换后的整数,转换失败返回0 private int SafeConvertToInt(object value) { if (value == null || value == DBNull.Value) return 0; try { return Convert.ToInt32(value); } catch { // 如果转换失败,尝试字符串解析 int result; if (int.TryParse(value.ToString(), out result)) return result; return 0; // 默认值 } } /// /// 安全转换为可空整数 /// /// 数据库值 /// 转换后的可空整数,转换失败返回null private int? SafeConvertToNullableInt(object value) { if (value == null || value == DBNull.Value) return null; try { return Convert.ToInt32(value); } catch { int result; if (int.TryParse(value.ToString(), out result)) return result; return null; // 转换失败返回null } } /// /// 安全转换为字符串 /// /// 数据库值 /// 转换后的字符串,转换失败返回空字符串 private string SafeConvertToString(object value) { if (value == null || value == DBNull.Value) return string.Empty; try { return value.ToString().Trim(); } catch { return string.Empty; } } /// /// 安全转换为可空日期时间 /// /// 数据库值 /// 转换后的可空日期时间,转换失败返回null private DateTime? SafeConvertToNullableDateTime(object value) { if (value == null || value == DBNull.Value) return null; try { return Convert.ToDateTime(value); } catch { // 尝试字符串解析 DateTime result; if (DateTime.TryParse(value.ToString(), out result)) return result; return null; // 所有尝试都失败 } } /// /// 安全转换为日期时间 /// /// 数据库值 /// 默认值 /// 转换后的日期时间,转换失败返回默认值 private DateTime SafeConvertToDateTime(object value, DateTime defaultValue) { DateTime? result = SafeConvertToNullableDateTime(value); return result ?? defaultValue; } /// /// 安全转换为小数 /// /// 数据库值 /// 转换后的小数,转换失败返回0 private decimal SafeConvertToDecimal(object value) { if (value == null || value == DBNull.Value) return 0m; try { return Convert.ToDecimal(value); } catch { decimal result; if (decimal.TryParse(value.ToString(), out result)) return result; return 0m; // 默认值 } } /// /// 安全转换为可空小数 /// /// 数据库值 /// 转换后的可空小数,转换失败返回null private decimal? SafeConvertToNullableDecimal(object value) { if (value == null || value == DBNull.Value) return null; try { return Convert.ToDecimal(value); } catch { decimal result; if (decimal.TryParse(value.ToString(), out result)) return result; return null; // 转换失败返回null } } /// /// 安全转换为布尔值 /// /// 数据库值 /// 转换后的布尔值,转换失败返回false private bool SafeConvertToBool(object value) { if (value == null || value == DBNull.Value) return false; try { // 处理数据库 BIT 类型 if (value is bool) return (bool)value; if (value is int) return ((int)value) != 0; if (value is string) { string strValue = ((string)value).ToLower().Trim(); return strValue == "1" || strValue == "true" || strValue == "t" || strValue == "是"; } // 最后尝试通用转换 return Convert.ToBoolean(value); } catch { return false; // 默认值 } } /// /// 安全转换为可空布尔值 /// /// 数据库值 /// 转换后的可空布尔值,转换失败返回null private bool? SafeConvertToNullableBool(object value) { if (value == null || value == DBNull.Value) return null; try { if (value is bool) return (bool)value; if (value is int) { int intValue = (int)value; if (intValue == 0) return false; if (intValue == 1) return true; return null; // 无效的布尔值 } if (value is string) { string strValue = ((string)value).ToLower().Trim(); if (strValue == "0" || strValue == "false" || strValue == "f" || strValue == "否") return false; if (strValue == "1" || strValue == "true" || strValue == "t" || strValue == "是") return true; return null; // 无效的布尔值 } bool result = Convert.ToBoolean(value); return result; } catch { return null; // 转换失败返回null } } #endregion // 获取新的HInterID // 判断单据号是否存在 public override bool IsExistBillNo(ref string sReturn, string sBillNo, DBUtility.ClsPub.Enum_BillStatus eBillStatus, Int64 lngMainID) { try { string sql = "SELECT COUNT(*) AS CNT FROM Cg_CarTrajectoryBillMain WHERE HBillNo = '" + sBillNo + "'"; if (lngMainID > 0) { sql += " AND HInterID <> " + lngMainID.ToString(); } DataSet ds = oCn.RunProcReturn(sql, "CheckBillNo"); if (ds != null && ds.Tables[0].Rows.Count > 0) { int count = Convert.ToInt32(ds.Tables[0].Rows[0]["CNT"]); return count > 0; } return false; } catch (Exception e) { sReturn = e.Message; return false; } } #endregion } }