unit U_CPType_TJHXMX;

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, ExtCtrls, cxContainer, cxEdit, cxImage, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdFTP,jpeg,IniFiles,ShellAPI,
  cxFilter, cxData, cxDataStorage, cxDBData, cxTextEdit, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, DBClient, Menus;

type
  TfrmCPType_TJHXMX = class(TForm)
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Panel2: TPanel;
    Panel1: TPanel;
    SaveDialog1: TSaveDialog;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    Panel16: TPanel;
    Label3: TLabel;
    YW: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    MXJGFactoryName: TEdit;
    MXJGFactoryNameHX: TEdit;
    MXSeHao: TEdit;
    MXTJPrice: TEdit;
    Button1: TButton;
    Label2: TLabel;
    MXTJNote: TEdit;
    cxImage1: TcxImage;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1MXJGFactoryName: TcxGridDBColumn;
    V1MXJGFactoryNameHX: TcxGridDBColumn;
    V1MXSeHao: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel3: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    filename1: TLabel;
    Label1: TLabel;
    V1MXTJPrice: TcxGridDBColumn;
    V1MXTJNote: TcxGridDBColumn;
    DataSource2: TDataSource;
    Ord_TJHX: TClientDataSet;
    Panel4: TPanel;
    Button2: TButton;
    Button3: TButton;
    V1ZCType: TcxGridDBColumn;
    Label7: TLabel;
    Panel5: TPanel;
    Label10: TLabel;
    Button4: TButton;
    Label11: TLabel;
    Label12: TLabel;
    MXColID: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxImage1DblClick(Sender: TObject);
    procedure TV1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure MXJGFactoryNameKeyPress(Sender: TObject; var Key: Char);
    procedure MXJGFactoryNameHXKeyPress(Sender: TObject; var Key: Char);
    procedure MXSeHaoKeyPress(Sender: TObject; var Key: Char);
    procedure MXTJPriceKeyPress(Sender: TObject; var Key: Char);
    procedure Button4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TV1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure V1MXTJNoteCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure MXTJNoteKeyPress(Sender: TObject; var Key: Char);
    procedure Button1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID,FMXID:String;
    lstPat: TStringList;
    procedure InitTree();
    procedure CSHLB();

  public
    FileName:string;
    FSHID,FMXSKID,FMXCPID,FMXXFID,FMXWBID:string;
    { Public declarations }
  end;

var
  frmCPType_TJHXMX: TfrmCPType_TJHXMX;

implementation
uses
  U_DataLink,U_Fun,U_BPZDY_Panel_M;

{$R *.dfm}

procedure TfrmCPType_TJHXMX.InitTree();
var jpg:TJpegImage;
    myStream: TADOBlobStream;
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,B.WJName,C.* from TJ_Base C ');
    sql.Add(' inner join XD_File B on B.XFID=C.XFID');
    SQL.Add(' inner join TP_File A on B.XFID=A.TFID');
    sql.Add(' where C.XFID='''+trim(Self.Name)+'''');
    sql.Add(' order by cast(C.ColorNo as int)');
    Open;
  end;
  filename1.Caption:=Trim(ADOQueryTree.fieldbyname('WJName').AsString);
  jpg:=TJpegImage.Create();
  myStream:=tadoblobstream.Create(tblobfield(ADOQueryTree.fieldbyname('FilesOther')),bmread);
  jpg.LoadFromStream(myStream);
  //jpg.free;
  cxImage1.Picture.Assign(jpg);
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,B.WJName,C.*,D.*,E.*,F.* from TJ_Base C ');
    sql.Add(' inner join XD_File B on B.XFID=C.XFID');
    SQL.Add(' inner join TP_File A on B.XFID=A.TFID');
    SQL.Add(' inner join TJ_Base_MX D on D.SHID=C.SHID');
    sql.Add(' left join BP_ColZDY_M E on E.ColID=D.MXColID');
    SQL.Add(' left join RT_PTColor_M F on F.PTID=E.PTID');
    sql.Add(' where C.XFID='''+trim(Self.Name)+'''');
    sql.Add(' order by cast(C.ColorNo as int)');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Ord_TJHX);
  SInitCDSData20(ADOQueryTree,Ord_TJHX);
end;

procedure TfrmCPType_TJHXMX.FormDestroy(Sender: TObject);
begin
  frmCPType_TJHXMX:=nil;
end;

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

procedure TfrmCPType_TJHXMX.CSHLB();
begin
  
end;
procedure TfrmCPType_TJHXMX.FormShow(Sender: TObject);
begin
  InitTree();
end;

procedure TfrmCPType_TJHXMX.Button1Click(Sender: TObject);
var maxno:string;
begin
  if application.MessageBox('确定要保存吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from TJ_Base_MX ');
    sql.Add('where MXID='''+trim(FMXID)+'''');
    open;
  end;
  with ADOQueryCmd do
  begin
    if IsEmpty then
    begin
      append;
      if GetLSNo(ADOQueryTemp,maxno,'MX','TJ_Base_MX',4,1)=False then
      begin
        Application.MessageBox('取ID最大号失败！','提示',0);
        Exit;
      end;
      FieldByName('MXID').Value:=Trim(maxno);
    end
    else
    begin
      edit;
      maxno:=Trim(ADOQueryCmd.fieldbyname('MXID').AsString);
    end;
    FieldByName('MXJGFactoryName').Value:=Trim(MXJGFactoryName.Text);
    FieldByName('MXJGFactoryNameHX').Value:=Trim(MXJGFactoryNameHX.Text);
    FieldByName('MXSeHao').Value:=Trim(MXSeHao.Text);
    FieldByName('MXTJPrice').Value:=Trim(MXTJPrice.Text);
    FieldByName('MXTJNote').Value:=Trim(MXTJNote.Text);
    FieldByName('SHID').Value:=Trim(FSHID);
    FieldByName('MXColID').Value:=Trim(MXColID.Text);
    FieldByName('MXSKID').Value:=Trim(FMXSKID);
    FieldByName('MXXFID').Value:=Trim(FMXXFID);
    post;
  end;
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,B.WJName,D.*,C.*,E.*,F.* from TJ_Base C ');
    sql.Add(' inner join XD_File B on B.XFID=C.XFID');
    SQL.Add(' inner join TP_File A on B.XFID=A.TFID');
    SQL.Add(' inner join TJ_Base_MX D on D.SHID=C.SHID');
    sql.Add(' left join BP_ColZDY_M E on E.ColID=D.MXColID');
    SQL.Add(' left join RT_PTColor_M F on F.PTID=E.PTID');
    sql.Add(' where C.XFID='''+trim(Self.Name)+'''');
    sql.Add(' order by cast(C.ColorNo as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Ord_TJHX);
  SInitCDSData20(ADOQueryTree,Ord_TJHX);
  Ord_TJHX.Locate('SHID',FSHID,[]);
  Button2.Click;
  //ModalResult:=1;
end;

procedure TfrmCPType_TJHXMX.cxImage1DblClick(Sender: TObject);
var
  IdFTP1: TIdFTP;
  FPath,FFName:string;
  FInt:integer;
  maxno:string;
  j,i:Integer;
  fPrintFile,fFileName:string;
begin
  if Application.MessageBox('原图下载需要等待时间，确定要查看原图吗？','提示',32+4)<>IDYES then Exit;
  try
    IdFTP1:=TIdFTP.Create(self);
    IdFTP1.Host :=PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    IdFTP1.Free;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  Panel16.Visible:=true;
  Panel16.Refresh;
  fPrintFile:= ExtractFilePath(Application.ExeName)+'Photo\';
  if not DirectoryExists(ExtractFileDir(FPrintFile)) then
    CreateDir(ExtractFileDir(fPrintFile));
  FFName:=Trim(FileName);
  FFName:=fPrintFile+FFName;
  if not FileExists(FFName) then
  begin
    IdFTP1.Get('YP\'+FileName,FFName);
  end;
  if IdFTP1.Connected then
  begin
    IdFTP1.Quit;
    IdFTP1.Free;
  end;
  Panel16.Visible:=false;
  ShellExecute(Handle, 'open',PChar(FFName),'', '', SW_SHOWNORMAL);
end;

procedure TfrmCPType_TJHXMX.TV1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  MXJGFactoryName.Text:=Trim(Ord_TJHX.fieldbyname('MXJGFactoryName').AsString);
  MXJGFactoryNameHX.Text:=Trim(Ord_TJHX.fieldbyname('MXJGFactoryNameHX').AsString);
  MXSeHao.Text:=Trim(Ord_TJHX.fieldbyname('MXSeHao').AsString);
  MXTJPrice.Text:=Trim(Ord_TJHX.fieldbyname('MXTJPrice').AsString);
  MXTJNote.Text:=Trim(Ord_TJHX.fieldbyname('MXTJNote').AsString);
  self.MXColID.Text:=Trim(Ord_TJHX.FieldByName('ColID').AsString);
  self.Label11.Caption:=Trim(Ord_TJHX.FieldByName('ColNo').AsString);
  self.Label12.Caption:=Trim(Ord_TJHX.FieldByName('ColNameKH').AsString);
  if Ord_TJHX.FieldByName('ColDaiMa').AsString<>'' then
  self.Panel5.Color:=StrToInt('$'+Trim(Ord_TJHX.FieldByName('ColDaiMa').AsString));
  FMXID:=Trim(Ord_TJHX.fieldbyname('MXID').AsString);
  Button1.Caption:='修改保存';
end;

procedure TfrmCPType_TJHXMX.Button2Click(Sender: TObject);
begin
  MXJGFactoryName.Text:='';
  MXJGFactoryNameHX.Text:='';
  MXSeHao.Text:='';
  MXTJPrice.Text:='';
  MXTJNote.Text:='';
  self.MXColID.Text:='';
  self.Label11.Caption:='';
  self.Label12.Caption:='';
  self.Panel5.Color:=clBtnFace;
  FMXID:='';
  Button1.Caption:='新增保存';
end;

procedure TfrmCPType_TJHXMX.Button3Click(Sender: TObject);
begin
  if Application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from TJ_Base_MX ');
    sql.Add('where MXID='''+trim(FMXID)+'''');
    ExecSQL;
  end;
  Ord_TJHX.Delete;
  Button2.Click;
end;

procedure TfrmCPType_TJHXMX.MXJGFactoryNameKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    MXJGFactoryNameHX.SetFocus;
  end;
end;

procedure TfrmCPType_TJHXMX.MXJGFactoryNameHXKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    MXSeHao.SetFocus;
  end;
end;

procedure TfrmCPType_TJHXMX.MXSeHaoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    MXTJPrice.SetFocus;
  end;
end;

procedure TfrmCPType_TJHXMX.MXTJPriceKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    MXTJNote.SetFocus;
  end;
end;

procedure TfrmCPType_TJHXMX.Button4Click(Sender: TObject);
begin
  try
    frmBPZDY_Panel_M:=TfrmBPZDY_Panel_M.Create(Application);
    with frmBPZDY_Panel_M do
    begin
      formid:='1';
      ScrollBox4.Visible:=false;
      if ShowModal=1 then
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('delete from BP_ColZDY_M ');
          sql.Add('where ColID='''+trim(self.MXColID.Text)+'''');
          ExecSQL;
        end;
        self.MXColID.Text:=Trim(Ord_SH.FieldByName('ColID').AsString);
        self.Label11.Caption:=Trim(Ord_SH.FieldByName('ColNo').AsString);
        self.Label12.Caption:=Trim(Ord_SH.FieldByName('ColNameKH').AsString);
        self.Panel5.Color:=StrToInt('$'+Trim(Ord_SH.FieldByName('ColDaiMa').AsString));
      end;
    end;
  finally
    frmBPZDY_Panel_M.Free;
  end;
end;

procedure TfrmCPType_TJHXMX.N1Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update TJ_Base_MX set ZCType='''' ');
    sql.add(' where SHID='''+trim(Ord_TJHX.fieldbyname('SHID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update TJ_Base_MX set ZCType=''主'' ');
    sql.add(' where MXID='''+trim(Ord_TJHX.fieldbyname('MXID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,B.WJName,D.*,C.*,E.*,F.* from TJ_Base C ');
    sql.Add(' inner join XD_File B on B.XFID=C.XFID');
    SQL.Add(' inner join TP_File A on B.XFID=A.TFID');
    SQL.Add(' inner join TJ_Base_MX D on D.SHID=C.SHID');
    sql.Add(' left join BP_ColZDY_M E on E.ColID=D.MXColID');
    SQL.Add(' left join RT_PTColor_M F on F.PTID=E.PTID');
    sql.Add(' where C.XFID='''+trim(Self.Name)+'''');
    sql.Add(' order by cast(C.ColorNo as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Ord_TJHX);
  SInitCDSData20(ADOQueryTree,Ord_TJHX);
  button2.Click;
end;

procedure TfrmCPType_TJHXMX.TV1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id:Integer;
begin
  Id:=Tv1.GetColumnByFieldName('ZCType').Index;
  if ARecord.Values[id]='主' then
    AStyle:=DataLink_YPGLBOM.clMenu
  else
   AStyle:=DataLink_YPGLBOM.SkyBlue;
end;

procedure TfrmCPType_TJHXMX.V1MXTJNoteCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var Id:Integer;
begin
  Id:=TV1.GetColumnByFieldName('ZCType').Index;
  if AViewInfo.GridRecord.Values[Id]='' then Exit;
  if AViewInfo.GridRecord.Values[Id]='主' then
  begin
    ACanvas.Brush.Color:=clMenu;
  end;
end;

procedure TfrmCPType_TJHXMX.MXTJNoteKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    Button1.SetFocus;
  end;
end;

procedure TfrmCPType_TJHXMX.Button1KeyPress(Sender: TObject;
  var Key: Char);
var maxno:string;
begin
  if Key=#13 then
  begin
    if application.MessageBox('确定要保存吗','提示',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from TJ_Base_MX ');
      sql.Add('where MXID='''+trim(FMXID)+'''');
      open;
    end;
    with ADOQueryCmd do
    begin
      if IsEmpty then
      begin
        append;
        if GetLSNo(ADOQueryTemp,maxno,'MX','TJ_Base_MX',4,1)=False then
        begin
          Application.MessageBox('取ID最大号失败！','提示',0);
          Exit;
        end;
        FieldByName('MXID').Value:=Trim(maxno);
      end
      else
      begin
        edit;
        maxno:=Trim(ADOQueryCmd.fieldbyname('MXID').AsString);
      end;
      FieldByName('MXJGFactoryName').Value:=Trim(MXJGFactoryName.Text);
      FieldByName('MXJGFactoryNameHX').Value:=Trim(MXJGFactoryNameHX.Text);
      FieldByName('MXSeHao').Value:=Trim(MXSeHao.Text);
      FieldByName('MXTJPrice').Value:=Trim(MXTJPrice.Text);
      FieldByName('MXTJNote').Value:=Trim(MXTJNote.Text);
      FieldByName('SHID').Value:=Trim(FSHID);
      FieldByName('MXColID').Value:=Trim(MXColID.Text);
      FieldByName('MXSKID').Value:=Trim(FMXSKID);
      FieldByName('MXXFID').Value:=Trim(FMXXFID);
      post;
    end;
    with ADOQueryTree do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName,B.WJName,D.*,C.*,E.*,F.* from TJ_Base C ');
      sql.Add(' inner join XD_File B on B.XFID=C.XFID');
      SQL.Add(' inner join TP_File A on B.XFID=A.TFID');
      SQL.Add(' inner join TJ_Base_MX D on D.SHID=C.SHID');
      sql.Add(' left join BP_ColZDY_M E on E.ColID=D.MXColID');
      SQL.Add(' left join RT_PTColor_M F on F.PTID=E.PTID');
      sql.Add(' where C.XFID='''+trim(Self.Name)+'''');
      sql.Add(' order by cast(C.ColorNo as int)');
      Open;
    end;
    SCreateCDS20(ADOQueryTree,Ord_TJHX);
    SInitCDSData20(ADOQueryTree,Ord_TJHX);
    Ord_TJHX.Locate('SHID',FSHID,[]);
    Button2.Click;
  end;
end;

end.
