unit U_ZDYHelpSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxCheckBox, cxTextEdit, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmZDYHelpSel = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Code: TcxGridDBColumn;
    V1Name: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBAdd: TToolButton;
    TBSave: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    TBEdit: TToolButton;
    V1Note: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    ZDYName: TEdit;
    V1Column1: TcxGridDBColumn;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ZDYNameChange(Sender: TObject);
    procedure V1NamePropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    flag,flagname,snote,MainType,ReturnStr,FGStr:string;
    fnote,forderno:Boolean;
    PPSTE,JiangeStr:integer;
    { Public declarations }
  end;

var
  frmZDYHelpSel: TfrmZDYHelpSel;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

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

procedure TfrmZDYHelpSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_ZDY where Type='''+flag+'''');
      if Trim(MainType)<>'' then
      begin
        sql.Add(' and MainType='''+Trim(MainType)+'''');
      end;
      Open;
    end;
    SCreateCDS(ADOQueryMain,ClientDataSet1);
    SInitCDSData(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZDYHelpSel.TBAddClick(Sender: TObject);
var
  i:Integer;
begin
  ZDYName.SetFocus;
  TV1.OptionsData.Editing:=True;
  TV1.OptionsSelection.CellSelect:=True;
  for i:=0 to 5 do
  begin
    with ClientDataSet1 do
    begin
      Append;
      Post;
    end;
  end;

end;

procedure TfrmZDYHelpSel.TBSaveClick(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from KH_ZDY where ZdyNo='''+Trim(flag)+'''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into KH_ZDY(ZDYNo,ZDYName,Type,MainType) select :ZDYNo,:ZDYName,:Type,:MainType ');
        Parameters.ParamByName('ZDYNo').Value:=Trim(flag);
        Parameters.ParamByName('ZDYName').Value:=Trim(flagname);
        Parameters.ParamByName('Type').Value:='Main';
        Parameters.ParamByName('MainType').Value:=Trim(MainType);
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      with ClientDataSet1 do
      begin
        First;
        while not eof do
        begin
          if Trim(ClientDataSet1.FieldByName('ZDYNO').AsString)='' then
          begin
            if GetLSNo(ADOQueryTemp,maxno,'SY','KH_ZDY',3,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取最大编号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('delete KH_ZDY where ZDYNO='''+Trim(ClientDataSet1.fieldbyname('ZDYNO').AsString)+'''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from KH_ZDY where 1<>1');
            Open;
          end;
          if Trim(ClientDataSet1.FieldByName('ZDYName').AsString)='' then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('名称不能为空！','提示',0);
            Exit;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('ZDYNo').Value:=Trim(maxno);
          ADOQueryCmd.FieldByName('ZDYName').Value:=ClientDataSet1.fieldbyname('ZDYName').AsString;
          ADOQueryCmd.FieldByName('note').Value:=Trim(snote);
          //ADOQueryCmd.FieldByName('orderno').Value:=ClientDataSet1.fieldbyname('Name').AsString;
          ADOQueryCmd.FieldByName('Type').Value:=flag;
          ADOQueryCmd.FieldByName('valid').Value:='Y';
          if Trim(MainType)<>'' then
            ADOQueryCmd.FieldByName('MainType').Value:=Trim(MainType);
          //ADOQueryCmd.FieldByName('sel').Value:=0;
          ADOQueryCmd.Post;
          ClientDataSet1.Edit;
          ClientDataSet1.FieldByName('ZDYNo').Value:=Trim(maxno);
          ClientDataSet1.Post;
          Next;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
 // TV1.OptionsData.Editing:=False;
end;

procedure TfrmZDYHelpSel.TBDelClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.FieldByName('ZDYNo').AsString)<>'') or
     (Trim(ClientDataSet1.FieldByName('ZDYname').AsString)<>'') then
  begin
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KH_ZDY where ZDYNo='''+Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
      SQL.Add(' and Type='''+Trim(flag)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmZDYHelpSel.TBCloseClick(Sender: TObject);
begin
  ModalResult:=2;
  ZDYName.SetFocus;
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  Close;
end;

procedure TfrmZDYHelpSel.FormShow(Sender: TObject);
var
  fsj,fsj1:string;
begin
  {if PPSTE=1 then
  begin
    Application.Terminate;
    Exit;
  end; }
  InitGrid();
  fsj:=Trim(flag)+'01';
  fsj1:=Trim(flagname)+'01';
  ReadCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  {if ClientDataSet1.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into KH_ZDY(ZDYNo,ZDYname,Type,note)');
      sql.Add('select '''+Trim(fsj)+'''');
      sql.Add(','''+Trim(fsj1)+'''');
      SQL.Add(','''+Trim(flag)+'''');
      sql.Add(','''+Trim(snote)+'''');
      ExecSQL;
    end;
    InitGrid();
  end;}
  //frmZDYHelp.Caption:=Trim(flagname)+'<'+Trim(flag)+'>';
  frmZDYHelpSel.Caption:=Trim(flagname);
end;

procedure TfrmZDYHelpSel.ToolButton1Click(Sender: TObject);
begin
  ReturnStr:='';
  with ClientDataSet1 do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean=True then
      begin
        if JiangeStr<>99 then
        begin
          if FGStr<>'' then
            ReturnStr:=ReturnStr+Trim(fieldbyname('ZDYName').AsString)+FGStr
          else
            ReturnStr:=ReturnStr+Trim(fieldbyname('ZDYName').AsString)+';'
        end
        else
          ReturnStr:=ReturnStr+Trim(fieldbyname('ZDYName').AsString);
      end;
      Next;
    end;
  end;
  if JiangeStr<>99 then
    ReturnStr:=Copy(ReturnStr,1,Length(ReturnStr)-1);
  ModalResult:=1;
end;

procedure TfrmZDYHelpSel.TBEditClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
end;

procedure TfrmZDYHelpSel.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.OptionsData.Editing=False then
  begin
    ModalResult:=1;
  end;
end;

procedure TfrmZDYHelpSel.ZDYNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS(ADOQueryMain,ClientDataSet1);
    SInitCDSData(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmZDYHelpSel.V1NamePropertiesEditValueChanged(Sender: TObject);
var
  maxno,mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    //Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ZdyName').Value:=Trim(mvalue);
    //Post;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from KH_ZDY where ZdyNo='''+Trim(flag)+'''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into KH_ZDY(ZDYNo,ZDYName,Type,MainType) select :ZDYNo,:ZDYName,:Type,:MainType ');
        Parameters.ParamByName('ZDYNo').Value:=Trim(flag);
        Parameters.ParamByName('ZDYName').Value:=Trim(flagname);
        Parameters.ParamByName('Type').Value:='Main';
        Parameters.ParamByName('MainType').Value:=Trim(MainType);
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      //ClientDataSet1.DisableControls;
      //with ClientDataSet1 do
      //begin
        //First;
        //while not eof do
        //begin
          if Trim(ClientDataSet1.FieldByName('ZDYNO').AsString)='' then
          begin
            if GetLSNo(ADOQueryTemp,maxno,'SY','KH_ZDY',3,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
              Application.MessageBox('取最大编号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.add('select * from KH_Zdy where Type='''+Trim(flag)+'''');
            if Trim(MainType)<>'' then
              SQL.Add(' and MainType='''+Trim(MainType)+'''');
            sql.Add(' and ZdyName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if ADOQueryTemp.RecordCount>1 then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
              Application.MessageBox('名称重复！','提示',0);
              Exit;
            end;
            if Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)='' then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
              Application.MessageBox('名称重复！','提示',0);
              Exit;
            end else
            begin
              if Trim(ADOQueryTemp.fieldbyname('ZdyNo').AsString)<>Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString) then
              begin
                ADOQueryCmd.Connection.RollbackTrans;
                //ClientDataSet1.EnableControls;
                Application.MessageBox('名称重复！','提示',0);
                Exit;
              end;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('delete KH_ZDY where ZDYNO='''+Trim(ClientDataSet1.fieldbyname('ZDYNO').AsString)+'''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from KH_ZDY where 1<>1');
            Open;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('ZDYNo').Value:=Trim(maxno);
          ADOQueryCmd.FieldByName('ZDYName').Value:=ClientDataSet1.fieldbyname('ZDYName').AsString;
          ADOQueryCmd.FieldByName('note').Value:=Trim(snote);
          //ADOQueryCmd.FieldByName('orderno').Value:=ClientDataSet1.fieldbyname('Name').AsString;
          ADOQueryCmd.FieldByName('Type').Value:=flag;
          ADOQueryCmd.FieldByName('valid').Value:='Y';
          if Trim(MainType)<>'' then
            ADOQueryCmd.FieldByName('MainType').Value:=Trim(MainType);
          //ADOQueryCmd.FieldByName('sel').Value:=0;
          ADOQueryCmd.Post;
          ClientDataSet1.Edit;
          ClientDataSet1.FieldByName('ZDYNo').Value:=Trim(maxno);
          //ClientDataSet1.Post;
         // Next;
        //end;
      //end;
     // ClientDataSet1.EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    //Application.MessageBox('保存成功！','提示',0);
    //TV1.OptionsData.Editing:=False;
    //TV1.OptionsSelection.CellSelect:=False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

end.
