unit U_MultiSelMach;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ADODB, DBClient, ExtCtrls;

type
  TfrmMultiSelMach = class(TForm)
    ToolBar2: TToolBar;
    TOk: TToolButton;
    Tclose: TToolButton;
    pics: TImageList;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1frameNo: TcxGridDBColumn;
    v1craftName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOConnection1: TADOConnection;
    ADOQueryDept: TADOQuery;
    DataSource1: TDataSource;
    cxGrid2: TcxGrid;
    tv2: TcxGridDBTableView;
    tv2deviceNo: TcxGridDBColumn;
    tv2deviceName: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMach: TADOQuery;
    DataSource2: TDataSource;
    cxGrid3: TcxGrid;
    tv3: TcxGridDBTableView;
    tv3deviceNo: TcxGridDBColumn;
    tv3deviceName: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_SelMach: TClientDataSet;
    DataSource3: TDataSource;
    CDS_SelMachdeviceNo: TStringField;
    CDS_SelMachdeviceName: TStringField;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_focus: TcxStyle;
    Panel1: TPanel;
    v1frameTopName: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TcloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TOkClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure tv2DblClick(Sender: TObject);
    procedure tv3DblClick(Sender: TObject);
  private
    procedure IntDeptGrid();
    procedure IntMachGrid();
  public
    fFrameNo:string;
  end;

var
  frmMultiSelMach: TfrmMultiSelMach;

implementation
uses
  U_global;
{$R *.dfm}
//////////////////////////////////////////////////////////
  //函数功能：初始化机台部门
/////////////////////////////////////////////////////////
procedure TfrmMultiSelMach.IntDeptGrid();
begin
  with ADOQueryDept do
  begin
    close;
    sql.clear;
    sql.Add('select distinct B.frameNo,B.frameName,B.frameTop,');
    sql.Add('frameTopName=(select frameName from yc_frame where frameNo=B.frameTop)'); 
    sql.Add('from dc_machine A');
    sql.Add('inner join yc_frame B on A.dept=B.frameNO');
    if trim(fFrameNo)<>'' then
      sql.Add('where A.dept='''+trim(fFrameNo)+'''');
    Open;
  end;
end;
//////////////////////////////////////////////////////////
  //函数功能：初始化机台部门
/////////////////////////////////////////////////////////
procedure TfrmMultiSelMach.IntMachGrid();
begin
  with ADOQueryMach do
  begin
    close;
    sql.clear;
    sql.Add('select *');
    sql.Add('from dc_machine A');
    sql.Add('where A.dept='''+trim(ADOQueryDept.fieldByName('frameNo').asString)+'''');
    Open;
  end;
end;
procedure TfrmMultiSelMach.FormShow(Sender: TObject);
begin
  IntDeptGrid();
end;

procedure TfrmMultiSelMach.FormCreate(Sender: TObject);
begin
  cxgrid3.Align :=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
  CDS_SelMach.Close ;
  CDS_SelMach.CreateDataSet;

end;

procedure TfrmMultiSelMach.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmMultiSelMach.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmMultiSelMach.TOkClick(Sender: TObject);
begin
  if CDS_SelMach.IsEmpty then
    ModalResult:=-1
  else
    ModalResult:=1;
end;

procedure TfrmMultiSelMach.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  IntMachGrid();
end;

procedure TfrmMultiSelMach.tv2DblClick(Sender: TObject);
begin
  if ADOQueryMach.IsEmpty then exit;
  if  CDS_selMach.Locate('deviceNo;deviceName', VarArrayOf([trim(ADOQueryMach.fieldByName('deviceNo').AsString),
                        trim(ADOQueryMach.fieldByName('deviceName').AsString)]),[loPartialKey]) then
    exit;
    
  with CDS_selMach do
  begin
    Append;
    fieldByName('deviceNo').Value :=trim(ADOQueryMach.fieldByName('deviceNo').AsString) ;
    fieldByName('deviceName').Value :=trim(ADOQueryMach.fieldByName('deviceName').AsString) ;
    Post;
  end;
end;

procedure TfrmMultiSelMach.tv3DblClick(Sender: TObject);
begin
  if CDS_SelMach.IsEmpty then exit;
  CDS_SelMach.Delete;
end;

end.
