RecycleBinDM.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. unit RecycleBinDM;
  2. interface
  3. uses
  4. SysUtils, Classes, DB, Provider, DBClient, ADODB, ZjIDTree,
  5. XMLDoc, XMLIntf, ConstVarUnit;
  6. type
  7. TRecycleBinData = class(TDataModule)
  8. atRecycleBin: TADOTable;
  9. cdsRecycleBin: TClientDataSet;
  10. dspRecycleBin: TDataSetProvider;
  11. cdsRecycleBinID: TIntegerField;
  12. cdsRecycleBinName: TWideStringField;
  13. cdsBills: TClientDataSet;
  14. cdsDrawingQuantity: TClientDataSet;
  15. cdsBillsID: TIntegerField;
  16. cdsBillsParentID: TIntegerField;
  17. cdsBillsNextSiblingID: TIntegerField;
  18. cdsBillsUnits: TWideStringField;
  19. cdsBillsQuantity: TFloatField;
  20. cdsBillsUnitPrice: TBCDField;
  21. cdsBillsTotalPrice: TBCDField;
  22. cdsBillsDesignQuantity: TFloatField;
  23. cdsBillsDesignQuantity2: TFloatField;
  24. cdsBillsDesignPrice: TFloatField;
  25. cdsBillsMemoStr: TMemoField;
  26. cdsBillsCode: TWideStringField;
  27. cdsBillsName: TWideStringField;
  28. cdsDrawingQuantityName: TWideStringField;
  29. cdsDrawingQuantityUnits: TWideStringField;
  30. cdsDrawingQuantityBillsID: TIntegerField;
  31. cdsDrawingQuantityDQuantity1: TFloatField;
  32. cdsDrawingQuantityMemoContext: TWideStringField;
  33. cdsRecycleBinCreateTime: TWideStringField;
  34. cdsRecycleBinFileName: TWideStringField;
  35. procedure DataModuleDestroy(Sender: TObject);
  36. procedure DataModuleCreate(Sender: TObject);
  37. procedure cdsRecycleBinAfterScroll(DataSet: TDataSet);
  38. procedure cdsBillsAfterScroll(DataSet: TDataSet);
  39. private
  40. FBillsTree: TZjIDTree;
  41. FConnection: TADOConnection;
  42. FXMLDoc: IXMLDocument;
  43. FLoading: Boolean;
  44. FFileList: TStrings;
  45. function GetNewID: Integer;
  46. procedure SetConnection(const Value: TADOConnection);
  47. procedure RefreshDrawingQuantity(ABillsID: Integer);
  48. procedure EmptyDateSets;
  49. function CreateXMLDoc: IXMLDocument;
  50. procedure LoadBillsAndDrawingQuantity(ANode: IXMLNode);
  51. procedure LoadBills(ANode: IXMLNode; AParentID: Integer);
  52. procedure LoadDrawingQuantity(ANode: IXMLNode);
  53. procedure ConnectBillsTree;
  54. procedure DisConnectTree;
  55. procedure DeleteFiles;
  56. public
  57. procedure InsertNode(const AProjName, AName: string);
  58. procedure DeleteCurrentNode;
  59. procedure ClearNodes;
  60. function GetCurrentFileName: string;
  61. procedure Save;
  62. procedure RefreshBills;
  63. property Connection: TADOConnection read FConnection write SetConnection;
  64. property BillsTree: TZjIDTree read FBillsTree;
  65. end;
  66. implementation
  67. uses Variants;
  68. {$R *.dfm}
  69. { TRecycleBinData }
  70. procedure TRecycleBinData.DeleteCurrentNode;
  71. begin
  72. if cdsRecycleBin.RecordCount > 0 then
  73. begin
  74. DeleteFile(GetCurrentFileName);
  75. FFileList.Delete(FFileList.IndexOf(GetCurrentFileName));
  76. cdsRecycleBin.Delete;
  77. end;
  78. end;
  79. function TRecycleBinData.GetCurrentFileName: string;
  80. var
  81. S: string;
  82. begin
  83. S := ExtractFilePath(ParamStr(0)) + cdsRecycleBinFileName.AsString;
  84. Result := S;
  85. end;
  86. function TRecycleBinData.GetNewID: Integer;
  87. begin
  88. cdsRecycleBin.Last;
  89. Result := cdsRecycleBinID.AsInteger + 1;
  90. end;
  91. procedure TRecycleBinData.InsertNode(const AProjName, AName: string);
  92. var
  93. iID: Integer;
  94. begin
  95. FLoading := True;
  96. try
  97. iID := GetNewID;
  98. cdsRecycleBin.Append;
  99. cdsRecycleBinID.Value := iID;
  100. cdsRecycleBinName.Value := AName;
  101. cdsRecycleBinCreateTime.Value := FormatDateTime('yyyyÄêmmÔÂddÈÕhhʱnn·ÖssÃë', Now);
  102. cdsRecycleBinFileName.Value := 'RecycleBin\' + AProjName + '\' + AName + '(' + cdsRecycleBinCreateTime.AsString + ').bak';
  103. cdsRecycleBin.Post;
  104. FFileList.Add(GetCurrentFileName);
  105. finally
  106. FLoading := False;
  107. end;
  108. end;
  109. procedure TRecycleBinData.Save;
  110. begin
  111. FFileList.Clear;
  112. cdsRecycleBin.ApplyUpdates(0);
  113. end;
  114. procedure TRecycleBinData.SetConnection(const Value: TADOConnection);
  115. begin
  116. FConnection := Value;
  117. atRecycleBin.Connection := FConnection;
  118. cdsRecycleBin.Open;
  119. end;
  120. procedure TRecycleBinData.DataModuleDestroy(Sender: TObject);
  121. begin
  122. DeleteFiles;
  123. FBillsTree.Free;
  124. FFileList.Free;
  125. end;
  126. procedure TRecycleBinData.DataModuleCreate(Sender: TObject);
  127. begin
  128. FBillsTree := TZjIDTree.Create;
  129. FBillsTree.KeyFieldName := 'ID';
  130. FBillsTree.ParentFieldName := 'ParentID';
  131. FBillsTree.NextSiblingFieldName := 'NextSiblingID';
  132. FBillsTree.AutoExpand := True;
  133. FFileList := TStringList.Create;
  134. end;
  135. procedure TRecycleBinData.cdsRecycleBinAfterScroll(DataSet: TDataSet);
  136. begin
  137. if not FLoading then
  138. RefreshBills;
  139. end;
  140. procedure TRecycleBinData.cdsBillsAfterScroll(DataSet: TDataSet);
  141. begin
  142. if not FLoading then
  143. RefreshDrawingQuantity(cdsBillsID.AsInteger);
  144. end;
  145. procedure TRecycleBinData.RefreshBills;
  146. begin
  147. if cdsRecycleBin.RecordCount = 0 then Exit;
  148. if not FileExists(GetCurrentFileName) then Exit;
  149. EmptyDateSets;
  150. FXMLDoc := CreateXMLDoc;
  151. try
  152. DisConnectTree;
  153. FXMLDoc.LoadFromFile(GetCurrentFileName);
  154. LoadBillsAndDrawingQuantity(FXMLDoc.DocumentElement);
  155. ConnectBillsTree;
  156. finally
  157. FXMLDoc := nil;
  158. end;
  159. end;
  160. procedure TRecycleBinData.RefreshDrawingQuantity(ABillsID: Integer);
  161. begin
  162. cdsDrawingQuantity.DisableControls;
  163. try
  164. cdsDrawingQuantity.Filtered := False;
  165. cdsDrawingQuantity.Filter := 'BillsID=' + IntToStr(ABillsID);
  166. cdsDrawingQuantity.Filtered := True;
  167. finally
  168. cdsDrawingQuantity.EnableControls;
  169. end;
  170. end;
  171. function TRecycleBinData.CreateXMLDoc: IXMLDocument;
  172. begin
  173. Result := TXMLDocument.Create(nil) as IXMLDocument;
  174. Result.Active := True;
  175. Result.Encoding := 'gb2312';
  176. Result.Options := Result.Options + [doNodeAutoIndent];
  177. Result.AddChild('SmartCost');
  178. end;
  179. procedure TRecycleBinData.EmptyDateSets;
  180. begin
  181. cdsBills.EmptyDataSet;
  182. cdsDrawingQuantity.EmptyDataSet;
  183. end;
  184. procedure TRecycleBinData.LoadBillsAndDrawingQuantity(ANode: IXMLNode);
  185. begin
  186. ANode := ANode.ChildNodes.FindNode(c_BillsList);
  187. if Assigned(ANode) and ANode.HasChildNodes then
  188. LoadBills(ANode.ChildNodes.First, -1);
  189. end;
  190. procedure TRecycleBinData.LoadBills(ANode: IXMLNode; AParentID: Integer);
  191. var
  192. strCode: string;
  193. begin
  194. if ANode = nil then Exit;
  195. if SameText(ANode.NodeName, c_BillsItem) then
  196. begin
  197. cdsBills.Append;
  198. cdsBillsID.Value := ANode.Attributes[c_ID];
  199. cdsBillsParentID.Value := AParentID;
  200. cdsBillsNextSiblingID.Value := ANode.Attributes[c_NextSiblingID];
  201. strCode := VarToStr(ANode.Attributes[c_Code]);
  202. if strCode <> '' then
  203. cdsBillsCode.Value := strCode
  204. else
  205. cdsBillsCode.Value := ANode.Attributes[c_BCode];
  206. cdsBillsName.Value := VarToStr(ANode.Attributes[c_Name]);
  207. cdsBillsUnits.Value := VarToStr(ANode.Attributes[c_Units]);
  208. cdsBillsQuantity.Value := ANode.Attributes[c_Quantity];
  209. cdsBillsDesignQuantity.Value := ANode.Attributes[c_DesignQuantity1];
  210. cdsBillsDesignQuantity2.Value := ANode.Attributes[c_DesignQuantity2];
  211. cdsBillsDesignPrice.Value := ANode.Attributes[c_DesignPrice];
  212. cdsBillsUnitPrice.Value := ANode.Attributes[c_UnitPrice];
  213. cdsBillsTotalPrice.Value := ANode.Attributes[c_TotalPrice];
  214. cdsBillsMemoStr.Value := ANode.Attributes[c_MemoString];
  215. cdsBills.Post;
  216. LoadDrawingQuantity(ANode);
  217. end;
  218. if ANode.HasChildNodes then
  219. LoadBills(ANode.ChildNodes.First, cdsBillsID.AsInteger);
  220. if Assigned(ANode.NextSibling) then
  221. LoadBills(ANode.NextSibling, AParentID);
  222. end;
  223. procedure TRecycleBinData.LoadDrawingQuantity(ANode: IXMLNode);
  224. var
  225. I: Integer;
  226. vNode: IXMLNode;
  227. begin
  228. ANode := ANode.ChildNodes.FindNode(c_DrawQList);
  229. if ANode = nil then Exit;
  230. for I := 0 to ANode.ChildNodes.Count - 1 do
  231. begin
  232. vNode := ANode.ChildNodes[I];
  233. cdsDrawingQuantity.Append;
  234. cdsDrawingQuantityBillsID.Value := vNode.Attributes[c_BillsID];
  235. cdsDrawingQuantityName.Value := vNode.Attributes[c_Name];
  236. cdsDrawingQuantityUnits.Value := vNode.Attributes[c_Units];
  237. cdsDrawingQuantityDQuantity1.Value := vNode.Attributes[c_DesignQuantity1];
  238. cdsDrawingQuantityMemoContext.Value := vNode.Attributes[c_MemoString];
  239. cdsDrawingQuantity.Post;
  240. end;
  241. end;
  242. procedure TRecycleBinData.ConnectBillsTree;
  243. begin
  244. FBillsTree.DataSet := cdsBills;
  245. FBillsTree.Active := True;
  246. end;
  247. procedure TRecycleBinData.DisConnectTree;
  248. begin
  249. FBillsTree.DataSet := nil;
  250. FBillsTree.Active := False;
  251. end;
  252. procedure TRecycleBinData.DeleteFiles;
  253. var
  254. I: Integer;
  255. begin
  256. for I := 0 to FFileList.Count - 1 do
  257. DeleteFile(FFileList[I]);
  258. end;
  259. procedure TRecycleBinData.ClearNodes;
  260. begin
  261. cdsRecycleBin.First;
  262. while not cdsRecycleBin.Eof do
  263. DeleteCurrentNode;
  264. end;
  265. end.