unit U_PurviewDC;

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;

type
  TfrmPurviewDC = 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;
    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 FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
  private
    procedure InitUser();
    procedure InitPurview();
    function savePurview(): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPurviewDC: TfrmPurviewDC;

implementation

uses
  U_DataLink, U_PublicFunction, U_UserDC, U_Fun;
{$R *.dfm}

function TfrmPurviewDC.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 TfrmPurviewDC.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;
  CreateAdoToCds(ADO_purview, CDS_purview);
  InitAdoToCds(ADO_purview, CDS_purview);
end;

procedure TfrmPurviewDC.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'' and Udept=''挡车工'' ');
    open;
  end;
  CreateAdoToCds(ADO_user, CDS_User);
  InitAdoToCds(ADO_user, CDS_User);
end;

procedure TfrmPurviewDC.FormCreate(Sender: TObject);
begin
  cxGrid2.Align := AlClient;
  InitUser();
  InitPurview();
end;

procedure TfrmPurviewDC.tbcloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'tv1', tv1);
  WriteCxGrid(self.Caption + 'tv2', tv2);
  close;
end;

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

procedure TfrmPurviewDC.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 TfrmPurviewDC.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 TfrmPurviewDC.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 TfrmPurviewDC.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 TfrmPurviewDC.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);
   { 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 TfrmPurviewDC.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 TfrmPurviewDC.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + 'tv1', tv1);
  readCxGrid(self.Caption + 'tv2', tv2);
end;

procedure TfrmPurviewDC.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmPurviewDC.FormDestroy(Sender: TObject);
begin
  frmPurviewDC := nil;
end;

end.

