unit U_SYDept;

interface

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

type
  TfrmSYDept = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ToolButton1: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    DPTopName: TEdit;
    DPName: TEdit;
    DPNo: TEdit;
    CDS_Tree: TClientDataSet;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label4: TLabel;
    ToolButton2: TToolButton;
    CDS_User: TClientDataSet;
    DSUser: TDataSource;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure DPNameKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();

  public
    { Public declarations }
  end;

var
  frmSYDept: TfrmSYDept;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmSYDept.InitTree();
var
  i:Integer;
begin
  try
    ADOQueryTree.DisableControls;
    with ADOQueryTree do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from SY_Dept order by DPlevel,DPOrder,DPName');
      Open;
    end;
    SCreateCDS20(ADOQueryTree,CDS_Tree);
    SInitCDSData20(ADOQueryTree,CDS_Tree);
    cxDBTreeList1.Items[0].Expand(false);
  finally
    ADOQueryTree.EnableControls;
  end;
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmSYDept.FormDestroy(Sender: TObject);
begin
  frmSYDept:=nil;
end;

procedure TfrmSYDept.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmSYDept.TBRafreshClick(Sender: TObject);
begin
  InitTree();
end;

procedure TfrmSYDept.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmSYDept.TBAddClick(Sender: TObject);
begin
  PState:=11;
  DPTopName.Text:=Trim(CDS_Tree.fieldbyname('DPName').AsString);
  //FTopID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  FCPID:='';
  DPName.Text:='';
  DPNo.Text:='';
  DPName.SetFocus;
end;

procedure TfrmSYDept.cxDBTreeList1Click(Sender: TObject);
begin
  PState:=22;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_Dept where DPID='''+Trim(CDS_Tree.fieldbyname('DPParent').AsString)+'''');
    Open;
  end;
  FCPID:=Trim(CDS_Tree.fieldbyname('DPID').AsString);
  DPTopName.Text:=Trim(ADOQueryTemp.fieldbyname('DPName').AsString);
  DPName.Text:=Trim(CDS_Tree.fieldbyname('DPName').AsString);
  DPNo.Text:=Trim(CDS_Tree.fieldbyname('DPNo').AsString);
end;

procedure TfrmSYDept.ToolButton1Click(Sender: TObject);
var
  maxId:String;
begin
  if Trim(DPName.Text)='' then
  begin
    Application.MessageBox('本级名称不能为空！','提示',0);
    Exit;
  end;
  if Trim(DPNo.Text)='' then
  begin
    Application.MessageBox('类别编码不能为空！','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if PState=11 then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'DP','SY_Dept',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('区最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxId:=Trim(FCPID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from SY_Dept where DPID='''+Trim(FCPID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if PState=11 then
      begin
        Append;
        FieldByName('DPID').Value:=Trim(maxId);
        FieldByName('DPName').Value:=Trim(DPName.Text);
        FieldByName('DPNo').Value:=Trim(DPNo.Text);
        FieldByName('DPParent').Value:=Trim(CDS_Tree.fieldbyname('DPID').AsString);
        FieldByName('DPLevel').Value:=CDS_Tree.fieldbyname('DPLevel').AsInteger+1;
        Post;
      end else
      if PState=22 then
      begin
        Edit;
        FieldByName('DPID').Value:=Trim(maxId);
        FieldByName('DPName').Value:=Trim(DPName.Text);
        FieldByName('DPNo').Value:=Trim(DPNo.Text);
        //FieldByName('CPParent').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
        //FieldByName('CPOrder').Value:=ADOQueryTree.fieldbyname('CPOrder').AsInteger+1;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmSYDept.TBDelClick(Sender: TObject);
begin
  if ADOQueryTree.FieldByName('DPLevel').AsInteger=0 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_Dept where DPParent='''+Trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经定义下级组织不能删除！','提示',0);
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_User where DPID='''+Trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('该组织已有人员不能删除！','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete SY_Dept where DPID='''+Trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
    sql.Add('delete SY_Dept where DPParent='''+Trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
    ExecSQL;
  end;
  InitTree();
end;

procedure TfrmSYDept.DPNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
    DPNo.SetFocus;
end;

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

procedure TfrmSYDept.ToolButton2Click(Sender: TObject);
begin
  if CDS_Tree.IsEmpty=False then
  begin
    if CDS_User.IsEmpty then Exit;
    if Application.MessageBox('确定要进行组织分配吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update SY_User Set DPID='''+Trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
      sql.Add(' where UserId='''+Trim(CDS_User.fieldbyname('UserId').AsString)+'''');
      ExecSQL;
    end;
    with CDS_User do
    begin
      Edit;
      FieldByName('Dept').Value:=Trim(CDS_Tree.fieldbyname('DPName').AsString);
      Post;
    end;
  end;
end;

procedure TfrmSYDept.cxDBTreeList1DblClick(Sender: TObject);
begin
  if CDS_Tree.FieldByName('DPLevel').Value=0 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select A.*,Dept=(select DPName from SY_Dept where DPID=A.DPID) ');
      SQL.Add(' from SY_User A where UserId not in(''Admin'',''Test'') ');
      SQL.Add(' and Valid=''Y'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_User);
    SInitCDSData20(ADOQueryTemp,CDS_User);
  end else
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Select_Dept_User :CYType');
      Parameters.ParamByName('CYType').Value:=Trim(CDS_Tree.fieldbyname('DPID').AsString);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_User);
    SInitCDSData20(ADOQueryTemp,CDS_User);
  end;
end;

end.
