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, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, 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;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v2sel: TcxGridDBColumn;
    ADO_Cmd: TADOQuery;
    tbyhadd: TToolButton;
    tbyhup: TToolButton;
    tbyhdelete: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ThreeImgList: TImageList;
    tv1Udept: TcxGridDBColumn;
    tv1UType: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    Panel2: TPanel;
    Label3: TLabel;
    UserName: TEdit;
    Label1: TLabel;
    Udept: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure tbcloseClick(Sender: TObject);
    procedure tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    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 UserNameChange(Sender: TObject);
  private
    Procedure InitUser();
    procedure InitPurview();
    function  savePurview():Boolean;
    { 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 ADO_purview do
  begin
    close;
    Parameters.ParamByName('pUserID').Value:=trim(CDS_User.fieldbyname('userID').AsString);
    open;
  end;
  SCreateCDS20(ADO_purview,CDS_purview);
  SInitCDSData20(ADO_purview,CDS_purview);
 // CreateAdoToCds(ADO_purview,CDS_purview);
  //InitAdoToCds(ADO_purview,CDS_purview);
  
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
  cxGrid2.Align:=AlClient;
  InitUser();
  InitPurview();
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.N1Click(Sender: TObject);
begin
  with CDS_purview do
  begin
    first;
    DisableControls;
    while not eof do
    begin
      edit;
      fieldbyname('sel').AsBoolean:=true;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
end;

procedure TfrmpurviewDL.N2Click(Sender: TObject);
begin
  with CDS_purview do
  begin
    first;
    DisableControls;
    while not eof do
    begin
      edit;
      fieldbyname('sel').AsBoolean:=false;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
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.UserNameChange(Sender: TObject);
begin
  if ADO_user.Active then
  begin
    SDofilter(ADO_user, SGetFilters(Panel2, 1, 2));
    SCreateCDS20(ADO_user, CDS_User);
    SInitCDSData20(ADO_user, CDS_User);
  end;
end;

end.
