unit U_JTSel;

interface

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

type
  TfrmJTSel = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Name: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    V1Note: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    MCNO: TEdit;
    ThreeImgList: TImageList;
    V1Column1: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label2: TLabel;
    V1Column2: TcxGridDBColumn;
    V1Column3: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    Label3: TLabel;
    MCCJ: TComboBox;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure MCNOChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    flag, flagname, snote, MainType, ReturnStr, FGStr, FBIAOSHI: string;
    fnote, forderno: Boolean;
    PPSTE, JiangeStr: integer;
    { Public declarations }
  end;

var
  frmJTSel: TfrmJTSel;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}

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

procedure TfrmJTSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select MCNO,MCCJ  ');
      sql.Add(' ,ZYConNo=(select Top 1 ConNo from Cloth_Main CM inner join Cloth_JiTai CJ on CM.MainId=CJ.MainId');
      sql.Add('           where CJ.CarNo=A.MCNO and CJ.StopFlag=0)');
      sql.Add(' ,C_Code=(select Top 1 C_Code from Cloth_Sub CM inner join Cloth_JiTai CJ on CM.MainId=CJ.MainId');
      sql.Add('           where CJ.CarNo=A.MCNO and CJ.StopFlag=0)');
      sql.Add(' ,C_CodeName=(select Top 1 C_CodeName from Cloth_Sub CM inner join Cloth_JiTai CJ on CM.MainId=CJ.MainId');
      sql.Add('           where CJ.CarNo=A.MCNO and CJ.StopFlag=0)');
      sql.Add(' from Machine A');
      sql.Add(' where  ');
      if cxTabControl1.TabIndex = 0 then
      begin
        sql.Add('   not exists(select StopFlag from Cloth_JiTai B inner join cloth_main X on B.mainid=X.mainid where B.CarNo=A.MCNO and B.StopFlag=0)');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        sql.Add('   exists(select StopFlag from Cloth_JiTai B inner join cloth_main X on B.mainid=X.mainid where B.CarNo=A.MCNO and B.StopFlag=0)');
      end;
      sql.Add(' order by MCCJ,MCNO');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJTSel.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmJTSel.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select Name=MCCJ from Machine   Group by MCCJ ';
  SInitComBoxBySql(ADOQueryTemp, MCCJ, False, fsj);
  InitGrid();
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select MCNO,MCCJ  ');
    sql.Add(' ,ZYConNo=(select Top 1 ConNo from Cloth_Main CM inner join Cloth_JiTai CJ on CM.MainId=CJ.MainId');
    sql.Add('           where CJ.CarNo=A.MCNO and CJ.StopFlag=0)');
    sql.Add(' ,C_Code=(select Top 1 C_Code from Cloth_Sub CM inner join Cloth_JiTai CJ on CM.MainId=CJ.MainId');
    sql.Add('           where CJ.CarNo=A.MCNO and CJ.StopFlag=0)');
    sql.Add(' ,C_CodeName=(select Top 1 C_CodeName from Cloth_Sub CM inner join Cloth_JiTai CJ on CM.MainId=CJ.MainId');
    sql.Add('           where CJ.CarNo=A.MCNO and CJ.StopFlag=0)');
    sql.Add(' from Machine A');
    sql.Add(' where  ');
    if cxTabControl1.TabIndex = 0 then
    begin
      sql.Add('   not exists(select StopFlag from Cloth_JiTai B inner join cloth_main X on B.mainid=X.mainid where B.CarNo=A.MCNO and B.StopFlag=0)');
    end
    else if cxTabControl1.TabIndex = 1 then
    begin
      sql.Add('   exists(select StopFlag from Cloth_JiTai B inner join cloth_main X on B.mainid=X.mainid where B.CarNo=A.MCNO and B.StopFlag=0)');
    end;
    sql.Add(' AND 1<>1 order by MCCJ,MCNO');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd, ClientDataSet2);
  SInitCDSData20(ADOQueryCmd, ClientDataSet2);
end;

procedure TfrmJTSel.ToolButton1Click(Sender: TObject);
begin
  {if cxTabControl1.TabIndex<>0 then
  begin
    Application.MessageBox('已启用的车号不能选择，请停用后选择。','提示',0);
    Exit;
  end; }
  if ClientDataSet2.ISEMPTY = FALSE then
  begin
    ReturnStr := '';
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if ClientDataSet2.FieldByName('SSel').AsBoolean = True then
        begin
          if JiangeStr <> 99 then
          begin
            if FGStr <> '' then
              ReturnStr := ReturnStr + Trim(ClientDataSet2.fieldbyname('MCNO').AsString) + FGStr
            else
              ReturnStr := ReturnStr + Trim(ClientDataSet2.fieldbyname('MCNO').AsString) + ';'
          end
          else
            ReturnStr := ReturnStr + Trim(ClientDataSet2.fieldbyname('MCNO').AsString);
        end;
        Next;
      end;
    end;
    if JiangeStr <> 99 then
      ReturnStr := Copy(ReturnStr, 1, Length(ReturnStr) - 1);
    ModalResult := 1;
    FBIAOSHI := '1';
  end
  else
  begin

    if cxTabControl1.TabIndex <> 0 then
      Exit;
    if ClientDataSet1.Locate('SSel', True, []) = False then
    begin
      Application.MessageBox('没有选择数据。', '提示', 0);
      Exit;
    end;
    ReturnStr := '';
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean = True then
        begin
          if JiangeStr <> 99 then
          begin
            if FGStr <> '' then
              ReturnStr := ReturnStr + Trim(fieldbyname('MCNO').AsString) + FGStr
            else
              ReturnStr := ReturnStr + Trim(fieldbyname('MCNO').AsString) + ';'
          end
          else
            ReturnStr := ReturnStr + Trim(fieldbyname('MCNO').AsString);
        end;
        Next;
      end;
    end;
    if JiangeStr <> 99 then
      ReturnStr := Copy(ReturnStr, 1, Length(ReturnStr) - 1);
    ModalResult := 1;
    FBIAOSHI := '2';
  end;

end;

procedure TfrmJTSel.MCNOChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
  end;
end;

procedure TfrmJTSel.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmJTSel.TV1DblClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex <> 0 then
  begin
    EXIT;
  end;
  if ClientDataSet2.Locate('MCNO', VarArrayOf([trim(ClientDataSet1.fieldbyname('MCNO').AsString)]), []) then
  begin
    Application.MessageBox('该车间已选择。', '提示', 0);
    Exit;
  end;
  with ClientDataSet2 do
  begin
    APPEND;
    ClientDataSet2.fieldbyname('SSEL').VALUE := TRUE;
    ClientDataSet2.fieldbyname('MCNO').VALUE := ClientDataSet1.fieldbyname('MCNO').ASSTRING;
    ClientDataSet2.fieldbyname('MCCJ').VALUE := ClientDataSet1.fieldbyname('MCCJ').ASSTRING;
    ClientDataSet2.fieldbyname('ZYConNo').VALUE := ClientDataSet1.fieldbyname('ZYConNo').ASSTRING;
    ClientDataSet2.fieldbyname('C_CodeName').VALUE := ClientDataSet1.fieldbyname('C_CodeName').ASSTRING;
    ClientDataSet2.fieldbyname('C_Code').VALUE := ClientDataSet1.fieldbyname('C_Code').ASSTRING;
//    ClientDataSet2.fieldbyname('').VALUE := ClientDataSet1.fieldbyname('').ASSTRING;
    POST;
  end;
end;

procedure TfrmJTSel.TV2DblClick(Sender: TObject);
begin
  ClientDataSet2.delete;
end;

end.

