unit U_purview;

interface

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

type
  TfrmpurviewDL = class(TForm)
    tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ToolBar1: TToolBar;
    tbqx: TToolButton;
    tbclose: TToolButton;
    tv1sel: TcxGridDBColumn;
    tv1userID: TcxGridDBColumn;
    tv1userName: TcxGridDBColumn;
    ADO_user: TADOQuery;
    DataSource1: TDataSource;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxFormName: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADO_purview: TADOQuery;
    DataSource2: TDataSource;
    CDS_User: TClientDataSet;
    CDS_purview: TClientDataSet;
    ADO_Cmd: TADOQuery;
    tbyhadd: TToolButton;
    tbyhup: TToolButton;
    tbyhdelete: TToolButton;
    ThreeImgList: TImageList;
    tv1Udept: TcxGridDBColumn;
    tv1UType: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    ADOQuery1: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    FormName: TEdit;
    Label2: TLabel;
    ModuleName: TEdit;
    ClientDataSet0: TClientDataSet;
    DataSource0: TDataSource;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Panel2: TPanel;
    cxGrid0: TcxGrid;
    Tv0: TcxGridDBTableView;
    V0FormName: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ClientDataSet5: TClientDataSet;
    DataSource5: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure tbcloseClick(Sender: TObject);
    procedure tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure tbqxClick(Sender: TObject);
    procedure tbyhaddClick(Sender: TObject);
    procedure tbyhupClick(Sender: TObject);
    procedure tbyhdeleteClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormNameChange(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv0CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    Procedure InitUser();
    procedure InitPurview();
    function  savePurview():Boolean;
    procedure CSHTv0();
    procedure CSHTv5();
    procedure TvoGD();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmpurviewDL: TfrmpurviewDL;

implementation
uses  U_User,U_DataLink,U_RTFun; //U_Main,U_PublicFunction,
{$R *.dfm}
function  TfrmpurviewDL.SavePurview():Boolean;
begin
  result:=false;
  ADO_Cmd.Connection.BeginTrans;
  try
    with ADO_Cmd do
    begin
      close;
      sql.Clear;
      sql.Add(' delete sy_purview ');
      sql.Add(' where userID='+quotedstr(trim(CDS_User.fieldbyname('userID').AsString)));
      execsql;
    end;
    with ADO_Cmd do
    begin
      close;
      sql.Clear;
      sql.Add(' select * from sy_purview ');
      sql.Add(' where userID='+quotedstr(trim(CDS_User.fieldbyname('userID').AsString)));
      open;
    end;
    with CDS_purview do
    begin
      first;
      DisableControls;
      while not eof do
      begin
        if fieldbyname('sel').AsBoolean then
        begin
          ADO_Cmd.Append;
          ADO_Cmd.FieldByName('userID').Value:=trim(CDS_User.fieldbyname('userID').AsString);
          ADO_Cmd.FieldByName('moduleID').Value:=trim(CDS_purview.fieldbyname('moduleID').AsString);
          ADO_Cmd.FieldByName('modulesubID').Value:=trim(CDS_purview.fieldbyname('modulesubID').AsString);
          ADO_Cmd.Post;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
    ADO_Cmd.Connection.CommitTrans;
    result:=true;
  except
    ADO_Cmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmpurviewDL.InitPurview();
begin
  if CDS_User.IsEmpty then exit;
  with ADOQuery1 do
  begin
    close;
    Parameters.ParamByName('pUserID').Value:=trim(CDS_User.fieldbyname('userID').AsString);
    open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet3);
  SInitCDSData20(ADOQuery1,ClientDataSet3);
  ClientDataSet3.First;
  with ADO_purview do
  begin
    close;
    sql.Clear;
    sql.Add('select A.ModuleID+''->''+A.moduleName as moduleName,B.*');
    sql.Add(' from Sy_moduleMain A');
    sql.Add(' inner join Sy_modulesub B on B.moduleID=A.moduleID');
    sql.Add('ORDER BY A.idx,B.idx');
    open;
  end;
  SCreateCDS20(ADO_purview,CDS_purview);
  SInitCDSData20(ADO_purview,CDS_purview);
  with ADO_Cmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select  Top 1 FormName from SY_ModuleSub B where not exists(');
    sql.Add('select * from SY_PurView A');
    sql.Add(' where A.ModuleId=B.ModuleId and A.ModuleSubId=B.ModuleSubId  ');
    sql.Add(' and A.UserId='''+Trim(CDS_User.fieldbyname('userID').AsString)+''')');
    SQL.Add(' order by ModuleId,ModuleSubId ');
    Open;
  end;
  CDS_purview.Locate('FormName',Trim(ADO_Cmd.fieldbyname('FormName').AsString),[]);

end;
Procedure TfrmpurviewDL.InitUser();
begin
  with ADO_user do
  begin
    close;
    sql.Clear;
    sql.Add(' select cast(0 as bit) as sel, * from SY_User WHERE Valid=''Y'' ');
    open;
  end;
  SCreateCDS20(ADO_user,CDS_User);
  SInitCDSData20(ADO_user,CDS_User);
  //CreateAdoToCds(ADO_user,CDS_User);
  //InitAdoToCds(ADO_user,CDS_User);
end;
procedure TfrmpurviewDL.FormCreate(Sender: TObject);
begin
  InitUser();

end;

procedure TfrmpurviewDL.tbcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmpurviewDL.tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  InitPurview();
end;

procedure TfrmpurviewDL.tbqxClick(Sender: TObject);
begin
  if CDS_User.IsEmpty then exit;
  if CDS_purview.IsEmpty then exit;
  if SavePurview() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
  end
  else
    application.MessageBox('数据保存失败！','提示信息',MB_ICONERROR);
end;

procedure TfrmpurviewDL.tbyhaddClick(Sender: TObject);
begin
  frmUser:=TfrmUser.create(self);
  with frmUser do
  begin
    formId:=0;
    fuserID:='';
    if showmodal=1 then
    begin
      InitUser();
      CDS_User.Locate('UserID',trim(fuserID),[]);
      InitPurview();
    end;
    free;
  end;
end;

procedure TfrmpurviewDL.tbyhupClick(Sender: TObject);
begin
  if CDS_User.IsEmpty then exit;
  if trim(CDS_User.FieldByName('userID').AsString)='ADMIN' then exit;
  frmUser:=TfrmUser.create(self);
  with frmUser do
  begin
    formId:=1;
    fuserID:=trim(CDS_User.fieldByName('UserID').asstring);
    //newPassword.PasswordChar:=#0;
    //DnewPassword.PasswordChar:=#0;
    {if trim(frmMain.username)='ADMIN' THEN
    begin
      newPassword.PasswordChar:=#0;
      DnewPassword.PasswordChar:=#0;
    end;  }
    if showmodal=1 then
    begin
      InitUser();
      CDS_User.Locate('UserID',trim(fuserID),[]);
      InitPurview();
    end;
    free;
  end;
end;

procedure TfrmpurviewDL.tbyhdeleteClick(Sender: TObject);
begin
  if CDS_User.IsEmpty then exit;
  if trim(CDS_User.FieldByName('userID').AsString)='ADMIN' then exit;
  if application.MessageBox('请确定要删除此用户吗？','警告信息!',1)=2 then exit;
  try
    with ADO_Cmd do
    begin
      close;
      sql.Clear;
      sql.Add(' delete SY_User ');
      sql.Add(' where userID='+quotedstr(trim(CDS_User.fieldbyname('USerID').AsString)));
      sql.Add(' delete sy_purview ');
      sql.Add(' where userID='+quotedstr(trim(CDS_User.fieldbyname('userID').AsString)));
      execsql;
    end;
    InitUser();
    InitPurview();
  except
    application.MessageBox('删除失败！','错误信息',MB_ICONERROR);
  end;
end;

procedure TfrmpurviewDL.FormDestroy(Sender: TObject);
begin
  frmpurviewDL:=nil;
end;

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

procedure TfrmpurviewDL.FormNameChange(Sender: TObject);
begin
  if ADO_purview.Active=False then Exit;
  SDofilter(ADO_purview,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADO_purview,CDS_purview);
  SInitCDSData20(ADO_purview,CDS_purview);
end;

procedure TfrmpurviewDL.Tv2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if CDS_purview.IsEmpty then
  begin
    V0FormName.Caption:='';
    V0FormName.Styles.Header.Color:=clDefault;
    CSHTv0();
    Exit;
  end;
  with ADO_Cmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.ModuleID+''->''+A.moduleName as moduleName,B.*');
    sql.Add('from Sy_moduleMain A');
    sql.Add('inner join Sy_modulesub B on B.moduleID=A.moduleID');
    sql.Add('where not exists( select * from Sy_purview C where C.moduleID=B.moduleID and') ;
    sql.Add(' C.moduleSubID=B.modulesubID and C.userID='''+Trim(CDS_User.fieldbyname('userID').AsString)+''')');
    sql.Add(' and A.moduleID='''+Trim(CDS_purview.fieldbyname('moduleID').AsString)+'''');
    sql.Add(' ORDER BY B.idx ');
    Open;
  end;
  SCreateCDS20(ADO_Cmd,ClientDataSet0);
  SInitCDSData20(ADO_Cmd,ClientDataSet0);
  V0FormName.Caption:='增加权限';
  V0FormName.Styles.Header.Color:=clYellow;
  ClientDataSet3.Locate('moduleID;modulesubID',
                    VarArrayOf([trim(CDS_purview.fieldbyname('moduleID').AsString), trim(CDS_purview.fieldbyname('modulesubID').AsString)])
                    ,[loPartialKey]);
 // TvoGD();
end;
procedure TfrmpurviewDL.TvoGD();
begin
  if ClientDataSet0.IsEmpty then
  begin
    cxGrid0.Height:=23*3;
  end else
  if ClientDataSet0.RecordCount<3 then
  begin
    cxGrid0.Height:=23*3;
  end else
  begin
    cxGrid0.Height:=(ClientDataSet0.RecordCount+2)*23;
  end;

end;

procedure TfrmpurviewDL.Tv3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ClientDataSet3.IsEmpty then
  begin
    V0FormName.Caption:='';
    V0FormName.Styles.Header.Color:=clDefault;
    CSHTv0();
    Exit;
  end;
  with ADO_Cmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.ModuleID+''->''+A.moduleName as moduleName,B.*');
    sql.Add('from Sy_moduleMain A');
    sql.Add('inner join Sy_modulesub B on B.moduleID=A.moduleID');
    sql.Add('where  exists( select * from Sy_purview C where C.moduleID=B.moduleID and') ;
    sql.Add(' C.moduleSubID=B.modulesubID and C.userID='''+Trim(CDS_User.fieldbyname('userID').AsString)+''')');
    sql.Add(' and A.moduleID='''+Trim(ClientDataSet3.fieldbyname('moduleID').AsString)+'''');
    sql.Add(' ORDER BY B.idx ');
    Open;
  end;
  SCreateCDS20(ADO_Cmd,ClientDataSet0);
  SInitCDSData20(ADO_Cmd,ClientDataSet0);
  V0FormName.Caption:='取消权限';
  V0FormName.Styles.Header.Color:=clFuchsia;
  CDS_purview.Locate('moduleID;modulesubID',
                    VarArrayOf([trim(ClientDataSet3.fieldbyname('moduleID').AsString), trim(ClientDataSet3.fieldbyname('modulesubID').AsString)])
                    ,[loPartialKey]);
  //TvoGD();
end;

procedure TfrmpurviewDL.tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  CSHTv0();
end;
procedure TfrmpurviewDL.CSHTv0();
begin
  with ADO_Cmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.ModuleID+''->''+A.moduleName as moduleName,B.*');
    sql.Add('from Sy_moduleMain A');
    sql.Add('inner join Sy_modulesub B on B.moduleID=A.moduleID');
    sql.Add('where 1=2') ;
    Open;
  end;
  SCreateCDS20(ADO_Cmd,ClientDataSet0);
  SInitCDSData20(ADO_Cmd,ClientDataSet0);
  SCreateCDS20(ADO_Cmd,ClientDataSet5);
  SInitCDSData20(ADO_Cmd,ClientDataSet5);
  V0FormName.Caption:='';
  V0FormName.Styles.Header.Color:=clDefault;
end;
procedure TfrmpurviewDL.CSHTv5();
begin
  with ADO_Cmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.ModuleID+''->''+A.moduleName as moduleName,B.*');
    sql.Add('from Sy_moduleMain A');
    sql.Add('inner join Sy_modulesub B on B.moduleID=A.moduleID');
    sql.Add('where A.moduleID='''+Trim(ClientDataSet0.fieldbyname('moduleID').asstring)+'''') ;
    Open;
  end;
  SCreateCDS20(ADO_Cmd,ClientDataSet5);
  SInitCDSData20(ADO_Cmd,ClientDataSet5);
end;
procedure TfrmpurviewDL.Tv0CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet0.IsEmpty then Exit;
  if V0FormName.Caption='取消权限' then
  begin
    with ADO_Cmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SY_Purview where UserId='''+Trim(CDS_User.fieldbyname('UserId').AsString)+'''');
      sql.Add(' and moduleID='''+Trim(ClientDataSet0.fieldbyname('moduleID').AsString)+'''');
      sql.Add(' and modulesubID='''+Trim(ClientDataSet0.fieldbyname('modulesubID').AsString)+'''');
      ExecSQL;
    end;
    with CDS_purview do
    begin
      Append;
      FieldByName('moduleID').Value:=trim(ClientDataSet0.fieldbyname('moduleID').AsString);
      FieldByName('modulesubID').Value:=trim(ClientDataSet0.fieldbyname('modulesubID').AsString);
      FieldByName('FormName').Value:=trim(ClientDataSet0.fieldbyname('FormName').AsString);
      FieldByName('ModuleName').Value:=trim(ClientDataSet0.fieldbyname('ModuleName').AsString);
      FieldByName('FormFile').Value:=ClientDataSet0.fieldbyname('FormFile').Value;
      FieldByName('FormId').Value:=ClientDataSet0.fieldbyname('FormId').Value;
      Post;
    end;
     ClientDataSet3.Locate('moduleID;modulesubID',
                    VarArrayOf([trim(ClientDataSet0.fieldbyname('moduleID').AsString), trim(ClientDataSet0.fieldbyname('modulesubID').AsString)])
                    ,[loPartialKey]);
     ClientDataSet3.Delete;
    ClientDataSet0.Delete;
  end else
  if V0FormName.Caption='增加权限' then
  begin
   { if ClientDataSet3.Locate('FormFile;FormId',
                      VarArrayOf([ClientDataSet0.fieldbyname('FormFile').Value, ClientDataSet0.fieldbyname('FormId').Value])
                      ,[loPartialKey])=True then
    begin
      Application.MessageBox('已存在相似权限的模块，不能再次添加!','提示',0);
      Exit;
    end; }
    if ClientDataSet3.Locate('moduleID;modulesubID',
                      VarArrayOf([trim(ClientDataSet0.fieldbyname('moduleID').AsString), trim(ClientDataSet0.fieldbyname('modulesubID').AsString)])
                      ,[loPartialKey])=False then
    begin
      with ADO_Cmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' insert into SY_Purview');
        sql.Add(' select :userID,:moduleID,:modulesubID');
        Parameters.ParamByName('userID').Value:=trim(CDS_User.fieldbyname('userID').AsString);
        Parameters.ParamByName('moduleID').Value:=trim(ClientDataSet0.fieldbyname('moduleID').AsString);
        Parameters.ParamByName('modulesubID').Value:=trim(ClientDataSet0.fieldbyname('modulesubID').AsString);
        ExecSQL;
      end;
      with ClientDataSet3 do
      begin
        Append;
        FieldByName('moduleID').Value:=trim(ClientDataSet0.fieldbyname('moduleID').AsString);
        FieldByName('modulesubID').Value:=trim(ClientDataSet0.fieldbyname('modulesubID').AsString);
        FieldByName('FormName').Value:=trim(ClientDataSet0.fieldbyname('FormName').AsString);
        FieldByName('ModuleName').Value:=trim(ClientDataSet0.fieldbyname('ModuleName').AsString);
        FieldByName('FormFile').Value:=ClientDataSet0.fieldbyname('FormFile').Value;
        FieldByName('FormId').Value:=ClientDataSet0.fieldbyname('FormId').Value;
        Post;
      end;
      CDS_purview.Locate('moduleID;modulesubID',
                    VarArrayOf([trim(ClientDataSet0.fieldbyname('moduleID').AsString), trim(ClientDataSet0.fieldbyname('modulesubID').AsString)])
                    ,[loPartialKey]);
      CDS_purview.Delete;
      ClientDataSet0.Delete;
    end;
  end;

end;

end.
