unit U_ZhengJianLeiXing;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, RzDBGrid, StdCtrls, RzButton, ExtCtrls, RzPanel;

type
  TFrm_LawUnits = class(TForm)
    RzPanel1: TRzPanel;
    Label1: TLabel;
    Label2: TLabel;
    rzbtbtn_Del: TRzBitBtn;
    rzbtbtn_Add: TRzBitBtn;
    rzbtbtn_Edit: TRzBitBtn;
    edt_LeiBie: TEdit;
    mmo_Remark: TMemo;
    RzPanel2: TRzPanel;
    RzDBGrid1: TRzDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure RzDBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure edt_LeiBieKeyPress(Sender: TObject; var Key: Char);
    procedure rzbtbtn_AddClick(Sender: TObject);
    procedure rzbtbtn_DelClick(Sender: TObject);
    procedure rzbtbtn_EditClick(Sender: TObject);
  private
    { Private declarations }
    function CheckRight(EditStatus:Integer): Boolean;    
  public
    { Public declarations }
    FTableName :string;     //表名称
  end;

var
  Frm_LawUnits: TFrm_LawUnits;

implementation

uses DataModuleU;


{$R *.dfm}

function TFrm_LawUnits.CheckRight(EditStatus: Integer): Boolean;
var
  m_sql :string;
begin
  result := False;
  if (edt_LeiBie.Text = '') and (EditStatus <> 1) then
  begin
    MessageBox(Handle,'证件类型名称不能够为空,请填写!','提示',MB_OK + MB_ICONINFORMATION);
    edt_LeiBie.SetFocus;
    Exit;
  end;
  case EditStatus of
    0: begin  ///当前为检查添加操作,主要检查是否有重复名字的
      m_sql := 'select id from ' + FTableName + ' where C_Name = ' + QuotedStr(edt_LeiBie.Text);
    end;
    2: Begin ///当前为检查修改操作,主要检查是否有重复名字的
      m_sql := 'select id from ' + FTableName + ' where id <> ' + RzDBGrid1.DataSource.DataSet.FieldByName('id').AsString
        + ' and C_Name = ' + QuotedStr(edt_LeiBie.Text);
    end;
  end;
  if (EditStatus = 0) or (EditStatus = 2) then
  begin
    DmFrm.OpenAdoquery(DmFrm.AQ,m_sql);
    if not DmFrm.AQ.IsEmpty then
    Begin
      MessageBox(Handle,'证件类型名称已存在,不能重复添加!','提示',MB_OK + MB_ICONINFORMATION);
      DmFrm.AQ.Close;
      edt_LeiBie.SetFocus;
      Exit;
    end;
  end
  else if EditStatus = 1 then
  Begin
    if MessageBox(Handle, PChar('确定要删除证件类型名称[' + RzDBGrid1.DataSource.DataSet.FieldByName('C_Name').AsString
        + ']吗?'), '确认', MB_YESNO + MB_ICONQUESTION) <> IDYES then
    begin
      Exit;
    end;
  end;

  Result := True;
end;

procedure TFrm_LawUnits.FormCreate(Sender: TObject);
var
  m_sql :string;
begin
  FTableName := ' T_LawUnits ';
  DmFrm.AdoQry_ChangShu.Close;
  RzDBGrid1.DataSource := DmFrm.ds_ChangShu;
  m_sql := 'select id,C_Name,C_Remark from ' + FTableName;
  DmFrm.OpenAdoquery(DmFrm.AdoQry_ChangShu,m_sql);
end;

procedure TFrm_LawUnits.RzDBGrid1CellClick(Column: TColumn);
begin
  if RzDBGrid1.DataSource.DataSet.Active then
  begin
    edt_LeiBie.Text := RzDBGrid1.DataSource.DataSet.FieldByName('C_Name').AsString;
    mmo_Remark.Lines.Text := RzDBGrid1.DataSource.DataSet.FieldByName('C_Remark').AsString;
  end;
end;

procedure TFrm_LawUnits.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  RzDBGrid1.DataSource.DataSet.Close;
  RzDBGrid1.DataSource := nil;
end;

procedure TFrm_LawUnits.edt_LeiBieKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then
  PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);
end;

procedure TFrm_LawUnits.rzbtbtn_AddClick(Sender: TObject);
var
  m_Sql :string;
begin
  if not CheckRight((Sender as TRzBitBtn).Tag) then
  Exit;
  m_Sql := 'Insert Into ' + FTableName + '(C_Name,C_Remark) Values(''%s'',''%s'')';
  m_Sql := Format(m_Sql,[edt_LeiBie.Text,mmo_Remark.Text]);
  if DmFrm.ExecAdoquery(DmFrm.AE,m_Sql) then
  MessageBox(Handle,'添加新的证件类型成功!','提示',MB_OK + MB_ICONINFORMATION);
  DmFrm.ReopenAdoquery(DmFrm.AdoQry_ChangShu);
  DmFrm.WriteOperatLog(Caption, (Sender as TRzBitBtn).Caption);  
end;

procedure TFrm_LawUnits.rzbtbtn_DelClick(Sender: TObject);
var
  m_Sql :string;
begin
  if not CheckRight((Sender as TRzBitBtn).Tag) then
  Exit;
  m_Sql := 'delete from ' + FTableName + ' where id = %d';
  m_Sql := Format(m_Sql,[RzDBGrid1.DataSource.DataSet.FieldByName('id').AsInteger]);
  if DmFrm.ExecAdoquery(DmFrm.AE,m_Sql) then
  MessageBox(Handle,'删除证件类型成功!','提示',MB_OK + MB_ICONINFORMATION);
  DmFrm.ReopenAdoquery(DmFrm.AdoQry_ChangShu);
  DmFrm.WriteOperatLog(Caption, (Sender as TRzBitBtn).Caption);  
end;

procedure TFrm_LawUnits.rzbtbtn_EditClick(Sender: TObject);
var
  m_Sql :string;
begin
  if not CheckRight((Sender as TRzBitBtn).Tag) then
  Exit;
  m_Sql := 'update ' + FTableName + ' set C_Name = ''%s'' ,C_Remark = ''%s'' where id = %d';
  m_Sql := Format(m_Sql,[edt_LeiBie.Text,mmo_Remark.Text,RzDBGrid1.DataSource.DataSet.FieldByName('id').AsInteger]);
  if DmFrm.ExecAdoquery(DmFrm.AE,m_Sql) then
  MessageBox(Handle,'修改证件类型成功!','提示',MB_OK + MB_ICONINFORMATION);
  DmFrm.ReopenAdoquery(DmFrm.AdoQry_ChangShu);
  DmFrm.WriteOperatLog(Caption, (Sender as TRzBitBtn).Caption);  
end;

end.