unit U_CPType;    //1

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, DBClient;

type
  TfrmCPType = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    DataSource1: TDataSource;
    ADOQueryTree10: TADOQuery;
    ToolButton1: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    CPTopName: TEdit;
    CPName: TEdit;
    CPNo: TEdit;
    ADOQueryTree: TClientDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure CPNameKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();

  public
    { Public declarations }
  end;

var
  frmCPType: TfrmCPType;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmCPType.InitTree();
var
  i:Integer;
begin
  with ADOQueryTree10 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree10,ADOQueryTree);
  SInitCDSData20(ADOQueryTree10,ADOQueryTree);
  cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPType.FormDestroy(Sender: TObject);
begin
  frmCPType:=nil;
end;

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

procedure TfrmCPType.TBRafreshClick(Sender: TObject);
begin
  InitTree();
end;

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

procedure TfrmCPType.TBAddClick(Sender: TObject);
begin
  PState:=11;
  CPTopName.Text:=Trim(ADOQueryTree.fieldbyname('CPName').AsString);
  //FTopID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  FCPID:='';
  CPName.Text:='';
  CPNo.Text:='';
  CPName.SetFocus;
end;

procedure TfrmCPType.cxDBTreeList1Click(Sender: TObject);
begin
  PState:=22;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_Type where CPID='''+Trim(ADOQueryTree.fieldbyname('CPParent').AsString)+'''');
    Open;
  end;
  FCPID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  CPTopName.Text:=Trim(ADOQueryTemp.fieldbyname('CPName').AsString);
  CPName.Text:=Trim(ADOQueryTree.fieldbyname('CPName').AsString);
  CPNo.Text:=Trim(ADOQueryTree.fieldbyname('CPNo').AsString);
end;

procedure TfrmCPType.ToolButton1Click(Sender: TObject);
var
  maxId:String;
begin
  if Trim(CPName.Text)='' then
  begin
    Application.MessageBox('类别名称不能为空！','提示',0);
    Exit;
  end;
  if Trim(CPNo.Text)='' then
  begin
    Application.MessageBox('类别编码不能为空！','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if PState=11 then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'CP','CP_Type',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('区最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxId:=Trim(FCPID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_Type where CPID='''+Trim(FCPID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if PState=11 then
      begin
        Append;
        FieldByName('CPID').Value:=Trim(maxId);
        FieldByName('CPName').Value:=Trim(CPName.Text);
        FieldByName('CPNo').Value:=Trim(CPNo.Text);
        FieldByName('CPParent').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
        FieldByName('CPLevel').Value:=ADOQueryTree.fieldbyname('CPLevel').AsInteger+1;
        Post;
      end else
      if PState=22 then
      begin
        Edit;
        FieldByName('CPID').Value:=Trim(maxId);
        FieldByName('CPName').Value:=Trim(CPName.Text);
        FieldByName('CPNo').Value:=Trim(CPNo.Text);
        //FieldByName('CPParent').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
        //FieldByName('CPOrder').Value:=ADOQueryTree.fieldbyname('CPOrder').AsInteger+1;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmCPType.TBDelClick(Sender: TObject);
begin
  if ADOQueryTree.FieldByName('CPLevel').AsInteger=0 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_Type where CPParent='''+Trim(ADOQueryTree.fieldbyname('CPID').AsString)+'''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经定义子类不能删除！','提示',0);
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYType='''+Trim(ADOQueryTree.fieldbyname('CPID').AsString)+'''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经有产品属于此类不能删除！','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CP_Type where CPID='''+Trim(ADOQueryTree.fieldbyname('CPID').AsString)+'''');
    sql.Add('delete CP_Type where CPParent='''+Trim(ADOQueryTree.fieldbyname('CPID').AsString)+'''');
    ExecSQL;
  end;
  InitTree();
end;

procedure TfrmCPType.CPNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
    CPNo.SetFocus;
end;

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

end.
