unit U_CPManage_BJ;

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;

type
  TfrmCPManage_BJ = 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;
    ToolButton2: TToolButton;
    Label1: TLabel;
    CYName: TEdit;
    Label4: TLabel;
    CYEName: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    CYNO: TEdit;
    v1Column16: TcxGridDBColumn;
    Panel2: TPanel;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    Label7: TLabel;
    CYKZ: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    CYCF: TEdit;
    CYECF: TEdit;
    Label10: TLabel;
    CYMF: TEdit;
    v1Column42: TcxGridDBColumn;
    ADOQueryTree: TClientDataSet;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    defstr1: TEdit;
    Label13: TLabel;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    DefStr2: TEdit;
    Label14: TLabel;
    defnote2: TEdit;
    Label15: TLabel;
    defnote3: TEdit;
    Label16: TLabel;
    ToolButton1: TToolButton;
    ToolButton5: TToolButton;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Panel3: TPanel;
    Image2: TImage;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton7Click(Sender: TObject);
    procedure CYSpecChange(Sender: TObject);
    procedure v1Column19PropertiesChange(Sender: TObject);
    procedure CYMFChange(Sender: TObject);
    procedure CYNameChange(Sender: TObject);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton5Click(Sender: TObject);
    procedure CYSpecKeyPress(Sender: TObject; var Key: Char);
    procedure CYNameKeyPress(Sender: TObject; var Key: Char);
    procedure defstr1KeyPress(Sender: TObject; var Key: Char);
    procedure CYCFKeyPress(Sender: TObject; var Key: Char);
    procedure Panel3DblClick(Sender: TObject);
  private
    { Private declarations }
    CPID:string;
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
    procedure TJGS();

  public
    { Public declarations }
  end;

var
  frmCPManage_BJ: TfrmCPManage_BJ;

implementation
uses
  U_DataLink,U_Fun,U_FileUp;

{$R *.dfm}

procedure TfrmCPManage_BJ.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[0].Expand(false);}
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPManage_BJ.InitGrid();
begin
  Panel2.Visible:=True;
  Panel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select A.*,B.CPName,TP.WBID,TP.FilesOther from CP_YDang A inner join CP_Type B On A.CYType=B.CPID ');
      sql.Add('left join TP_File TP on TP.WBID=A.CYID ');
      sql.add('where 1=1');
      if CYNO.Text<>'' then
      sql.add(' and A.CYNO like '''+'%'+trim(CYNO.Text)+'%'+'''');
      if CYSpec.Text<>'' then
      sql.add(' and A.CYSpec like '''+'%'+trim(CYSpec.Text)+'%'+'''');
      if CYName.Text<>'' then
      sql.add(' and A.CYName like '''+'%'+trim(CYName.Text)+'%'+'''');
      if defstr1.Text<>'' then
      sql.add(' and A.defstr1 like '''+'%'+trim(defstr1.Text)+'%'+'''');
      if CYCF.Text<>'' then
      sql.add(' and A.CYCF like '''+'%'+trim(CYCF.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible:=False;
end;

procedure TfrmCPManage_BJ.TJGS();
begin
  {i:=0;
  j:=0;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('CYPriceKg').Value=0 then
      begin
        i:=i+1;
      end else
      begin
        j:=j+1;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  Label11.Caption:='定价样品数量:'+Trim(IntToStr(j));
  Label12.Caption:='未定价样品数量:'+Trim(IntToStr(i));}
end;
procedure TfrmCPManage_BJ.FormDestroy(Sender: TObject);
begin
  frmCPManage_BJ:=nil;
end;

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

procedure TfrmCPManage_BJ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品列表YT',Tv1,'样品管理');
  if  DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209',sw_hide);
  Close;
end;
procedure TfrmCPManage_BJ.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_Main.fieldbyname('CYID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete CP_YDang where CYID='''+Trim(CDS_Main.fieldbyname('CYID').AsString)+'''');
      ExecSQL;
    end;
  end;
  
  CDS_Main.Delete;
end;

procedure TfrmCPManage_BJ.FormShow(Sender: TObject);
begin
  ReadCxGrid('样品列表YT',Tv1,'样品管理');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID='''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_CYNO);
  SInitCDSData20(ADOQueryTemp,CDS_CYNO);
    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:='XXXXXXX';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  CDS_CYNO.Delete;
end;

procedure TfrmCPManage_BJ.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCPManage_BJ.ToolButton3Click(Sender: TObject);
var
 i:Integer;
 FieldName:String;
begin
  {ry
    frmCPAdd:=TfrmCPAdd.Create(Application);
    with frmCPAdd do
    begin
      CopyInt:=0;
      FCYID:=Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      FCPID:=Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        Self.CDS_Main.Edit;
        for i:=0 to frmCPAdd.Tv1.ColumnCount-1 do
        begin
          FieldName:=frmCPAdd.Tv1.Columns[i].DataBinding.FilterFieldName;
          if Trim(FieldName)<>'' then
          begin
            Self.CDS_Main.FieldByName(FieldName).Value:=frmCPAdd.CDS_Sub.FieldByName(FieldName).Value;
          end;
        end;
        CDS_Main.FieldByName('CPName').Value:=Trim(ADOQueryTree.fieldbyname('CPName').AsString);
        CDS_Main.FieldByName('CYID').Value:=CDS_Sub.fieldbyname('CYID').Value;
        CDS_Main.FieldByName('CYType').Value:=CDS_Sub.fieldbyname('CYType').Value;
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmCPAdd.Free;
  end;}
end;

procedure TfrmCPManage_BJ.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    //TJGS();
  end;
end;

procedure TfrmCPManage_BJ.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  {f Key=#13 then
  begin
    if Trim(CYID.Text)='' then Exit;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add('exec P_Select_CP_YDang :Code,1,'''' ');
      Parameters.ParamByName('Code').Value:=Trim(CYID.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    CYID.Text:='';
    ADOQueryTree.Locate('CPID',CPID,[]);
  end;}
end;

procedure TfrmCPManage_BJ.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 TfrmCPManage_BJ.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmCPManage_BJ.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 TfrmCPManage_BJ.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 TfrmCPManage_BJ.InitImage();
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  if CDS_Main.IsEmpty then Exit;
  if Trim(CDS_Main.fieldbyname('CYID').AsString)='' then
  begin
    Exit;
  end;
  //if cxPageControl1.ActivePageIndex=6 then
  begin
    //Image2.Picture.Assign(nil);
    try
      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from TP_File A where A.WBID='''+Trim(CDS_Main.fieldbyname('CYID').AsString)+'''');
        sql.Add(' and TFType=''样品'' ');
        open;
        if RecordCount>0 then
        begin
          if trim(ADOQueryTemp.fieldbyname('FilesOther').AsString)<>'' then
          begin
            myStream:=tadoblobstream.Create(tblobfield(ADOQueryTemp.fieldbyname('FilesOther')),bmread);
            if myStream=nil then exit;
            jpg:=TJPEGImage.Create;
            jpg.LoadFromStream(myStream);
            Image2.Picture.Assign(jpg);
          end;
        end
        else
        Image2.Picture.Assign(nil);
      end;
    finally
      jpg.Free;
      myStream.Free;
    end;
  end;
end;

procedure TfrmCPManage_BJ.ToolButton7Click(Sender: TObject);
var
 i:Integer;
 FieldName:String;
begin

  {try
    frmCPAdd:=TfrmCPAdd.Create(Application);
    with frmCPAdd do
    begin
      CopyInt:=1;
      FCYID:=Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      FCPID:=Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        Self.CDS_Main.Append;
        for i:=0 to frmCPAdd.Tv1.ColumnCount-1 do
        begin
          FieldName:=frmCPAdd.Tv1.Columns[i].DataBinding.FilterFieldName;
          if Trim(FieldName)<>'' then
          begin
            Self.CDS_Main.FieldByName(FieldName).Value:=frmCPAdd.CDS_Sub.FieldByName(FieldName).Value;
          end;
        end;
        CDS_Main.FieldByName('CPName').Value:=Trim(ADOQueryTree.fieldbyname('CPName').AsString);
        CDS_Main.FieldByName('CYID').Value:=CDS_Sub.fieldbyname('CYID').Value;
        CDS_Main.FieldByName('CYType').Value:=CDS_Sub.fieldbyname('CYType').Value;
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmCPAdd.Free;
  end;}
end;

procedure TfrmCPManage_BJ.CYSpecChange(Sender: TObject);
begin
  if Length(Trim(CYSpec.Text))<3 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_BJ.v1Column19PropertiesChange(Sender: TObject);
var
  mvalue:Boolean;
begin
 { mvalue:=TcxCheckBox(Sender).EditingValue;
  if mvalue=True then
  begin
    with CDS_CYNO do
    begin
      if Locate('CYID',Trim(CDS_Main.fieldbyname('CYID').AsString),[])=False then
      begin
        Append;
        FieldByName('CYID').Value:=Trim(CDS_Main.fieldbyname('CYID').AsString);
        Post;
      end;
    end;
  end else
  begin
    //with CDS_CYNO do
    //begin
    CDS_CYNO.Locate('CYID',Trim(CDS_Main.fieldbyname('CYID').AsString),[]);
    CDS_CYNO.Delete;
    //end;
  end; }
end;

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

end;

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

end;

procedure TfrmCPManage_BJ.CYNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(CYNO.Text)='' then Exit;
    if Length(Trim(CYNO.Text))<4 then Exit;
    initgrid();
  end;
end;

procedure TfrmCPManage_BJ.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult:=1;  
end;

procedure TfrmCPManage_BJ.ToolButton5Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  try
    frmFileUp:=TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text:=Trim(Self.CDS_Main.fieldbyname('CYNO').AsString);
      CYID:=Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.CDS_Main.Locate('CYID',CYID,[]);
      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmCPManage_BJ.CYSpecKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    Initgrid();
  end;
end;

procedure TfrmCPManage_BJ.CYNameKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    Initgrid();
  end;
end;

procedure TfrmCPManage_BJ.defstr1KeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    Initgrid();
  end;
end;

procedure TfrmCPManage_BJ.CYCFKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    Initgrid();
  end;
end;

procedure TfrmCPManage_BJ.Panel3DblClick(Sender: TObject);
begin
  InitGrid();
  TJGS();
end;

end.
