unit U_FLManageSEL;

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, dxSkinscxPCPainter;

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

  TfrmflManageSEL = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree20: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Panel1: TPanel;
    Label2: TLabel;
    CYID: TEdit;
    TBFilter: TToolButton;
    v1CYNo: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Label3: TLabel;
    CYNO: TEdit;
    Panel2: TPanel;
    v1Column19: TcxGridDBColumn;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    ADOQueryTree: TClientDataSet;
    Label11: TLabel;
    Label12: TLabel;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    v1Column39: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label4: TLabel;
    CYEName: TEdit;
    v1Column6: TcxGridDBColumn;
    Label1: TLabel;
    CYColor: TEdit;
    Label6: TLabel;
    CYCF: TEdit;
    v1Column7: TcxGridDBColumn;
    OpenDialog1: TOpenDialog;
    v1Column10: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    cxgrdCYCNHS: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    RMBarCodeObject1: TRMBarCodeObject;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    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 CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBImportClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    CurrentPage, RecordsNumber: Integer;
    CPID: string;
    PState: Integer;
    FCPID, FTopID: string;
    FJurisdiction: string;
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    { Public declarations }
  end;

var
  Mach: array of TfrmSlt;

var
  frmflManageSEL: TfrmflManageSEL;

implementation

uses
  U_DataLink, U_Fun, StrUtils;

{$R *.dfm}

procedure TfrmflManageSEL.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree20 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree20, ADOQueryTree);
  SInitCDSData20(ADOQueryTree20, ADOQueryTree);

  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmflManageSEL.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Panel2.Visible := True;
  Panel2.Refresh;

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

  if trim(Pwhere) <> '' then
  begin

    fwhere := fwhere + ' and ' + trim(Pwhere)

  end;
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' select *  ');
      sql.Add(' ,CYNoLX=[dbo].[F_FL_Get_LX](CYNo)   ');
      sql.Add('  ,IsImg=cast((case when (select COUNT(T.CYID) from XD_File T where T.FileType = ''YP'' and T.CYID=A.CYID)=0 then 0 else 1 end) as bit )  ');
      sql.Add('   from fl_ydang A where 1=1 ');
      sql.Add(fwhere);

//     ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
end;
//  Panel2.Visible := True;
//  Panel2.Refresh;
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      SQL.Clear;
//      SQL.Add('exec P_Select_CP_YDang :Code,:PState,:CYType');
//      Parameters.ParamByName('Code').Value := '';
//      Parameters.ParamByName('PState').Value := 0;
//      Parameters.ParamByName('CYType').Value := Trim(ADOQueryTree.fieldbyname('CPID').AsString);
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, CDS_Main);
//    SInitCDSData20(ADOQueryMain, CDS_Main);
//  finally
//    ADOQueryMain.EnableControls;
//  end;
//  Panel2.Visible := False;
//end;

procedure TfrmflManageSEL.FormDestroy(Sender: TObject);
begin
  //frmCPManage:=nil;
end;

procedure TfrmflManageSEL.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
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);
//  writeCxGrid(self.Caption+tv1.Name,tv1);
 // Application := mainApplication;
  Action := cafree;
end;

procedure TfrmflManageSEL.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
  if DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209', sw_hide);
  Close;
end;

procedure TfrmflManageSEL.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  while CDS_Main.Locate('SSel', true, []) do
  begin
    if Trim(CDS_Main.fieldbyname('CYID').AsString) <> '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete FL_YDang where CYID=''' + Trim(CDS_Main.fieldbyname('CYID').AsString) + '''');
        ExecSQL;
      end;
    end;
    CDS_Main.Delete;
  end;

end;

procedure TfrmflManageSEL.FormShow(Sender: TObject);
begin
  Panel3.Align := alclient;

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

  InitGrid();

end;

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

procedure TfrmflManageSEL.TBRafreshClick(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmflManageSEL.TBFilterClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

end;

procedure TfrmflManageSEL.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmflManageSEL.CYNoChange(Sender: TObject);
begin
  //if Length(Trim(TEdit(Sender).Text))<4 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

end;

procedure TfrmflManageSEL.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel('辅料档案', cxGrid1);
end;

procedure TfrmflManageSEL.TBImportClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3: string;
begin
  FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
  FCPName := Trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);

  if GetLSNo(ADOQueryCmd, maxId, 'Y', 'FL_YDang', 2, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;

  if trim(FCPID) = '' then
    exit;
  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;

    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    k := 0;
    for i := 1 to 20 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        break;
      if trim(excelApp.Cells[1, i].value) = '类型' then
      begin
        dFdDy[k].inc := i;
        LX := i;
        dFdDy[k].FDdys := 'CPName';
        dFdDy[k].FdDysName := '类型';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品编号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYNo';
        dFdDy[k].FdDysName := '产品编号';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYName';
        dFdDy[k].FdDysName := '产品名称';
      end;
      if trim(excelApp.Cells[1, i].value) = '原料成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYSpec';
        dFdDy[k].FdDysName := '原料成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCF';
        dFdDy[k].FdDysName := '成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '门幅' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYMF';
        dFdDy[k].FdDysName := '门幅';
      end;
      if trim(excelApp.Cells[1, i].value) = '克重' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYKZ';
        dFdDy[k].FdDysName := '克重';
      end;
      if trim(excelApp.Cells[1, i].value) = '花型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYHX';
        dFdDy[k].FdDysName := '花型';
      end;

      if trim(excelApp.Cells[1, i].value) = '颜色' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYColor';
        dFdDy[k].FdDysName := '颜色';
      end;

      if trim(excelApp.Cells[1, i].value) = '间距' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZhenXing';
        dFdDy[k].FdDysName := '间距';
      end;

      if trim(excelApp.Cells[1, i].value) = '机型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CarType';
        dFdDy[k].FdDysName := '机型';
      end;
      if trim(excelApp.Cells[1, i].value) = '产量' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCL';
        dFdDy[k].FdDysName := '产量';
      end;

      if trim(excelApp.Cells[1, i].value) = '纱长' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ShaChang';
        dFdDy[k].FdDysName := '纱长';
      end;

      if trim(excelApp.Cells[1, i].value) = '备注' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYNote';
        dFdDy[k].FdDysName := '备注';
      end;
      k := k + 1;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    for i := 2 to ExcelRowCount do
    begin

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from FL_YDang where 1=2');
        open;
      end;
      if trim(excelApp.Cells[i, LX].value) <> trim(FCPName) then
        continue;
      ADOQueryCmd.Append;
      for j := 0 to k - 1 do
      begin
        if trim(dFdDy[j].FDdys) = '' then
          continue;
        t1 := dFdDy[j].FDdys;
        t2 := inttostr(dFdDy[j].inc);
        t3 := excelApp.Cells[i, dFdDy[j].inc].Value;
        if trim(dFdDy[j].FDdys) <> 'CPName' then
          ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;
      ADOQueryCmd.FieldByName('CYValid').Value := 'Y';
      ADOQueryCmd.FieldByName('CYType').Value := Trim(FCPID);
      ADOQueryCmd.FieldByName('CYID').Value := maxId + inttostr(i);
      ADOQueryCmd.Post;

    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存数据错误！', '错误信息', MB_ICONERROR);
    exit;
  end;
end;

procedure TfrmflManageSEL.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 TfrmflManageSEL.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then
    Exit;
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYID='''+Trim(CDS_Main.fieldbyname('CYID').asstring)+'''');
    Open;
  end;
  CPID:=Trim(ADOQueryTemp.fieldbyname('CYType').AsString);
  ADOQueryTree.Locate('CPID',CPID,[]);  }
  InitImage();
end;

procedure TfrmflManageSEL.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_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName from TP_File A,XD_File B ');
      sql.add('where A.TFID =B.XFID and A.WBID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').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 TfrmflManageSEL.CYNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmflManageSEL.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmflManageSEL.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

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

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

procedure TfrmflManageSEL.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

end.

