unit U_UserHelpSel;

interface

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

type
  TfrmUserHelpSel = class(TForm)
    ToolBar2: TToolBar;
    TOk: TToolButton;
    TBClose: TToolButton;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1Code: TcxGridDBColumn;
    tv1Name: TcxGridDBColumn;
    DBGrid1Level1: TcxGridLevel;
    GroupBox1: TGroupBox;
    ADOQueryDept: TADOQuery;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    tv1Column1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    ADOQueryHelp: TADOQuery;
    DataSource2: TDataSource;
    Label1: TLabel;
    Edt_name: TEdit;
    ImageList1: TImageList;
    cxStyleTree: TcxStyle;
    Label2: TLabel;
    Edt_Code: TEdit;
    Splitter1: TSplitter;
    ThreeImgList: TImageList;
    ClientDataSet1: TClientDataSet;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure Edt_nameChange(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
  private
    procedure InitTree();
    procedure DoQuery();
    procedure FilterData(strdepotno,strcode,strname:string);
  public
    fFrameNo:string;
    fFrameName:string;
    fIsYwy:boolean;
    ReturnStr:string;
  end;

var
  frmUserHelpSel: TfrmUserHelpSel;

implementation
uses
  U_DataLink,U_Fun;
{$R *.dfm}
///////////////////////////////////////////////////////////
//////功能：过滤数据
///////////////////////////////////////////////////////////
procedure TfrmUserHelpSel.FilterData(strdepotno,strcode,strname:string);
var
  filterStr:string;
begin
  filterStr:='';
  if trim(strdepotno)<>'' then
  filterStr:=filterStr+' and dept like '+quotedstr('%'+trim(strdepotno)+'%');
  if trim(strcode)<>'' then
  filterStr:=filterStr+' and userID like '+quotedstr('%'+trim(strcode)+'%');
  if trim(strname)<>'' then
  filterStr:=filterStr+' and userName like '+quotedstr('%'+trim(strname)+'%');

  try
    ADOQueryHelp.DisableControls ;
    if trim(filterStr)='' then
    begin
      ADOQueryHelp.Filtered:=false;
      ADOQueryHelp.EnableControls;
      exit;
   end;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-5));
   // showmessage(filterStr);
    with ADOQueryHelp do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
   finally
    ADOQueryHelp.EnableControls;
    SCreateCDS20(ADOQueryHelp,ClientDataSet1);
    SInitCDSData20(ADOQueryHelp,ClientDataSet1);
  end;
end;
//////////////////////////////////////////////////////
procedure TfrmUserHelpSel.InitTree();
var
  strsql,strwhere:string;
begin
  strsql:=' SELECT distinct UDept=''ALL'',UDeptTop='''',UDeptName=''所有部门'',Framelevel=0   '+
          ' union                                                   '+
          ' SELECT distinct UDept,UDeptTop=''ALL'',UDept,Framelevel=1 '+
          ' FROM BD_staff A                                '+
          ' WHERE USERID<>''ADMIN'' and ISNULL(UDept,'''')<>'''' ';

  strwhere:=' where A.Valid=''Y''  ';
{  if fIsYwy then
    strwhere:=strwhere+' and A.YWYFlag=''Y''       ';
  if trim(fFrameNo)<>'' then
  strwhere:=strwhere+' and A.dept='+quotedstr(trim(fFrameNo));
  if trim(fFrameName)<>'' then
  strwhere:=strwhere+' and B.frameName like '+quotedstr('%'+trim(fFrameName)+'%');
  strsql:=strsql+ strwhere;   }

  with ADOQueryDept do
  begin
    close;
    sql.Clear ;
    sql.Add( strsql);
    Open;
  end;
  cxDBTreeList1.FullExpand;

  DoQuery();
end;

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

procedure TfrmUserHelpSel.FormCreate(Sender: TObject);
begin
  cxGrid1.Align :=alclient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=DConString;
    Connected:=true;
  end;
end;
//////////////////////////////////////////////////
  //
//////////////////////////////////////////////////
procedure TfrmUserHelpSel.DoQuery();
var
  strsql,strwhere:string;
begin
  strsql:=' select *   '+
          ' from BD_staff A                         '+
          ' where isnull(UserID,'''')<>''ADMIN''   ';
  strwhere:=' and A.Valid=''Y'' ';
  if fIsYwy then
  strwhere:=strwhere+' and A.Isywy=1        ';

  strsql:=strsql+ strwhere +' order by A.userID ';

  with ADOQueryHelp do
  begin
    close;
    sql.Clear ;
    sql.Add( strsql);
    Open;
  end;
    SCreateCDS20(ADOQueryHelp,ClientDataSet1);
    SInitCDSData20(ADOQueryHelp,ClientDataSet1);
end;

procedure TfrmUserHelpSel.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmUserHelpSel.TOkClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then exit;

  ReturnStr:='';
  with ClientDataSet1 do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean=True then
      begin
        ReturnStr:=ReturnStr+Trim(fieldbyname('UserName').AsString)+';'
      end;
      Next;
    end;
  end;
 // if JiangeStr<>99 then
 IF  ReturnStr<>'' then
    ReturnStr:=Copy(ReturnStr,1,Length(ReturnStr)-1);
  ModalResult:=1;
end;

procedure TfrmUserHelpSel.tv1DblClick(Sender: TObject);
begin
  Tok.Click ;
end;

procedure TfrmUserHelpSel.Edt_nameChange(Sender: TObject);
begin
  FilterData('',edt_code.Text,Edt_name.Text);
end;

procedure TfrmUserHelpSel.cxDBTreeList1Click(Sender: TObject);
begin
  if ADOQueryDept.IsEmpty then exit;
  if trim(ADOQueryDept.FieldByName('Framelevel').AsString)='0' then
    FilterData('','','')
  else
    FilterData(ADOQueryDept.FieldByName('UDept').AsString,'','');
end;

end.
