unit U_User;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, cxLookAndFeelPainters, StdCtrls, cxButtons, DB, ADODB;

type
  TfrmUser = class(TForm)
    userID: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    userName: TEdit;
    newPassword: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    DnewPassword: TEdit;
    cxOK: TcxButton;
    cxClose: TcxButton;
    ADOQueryTmp: TADOQuery;
    ADO_Cmd: TADOQuery;
    Label5: TLabel;
    Udept: TEdit;
    Label6: TLabel;
    UType: TEdit;
    procedure cxCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxOKClick(Sender: TObject);
    procedure userIDKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
  private

    fPassWord:string;
    fIsYGCodezdsc:Boolean;
    procedure DoQuery();
    function SaveData():Boolean;
    { Private declarations }
  public
    formId:integer;
    fuserID:string;
    { Public declarations }
  end;
const
  keys='ljb^0122!@#*&^%$';
var
  frmUser: TfrmUser;

implementation
uses U_DataLink,U_RTFun,ElAES,AES; //U_Main,,U_PublicFunction;
{$R *.dfm}
function TfrmUser.SaveData():Boolean;
begin
  result:=false;
  ADO_Cmd.Connection.BeginTrans;
  try
    if trim(newPassword.Text)<>'' then
      fPassWord:=trim(EncryptString(trim(newPassword.text),keys))
    else
      fPassWord:='';

      with ADO_Cmd do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from SY_User ');
        sql.Add(' where userID='+quotedstr(trim(fuserID)));
        open;

        if ADO_Cmd.IsEmpty then  ADO_Cmd.Append
        else ADO_Cmd.Edit;
        ADO_Cmd.FieldByName('userID').Value:=trim(fuserID);
        ADO_Cmd.FieldByName('username').Value:=trim(username.Text);
        ADO_Cmd.FieldByName('passWord').Value:=trim(fPassWord);
        ADO_Cmd.FieldByName('Udept').Value:=trim(Udept.Text);
        ADO_Cmd.FieldByName('UType').Value:=trim(UType.Text);
        ADO_Cmd.FieldByName('valid').Value:='Y';
        ADO_Cmd.Post;
      end;

    //IF frmMain.userID<>'ADMIN' then
    begin
   {   with ADO_Cmd do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from SY_UserF ');
        sql.Add(' where userIDF='+quotedstr(trim(fuserID)));
        sql.Add(' and userID='+quotedstr(trim(frmMain.userID)));
        open;

        if ADO_Cmd.IsEmpty then  ADO_Cmd.Append
        else ADO_Cmd.Edit;
        ADO_Cmd.FieldByName('userID').Value:=trim(frmMain.userID);
        ADO_Cmd.FieldByName('userIDF').Value:=trim(fuserID);
        ADO_Cmd.Post;
      end;  }
    end;

    ADO_Cmd.Connection.CommitTrans;
    result:=true;
  except
    ADO_Cmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmUser.DoQuery();
begin
  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from SY_User A ');
    sql.Add(' where userID='+quotedstr(trim(fuserID)));
    open;
    if not IsEmpty then
    begin
      userID.Text:=trim(fieldbyname('userID').AsString);
      userName.Text:=trim(fieldbyname('userName').AsString);
      Udept.Text:=trim(fieldbyname('Udept').AsString);
      UType.Text:=trim(fieldbyname('UType').AsString);
      if trim(fieldbyname('passWord').AsString)<>'' then
        fPassWord:=trim(DecryptString(trim(fieldbyname('passWord').AsString),keys))
      else
        fPassWord:='';
      newPassword.Text:=trim(fPassWord);
      DnewPassword.Text:=trim(fPassWord);
    end;
  end;
end;

procedure TfrmUser.cxCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmUser.FormCreate(Sender: TObject);
begin
  with ADO_Cmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from SY_SET');
    open;
    if not IsEmpty then
    begin
      fIsYGCodezdsc:=fieldbyname('IsYGCodezdsc').AsBoolean;
    end;
  end;

 
end;

procedure TfrmUser.cxOKClick(Sender: TObject);
begin
  if trim(userID.Text)='' then
  begin
    application.MessageBox('编号不能为空！','错误信息',MB_ICONERROR);
    userName.Text;
    exit;
  end;
  if trim(userName.Text)='' then
  begin
    application.MessageBox('名称不能为空！','错误信息',MB_ICONERROR);
    userName.Text;
    exit;
  end;
  if trim(newPassword.Text)<> trim(dnewPassword.Text) then
  begin
    application.MessageBox('新密码两次输入不一致！','错误信息',MB_ICONERROR);
    newPassword.Text:='';
    dnewPassword.Text:='';
    exit;
  end;
  if formId=0 then
    begin
      if  fIsYGCodezdsc then
      begin
       // if not GetMaxBh(ADO_Cmd,fuserID,'SY_USER','YG','0',3) then exit;
      end
      else
      begin
        fuserID:=trim(userID.Text);
      end;

      with ADO_Cmd do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from SY_user');
        sql.Add(' where userID='+quotedstr(trim(fuserID))) ;
        open;
        if not isempty then
        begin
          application.MessageBox('编号重复！','错误信息',MB_ICONERROR);
          exit;
        end;
      end;
    end;
  if SaveData() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
    ModalResult:=1;
  end
  else
    application.MessageBox('数据保存失败！','提示信息',MB_ICONERROR);
end;

procedure TfrmUser.userIDKeyPress(Sender: TObject; var Key: Char);
begin
 Key:=UpCase(Key);
  if key=#13 then
  begin
    perform(WM_NEXTDLGCTL,0,0);
    Key := #0;
  end;
end;

procedure TfrmUser.FormShow(Sender: TObject);
begin
 if formId=0  then
  begin
    if fIsYGCodezdsc then
    begin
      userID.Enabled:=false;
      userID.Text:='自动生成';
    end;
  end
  else
  begin
    userID.Enabled:=false;
  end;
  DoQuery();
end;

end.
