unit U_ZDYHelpDL;

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, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,
  cxTLData, cxDropDownEdit;

type
  TfrmZDYDLHelp = 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;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    ZDYName: TEdit;
    ThreeImgList: TImageList;
    ADOQueryChild: TADOQuery;
    DataSource2: TDataSource;
    cxDBTreeList1: TcxDBTreeList;
    cxColumn2: TcxDBTreeListColumn;
    V1Column1: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    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 cxDBTreeList1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    MainType,MainTypeName,flag,flagname,snote:string;
    fnote,forderno:Boolean;
    { Public declarations }
  end;

var
  frmZDYDLHelp: TfrmZDYDLHelp;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmZDYDLHelp.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=DConString;
    Connected:=true;
  end;
end;

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

procedure TfrmZDYDLHelp.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,TypeName=(select ZdyName from KH_Zdy B where B.ZdyNO=A.Type) from KH_Zdy A');
      if Trim(ADOQueryChild.fieldbyname('frameNo').AsString)<>'QB' then
        sql.Add('where  Type='''+Trim(ADOQueryChild.fieldbyname('frameNo').AsString)+'''')
      else
        sql.Add('where  MainType='''+Trim(MainType)+''' and Type<>''Main'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZDYDLHelp.TBAddClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
  with ClientDataSet1 do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmZDYDLHelp.TBSaveClick(Sender: TObject);
var
  maxno,fsj:string;
  i:Integer;
begin
  if ClientDataSet1.IsEmpty then Exit;
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  if Trim(ClientDataSet1.fieldbyname('TypeName').AsString)='' then
  begin
    Application.MessageBox('类别不能为空！','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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:=TcxComboBox(V1Column1).Properties.Items.Strings[1];
          i:=(V1Column1.Properties as TcxComboBoxProperties).Items.IndexOf(ClientDataSet1.fieldbyname('TypeName').AsString);
          fsj:=TA((V1Column1.Properties as TcxComboBoxProperties).Items.Objects[i]).S;
          ADOQueryCmd.FieldByName('Type').Value:=Trim(fsj);
          ADOQueryCmd.FieldByName('valid').Value:='Y';
          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 TfrmZDYDLHelp.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(ADOQueryChild.fieldbyname('frameNO').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmZDYDLHelp.TBCloseClick(Sender: TObject);
begin
  ModalResult:=2;
  Close;
end;

procedure TfrmZDYDLHelp.FormShow(Sender: TObject);
var
  fsj,fsj1:string;
begin
  fsj:='select ZdyNo code,ZdyName name from KH_Zdy where  MainType='''+MainType+''' and Type=''Main''';
  SInitCxGridComboBoxBySql(ADOQueryTemp,V1Column1,fsj,1,True,'');
  fsj:= ' select frameNo=''QB'',frameName=''全部'',TopFrameNo='''+Trim(MainType)+''''+
       '  union all select frameNo=ZdyNo,frameName=ZdyName,TopFrameNo=MainType '+
       ' from KH_Zdy A '+
       ' where A.MainType='''+MainType+''' and Type=''Main''';
  with ADOQueryChild do
  begin
    Close;
    sql.Clear;
    sql.Add(fsj);
    Open;
  end;
  cxDBTreeList1.FullExpand;
  InitGrid();
  
  frmZDYDLHelp.Caption:=Trim(MainTypename)+'<'+Trim(MainType)+'>';
end;

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

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

procedure TfrmZDYDLHelp.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 TfrmZDYDLHelp.ZDYNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmZDYDLHelp.cxDBTreeList1Click(Sender: TObject);
begin
  InitGrid();
end;

end.
