unit uCutCopyPaste;

interface

uses
  System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
  FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async,
  FireDAC.DApt, FireDAC.UI.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Phys,
  FireDAC.VCLUI.Wait, FireDAC.Phys.MySQLDef, FireDAC.Phys.MySQL, Data.DB, FireDAC.Comp.Client,
  FireDAC.Comp.DataSet,
  Vcl.ComCtrls, // Для TTreeView
  Contnrs,vcl.dialogs,
  syncobjs,vcl.Forms,vcl.Controls, uDBConnection;




type
  TCutCopyPasteThread = class(TThread)
  private


  Procedure ListParentAndAllChildren(NodeID:Integer);
  Procedure ListAllChecked;


    { Private declarations }
  protected
    procedure Execute; override;
  end;


 var IDList:Tstringlist;
     CheckedNodes:array of integer;
     CS:TCriticalSection;
     DBConnection:TDBConnection;

implementation

uses MainUnit;



procedure TCutCopyPasteThread.Execute;
begin

DBConnection:=TDBConnection.Create(nil);
DBConnection.Connect;




DBConnection.Free;
  { Place thread code here }
end;

procedure TCutCopyPasteThread.ListAllChecked;
var i:integer; Node:TTreeNode; NodeID:integer;
    TreeView:TTreeView;
begin

 TreeView:=MainForm.TreeView1;

 if TreeView.Items.GetFirstNode=nil then exit else

    Node:=TreeView.Items.GetFirstNode;

    repeat

    if Node.StateIndex=2 then
     begin
      NodeID:=Integer(Node.Data^);
      ListParentAndAllChildren(NodeID); // Добавляем в ListID
     end;


    Node:=Node.GetNext;
    until not Assigned(Node) ;


end;


procedure TCutCopyPasteThread.ListParentAndAllChildren(NodeID: Integer);
var qSelectAllChildren:TFDQuery;

begin

   qSelectAllChildren:=TFDQuery.Create(nil);
   qSelectAllChildren.Connection:=DBConnection.FDConnection;

   //Если ID не был добавлен ранее, тогда добавляем
   if IDList.IndexOf(NodeID.Tostring)=-1 then
   IDList.Add(NodeID.Tostring); // Записываем родителя и всех детей


   //Ищем всех детей
   with qSelectAllChildren do
   begin

    SQL.Text:='SELECT * FROM treeview_db.tree where IdParent='+NodeID.ToString;

    Open();

     while not eof do
     begin
     // Для каждого ребенка вызываем рекурсивно
     ListParentAndAllChildren(FieldByName('id').AsInteger);

     Next;
     end;

    Close();
   end;

   qSelectAllChildren.Free;

end;


end.
