unit U_GetGlide10;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids, ExtCtrls, ComCtrls, ToolWin,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid,StrUtils, ImgList;

type
  TfrmGetGlide10 = class(TForm)
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    ADOQuery1GlideNo: TStringField;
    ADOQuery1GlideName: TStringField;
    ADOQuery1GlidePara: TStringField;
    ADOQuery1GlideNote: TStringField;
    AD_sub: TADODataSet;
    Ds_sub: TDataSource;
    AD_GY_Fllb: TADODataSet;
    Ds_Gy_Fllb: TDataSource;
    Splitter2: TSplitter;
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ToolBar1: TToolBar;
    BtnFind: TToolButton;
    BtnOk: TToolButton;
    BtnClose: TToolButton;
    TreeView1: TTreeView;
    Panel2: TPanel;
    Splitter1: TSplitter;
    Panel5: TPanel;
    dbGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    dbGrid1Level1: TcxGridLevel;
    tv1glideNo: TcxGridDBColumn;
    tv1glideName: TcxGridDBColumn;
    tv1glidePara: TcxGridDBColumn;
    tv1glideNote: TcxGridDBColumn;
    Panel3: TPanel;
    Panel6: TPanel;
    DG_Sub: TDBGrid;
    ADOConnection1: TADOConnection;
    ImageList24: TImageList;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnCloseClick(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
    procedure BtnFindClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DG_SubDblClick(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    this_code :string;
    procedure DoQuery();
    procedure IntiTree();
    procedure DoFilter(mDept:string);
    procedure IntiGroupTree();
  public
    fSysCode:string;
    fGroupType:string;
  end;

var
  frmGetGlide10: TfrmGetGlide10;

implementation

{$R *.dfm}

uses
  U_global;

type
PMyRec = ^TMyRec;
TMyRec = record
  wbcode: string;
  Tvtem_str: string;
end;

procedure TfrmGetGlide10.FormCreate(Sender: TObject);
begin
  Panel2.Align := AlClient;
  fGroupType:='ZDY';
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;


end;

procedure TfrmGetGlide10.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    BtnFindClick(Sender);
end;

procedure TfrmGetGlide10.BtnCloseClick(Sender: TObject);
begin
   Close;
end;

procedure TfrmGetGlide10.BtnOkClick(Sender: TObject);
var
   i :integer;
begin
  if AD_sub.IsEmpty then
  begin
    application.MessageBox('请选择您要的工序!','提示信息',0);
    exit;
  end;
  ModalResult:=1;
end;

procedure TfrmGetGlide10.BtnFindClick(Sender: TObject);
begin
  DoFilter('');
end;

procedure TfrmGetGlide10.DBGrid1DblClick(Sender: TObject);
var
   i :integer;
begin
   if not ADOQuery1.Active then exit;
   if ADOQuery1.RecordCount = 0 then exit;
   with Ad_Sub,dg_Sub do
   begin
      DisableControls;
      SetFocus;
      append;
      for i := 0 to Columns.Count -1 do
      begin
         Columns[i].Field.ReadOnly := False;
         Fieldvalues[Columns[i].FieldName] := ADOQuery1.Fieldbyname(Columns[i].FieldName).Value;
      end;
      SelectedField.FocusControl;
      update();
      EnableControls;
   end;
    Ad_Sub.Post;
   dg_Sub.Refresh;
end;

procedure TfrmGetGlide10.DG_SubDblClick(Sender: TObject);
begin
   if  Ad_Sub.IsEmpty  then exit;
   try
      dg_Sub.SetFocus;
      Ad_Sub.Delete;
   except
      //
   end;
end;

procedure TfrmGetGlide10.IntiTree();
var
   MyRecPtr: PMyRec;
   tmpstr :string;
begin
  try
  	New(MyRecPtr);
  	MyRecPtr^.wbcode := 'R';
  	MyRecPtr^.Tvtem_str := '全部';
  	with TreeView1 do
  	begin
      Items.Clear;
      Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
  	end;
  	with TreeView1,AD_GY_Fllb do
  	begin
      tmpstr := 'select distinct deptOrderid, isnull(glideDept,''其它'') as glideDept,'+
                'isnull(glideDeptName,''其它'') as glideDeptName  from V_MC_glide';
      tmpstr := tmpstr +' order by deptOrderid,glidedept';
      active := false;
      CommandText := tmpstr;
      active := true;
      if recordcount = 0 then exit;
      first;
      while not Eof do
      begin
  			New(MyRecPtr);
  			MyRecPtr^.wbcode := trim(fieldbyname('glidedept').Asstring);
        MyRecPtr^.Tvtem_str := trim(fieldbyname('glideDeptName').Asstring);
        Items.AddChildObject(Items[0],MyRecPtr^.Tvtem_str, MyRecPtr);
        Next;
      end;
      close;
      Items[0].Selected := True;
      Items[0].Expand(true);
   end;
  except
    //
  end;

end;

procedure TfrmGetGlide10.IntiGroupTree();
var
   MyRecPtr: PMyRec;
   tmpstr :string;
begin
  try
  {
  	New(MyRecPtr);
  	MyRecPtr^.wbcode := 'R';
  	MyRecPtr^.Tvtem_str := '全部';
  }
  	with TreeView1 do
  	begin
      Items.Clear;
      //Items.AddObject(nil, MyRecPtr^.Tvtem_str, MyRecPtr);
  	end;
    
  	with TreeView1,AD_GY_Fllb do
  	begin
      tmpstr := 'select code ,Name from xc_custcode';
      tmpstr := tmpstr +' where flag =''GlideGROUP'' and valid = ''Y''';
      if trim(fSysCode)<>'' then
        tmpstr:=tmpstr+' and Note= '+quotedStr(trim(fSysCode));

      tmpstr := tmpstr +' order by orderNo';
      active := false;
      CommandText := tmpstr;
      active := true;
      if recordcount = 0 then exit;
      first;
      while not Eof do
      begin
  			New(MyRecPtr);
  			MyRecPtr^.wbcode := trim(fieldbyname('code').Asstring);
        MyRecPtr^.Tvtem_str := trim(fieldbyname('Name').Asstring);
        Items.AddChildObject(nil,MyRecPtr^.Tvtem_str, MyRecPtr);
        Next;
      end;
      close;
      //Items[0].Selected := True;
      //Items[0].Expand(true);
   end;
  except
    //
  end;

end;
procedure TfrmGetGlide10.TreeView1Click(Sender: TObject);
begin
   try
      this_code := trim(PMyRec(TreeView1.Selected.Data).wbcode);
      Dofilter(this_code);
   except
      //
   end;
end;

procedure TfrmGetGlide10.tv1DblClick(Sender: TObject);
var
   i :integer;
begin
   if  ADOQuery1.IsEmpty  then exit;
   with Ad_Sub,dg_Sub do
   begin
      DisableControls;
      SetFocus;
      append;
      for i := 0 to Columns.Count -1 do
      begin
         Columns[i].Field.ReadOnly := False;
         Fieldvalues[Columns[i].FieldName] := ADOQuery1.Fieldbyname(Columns[i].FieldName).Value;
      end;
      SelectedField.FocusControl;
      update();
      EnableControls;
   end;
    Ad_Sub.Post;
   dg_Sub.Refresh;
end;
////////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////////
procedure TfrmGetGlide10.DoQuery();
begin
  try
    ADOQuery1.DisableControls ;
    with ADOQuery1 do
    begin
      close;
      sql.clear;
      filtered:=false;
      if fGroupType<>'ZDY' THEN
      BEGIN
        sql.Add('select *,isnull(glideDept,''其它'') as dept from V_MC_glide');
        sql.Add('order By orderid,glideNo');
      END
      ELSE
      BEGIN
        sql.Add('select A.*,B.glideGroup,B.glidegroupName,C.Note as sysCode');
        sql.Add('from MC_glide A');
        sql.Add('left join mc_group_glide B on A.glideNO=B.glideNo');
        sql.Add('INNER join xc_custCode C on C.code=B.glideGroup and flag=''GLIDEGROUP''');
        if trim(fSysCode)<>'' then
          sql.Add('and C.Note= '+quotedStr(trim(fSysCode)));
        sql.Add('order By B.orderid');
      END;
      Open;
    end;
  finally
    ADOQuery1.EnableControls ;
  end;
end;
/////////////////////////////////////////
procedure TfrmGetGlide10.DoFilter(mDept:string);
var
  filterStr:string;
begin
  if fGroupType='ZDY' then
  begin
    filterStr:=' and glideGroup= '+quotedStr(trim(mDept));
  end
  else
  begin
    if mDept<>'R' then
      filterStr:=' and Dept= '+quotedStr(trim(mDept));
  end;

  if trim(edit1.Text)<>'' then
    filterStr:=filterStr+' and glideName like '+quotedStr('%'+trim(edit1.Text)+'%');

  //if (fGroupType='ZDY') and (trim(fSysCode)<>'') then
  //  filterStr:=filterStr+' and sysCode= '+quotedStr(trim(fSysCode));

  try
    ADOQuery1.DisableControls ;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
    with ADOQuery1 do
    begin
      filtered:=false;
      if trim(filterStr)<>'' then
      begin
        filter:=filterStr;
        filtered:=true;
      end;
    end;
  finally
    ADOQuery1.EnableControls ;
  end;
end;

procedure TfrmGetGlide10.FormShow(Sender: TObject);
begin
  IntiGroupTree();
  if TreeView1.Items.Count <=0 then
  begin
    fGroupType:='DEPT';
    IntiTree();
  end;
  DoQuery();
  with AD_Sub do
  begin
    Disablecontrols;
    Active:=False;
    Active:=True;
    Enablecontrols;
  end;
end;

end.
