unit U_YGInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ExtDlgs, cxSpinEdit,
  cxTimeEdit;

type
  TfrmYGInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    Panel3: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    YGName: TEdit;
    YGSex: TComboBox;
    GangWei: TBtnEditA;
    MinZu: TBtnEditA;
    JiGuan: TEdit;
    HunYin: TBtnEditA;
    ChuShengDate: TDateTimePicker;
    MianMao: TBtnEditA;
    XueLi: TBtnEditA;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GroupBox1: TGroupBox;
    Image1: TImage;
    Label1: TLabel;
    ZhiCheng: TBtnEditA;
    Label2: TLabel;
    JSTeChang: TEdit;
    Label13: TLabel;
    YYAiHao: TEdit;
    Label15: TLabel;
    BYXueXiao: TBtnEditA;
    Label17: TLabel;
    ZhuanYe: TBtnEditA;
    Label18: TLabel;
    ZhuZhi: TEdit;
    Label19: TLabel;
    ShenFenNo: TEdit;
    Label20: TLabel;
    JTTel: TEdit;
    Label21: TLabel;
    Phone: TEdit;
    Label22: TLabel;
    EMail: TEdit;
    Label23: TLabel;
    JJLXPerson: TEdit;
    Label24: TLabel;
    JJLXPhone: TEdit;
    Label25: TLabel;
    JJLXGX: TBtnEditA;
    Label27: TLabel;
    YPNote: TRichEdit;
    OpenPictureDialog1: TOpenPictureDialog;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource3: TDataSource;
    BYDate: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    Dept: TBtnEditA;
    Label26: TLabel;
    GHNo: TEdit;
    Label28: TLabel;
    FHType: TBtnEditA;
    Label29: TLabel;
    MBMoney: TEdit;
    Label30: TLabel;
    GRNXMoney: TEdit;
    Label31: TLabel;
    ZhuanZhengDate: TDateTimePicker;
    Label32: TLabel;
    XZNo: TEdit;
    Label33: TLabel;
    YWSNXSMoney: TEdit;
    Label34: TLabel;
    Label35: TLabel;
    FKType: TBtnEditA;
    FKSYSTEM: TBtnEditA;
    Label36: TLabel;
    Label3: TLabel;
    NXZZType: TBtnEditA;
    Label37: TLabel;
    GRYearPF: TEdit;
    SYDate: TcxTimeEdit;
    Label39: TLabel;
    XBTime: TcxTimeEdit;
    Label16: TLabel;
    Label38: TLabel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SKBankBtnDnClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure MinZuBtnClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure DeptBtnClick(Sender: TObject);
    procedure GangWeiBtnClick(Sender: TObject);
    procedure FHTypeBtnClick(Sender: TObject);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    function  SaveMXData(Tv11:TcxGridDBTableView;CDS1:TClientDataSet;FMaxNo,FYOType:string):Boolean;
    procedure SaveImageOther();
    procedure InitImage();
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId:String;
    { Public declarations }
  end;

var
  frmYGInPut: TfrmYGInPut;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_SYDeptView,U_ZDYBZType;

{$R *.dfm}

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

procedure TfrmYGInPut.InitData();
begin
  cxPageControl1.ActivePageIndex:=0;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn where YGID='''+Trim(FMainId)+'''');
    Open;
  end;
  if ADOQuery1.IsEmpty=false then 
  SCSHDataNew(ADOQuery1,Panel3,2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn_Other where YGID='''+Trim(FMainId)+'''');
    sql.Add(' and YOType=''家庭成员'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet1);
  SInitCDSData20(ADOQuery1,ClientDataSet1);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn_Other where YGID='''+Trim(FMainId)+'''');
    sql.Add(' and YOType=''培训经历'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  TPInt:=0;
  InitImage();
end;


procedure TfrmYGInPut.FormShow(Sender: TObject);
begin
  ChuShengDate.Date:=SGetServerDate(ADOQuery1);
  SYDate.Time:=StrToTime('08:00:00');
  XBTime.Time:=StrToTime('08:00:00');
  ZhuanZhengDate.Date:=SGetServerDate(ADOQuery1);
  InitData();
end;

function TfrmYGInPut.SaveData():Boolean;
var
  maxno,FHZStatus:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'','OA_YG_DangAn',4,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from OA_YG_DangAn where YGId='''+Trim(FMainId)+'''');
      Open;
    end;
    FHZStatus:=Trim(ADOCmd.fieldbyname('HZStatus').AsString);
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('YGId').Value:=Trim(maxno);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('YGType').Value:='正式';
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd,'OA_YG_DangAn',Panel3,2);
      FieldByName('YGEName').AsString:=Trim(YGName.Text);
      Post;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OA_YG_DangAn where ShenFenNo='''+Trim(ShenFenNo.Text)+'''');
      Open;
    end;
    if ADOTemp.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('身份证号重复，请核实!','提示',0);
      Exit;
    end;
    if TPInt=1 then
    begin
      SaveImageOther();
    end else
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('delete TP_File where WBID='''+Trim(maxno)+''' and TFType=''YG'' ');
        ExecSQL;
      end;
    end;
    if SaveMXData(Tv1,ClientDataSet1,maxno,'家庭成员')=False then Exit;
    if SaveMXData(Tv2,ClientDataSet2,maxno,'培训经历')=False then Exit;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

function TfrmYGInPut.SaveMXData(Tv11:TcxGridDBTableView;CDS1:TClientDataSet;FMaxNo,FYOType:string):Boolean;
var
  maxsubno,FYOID:String;
begin
  Result:=False;
  try
    CDS1.DisableControls;
    with CDS1 do
    begin
      First;
      while not Eof do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from OA_YG_DangAn_Other where YOID='''+Trim(CDS1.fieldbyname('YOID').AsString)+'''');
          open;
        end;
        FYOID:=Trim(ADOCmd.fieldbyname('YOID').AsString);
        if Trim(FYOID)='' then
        begin
          if GetLSNo(ADOCmd,maxsubno,'YO','OA_YG_DangAn_Other',4,1)=False then
          begin
            Result:=False;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(FYOID);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_DangAn_Other where YOID='''+Trim(FYOID)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FYOID)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('YGId').Value:=Trim(FMaxNo);
          FieldByName('YOType').Value:=Trim(FYOType);
          FieldByName('YOID').Value:=Trim(maxsubno);
          if Trim(FYOID)='' then
          begin
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          RTSetSaveDataCDS(ADOCmd,Tv11,CDS1,'OA_YG_DangAn_Other',0);
          Post;
        end;
        Edit;
        FieldByName('YOID').Value:=Trim(maxsubno);
        Next;
      end;
    end;
    CDS1.EnableControls;
    Result:=True;
  except
    CDS1.EnableControls;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存明细异常!','提示',0);
  end;
end;

procedure TfrmYGInPut.InitImage();
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  if Trim(FMainId)='' then Exit;
  Image1.Picture.Assign(nil);
  try
    with ADOQuery1  do
    begin
      close;
      sql.Clear;
      sql.Add(' select * from TP_File A where A.WBID='''+Trim(FMainId)+'''');
      sql.Add(' and TFType=''YG'' ');
      open;
      if RecordCount>0 then
      begin
        TPInt:=1;
        if trim(ADOQuery1.fieldbyname('FilesOther').AsString)<>'' then
        begin
          myStream:=tadoblobstream.Create(tblobfield(ADOQuery1.fieldbyname('FilesOther')),bmread);
          if myStream=nil then exit;
          jpg:=TJPEGImage.Create;
          jpg.LoadFromStream(myStream);
          Image1.Picture.Assign(jpg);
        end;
      end;
    end;
  finally
    jpg.Free;
    myStream.Free;
  end;
end;

procedure TfrmYGInPut.SKBankBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;



procedure TfrmYGInPut.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  if Trim(YGName.Text)='' then
  begin
    Application.MessageBox('姓名不能为空！','提示',0);
    Exit;
  end;
  if Trim(GangWei.Text)='' then
  begin
    Application.MessageBox('岗位不能为空!','提示',0);
    Exit;
  end;
  if Trim(Dept.Text)='' then
  begin
    Application.MessageBox('部门不能为空!','提示',0);
    Exit;
  end;
  if Trim(FHType.Text)='' then
  begin
    Application.MessageBox('班组类型不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmYGInPut.MinZuBtnClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYBZType:=TfrmZDYBZType.Create(Application);
    with frmZDYBZType do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYBZType.Free;
  end;
end;


procedure TfrmYGInPut.N1Click(Sender: TObject);
begin
  OpenPictureDialog1.Execute;
  if Trim(OpenPictureDialog1.FileName)='' then
  begin
    Exit;
  end;
  Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  TPInt:=1;
end;

procedure TfrmYGInPut.N2Click(Sender: TObject);
begin
  if TPInt=0 then Exit;
  if Application.MessageBox('确定要删除图片吗？','提示',32+4)<>IDYES then Exit;
  Image1.Picture:=nil;
  Image1.Picture.Assign(nil);
  TPInt:=0;
end;
procedure TfrmYGInPut.SaveImageOther();
var
  AJpeg: TJPEGImage;
  myStream: TADOBlobStream;
  ImgMaxNo:String;
  i,j: Integer;
  PatFile: String;
  FTPPath,FConNo,MaxNo,FTFID:string;
begin
  if Image1.Picture=nil then Exit;
  AJpeg:=TJpegImage.Create();
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from TP_File where WBID='''+Trim(FMainId)+'''');
    sql.Add(' and TFType=''YG'' ');
    Open;
  end;
  FTFID:=Trim(ADOTemp.fieldbyname('TFID').AsString);
  if Trim(FTFID)='' then
  begin
    if GetLSNo(ADOCmd,ImgMaxNo,'TF','TP_File',4,1)=False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取图片表最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    ImgMaxNo:=Trim(FTFID);
  end;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add( ' select *  from TP_File where TFID='''+Trim(FTFID)+'''');
    open;
  end;
  with ADOCmd do
  begin
    if Trim(FTFID)='' then
    begin
      Append;
      FieldByName('Filler').Value:=Trim(DName);
    end
    else
    begin
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
    end;
    FieldByName('TFID').Value:=Trim(ImgMaxNo);
    FieldByName('WBID').Value:=Trim(FMainId);
    FieldByName('TFType').Value:='YG';
    AJpeg.Assign(Image1.Picture.Graphic);
    //CreThumb(AJpeg,Image1,160, 120);
    myStream := TADOBlobStream.Create(TBlobField(ADOCmd.FieldByName('FilesOther')), bmWrite);
    AJpeg.Assign(Image1.Picture.Graphic);
    AJpeg.SaveToStream(myStream);
    myStream.Free;
    Post;
  end;
end;

procedure TfrmYGInPut.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='JJLXGX';
      flagname:='与本人关系';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet1 do
        begin
          Edit;
          FieldByName('JTLXGX').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYGInPut.ToolButton1Click(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex=0 then Exit;
  if cxPageControl1.ActivePageIndex=1 then
  begin
    with ClientDataSet1 do
    begin
      Append;
      Post;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    with ClientDataSet2 do
    begin
      Append;
      Post;
    end;
  end;
end;

procedure TfrmYGInPut.ToolButton2Click(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex=0 then Exit;
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if ClientDataSet1.IsEmpty then Exit;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete OA_YG_DangAn_Other where YOID='''+Trim(ClientDataSet1.fieldbyname('YOID').AsString)+'''');
      ExecSQL;
    end;
    ClientDataSet1.Delete;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    if ClientDataSet2.IsEmpty then Exit;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete OA_YG_DangAn_Other where YOID='''+Trim(ClientDataSet2.fieldbyname('YOID').AsString)+'''');
      ExecSQL;
    end;
    ClientDataSet2.Delete;
  end;
end;

procedure TfrmYGInPut.DeptBtnClick(Sender: TObject);
begin
  try
    frmSYDeptView:=TfrmSYDeptView.Create(Application);
    with frmSYDeptView do
    begin
      if ShowModal=1 then
      begin
        Self.Dept.Text:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPName').AsString);
        //Self.Dept.TxtCode:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPID').AsString);
      end;
    end;
  finally
    frmSYDeptView.Free;
  end;
end;

procedure TfrmYGInPut.GangWeiBtnClick(Sender: TObject);
begin
  try
    frmSYDeptView:=TfrmSYDeptView.Create(Application);
    with frmSYDeptView do
    begin
      if ShowModal=1 then
      begin
        Self.GangWei.Text:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPName').AsString);
        Self.GangWei.TxtCode:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPID').AsString);
      end;
    end;                
  finally
    frmSYDeptView.Free;
  end;
end;

procedure TfrmYGInPut.FHTypeBtnClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYBZType:=TfrmZDYBZType.Create(Application);
    with frmZDYBZType do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        SYDate.Time:=ClientDataSet1.fieldbyname('SBTime').AsDateTime;
        XBTime.Time:=ClientDataSet1.fieldbyname('XBTime').AsDateTime;
      end;
    end;
  finally
    frmZDYBZType.Free;
  end;
end;

end.
