unit U_SYDeptView; 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 TfrmSYDeptView = 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; Label5: TLabel; DPOrder: TEdit; ToolButton3: TToolButton; 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); procedure Tv4DblClick(Sender: TObject); procedure ToolButton3Click(Sender: TObject); private { Private declarations } PState:Integer; FCPID,FTopID:String; procedure InitTree(); public { Public declarations } end; var frmSYDeptView: TfrmSYDeptView; implementation uses U_DataLink,U_RTFun; {$R *.dfm} procedure TfrmSYDeptView.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; SCreateCDS(ADOQueryTree,CDS_Tree); SInitCDSData(ADOQueryTree,CDS_Tree); //cxDBTreeList1.Items[0].Expand(false); cxDBTreeList1.Items[0].Expand(True); finally ADOQueryTree.EnableControls; end; //cxDBTreeList1.Items[1].Expand(False); end; procedure TfrmSYDeptView.FormDestroy(Sender: TObject); begin frmSYDeptView:=nil; end; procedure TfrmSYDeptView.FormClose(Sender: TObject; var Action: TCloseAction); begin Action:=caFree; end; procedure TfrmSYDeptView.TBRafreshClick(Sender: TObject); begin InitTree(); end; procedure TfrmSYDeptView.TBCloseClick(Sender: TObject); begin Close; end; procedure TfrmSYDeptView.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 TfrmSYDeptView.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); DPOrder.Text:=Trim(CDS_Tree.fieldbyname('DPOrder').AsString); end; procedure TfrmSYDeptView.ToolButton1Click(Sender: TObject); var maxId:String; FInt:Integer; begin if Trim(DPName.Text)='' then begin Application.MessageBox('本级名称不能为空!','提示',0); Exit; end; {if Trim(DPNo.Text)='' then begin Application.MessageBox('类别编码不能为空!','提示',0); Exit; end;} if Trim(DPOrder.Text)='' then begin DPOrder.Text:='99'; end; if TryStrToInt(Trim(DPOrder.Text),FInt)=False 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; FieldByName('DPOrder').Value:=StrToInt(Trim(DPOrder.Text)); 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('DPOrder').Value:=StrToInt(Trim(DPOrder.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 TfrmSYDeptView.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 TfrmSYDeptView.DPNameKeyPress(Sender: TObject; var Key: Char); begin if Key=#13 then DPNo.SetFocus; end; procedure TfrmSYDeptView.FormShow(Sender: TObject); begin InitTree(); end; procedure TfrmSYDeptView.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 TfrmSYDeptView.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; SCreateCDS(ADOQueryTemp,CDS_User); SInitCDSData(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; SCreateCDS(ADOQueryTemp,CDS_User); SInitCDSData(ADOQueryTemp,CDS_User); end; ModalResult:=1; end; procedure TfrmSYDeptView.Tv4DblClick(Sender: TObject); begin ModalResult:=1; end; procedure TfrmSYDeptView.ToolButton3Click(Sender: TObject); begin ModalResult:=1; end; end.