chenhaozhe
2025-12-29 7879f7a29945ed155e057d064c867e0839d081dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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<Model.ClsCg_CarTrajectoryBillSub> DetailColl = new List<Model.ClsCg_CarTrajectoryBillSub>();
 
        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 安全转换方法
 
        /// <summary>
        /// 安全转换为整数
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的整数,转换失败返回0</returns>
        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; // 默认值
            }
        }
 
        /// <summary>
        /// 安全转换为可空整数
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的可空整数,转换失败返回null</returns>
        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
            }
        }
 
        /// <summary>
        /// 安全转换为字符串
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的字符串,转换失败返回空字符串</returns>
        private string SafeConvertToString(object value)
        {
            if (value == null || value == DBNull.Value)
                return string.Empty;
 
            try
            {
                return value.ToString().Trim();
            }
            catch
            {
                return string.Empty;
            }
        }
 
        /// <summary>
        /// 安全转换为可空日期时间
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的可空日期时间,转换失败返回null</returns>
        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; // 所有尝试都失败
            }
        }
 
        /// <summary>
        /// 安全转换为日期时间
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换后的日期时间,转换失败返回默认值</returns>
        private DateTime SafeConvertToDateTime(object value, DateTime defaultValue)
        {
            DateTime? result = SafeConvertToNullableDateTime(value);
            return result ?? defaultValue;
        }
 
        /// <summary>
        /// 安全转换为小数
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的小数,转换失败返回0</returns>
        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; // 默认值
            }
        }
 
        /// <summary>
        /// 安全转换为可空小数
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的可空小数,转换失败返回null</returns>
        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
            }
        }
 
        /// <summary>
        /// 安全转换为布尔值
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的布尔值,转换失败返回false</returns>
        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; // 默认值
            }
        }
 
        /// <summary>
        /// 安全转换为可空布尔值
        /// </summary>
        /// <param name="value">数据库值</param>
        /// <returns>转换后的可空布尔值,转换失败返回null</returns>
        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
    }
}