StageCompareDm.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. unit StageCompareDm;
  2. {--------------------------------------
  3. 为减少内存占用,项目(当期数据)打开时,该单元不打开
  4. 仅在界面切换至审核比较数据时,更新加载该单元数据,当界面再次切换,应关闭该单元。
  5. 缺陷:每次用户点击切换界面至审核比较时,将消耗较多时间
  6. --------------------------------------}
  7. interface
  8. uses
  9. SysUtils, Classes, sdDB, sdProvider, ADODB;
  10. const
  11. TableDeleteSql = 'Drop Table StageCompare';
  12. TableCreateSql = 'Select BillsID, GatherQuantity As Quantity0, GatherTotalPrice As TotalPrice0,'+
  13. ' GatherQuantity As Quantity1, GatherTotalPrice As TotalPrice1,'+
  14. ' GatherQuantity As Quantity2, GatherTotalPrice As TotalPrice2,'+
  15. ' GatherQuantity As Quantity3, GatherTotalPrice As TotalPrice3,'+
  16. ' GatherQuantity As Quantity4, GatherTotalPrice As TotalPrice4,'+
  17. ' GatherQuantity As Quantity5, GatherTotalPrice As TotalPrice5,'+
  18. ' GatherQuantity As Quantity6, GatherTotalPrice As TotalPrice6,'+
  19. ' GatherQuantity As Quantity7, GatherTotalPrice As TotalPrice7,'+
  20. ' GatherQuantity As Quantity8, GatherTotalPrice As TotalPrice8,'+
  21. ' GatherQuantity As Quantity9, GatherTotalPrice As TotalPrice9,'+
  22. ' GatherQuantity As Quantity10, GatherTotalPrice As TotalPrice10,'+
  23. ' GatherQuantity As Quantity11, GatherTotalPrice As TotalPrice11,'+
  24. ' GatherQuantity As Quantity12, GatherTotalPrice As TotalPrice12,'+
  25. ' GatherQuantity As Quantity13, GatherTotalPrice As TotalPrice13,'+
  26. ' GatherQuantity As Quantity14, GatherTotalPrice As TotalPrice14'+
  27. ' Into StageCompare From Refer Where 1=2';
  28. KeyCreateSql = 'Alter Table StageCompare Add Constraint PrimaryKey Primary Key (BillsID)';
  29. InsertSql = 'Insert Into StageCompare (BillsID, Quantity%d, TotalPrice%d)'+
  30. ' Select BillsID, GatherQuantity, GatherTotalPrice'+
  31. ' From %s';
  32. UpdateSql = 'Update StageCompare As S, %s As A'+
  33. ' Set S.Quantity%d = A.GatherQuantity, S.TotalPrice%d = A.GatherTotalPrice'+
  34. ' Where S.BillsID = A.BillsID';
  35. type
  36. TStageCompareData = class(TDataModule)
  37. sdpStageCompare: TsdADOProvider;
  38. sddStageCompare: TsdDataSet;
  39. private
  40. FPhaseData: TObject;
  41. function CheckCompareExist: Boolean;
  42. procedure ExecuteSql(const ASql: string);
  43. procedure ReloadAllCompareData;
  44. procedure InsertLastesyStageData(AIndex: Integer; const ATableName: string);
  45. procedure UpdateHistoryStageData(AIndex: Integer; const ATableName: string);
  46. public
  47. constructor Create(APhaseData: TObject);
  48. destructor Destroy; override;
  49. procedure Open;
  50. procedure Close;
  51. end;
  52. implementation
  53. uses
  54. PhaseData;
  55. {$R *.dfm}
  56. { TStageCompareData }
  57. function TStageCompareData.CheckCompareExist: Boolean;
  58. var
  59. Tables: TStrings;
  60. begin
  61. Tables := TStringList.Create;
  62. try
  63. TPhaseData(FPhaseData).ADOConnection.GetTableNames(Tables);
  64. Result := Tables.IndexOf('StageCompare') > -1;
  65. finally
  66. Tables.Free;
  67. end;
  68. end;
  69. procedure TStageCompareData.Close;
  70. begin
  71. sddStageCompare.Close;
  72. end;
  73. constructor TStageCompareData.Create(APhaseData: TObject);
  74. begin
  75. inherited Create(nil);
  76. FPhaseData := APhaseData;
  77. sdpStageCompare.Connection := TPhaseData(FPhaseData).ADOConnection;
  78. end;
  79. destructor TStageCompareData.Destroy;
  80. begin
  81. inherited;
  82. end;
  83. procedure TStageCompareData.ExecuteSql(const ASql: string);
  84. var
  85. FQuery: TADOQuery;
  86. begin
  87. FQuery := TADOQuery.Create(nil);
  88. try
  89. FQuery.Connection := TPhaseData(FPhaseData).ADOConnection;
  90. FQuery.SQL.Clear;
  91. FQuery.SQL.Add(ASql);
  92. FQuery.ExecSQL;
  93. finally
  94. FQuery.Free;
  95. end;
  96. end;
  97. procedure TStageCompareData.InsertLastesyStageData(AIndex: Integer;
  98. const ATableName: string);
  99. var
  100. sSql: string;
  101. begin
  102. sSql := Format(InsertSql, [AIndex, AIndex, ATableName]);
  103. ExecuteSql(sSql);
  104. end;
  105. procedure TStageCompareData.Open;
  106. begin
  107. ReloadAllCompareData;
  108. sddStageCompare.Open;
  109. end;
  110. procedure TStageCompareData.ReloadAllCompareData;
  111. var
  112. iStage: Integer;
  113. begin
  114. if CheckCompareExist then
  115. ExecuteSql(TableDeleteSql);
  116. ExecuteSql(TableCreateSql);
  117. ExecuteSql(KeyCreateSql);
  118. with TPhaseData(FPhaseData) do
  119. begin
  120. InsertLastesyStageData(AuditCount, StageTableName[AuditCount]);
  121. if AuditCount > 0 then
  122. for iStage := 0 to AuditCount - 1 do
  123. UpdateHistoryStageData(iStage, StageTableName[iStage]);
  124. end;
  125. end;
  126. procedure TStageCompareData.UpdateHistoryStageData(AIndex: Integer;
  127. const ATableName: string);
  128. var
  129. sSql: string;
  130. begin
  131. sSql := Format(UpdateSql, [ATableName, AIndex, AIndex]);
  132. ExecuteSql(sSql);
  133. end;
  134. end.