StageCompareDm.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. unit StageCompareDm;
  2. {--------------------------------------
  3. 为减少内存占用,项目(当期数据)打开时,该单元不打开
  4. 仅在界面切换至审核比较数据时,更新加载该单元数据,当界面再次切换,应关闭该单元。
  5. 缺陷:每次用户点击切换界面至审核比较时,将消耗较多时间
  6. --------------------------------------}
  7. interface
  8. uses
  9. SysUtils, Classes, sdDB, sdProvider, ADODB, mDataRecord;
  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. DeleteCacheSql = 'Delete From StageCompare Where '+
  36. ' (IsNull(Quantity0) or Quantity0 = 0) and (IsNull(TotalPrice0) or TotalPrice0 = 0)'+
  37. ' and (IsNull(Quantity1) or Quantity1 = 0) and (IsNull(TotalPrice1) or TotalPrice1 = 0)'+
  38. ' and (IsNull(Quantity2) or Quantity2 = 0) and (IsNull(TotalPrice2) or TotalPrice2 = 0)'+
  39. ' and (IsNull(Quantity3) or Quantity3 = 0) and (IsNull(TotalPrice3) or TotalPrice3 = 0)'+
  40. ' and (IsNull(Quantity4) or Quantity4 = 0) and (IsNull(TotalPrice4) or TotalPrice4 = 0)'+
  41. ' and (IsNull(Quantity5) or Quantity5 = 0) and (IsNull(TotalPrice5) or TotalPrice5 = 0)'+
  42. ' and (IsNull(Quantity6) or Quantity6 = 0) and (IsNull(TotalPrice6) or TotalPrice6 = 0)'+
  43. ' and (IsNull(Quantity7) or Quantity7 = 0) and (IsNull(TotalPrice7) or TotalPrice7 = 0)'+
  44. ' and (IsNull(Quantity8) or Quantity8 = 0) and (IsNull(TotalPrice8) or TotalPrice8 = 0)'+
  45. ' and (IsNull(Quantity9) or Quantity9 = 0) and (IsNull(TotalPrice9) or TotalPrice9 = 0)'+
  46. ' and (IsNull(Quantity10) or Quantity10 = 0) and (IsNull(TotalPrice10) or TotalPrice10 = 0)'+
  47. ' and (IsNull(Quantity11) or Quantity11 = 0) and (IsNull(TotalPrice11) or TotalPrice11 = 0)'+
  48. ' and (IsNull(Quantity12) or Quantity12 = 0) and (IsNull(TotalPrice12) or TotalPrice12 = 0)'+
  49. ' and (IsNull(Quantity13) or Quantity13 = 0) and (IsNull(TotalPrice13) or TotalPrice13 = 0)'+
  50. ' and (IsNull(Quantity14) or Quantity14 = 0) and (IsNull(TotalPrice14) or TotalPrice14 = 0)';
  51. type
  52. TStageCompareData = class(TDataModule)
  53. sdpStageCompare: TsdADOProvider;
  54. sddStageCompare: TsdDataSet;
  55. procedure sddStageCompareGetRecordClass(
  56. var ARecordClass: TsdRecordClass);
  57. private
  58. FPhaseData: TObject;
  59. function CheckCompareExist: Boolean;
  60. procedure ExecuteSql(const ASql: string);
  61. procedure ReloadAllCompareData;
  62. procedure InsertLastesyStageData(AIndex: Integer; const ATableName: string);
  63. procedure UpdateHistoryStageData(AIndex: Integer; const ATableName: string);
  64. public
  65. constructor Create(APhaseData: TObject);
  66. destructor Destroy; override;
  67. procedure Open;
  68. procedure Close;
  69. function HasPhaseData(AID: Integer): Boolean;
  70. end;
  71. implementation
  72. uses
  73. PhaseData;
  74. {$R *.dfm}
  75. { TStageCompareData }
  76. function TStageCompareData.CheckCompareExist: Boolean;
  77. var
  78. Tables: TStrings;
  79. begin
  80. Tables := TStringList.Create;
  81. try
  82. TPhaseData(FPhaseData).ADOConnection.GetTableNames(Tables);
  83. Result := Tables.IndexOf('StageCompare') > -1;
  84. finally
  85. Tables.Free;
  86. end;
  87. end;
  88. procedure TStageCompareData.Close;
  89. begin
  90. sddStageCompare.Close;
  91. end;
  92. constructor TStageCompareData.Create(APhaseData: TObject);
  93. begin
  94. inherited Create(nil);
  95. FPhaseData := APhaseData;
  96. sdpStageCompare.Connection := TPhaseData(FPhaseData).ADOConnection;
  97. end;
  98. destructor TStageCompareData.Destroy;
  99. begin
  100. inherited;
  101. end;
  102. procedure TStageCompareData.ExecuteSql(const ASql: string);
  103. var
  104. FQuery: TADOQuery;
  105. begin
  106. FQuery := TADOQuery.Create(nil);
  107. try
  108. FQuery.Connection := TPhaseData(FPhaseData).ADOConnection;
  109. FQuery.SQL.Clear;
  110. FQuery.SQL.Add(ASql);
  111. FQuery.ExecSQL;
  112. finally
  113. FQuery.Free;
  114. end;
  115. end;
  116. function TStageCompareData.HasPhaseData(AID: Integer): Boolean;
  117. var
  118. Rec: TStageCompareRecord;
  119. begin
  120. Result := False;
  121. Rec := TStageCompareRecord(sddStageCompare.FindKey('idxID', AID));
  122. if not Assigned(Rec) then Exit;
  123. Result := (Rec.TotalPrice0.AsFloat <> 0)
  124. or (Rec.TotalPrice1.AsFloat <> 0)
  125. or (Rec.TotalPrice2.AsFloat <> 0)
  126. or (Rec.TotalPrice3.AsFloat <> 0)
  127. or (Rec.TotalPrice4.AsFloat <> 0)
  128. or (Rec.TotalPrice5.AsFloat <> 0)
  129. or (Rec.TotalPrice6.AsFloat <> 0)
  130. or (Rec.TotalPrice7.AsFloat <> 0)
  131. or (Rec.TotalPrice8.AsFloat <> 0)
  132. or (Rec.TotalPrice9.AsFloat <> 0)
  133. or (Rec.TotalPrice10.AsFloat <> 0)
  134. or (Rec.TotalPrice11.AsFloat <> 0)
  135. or (Rec.TotalPrice12.AsFloat <> 0)
  136. or (Rec.TotalPrice13.AsFloat <> 0)
  137. or (Rec.TotalPrice14.AsFloat <> 0);
  138. end;
  139. procedure TStageCompareData.InsertLastesyStageData(AIndex: Integer;
  140. const ATableName: string);
  141. var
  142. sSql: string;
  143. begin
  144. sSql := Format(InsertSql, [AIndex, AIndex, ATableName]);
  145. ExecuteSql(sSql);
  146. end;
  147. procedure TStageCompareData.Open;
  148. begin
  149. ReloadAllCompareData;
  150. sddStageCompare.Open;
  151. if not Assigned(sddStageCompare.FindIndex('idxID')) then
  152. sddStageCompare.AddIndex('idxID', 'BillsID');
  153. end;
  154. procedure TStageCompareData.ReloadAllCompareData;
  155. var
  156. iStage: Integer;
  157. begin
  158. if CheckCompareExist then
  159. ExecuteSql(TableDeleteSql);
  160. ExecuteSql(TableCreateSql);
  161. ExecuteSql(KeyCreateSql);
  162. with TPhaseData(FPhaseData) do
  163. begin
  164. InsertLastesyStageData(AuditCount, StageTableName[AuditCount]);
  165. if AuditCount > 0 then
  166. for iStage := 0 to AuditCount - 1 do
  167. UpdateHistoryStageData(iStage, StageTableName[iStage]);
  168. end;
  169. ExecuteSql(DeleteCacheSql);
  170. end;
  171. procedure TStageCompareData.UpdateHistoryStageData(AIndex: Integer;
  172. const ATableName: string);
  173. var
  174. sSql: string;
  175. begin
  176. sSql := Format(UpdateSql, [ATableName, AIndex, AIndex]);
  177. ExecuteSql(sSql);
  178. end;
  179. procedure TStageCompareData.sddStageCompareGetRecordClass(
  180. var ARecordClass: TsdRecordClass);
  181. begin
  182. ARecordClass := TStageCompareRecord;
  183. end;
  184. end.