BidLotDM.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. unit BidLotDM;
  2. interface
  3. uses
  4. SysUtils,
  5. Classes,
  6. DB,
  7. ConstTypeUnit,
  8. ADODB;
  9. type
  10. TBidLotDataModule = class(TDataModule)
  11. atBidLot: TADOTable;
  12. atBidLotID: TIntegerField;
  13. atBidLotProjName: TWideStringField;
  14. atBidLotAliasName: TWideStringField;
  15. atBidLotFullName: TWideStringField;
  16. atBidLotFlag: TIntegerField;
  17. procedure atBidLotAfterPost(DataSet: TDataSet);
  18. private
  19. FProject: TObject;
  20. function MaxBidLotID: Integer;
  21. function CheckBidLot(const aProjName, aFullName: string): Boolean;
  22. procedure BeginRefresh;
  23. procedure EndRefresh;
  24. procedure ExecuteSQL(const aSQL: string);
  25. procedure AddBidLot(const aProjName, aFullName: string);
  26. procedure DeleteBidLot(const aProjName, aFullName: string);
  27. procedure ReNameBidLot(const aProjName, aFullName: string);
  28. function GetBidLotDB: TDataSet;
  29. procedure SetProject(const Value: TObject);
  30. public
  31. procedure Notify(aOperation: TBidLotOperation; const aProjName, aFullName: string);
  32. procedure RefreshBidLot(aStrings: TStrings);
  33. procedure SyncProjectView;
  34. property Project: TObject read FProject write SetProject;
  35. property BidLotDB: TDataSet read GetBidLotDB;
  36. end;
  37. implementation
  38. uses
  39. ScProjectManager;
  40. {$R *.dfm}
  41. { TBidLotDataModule }
  42. procedure TBidLotDataModule.AddBidLot(const aProjName, aFullName: string);
  43. begin
  44. if not atBidLot.Active then Exit;
  45. atBidLot.Append;
  46. atBidLotID.Value := MaxBidLotID;
  47. atBidLotProjName.Value := aProjName;
  48. atBidLotAliasName.Value := aProjName;
  49. atBidLotFullName.Value := aFullName;
  50. atBidLotFlag.Value := 1;
  51. atBidLot.Post;
  52. end;
  53. procedure TBidLotDataModule.BeginRefresh;
  54. var
  55. sSQL: string;
  56. begin
  57. sSQL := 'Update BidLot Set Flag = 0';
  58. ExecuteSQL(sSQL);
  59. end;
  60. function TBidLotDataModule.CheckBidLot(const aProjName,
  61. aFullName: string): Boolean;
  62. var
  63. aqQuery: TADOQuery;
  64. begin
  65. aqQuery := TADOQuery.Create(nil);
  66. try
  67. aqQuery.Connection := TProject(FProject).Connection;
  68. aqQuery.SQL.Text := Format('Select * From BidLot Where ' +
  69. '(ProjName = ''%s'') and (FullName = ''%s'')',
  70. [aProjName, aFullName]);
  71. aqQuery.Open;
  72. Result := aqQuery.RecordCount > 0;
  73. if Result then
  74. begin
  75. aqQuery.Edit;
  76. aqQuery.FieldByName('Flag').AsInteger := 1;
  77. aqQuery.Post;
  78. end;
  79. finally
  80. aqQuery.Free;
  81. end;
  82. end;
  83. procedure TBidLotDataModule.DeleteBidLot(const aProjName,
  84. aFullName: string);
  85. var
  86. sSQL: string;
  87. begin
  88. sSQL := Format('Delete * From BidLot Where ' +
  89. '(ProjName = ''%s'') and (FullName = ''%s'')',
  90. [aProjName, aFullName]);
  91. ExecuteSQL(sSQL);
  92. // atBidLot.Refresh;
  93. end;
  94. procedure TBidLotDataModule.EndRefresh;
  95. var
  96. sSQL: string;
  97. begin
  98. sSQL := 'Delete * From BidLot Where Flag = 0';
  99. ExecuteSQL(sSQL);
  100. end;
  101. procedure TBidLotDataModule.ExecuteSQL(const aSQL: string);
  102. var
  103. aqQuery: TADOQuery;
  104. begin
  105. aqQuery := TADOQuery.Create(nil);
  106. try
  107. aqQuery.Connection := TProject(FProject).Connection;
  108. aqQuery.SQL.Text := aSQL;
  109. aqQuery.ExecSQL;
  110. finally
  111. aqQuery.Free;
  112. end;
  113. end;
  114. function TBidLotDataModule.GetBidLotDB: TDataSet;
  115. begin
  116. Result := atBidLot;
  117. end;
  118. function TBidLotDataModule.MaxBidLotID: Integer;
  119. var
  120. aqQuery: TADOQuery;
  121. begin
  122. aqQuery := TADOQuery.Create(nil);
  123. try
  124. aqQuery.Connection := TProject(FProject).Connection;
  125. aqQuery.SQL.Text := 'Select Max(ID) As ID From BidLot';
  126. aqQuery.Open;
  127. Result := aqQuery.FieldByName('ID').AsInteger + 1;
  128. finally
  129. aqQuery.Free;
  130. end;
  131. end;
  132. procedure TBidLotDataModule.Notify(aOperation: TBidLotOperation;
  133. const aProjName, aFullName: string);
  134. begin
  135. if FProject = nil then Exit;
  136. case aOperation of
  137. boAdd : AddBidLot(aProjName, aFullName);
  138. boDelete : DeleteBidLot(aProjName, aFullName);
  139. boReName : ReNameBidLot(aProjName, aFullName);
  140. end;
  141. TProject(FProject).Changed := True;
  142. end;
  143. procedure TBidLotDataModule.RefreshBidLot(aStrings: TStrings);
  144. var
  145. I: Integer;
  146. sProjName, sFullName: string;
  147. begin
  148. BeginRefresh;
  149. for I := 0 to aStrings.Count - 1 do
  150. begin
  151. sProjName := aStrings[I];
  152. sFullName := string(aStrings.Objects[I]);
  153. if not CheckBidLot(sProjName, sFullName) then
  154. AddBidLot(sProjName, sFullName);
  155. end;
  156. EndRefresh;
  157. atBidLot.Active := False;
  158. atBidLot.Active := True;
  159. atBidLot.First;
  160. end;
  161. procedure TBidLotDataModule.ReNameBidLot(const aProjName,
  162. aFullName: string);
  163. var
  164. sSQL: string;
  165. begin
  166. sSQL := Format('Update BidLot Set ProjName = ''%s'' Where ' +
  167. 'FullName = ''%s''',
  168. [aProjName, aFullName]);
  169. ExecuteSQL(sSQL);
  170. end;
  171. procedure TBidLotDataModule.SetProject(const Value: TObject);
  172. begin
  173. FProject := Value;
  174. if Assigned(FProject) then
  175. begin
  176. atBidLot.Connection := TProject(FProject).Connection;
  177. atBidLot.Active := True;
  178. end;
  179. end;
  180. procedure TBidLotDataModule.SyncProjectView;
  181. var
  182. sgsBidLot: TStrings;
  183. begin
  184. atBidLot.Active := False;
  185. atBidLot.Active := True;
  186. sgsBidLot := TStringList.Create;
  187. try
  188. atBidLot.First;
  189. while not atBidLot.Eof do
  190. begin
  191. sgsBidLot.Add(atBidLotAliasName.Value);
  192. atBidLot.Next;
  193. end;
  194. TProject(FProject).BidLotList := sgsBidLot;
  195. finally
  196. sgsBidLot.Free;
  197. end;
  198. end;
  199. procedure TBidLotDataModule.atBidLotAfterPost(DataSet: TDataSet);
  200. begin
  201. TProject(FProject).Changed := True;
  202. end;
  203. end.