unit U_SYDeptView_PF;

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,comobj,
  cxLookAndFeels, cxLookAndFeelPainters, cxTLdxBarBuiltInMenu, cxNavigator;

type
  TfrmSYDeptView_PF = 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;
    CDS_Tree: TClientDataSet;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    TVPFYDian: TcxGridDBColumn;
    TVPFMNum: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_User: TClientDataSet;
    DSUser: TDataSource;
    ToolButton3: TToolButton;
    v4PFType: TcxGridDBColumn;
    TDR: TToolButton;
    OpenDialog1: TOpenDialog;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TDRClick(Sender: TObject);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();

  public
    { Public declarations }
  end;

var
  frmSYDeptView_PF: TfrmSYDeptView_PF;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmSYDeptView_PF.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);
    cxDBTreeList1.Items[0].Expand(True);
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from PF_User ');
      sql.Add('where DPID='''+trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_User);
    SInitCDSData20(ADOQueryTemp,CDS_User);
  finally
    ADOQueryTree.EnableControls;
  end;
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmSYDeptView_PF.FormDestroy(Sender: TObject);
begin
  frmSYDeptView_pf:=nil;
end;

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

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

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

procedure TfrmSYDeptView_PF.TBAddClick(Sender: TObject);
begin
  with CDS_User do
  begin
    Append;
    FieldByName('DPID').AsString:=Trim(CDS_Tree.fieldbyname('DPID').AsString);
    post;
  end;
end;

procedure TfrmSYDeptView_PF.ToolButton1Click(Sender: TObject);
var
  maxId:String;
begin
  ToolBar1.SetFocus;
  if CDS_User.IsEmpty then Exit;
  with CDS_User do
  begin
    First;
    while not Eof do
    begin
      if CDS_User.FieldByName('PUID').AsString='' then
      begin
        if GetLSNo(ADOQueryCmd,maxId,'PU','PF_User',3,1)=False then
        begin
          Application.MessageBox('区最大号失败！','提示',0);
          Exit;
        end;
      end
      else
      begin
        maxId:=Trim(CDS_User.FieldByName('PUID').AsString);
      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        SQL.Add('select * from PF_User where PUID='''+trim(Maxid)+'''');
        open;
        if ADOQueryCmd.IsEmpty then
        begin
          Append;
        end
        else
          edit;
        FieldByName('PUID').Value:=trim(Maxid);
        FieldByName('PFYDian').Value:=Trim(CDS_User.FieldByName('PFYDian').AsString);
        FieldByName('PFMNum').Value:=CDS_User.FieldByName('PFMNum').AsFloat;
        FieldByName('DPID').Value:=Trim(CDS_User.fieldbyname('DPID').AsString);
        FieldByName('PFType').Value:=Trim(CDS_User.fieldbyname('PFType').AsString);
        post;
      end;
      Next;
    end;
  end;
  Application.MessageBox('保存成功!','提示',0);
end;

procedure TfrmSYDeptView_PF.TBDelClick(Sender: TObject);
begin
  if CDS_User.IsEmpty then exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete from PF_User where PUID='''+Trim(CDS_User.fieldbyname('PUID').AsString)+'''');
    ExecSQL;
  end;
  CDS_User.Delete;
end;

procedure TfrmSYDeptView_PF.FormShow(Sender: TObject);
begin
  if PState<>1 then
  begin
    ToolButton3.Visible:=False;
    TBAdd.Visible:=True;
    TBDel.Visible:=True;
    ToolButton1.Visible:=True;
  end
  else
  begin
    ToolButton3.Visible:=True;
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    ToolButton1.Visible:=False;
  end;
  InitTree();
end;

procedure TfrmSYDeptView_PF.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmSYDeptView_PF.cxDBTreeList1Click(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from PF_User ');
    sql.Add('where DPID='''+trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_User);
  SInitCDSData20(ADOQueryTemp,CDS_User);
end;

procedure TfrmSYDeptView_PF.cxDBTreeList1DblClick(Sender: TObject);
begin
  if PState=1 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from PF_User ');
      sql.Add('where DPID='''+trim(CDS_Tree.fieldbyname('DPID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_User);
    SInitCDSData20(ADOQueryTemp,CDS_User);
    ModalResult:=1;
  end;
end;

procedure TfrmSYDeptView_PF.TDRClick(Sender: TObject);
begin
    {try
        OpenDialog1.Filter:='xls(*.xls)|*.xls|全部(*.*)|*.*';
        OpenDialog1.Options:=[ofOverwritePrompt];
        OpenDialog1.FileName:='组织评分列表';
        if OpenDialog1.Execute then
            if Assigned(cxGrid4) then
            begin
                try
                    ExportGrid4ToExcel(OpenDialog1.FileName,cxGrid4);
                except
                    application.MessageBox('创建失败,源文件可能处于编辑状态!','提示信息',0);
                    exit;
                end;
                application.MessageBox('成功导入!','提示信息',0);
            end
            else
               application.MessageBox('导入失败!','提示信息',0);
    finally
        OpenDialog1.Free;
    end;}
end;

end.
