unit U_PatternInfo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, jpeg, U_SLT, ComObj, Menus,
  cxLookAndFeels, cxLookAndFeelPainters, cxTLdxBarBuiltInMenu, cxNavigator,
  dxBarBuiltInMenu, cxPC, Math, RM_BarCode, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, IdExplicitTLSClientServerBase, U_BaseList, cxContainer,
  dxDBBarCode, dxBarCode, Vcl.Clipbrd, cxTextEdit, BtnEdit, StrUtils,
  cxButtonEdit;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmPatternInfo = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Panel1: TPanel;
    TBFilter: TToolButton;
    v1CYNo: TcxGridDBColumn;
    DS_1: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_1: TClientDataSet;
    TBExport: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    TBUP: TToolButton;
    Label3: TLabel;
    C_Code: TEdit;
    Panel2: TPanel;
    v1SSel: TcxGridDBColumn;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    Label11: TLabel;
    Label12: TLabel;
    Panel3: TPanel;
    adoqueryPicture: TADOQuery;
    v1Column9: TcxGridDBColumn;
    Label4: TLabel;
    C_Name: TEdit;
    TBAdd: TToolButton;
    OpenDialog1: TOpenDialog;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    Panel7: TPanel;
    Panel4: TPanel;
    Label14: TLabel;
    LBCPAP1: TLabel;
    Button1: TButton;
    Button2: TButton;
    TCBNOR1: TComboBox;
    RMBarCodeObject1: TRMBarCodeObject;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    Tv1Column9: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    N3: TMenuItem;
    Panel9: TPanel;
    Panel10: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    ADOQueryPrint: TADOQuery;
    CDS_Label: TClientDataSet;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADOQueryHZ: TADOQuery;
    Tv1Column2: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Tv1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ADOQuery1: TADOQuery;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    Tv1Column10: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBFilterClick(Sender: TObject);
    procedure CYNoChange(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBUPClick(Sender: TObject);
    procedure C_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure TBAddClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TCBNOR1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TextEdit(Sender: TObject);
    procedure HXNameBtnUpClick(Sender: TObject);
    procedure HXNameBtnDnClick(Sender: TObject);
    procedure cbbHXChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v1CYNoPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TV3CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1Column8PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    CurrentPage, RecordsNumber: Integer;
    CTID: string;
    PState: Integer;
    FCTID, FTopID, FJurisdiction, FCTType: string;

    procedure SetStatus();
    procedure InitGrid(EditName: string);
    procedure InitGY();
    procedure InitHZ();
    procedure ReadINIFile();
    procedure InitImage();
    function SaveData(): Boolean;
    procedure LookImage(FileName: string);

  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    { Public declarations }
  end;

var
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_RTFun, U_ClothInfoInput, U_CloInfoFileUp, U_ClothTypeSel,
  U_ZDYHelp, U_LabelPrint, U_DictSel;

{$R *.dfm}

procedure TfrmPatternInfo.LookImage(FileName: string);
var
  sFieldName: string;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except

  end;

  if IdFTP1.Connected then
  begin
    application.ProcessMessages;
    try
//      ShowMessage(PChar(Trim('D:\' + Trim(FileName))));
      IdFTP1.Get(Trim(UserDataFlag + 'YP\' + FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmPatternInfo.SetStatus();
begin
  TBAdd.Enabled := False;
  TBDel.Enabled := False;
  TBUP.Enabled := False;
  ToolButton4.Enabled := false;
  if FJurisdiction <> '查询' then
  begin
    TBAdd.Enabled := true;
    TBDel.Enabled := true;
    TBUP.Enabled := true;
    ToolButton4.Enabled := true;
  end;
end;

procedure TfrmPatternInfo.InitGrid(EditName: string);
var
  fwhere, MBCIID, Pwhere: string;
begin
  Panel2.Visible := True;
  Panel2.Refresh;
  if not CDS_1.IsEmpty then
    MBCIID := Trim(CDS_1.FieldByName('BCIID').AsString)
  else
    MBCIID := '';

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere)
    else
      fwhere := ' where ' + trim(Pwhere);
  end;

  if trim(EditName) <> 'C_Name' then
  begin
//    if CDS_3.IsEmpty = False then
//    begin
    if fwhere <> '' then
      fwhere := fwhere + ' and C_Technology=' + QuotedStr(trim(cds_2.FieldByName('C_Technology').AsString)) + ' and C_Technologytype=' + QuotedStr(trim(cds_3.FieldByName('C_Technology').AsString))
    else
      fwhere := ' where C_Technology=' + QuotedStr(trim(cds_2.FieldByName('C_Technology').AsString)) + ' and C_Technologytype=' + QuotedStr(trim(cds_3.FieldByName('C_Technology').AsString));
//    end;

  end;

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' exec P_BS_PatInfo_Get  ');
      sql.Add(' @CTID='''' ');
      sql.Add(',@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(sql.Text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
    TV1.DataController.Filter.Clear;
    LBCPAP1.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;
  Panel2.Visible := False;

  if MBCIID <> '' then
    CDS_1.Locate('BCIID', MBCIID, []);

end;

procedure TfrmPatternInfo.InitHZ();
var
  FTechnology: string;
begin
  if cds_2.IsEmpty = False then
    FTechnology := cds_2.FieldByName('C_Technology').asstring;

  with ADOQueryHZ do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,DictName as C_Technology from Sy_Dict A where A.Type=''GYName''');
    Open;
  end;
  SCreateCDS(ADOQueryHZ, CDS_2);
  SInitCDSData(ADOQueryHZ, CDS_2);

  if FTechnology <> '' then
    cds_2.Locate('C_Technology', FTechnology, []);

end;

procedure TfrmPatternInfo.InitGY();
var
  FTechnology: string;
begin
  if cds_3.IsEmpty = False then
    FTechnology := cds_3.FieldByName('C_Technology').asstring;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,itemtext as C_Technology from SY_Dict_Item A where A.DictId=''' + cds_2.FieldByName('DictId').AsString + '''');
    Open;
  end;
  SCreateCDS(ADOQuery1, CDS_3);
  SInitCDSData(ADOQuery1, CDS_3);
  if FTechnology <> '' then
    cds_3.Locate('C_Technology', FTechnology, []);
end;

procedure TfrmPatternInfo.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  inherited;
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action := cafree;
end;

procedure TfrmPatternInfo.FormCreate(Sender: TObject);
begin
  inherited;
  FJurisdiction := Trim(Self.fParameters1);
  FCTType := Trim(self.fParameters2);
end;

procedure TfrmPatternInfo.TBCloseClick(Sender: TObject);
begin

  if DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209', sw_hide);
  Close;
end;

procedure TfrmPatternInfo.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    while CDS_1.Locate('SSel', true, []) do
    begin
      if Trim(CDS_1.fieldbyname('BCIID').AsString) <> '' then
      begin
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_Glide A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID ');
          sql.Add('where A.ProcessName=''' + Trim(CDS_1.fieldbyname('C_Technology').AsString) + ''' ');
          sql.Add('AND A.ProcessTypeName=''' + Trim(CDS_1.fieldbyname('C_TechnologyType').AsString) + ''' ');
          sql.Add('AND A.PatternNo=''' + Trim(CDS_1.fieldbyname('C_Code').AsString) + ''' ');
          open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          Application.MessageBox('花型编号在产品管理中已存在不能删除,！', '提示', 0);
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('delete Bs_Pattern_Info where BCIID=''' + Trim(CDS_1.fieldbyname('BCIID').AsString) + '''');
          ExecSQL;
        end;
      end;
      CDS_1.Delete;
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败！', '提示', 0);
  end;

  TBRafresh.Click;

end;

procedure TfrmPatternInfo.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(self.Caption + 'TV1', Tv1, '样品管理');

//  pnlreport.Top := FTop + 110;
  application.ProcessMessages;

  RecordsNumber := 500;
  CurrentPage := 1;
  SetStatus();
  InitHZ();
  InitGY();
  InitGrid('');

end;

procedure TfrmPatternInfo.cbbHXChange(Sender: TObject);
begin
  InitImage();

end;

procedure TfrmPatternInfo.cxDBTreeList1DblClick(Sender: TObject);
begin
  InitGrid('');
end;

procedure TfrmPatternInfo.TBRafreshClick(Sender: TObject);
begin
  initHZ();
  InitGY();
  InitGrid('');
end;

procedure TfrmPatternInfo.TBFilterClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;

end;

procedure TfrmPatternInfo.CYNoChange(Sender: TObject);
begin
  //if Length(Trim(TEdit(Sender).Text))<4 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;

end;

procedure TfrmPatternInfo.TBExportClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxGrid1);
end;

procedure TfrmPatternInfo.TBUPClick(Sender: TObject);
var
  ffbciid: string;
begin
  try
    ffbciid := Trim(Self.CDS_1.fieldbyname('BCIID').AsString);
    frmCloInfoFileUp := TfrmCloInfoFileUp.Create(Application);
    with frmCloInfoFileUp do
    begin
      Code.Text := Trim(Self.CDS_1.fieldbyname('C_Code').AsString);
      FBCIID := Trim(Self.CDS_1.fieldbyname('BCIID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmCloInfoFileUp.Free;
  end;
  initgrid('');
  Self.CDS_1.Locate('BCIID', ffbciid, []);
  InitImage();
end;

procedure TfrmPatternInfo.ReadINIFile();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmPatternInfo.InitImage();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin

  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_1.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,A.FileName from TP_File A  ');
      sql.add('where  A.WBID=' + quotedstr(trim(CDS_1.fieldbyname('BCIID').AsString)));

      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmPatternInfo.C_CodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    CurrentPage := 1;
    InitGrid(Trim(TEdit(Sender).name));
  end;
end;

procedure TfrmPatternInfo.TBAddClick(Sender: TObject);
begin
  if CDS_3.IsEmpty then
  begin
    Application.MessageBox('工艺类型不能为空！', '提示', 0);
    Exit;
  end;

  with CDS_1 do
  begin
    Append;
    FieldByName('FILLTIME').Value := SGetServerDate(ADOQueryTemp);
    FieldByName('FILLer').Value := Trim(DName);
    FieldByName('IsImg').AsBoolean := False;

    FieldByName('C_Technology').AsString := cds_2.FieldByName('C_Technology').AsString;
    FieldByName('C_TechnologyType').AsString := cds_3.FieldByName('C_Technology').AsString;
    Post;
  end;

  self.SaveData();

end;

function TfrmPatternInfo.SaveData(): Boolean;
var
  maxId, CRID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if CDS_1.FieldByName('BCIID').AsString = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'BPI', 'Bs_Pattern_Info', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxId := CDS_1.FieldByName('BCIID').AsString;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Bs_Pattern_Info where BCIID=''' + trim(maxId) + ''' ');
      Open;
    end;

    with ADOQueryCmd do
    begin
      if ADOQueryCmd.IsEmpty then
      begin
        Append;
        FieldByName('BCIID').Value := Trim(maxId);
        FieldByName('Filler').Value := cds_1.FieldByName('Filler').AsString;
//        FieldByName('FillTime').Value := cds_1.FieldByName('FillTime').Value;
      end
      else
      begin
        FieldByName('editer').Value := trim(DName);
        FieldByName('edittime').Value := SGetServerDate(ADOQueryTemp);
      end;
      RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Bs_Pattern_Info', 1);

      FieldByName('C_Technology').Value := cds_1.FieldByName('C_Technology').Value;
      FieldByName('C_TechnologyType').Value := cds_1.FieldByName('C_TechnologyType').Value;
      Post;
    end;
    with CDS_1 do
    begin
      Edit;
      FieldByName('BCIID').Value := Trim(maxId);
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!', '提示', 0);
  end;
end;

procedure TfrmPatternInfo.TextEdit(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;

  end;
  if mvalue = '' then
    mvalue := '0';

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update BS_Cloth_Info set  ' + FFieldName + '=' + (Trim(mvalue)));
    sql.Add('where   BCIID=' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString)));
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPatternInfo.ToolButton1Click(Sender: TObject);
begin
  inherited;
  WriteCxGrid(self.Caption + 'TV1', Tv1, '样品管理');
end;

procedure TfrmPatternInfo.ToolButton2Click(Sender: TObject);
var
  WSql: string;
  fPrintFile, fPrintFile10, FMainID, LBName: string;
begin
  if CDS_1.IsEmpty then
    Exit;

  LBName := '样品详情';
  ExportFtErpFile(LBName + '.rmf', ADOQueryTemp);

  Tv1.OnFocusedRecordChanged := nil;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := '';
  CDS_1.DisableControls;

  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString));
        end
        else
        begin
          WSql := quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString));
        end;
      end;
      Next;
    end;
  end;

  CDS_1.Locate('SSel', True, []);

  CDS_1.EnableControls;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_BS_PatInfo_PrtImg @Filtration=' + quotedstr(Trim(WSql)));
    Open;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + LBName + '.rmf';
  if FileExists(fPrintFile) then
  begin

    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;

    RM1.ShowReport
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmPatternInfo.ToolButton3Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  Tv1.OnFocusedRecordChanged := nil;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := '';
  CDS_1.DisableControls;

  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString));
        end
        else
        begin
          WSql := quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString));
        end;
      end;
      Next;
    end;
  end;

  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'PatternInfoPrint';
      FFiltration1 := WSql;

//      label3.Visible := true;
//      label4.Visible := true;
//
//      inputcode.visible := True;
//      inputnote.visible := true;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmPatternInfo.ToolButton4Click(Sender: TObject);
begin
  inherited;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Bs_Pattern_Info where BCIID =''' + cds_1.FieldByName('BCIID').AsString + ''' ');
    open;
  end;

  with CDS_1 do
  begin
    Append;
    FieldByName('FILLTIME').Value := SGetServerDate(ADOQueryTemp);
    FieldByName('FILLer').Value := Trim(DName);
    FieldByName('IsImg').AsBoolean := False;

    FieldByName('C_Technology').AsString := cds_2.FieldByName('C_Technology').AsString;
    FieldByName('C_TechnologyType').AsString := cds_3.FieldByName('C_Technology').AsString;
    FieldByName('C_Name').AsString := ADOQueryCmd.FieldByName('C_Name').AsString;
    FieldByName('C_Source').AsString := ADOQueryCmd.FieldByName('C_Source').AsString;
    FieldByName('C_TechnologyNote').AsString := ADOQueryCmd.FieldByName('C_TechnologyNote').AsString;
    FieldByName('Price').Value := ADOQueryCmd.FieldByName('Price').Value;
    FieldByName('CINote').AsString := ADOQueryCmd.FieldByName('CINote').AsString;

    Post;
  end;

  self.SaveData();
end;

procedure TfrmPatternInfo.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  InitImage();
end;

procedure TfrmPatternInfo.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  FTechnology: string;
begin
  inherited;

  FTechnology := cds_2.FieldByName('C_Technology').asstring;

  try
    frmDictSel := TfrmDictSel.Create(Application);
    with frmDictSel do
    begin
      flag := 'GYName';
      flagname := '工序名称';
      f1note := true;
      V1Note.Caption := '工序编号';

      f2note := true;
      V2Note.Caption := '工序编号';

      cxgrid2.Visible := false;
      tv1.OnCellDblClick := TV1CellDblClick;
      if ShowModal = 1 then
      begin
        with cds_1 do
        begin
          edit;
          FieldByName('C_Technology').Value := Trim(ClientDataSet1.fieldbyname('DictName').AsString);
          post;
        end;
      end;
    end;
  finally
    frmDictSel.Free;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update Bs_Pattern_Info set  C_Technology =' + QuotedStr(Trim(CDS_1.fieldbyname('C_Technology').AsString)));
        sql.Add(',editer=''' + trim(DName) + ''' ');
        sql.Add(',edittime=getdate()');
        sql.Add('where   BCIID=' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString)));
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
    end;
    Tv1.Controller.EditingController.ShowEdit();
  end;
  InitHZ();
  cds_2.Locate('C_Technology', FTechnology, []);
end;

procedure TfrmPatternInfo.Tv1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;

  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update Bs_Pattern_Info set  ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
      sql.Add(',editer=''' + trim(DName) + ''' ');
      sql.Add(',edittime=getdate()');
      sql.Add('where   BCIID=' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString)));
      ExecSQL;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from Bs_Pattern_Info where C_Code=''' + Trim(CDS_1.fieldbyname('C_Code').AsString) + '''');
      sql.add(' and C_Technology=''' + Trim(CDS_1.fieldbyname('C_Technology').AsString) + ''' ');
      sql.add(' and C_TechnologyType=''' + Trim(CDS_1.fieldbyname('C_TechnologyType').AsString) + ''' ');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('同工艺类型下花型编号重复！', '提示', 0);
        TBRafresh.Click;
        Exit;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPatternInfo.TV2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  InitGY();
  initgrid('');
  InitImage();
end;

procedure TfrmPatternInfo.TV3CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  initgrid('');
  InitImage();
end;

procedure TfrmPatternInfo.v1CYNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from BS_Cloth_Glide A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID ');
    sql.Add('where A.ProcessName=''' + Trim(CDS_1.fieldbyname('C_Technology').AsString) + ''' ');
    sql.Add('AND A.ProcessTypeName=''' + Trim(CDS_1.fieldbyname('C_TechnologyType').AsString) + ''' ');
    sql.Add('AND A.PatternNo=(select C_Code from Bs_Pattern_Info X where X.BCIID=' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString)) + ' ) ');
    open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('花型编号在产品管理中已存在不能修改,！', '提示', 0);
    initgrid('');
    InitImage();
    Exit;
  end;

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;

  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update Bs_Pattern_Info set  ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
      sql.Add(',editer=''' + trim(DName) + ''' ');
      sql.Add(',edittime=getdate()');
      sql.Add('where   BCIID=' + quotedstr(Trim(CDS_1.fieldbyname('BCIID').AsString)));
      ExecSQL;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from Bs_Pattern_Info where C_Code=''' + Trim(CDS_1.fieldbyname('C_Code').AsString) + '''');
      sql.add(' and C_Technology=''' + Trim(CDS_1.fieldbyname('C_Technology').AsString) + ''' ');
      sql.add(' and C_TechnologyType=''' + Trim(CDS_1.fieldbyname('C_TechnologyType').AsString) + ''' ');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('同工艺类型下花型编号重复！', '提示', 0);
        TBRafresh.Click;
        Exit;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPatternInfo.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True);
end;

procedure TfrmPatternInfo.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False);
end;

procedure TfrmPatternInfo.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmPatternInfo.TCBNOR1Change(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR1.Text);
  CurrentPage := 1;
  C_Code.SetFocus;
  InitGrid('');
end;

procedure TfrmPatternInfo.HXNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';

end;

procedure TfrmPatternInfo.HXNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'HX' + Trim(Self.CDS_1.fieldbyname('CYNO').AsString);
      flagname := '花型';
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPatternInfo.Button1Click(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid('');
end;

procedure TfrmPatternInfo.Button2Click(Sender: TObject);
begin
  if CurrentPage < CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid('');
end;

end.

