⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcammanagerform.pas

📁 销售软件
💻 PAS
字号:
(*===========================================================*)
(*                                                           *)
(*              Jerk Computer Assembly Manager               *)
(*                                                           *)
(*                    程序作者:杨芹勍                       *)
(*          武汉科技大学 理学院 信息与计算科学031班          *)
(*                  武汉科技大学 莘特工作室                  *)
(*                                                           *)
(*              IDE:Borland Delphi 2006 Update 2            *)
(*                 第三方控件:Raize 4.03                    *)
(*             数据库:Microsoft SQL Server 2000             *)
(*            数据库访问引擎:原生ADO(ADODB_TLB)           *)
(*           数据库管理引擎:JERK DBMANAGER ALPHA            *)
(*                                                           *)
(*           此软件及源代码归 JERK SYSTEM 版权所有           *)
(*            (C)Copyright 2002-2006 Jerk System.            *)
(*                                                           *)
(*===========================================================*)

unit JCAMManagerForm;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  TypInfo,
  Dialogs,
  ComCtrls,
  ADODB_TLB,
  RzListVw,
  JSDBListView,
  Mask,
  Menus,
  ExtCtrls,
  StdCtrls,
  StrUtils,
  RzPanel,
  RzSplit,
  RzButton,
  RzEdit,
  JCAMUtils,
  JCAMConsts,
  JCAMConnectionForm,
  JCAMTableViewEditor,
  JCAMTableExportToExcel;

type
  TJCAMManagerFormClass = class of TfrmJCAMManagerForm;

  PfrmJCAMManagerForm = ^TfrmJCAMManagerForm;
  TfrmJCAMManagerForm = class( TfrmJCAMConnectionForm )
    RzSizePanel1: TRzSizePanel;
    pnlInfoEditor: TRzPanel;
    RzPanel1: TRzPanel;
    dvwInfoList: TJSDBListView;
    tbrBtns: TRzToolbar;
    btnInfoSearch: TRzToolButton;
    RzSpacer1: TRzSpacer;
    btnSelectAll: TRzToolButton;
    btnInfoListAll: TRzToolButton;
    btnInfoRefresh: TRzToolButton;
    tbrSimpleInfoSearch: TRzToolbar;
    btnSimpleInfoSearch: TRzToolButton;
    RzSpacer2: TRzSpacer;
    btnExportData: TRzToolButton;
    pmExportData: TPopupMenu;
    miEDAllItems: TMenuItem;
    miEDSelectedItems: TMenuItem;
    btnStatInfo: TRzToolButton;
    procedure FormCreate( Sender: TObject );
    procedure FormDestroy( Sender: TObject );
    procedure btnInfoSearchClick( Sender: TObject );
    procedure btnSelectAllClick( Sender: TObject );
    procedure btnInfoListAllClick( Sender: TObject );
    procedure btnInfoRefreshClick( Sender: TObject );
    procedure btnSimpleInfoSearchClick( Sender: TObject );
    procedure pEDTKeyPress( Sender: TObject; var Key: Char );
    procedure pmExportDataPopup( Sender: TObject );
    procedure miEDAllItemsClick( Sender: TObject );
    procedure miEDSelectedItemsClick( Sender: TObject );
    procedure btnStatInfoClick( Sender: TObject );
  private
    m_frmTableViewEditor: TfrmTableViewEditorForm;
    m_sTableViewName: string;
    m_sKeyFieldName: string;
    m_sSaveInfoProcName: string;
    m_sDeleteInfoProcName: string;
    m_sInfoSearchCondition: string;
    procedure pExportAllItems;
    procedure pExportSelectedItems;
    procedure SetKeyFieldName( const Value: string );
    procedure SetTableViewName( const Value: string );
    procedure SetSaveInfoProcName( const Value: string );
    procedure SetDeleteInfoProcName( const Value: string );
    procedure SetInfoSearchCondition( const Value: string );
  public
    property DBListView: TJSDBListView
      read dvwInfoList
      write dvwInfoList;
    property TableViewName: string
      read m_sTableViewName
      write SetTableViewName;
    property KeyFieldName: string
      read m_sKeyFieldName
      write SetKeyFieldName;
    property SaveInfoProcName: string
      read m_sSaveInfoProcName
      write SetSaveInfoProcName;
    property DeleteInfoProcName: string
      read m_sDeleteInfoProcName
      write SetDeleteInfoProcName;
    property Connection;
    property TableViewEditorForm: TfrmTableViewEditorForm
      read m_frmTableViewEditor
      write m_frmTableViewEditor;
    property InfoSearchCondition: string
      read m_sInfoSearchCondition
      write SetInfoSearchCondition;
    procedure StartSurf; virtual;
    procedure SelectAllInfo; virtual;
    procedure FocusSimpleSearch; virtual;
    procedure UpdateStatusCaption; virtual;
    procedure SetCustomEditTag( edtCustomEdit: TCustomEdit; sFieldName: string
      ); virtual;
    procedure SelectItemsByCondition( sCondition: string ); virtual;
    procedure ListItemsByCondition( sCondition: string ); virtual;
    procedure ListAllItems; virtual;
    procedure StatInfo; virtual;
  end;

var
  frmJCAMManagerForm: TfrmJCAMManagerForm;

implementation

{$R *.dfm}

uses
  untMain,
  untStatInfo,
  untInfoSearch,
  untTField;

procedure TfrmJCAMManagerForm.btnInfoListAllClick( Sender: TObject );
begin
  inherited;

  ListAllItems;
end;

procedure TfrmJCAMManagerForm.btnInfoRefreshClick( Sender: TObject );
begin
  inherited;

  ListAllItems;
end;

procedure TfrmJCAMManagerForm.btnInfoSearchClick( Sender: TObject );
begin
  { 高级搜索 }

  inherited;

  frmInfoSearch := TfrmInfoSearch.Create( Self );
  with frmInfoSearch do
  begin
    Connection := Self.Connection;
    TableViewName := Self.TableViewName;
    KeyFieldName := Self.KeyFieldName;
    StartSurf;
    ShowModal;
  end;
end;

procedure TfrmJCAMManagerForm.btnSelectAllClick( Sender: TObject );
begin
  inherited;

  SelectAllInfo;
end;

procedure TfrmJCAMManagerForm.btnSimpleInfoSearchClick( Sender: TObject );
const
  STR_FIELDSCONDITION: string = ' AND ';
var
  ctl: TControl;
  pfp: PFieldProp;
  sCondition, sFieldValue: string;
  n: Integer;
begin
  { 快速定位 }

  inherited;

  sCondition := EmptyStr;
  for n := 0 to tbrSimpleInfoSearch.ControlCount - 1 do
  begin
    ctl := tbrSimpleInfoSearch.Controls[ n ];
    if ctl is TCustomEdit then
    begin
      pfp := PFieldProp( Pointer( TCustomEdit( ctl ).tag ) );
      sFieldValue := TCustomEdit( ctl ).Text;

      if sFieldValue <> EmptyStr then
      begin
        // TODO: 生成SQL语句
        sCondition := sCondition + '[' + pfp^.FieldName + ']=';
        if pfp^.FieldType <= adBoolean then
          sCondition := sCondition + sFieldValue
        else
          sCondition := sCondition + '''' + sFieldValue + '''';
        sCondition := sCondition + STR_FIELDSCONDITION;
      end;
    end;
  end;
  if sCondition <> EmptyStr then
  begin
    sCondition := LeftBStr( sCondition, Length( sCondition ) - Length(
      STR_FIELDSCONDITION ) );
    SelectItemsByCondition( sCondition );
  end;
end;

procedure TfrmJCAMManagerForm.btnStatInfoClick( Sender: TObject );
begin
  inherited;

  StatInfo;
end;

procedure TfrmJCAMManagerForm.FocusSimpleSearch;
var
  ctl: TControl;
  n: Integer;
begin
  for n := 0 to tbrSimpleInfoSearch.ControlCount - 1 do
  begin
    ctl := tbrSimpleInfoSearch.Controls[ n ];
    if ctl is TCustomEdit then
    begin
      TWinControl( ctl ).SetFocus;
      Break;
    end;
  end;
end;

procedure TfrmJCAMManagerForm.FormCreate( Sender: TObject );
begin
  inherited;

  TableViewName := EmptyStr;
  KeyFieldName := EmptyStr;
  SaveInfoProcName := EmptyStr;
  DeleteInfoProcName := EmptyStr;
  Connection := nil;
end;

procedure TfrmJCAMManagerForm.FormDestroy( Sender: TObject );
begin
  inherited;

  if m_frmTableViewEditor <> nil then
    FreeAndNil( m_frmTableViewEditor );
end;

procedure TfrmJCAMManagerForm.ListAllItems;
begin
  { 列出所有Item }

  InfoSearchCondition := EmptyStr;
  ListItemsByCondition( EmptyStr );
end;

procedure TfrmJCAMManagerForm.ListItemsByCondition( sCondition: string );
begin
  { 按照条件列出Item }

  if Connection <> nil then
  begin
    with TableViewEditorForm do
    begin
      LoadCondition := sCondition;
      StartSurf( False );
    end;
    if dvwInfoList.Items.Count > 1 then
      dvwInfoList.Selected := dvwInfoList.Items[ 0 ];
    dvwInfoList.SetFocus;
  end;
end;

procedure TfrmJCAMManagerForm.miEDAllItemsClick( Sender: TObject );
begin
  inherited;

  pExportAllItems;
end;

procedure TfrmJCAMManagerForm.miEDSelectedItemsClick( Sender: TObject );
begin
  inherited;

  pExportSelectedItems;
end;

procedure TfrmJCAMManagerForm.pEDTKeyPress( Sender: TObject; var Key: Char );
begin
  if ( Sender is TCustomEdit ) and ( Key = #13 ) then
    btnSimpleInfoSearchClick( Self );
end;

procedure TfrmJCAMManagerForm.pExportAllItems;
var
  tete: TJCAMTableExportToExcel;
begin
  { 导出所有数据 }

  tete := TJCAMTableExportToExcel.Create;
  with tete do
  begin
    Connection := Self.Connection;
    DBListView := dvwInfoList;
    TableViewName := Self.TableViewName;
    KeyFieldName := Self.KeyFieldName;
    Condition := InfoSearchCondition;
    ConnectExcel;
    ExportTableData( ExcelWorkSheet );
  end;
  FreeAndNil( tete );
end;

procedure TfrmJCAMManagerForm.pExportSelectedItems;
var
  tete: TJCAMTableExportToExcel;
begin
  { 导出选中的记录 }

  tete := TJCAMTableExportToExcel.Create;
  with tete do
  begin
    Connection := Self.Connection;
    DBListView := dvwInfoList;
    TableViewName := Self.TableViewName;
    KeyFieldName := Self.KeyFieldName;
    Condition := InfoSearchCondition;
    ConnectExcel;
    ExportSelected := esExportSelected;
    ExportTableData( ExcelWorkSheet );
  end;
  FreeAndNil( tete );
end;

procedure TfrmJCAMManagerForm.pmExportDataPopup( Sender: TObject );
begin
  { 导出数据菜单弹出 }

  inherited;

  // TODO: 判断dvw中的项目
  miEDAllItems.Enabled := dvwInfoList.Items.Count <> 0;
  miEDSelectedItems.Enabled := dvwInfoList.SelCount <> 0;
end;

procedure TfrmJCAMManagerForm.SelectAllInfo;
begin
  { 全选 }

  with dvwInfoList do
  begin
    SelectAll;
    SetFocus;
  end;
end;

procedure TfrmJCAMManagerForm.SelectItemsByCondition( sCondition: string );
var
  li: TListItem;
  rs: _Recordset;
  nKeyFieldValue: Integer;
begin
  { 按照语句选择Item }

  if ( Connection <> nil ) and ( sCondition <> EmptyStr ) then
  begin
    rs := CoRecordset.Create;

    // TODO: 由条件获取记录集
    rs.Open( 'SELECT [' + KeyFieldName + '] FROM [' + TableViewName + '] WHERE '
      + sCondition, Connection, adOpenForwardOnly, adLockReadOnly, adCmdText );
    if RecordsetValid( rs ) then
    begin
      dvwInfoList.Selected := nil;
      repeat
        nKeyFieldValue := rs.Fields[ KeyFieldName ].Value;

        // TODO: 获取指定ID的Item
        li := dvwInfoList.GetItemByKeyFieldValue( nKeyFieldValue );
        if li <> nil then
          li.Selected := True;

        rs.MoveNext;
      until rs.EOF;

      // TODO: 选定找到的最后一项
      if li <> nil then
      begin
        li.MakeVisible( True );
        li.Focused := True;
      end;
      dvwInfoList.SetFocus;
    end;
    rs := nil;
  end;
end;

procedure TfrmJCAMManagerForm.SetCustomEditTag( edtCustomEdit: TCustomEdit;
  sFieldName: string );
var
  rs: _Recordset;
  pfp: PFieldProp;
  mtd: TMethod;
  kpe: TKeyPressEvent;
  //ne: TNotifyEvent;
begin
  { 设置快速定位字段的tag }

  if ( Connection <> nil ) and ( edtCustomEdit <> nil ) and ( sFieldName <>
    EmptyStr ) then
  begin
    rs := CoRecordset.Create;
    rs.Open( 'SELECT TOP 0 [' + sFieldName + '] FROM [' + TableViewName + ']',
      Connection, adOpenKeyset, adLockReadOnly, adCmdText );
    if rs.State <> adStateClosed then
    begin
      New( pfp );
      edtCustomEdit.Tag := Integer( pfp );
      with pfp^ do
      begin
        FieldName := sFieldName;
        FieldType := rs.Fields[ sFieldName ].type_;
      end;
      rs.Close;

      // TODO: 设置收到KeyPress事件
      kpe := pEDTKeypress;
      mtd.Data := Self;
      mtd.Code := @kpe;
      SetMethodProp( edtCustomEdit, 'OnKeyPress', mtd );
      SetOrdProp( edtCustomEdit, 'FocusColor', CL_INFOEDITCOLORFOCUS );
    end;
    rs := nil;
  end;
end;

procedure TfrmJCAMManagerForm.SetDeleteInfoProcName( const Value: string );
begin
  m_sDeleteInfoProcName := Value;
end;

procedure TfrmJCAMManagerForm.SetInfoSearchCondition( const Value: string );
begin
  m_sInfoSearchCondition := Value;
end;

procedure TfrmJCAMManagerForm.SetKeyFieldName( const Value: string );
begin
  m_sKeyFieldName := Value;
end;

procedure TfrmJCAMManagerForm.SetSaveInfoProcName( const Value: string );
begin
  m_sSaveInfoProcName := Value;
end;

procedure TfrmJCAMManagerForm.SetTableViewName( const Value: string );
begin
  m_sTableViewName := Value;
end;

procedure TfrmJCAMManagerForm.StartSurf;
begin
  Screen.Cursor := crHourGlass;

  // TODO: 开始载入
  m_frmTableViewEditor := TfrmTableViewEditorForm.Create( Self );
  with m_frmTableViewEditor do
  begin
    Connection := Self.Connection;
    Parent := pnlInfoEditor;
    Show;
    DBListView := dvwInfoList;
    TableViewName := Self.TableViewName;
    KeyFieldName := Self.KeyFieldName;
    SaveInfoProcName := Self.SaveInfoProcName;
    DeleteInfoProcName := Self.DeleteInfoProcName;
    StartSurf;
  end;
  dvwInfoList.SetFocus;

  Screen.Cursor := crDefault;
end;

procedure TfrmJCAMManagerForm.StatInfo;
begin
  { 统计信息 }

  if frmStatInfo <> nil then
    FreeAndNil( frmStatInfo );
  frmStatInfo := TfrmStatInfo.Create( Self );
  frmStatInfo.ShowModal;
  FreeAndNil( frmStatInfo );
end;

procedure TfrmJCAMManagerForm.UpdateStatusCaption;
begin
  { 更新状态栏的标题 }

  with frmMain do
  begin
    StatusItemsCount := EmptyStr;
    StatusSelectedCount := EmptyStr;
    StatusSelectedIndex := EmptyStr;
    StatusRemark := EmptyStr;
    StatusItemsCount := Format( RSTR_MAIN_STATUSITEMSCOUNT, [ IntToStr(
        dvwInfoList.Items.Count ) ] );
    StatusSelectedCount := Format( RSTR_MAIN_STATUSSELECTEDCOUNT, [ IntToStr(
        dvwInfoList.SelCount ) ] );
    if dvwInfoList.Selected <> nil then
      StatusSelectedIndex := Format( RSTR_MAIN_STATUSSELECTEDINDEX, [ IntToStr(
          dvwInfoList.Selected.Index + 1 ) ] );
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -