unit U_RhlHelp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, ADODB, ToolWin, Grids, DBGrids, StdCtrls,
  ExtCtrls, DBClient, ImgList;
type
   PMyRec = ^TMyRec;
   TMyRec = record
   wbcode: string;
   Tvtem_str: string;
end;
type
  TfrmRhlHelp = class(TForm)
    TreeView1: TTreeView;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1dyeCode: TcxGridDBColumn;
    tv1chnName: TcxGridDBColumn;
    DBGrid1Level1: TcxGridLevel;
    tv1DyePropertyName: TcxGridDBColumn;
    ADOQueryTmp: TADOQuery;
    ADOConnection1: TADOConnection;
    Splitter1: TSplitter;
    Panel2: TPanel;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Edt_code: TEdit;
    Edt_Name: TEdit;
    Panel4: TPanel;
    Panel6: TPanel;
    Dg_Sub: TDBGrid;
    ToolBar1: TToolBar;
    BtnFind: TToolButton;
    BtnOk: TToolButton;
    BtnClose: TToolButton;
    ADOQueryChild: TADOQuery;
    ADOQueryRhl: TADOQuery;
    DataSource1: TDataSource;
    CDS_Sel: TClientDataSet;
    DataSource2: TDataSource;
    CDS_SelDyeCode: TStringField;
    CDS_SelDyeName: TStringField;
    CDS_SelDyeProperty: TStringField;
    CDS_SelDyePropertyName: TStringField;
    CDS_SelDyeUnit: TStringField;
    CDS_SelDyeUnitName: TStringField;
    CDS_SelRlUnit: TStringField;
    CDS_SelRlUnitName: TStringField;
    CDS_SelDyeCost: TFloatField;
    CDS_SelModel: TStringField;
    ImageList24: TImageList;
    tv1zjm: TcxGridDBColumn;
    CDS_SeldyeCraft: TStringField;
    tv1dyeCraft: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure BtnCloseClick(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure BtnFindClick(Sender: TObject);
    procedure Edt_codeChange(Sender: TObject);
    procedure Edt_NameChange(Sender: TObject);
    procedure Dg_SubDblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure InitTree();
    procedure AddChildNode(parentNode:TtreeNode;mNo:string);
    procedure InitGrid();
    procedure DoQuery();
    procedure DoFilter();
  public
    fRlType:string;
  end;

var
  frmRhlHelp: TfrmRhlHelp;

implementation
uses
  U_global;
{$R *.dfm}

procedure TfrmRhlHelp.InitTree();
var
   MyRecPtr: PMyRec;
   tmpstr :string;
   NodeXX,CNode :TTreeNode;
   array_Node:array of TTreeNOde;
   i:integer;
begin
  try
    	 New(MyRecPtr);
  	   MyRecPtr^.wbcode := 'ALL';
  	   MyRecPtr^.Tvtem_str := '全部';
       with TreeView1 do
  	   begin
         Items.Clear;
         CNode := Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
         CNode.ImageIndex := 1;
         CNode.SelectedIndex := 2;
  	   end;

       with TreeView1 do
  	   begin
         Items.Clear;
         CNode := Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
         CNode.ImageIndex := 1;
         CNode.SelectedIndex := 2;
  	   end;
  	   with TreeView1,ADOQueryTmp do
  	   begin
         close;
         sql.clear;
         sql.Add('select code,name from xc_code');
         sql.Add('where flag = ''DYETYPE'' AND valid = ''Y''');
         if trim(fRlType)<>'' then
           sql.Add('and name='+quotedStr(fRlType));
         sql.Add('order by code');
         Open;
         if recordcount = 0 then
         begin
           application.MessageBox('对不起,基础表里未定义染化料基础分类:DYETYPE!','提示信息',0);
           exit;
         end;

         setLength(array_Node, RecordCount);
         i:=0;
         while not Eof do
         begin
  			   New(MyRecPtr);
  			   MyRecPtr^.wbcode := trim(fieldbyname('code').Asstring);
           MyRecPtr^.Tvtem_str := trim(fieldbyname('name').Asstring);
           NodeXX := Items.AddChildObject(TreeView1.Items[0], MyRecPtr^.Tvtem_str, MyRecPtr);
           NodeXX.ImageIndex := 1;
           NodeXX.SelectedIndex := 2;
           array_Node[i]:=NodeXX;
           INC(i);
           next;
         end;
         Close;
      end;

     ///////////////////////////////////////
     with ADOQueryChild do
     begin
       close;
       filtered:=false;
       sql.Clear ;
       sql.Add('select * from xc_code');
       sql.Add('where CLASSFLAG=''DYECLASS'' and valid=''Y''');
       Open;  
     end;
     for i:=0 to Length(array_Node)-1 do
     begin
       //创建子配方
       AddChildNode(array_Node[i],PMyRec(array_Node[i].Data)^.wbcode);
     end;

     TreeView1.Items[0].Selected := True;
     TreeView1.Items[0].Expand(true);
     InitGrid();
   except
       ;
   end;

end;


procedure TfrmRhlHelp.FormCreate(Sender: TObject);
begin
  panel2.Align :=alclient;
  CDS_sel.Close;
  CDS_Sel.CreateDataSet; 
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
  
end;

procedure TfrmRhlHelp.BtnCloseClick(Sender: TObject);
begin
  close;
end;
//////////////////////////////////////////////////////////////////
  //函数功能：增加子节点
//////////////////////////////////////////////////////////////////
procedure TfrmRhlHelp.AddChildNode(parentNode:TtreeNode;mNo:string);
var
  NodeInfo: PMyRec;
  mNode:TtreeNode;
  i:integer;
  array_Node:array of TTreeNOde;
begin
  with ADOQueryChild do
  begin

    filtered:=false;
    filter:='flag ='''+PMyRec(parentNode.Data)^.wbcode+'''';
    filtered:=true;
    Open;

    if RecordCount<=0 then exit;
    //////////////////////
    setLength(array_Node, RecordCount);
    i:=0;
    while not Eof do
    begin
      with treeView1 do
      begin
        New(NodeInfo);

        //编号
        NodeInfo^.wbcode:=trim(fieldByName('code').AsString);
        //名称
        NodeInfo^.Tvtem_str :=fieldByName('Name').AsString
                              +'('+trim(fieldByName('code').AsString)+')';
        mNode:=Items.AddChildObject(parentNode, NodeInfo^.Tvtem_str, NodeInfo);
        array_Node[i]:=mNode;
        INC(i);
      end;
      Next;
    end;

     for i:=0 to Length(array_Node)-1 do
     begin
       //创建子
       AddChildNode(array_Node[i],PMyRec(array_Node[i].Data)^.wbcode);
     end;
  end;

end;
/////////////////////////////////////////////////////////
  //
/////////////////////////////////////////////////////////
procedure TfrmRhlHelp.InitGrid();
var
   NodeXX:TTreeNode;
   mCode:string;
begin
   NodeXX := TreeView1.Selected;
   mCode := trim(PMyRec(NodeXX.Data).wbcode);
   //不是全部产品
   if (mCode <> 'ALL') then
   begin
     //主类产品
     if (mCode = 'RL') or (mCode = 'ZJ') then
     begin
       with ADOQueryRhl do
       begin
         Close;
         SQL.Clear;
         filtered:=false;
         //A.dyeCode,A.chnname as dyeName,A.DyePro
         SQL.Add('select * from  V_SC_dyeInfo A ' );
         sql.Add('where dyeProperty='''+mCode+'''');
         sql.Add('and dyeValid=''Y''');
         Open;
       end
     end
     //不是主类产品
     else
     begin
       with ADOQueryRhl do
       begin
         Close;
         SQL.Clear;
         filtered:=false;
         SQL.Add('select * from  V_SC_dyeInfo A ' );
         sql.Add('inner join md_class_type B on A.dyecode = B.datano');
         sql.Add('and A.dyeproperty IN( ''RL'',''ZJ'')');
         sql.Add('and B.typeNo='''+mCode+'''');
         sql.Add('and A.dyeValid=''Y''');
         Open;
       end
     end;
   end
   else
   begin
       with ADOQueryRhl do
       begin
         Close;
         SQL.Clear;
         filtered:=false;
         SQL.Add('select * from  V_SC_dyeInfo A ' );
         sql.Add('where dyeValid=''Y''');
         if trim(fRlType)<>'' then
           sql.Add('and dyePropertyName='+quotedStr(fRlType));
         Open;
       end
   end;
end;
procedure TfrmRhlHelp.TreeView1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmRhlHelp.tv1DblClick(Sender: TObject);
begin
  with CDS_Sel do
  begin
    Append;
    fieldByName('dyeCode').value:=trim(ADOQueryRhl.fieldByName('dyeCode').AsString);
    fieldByName('dyeName').value:=trim(ADOQueryRhl.fieldByName('chnName').AsString);
    fieldByName('dyeProperty').value:=trim(ADOQueryRhl.fieldByName('dyeProperty').AsString);
    fieldByName('dyePropertyName').value:=trim(ADOQueryRhl.fieldByName('dyePropertyName').AsString);
    fieldByName('dyeUnit').value:=trim(ADOQueryRhl.fieldByName('dyeUnit').AsString);
    fieldByName('dyeUnitName').value:=trim(ADOQueryRhl.fieldByName('dyeUnitName').AsString);
    fieldByName('RlUnit').value:=trim(ADOQueryRhl.fieldByName('RlUnit').AsString);
    fieldByName('RlUnitName').value:=trim(ADOQueryRhl.fieldByName('RlUnitName').AsString);
    fieldByName('dyeCost').value:=ADOQueryRhl.fieldByName('dyeCost').AsFloat;
    fieldByName('model').value:=trim(ADOQueryRhl.fieldByName('model').AsString);
    fieldByName('dyeCraft').value:=trim(ADOQueryRhl.fieldByName('dyeCraft').AsString);
    Post;
  end;
end;

procedure TfrmRhlHelp.BtnOkClick(Sender: TObject);
begin
  if CDS_sel.IsEmpty then
    ModalResult:=-1
  else
    ModalResult:=1;
end;
//////////////////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////////////////
procedure TfrmRhlHelp.DoQuery();
var
  whereStr:string;
begin
  if trim(Edt_code.Text)<>'' then
    whereStr:=' and dyeCode like '+quotedStr('%'+trim(Edt_code.Text)+'%');
  if trim(edt_Name.Text)<>'' then
    whereStr:=' and chnName like '+quotedStr('%'+trim(Edt_Name.Text)+'%');

  try
    ADOQueryRhl.DisableControls ;
    with ADOQueryRhl do
    begin
      Close;
      SQL.Clear;
      filtered:=false;
      SQL.Add('select * from  V_SC_dyeInfo A ' );
      sql.Add('where 1=1');
      sql.Add('and dyeValid=''Y''');
      sql.Add(whereStr);
      Open;
    end;
  finally
    ADOQueryRhl.EnableControls;
  end;

end;
//////////////////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////////////////
procedure TfrmRhlHelp.DoFilter();
var
  whereStr:string;
begin
  if trim(Edt_code.Text)<>'' then
    whereStr:='dyeCode like '+quotedStr('%'+trim(Edt_code.Text)+'%');
  if trim(edt_Name.Text)<>'' then
    whereStr:='chnName like '+quotedStr('%'+trim(Edt_Name.Text)+'%');
  try
    ADOQueryRhl.DisableControls ;
    with ADOQueryRhl do
    begin
      filtered:=false;
      filter:= whereStr;
      filtered:=true;
    end;
  finally
    ADOQueryRhl.EnableControls;
  end;

end;
procedure TfrmRhlHelp.BtnFindClick(Sender: TObject);
begin
  DoQuery();
end;

procedure TfrmRhlHelp.Edt_codeChange(Sender: TObject);
begin
  DoFilter();
end;

procedure TfrmRhlHelp.Edt_NameChange(Sender: TObject);
begin
  DoFilter();
end;

procedure TfrmRhlHelp.Dg_SubDblClick(Sender: TObject);
begin
  if CDS_sel.IsEmpty then exit;
  CDS_sel.Delete ;
end;

procedure TfrmRhlHelp.FormShow(Sender: TObject);
begin
  InitTree();
end;

end.
