unit U_CPTypeM;

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, cxLookAndFeels, cxLookAndFeelPainters, cxTLdxBarBuiltInMenu,
  cxClasses;

type
  TfrmCPTypeM = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ToolButton1: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    CPTopName: TEdit;
    CPName: TEdit;
    CPNo: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label4: TLabel;
    GSCPName: TEdit;
    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);
    procedure cxDBTreeList1DblClick(Sender: TObject);
  private
    { Private declarations }
    PState: Integer;
    FCPID, FTopID: string;
    procedure InitTree();
    procedure CSHLB();

  public
    { Public declarations }
  end;

var
  frmCPTypeM: TfrmCPTypeM;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}

procedure TfrmCPTypeM.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select *');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' then RTrim(CPNo)+''/''+CPName else CPName end as CP ');
    SQL.Add('from CP_TypeM order by CPlevel,CPOrder,CPName');
    Open;
  end;
  cxDBTreeList1.Items[0].Expand(True);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPTypeM.FormDestroy(Sender: TObject);
begin
  frmCPTypeM := nil;
end;

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

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

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

procedure TfrmCPTypeM.TBAddClick(Sender: TObject);
begin
  PState := 11;
  if ADOQueryTree.FieldByName('CPLevel').Value = 1 then
  begin
    CPTopName.Text := '';
  end;
  if ADOQueryTree.FieldByName('CPLevel').Value = 2 then
  begin
    CPTopName.Text := Trim(ADOQueryTree.fieldbyname('CPName').AsString);
  end;

  //FTopID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  FCPID := '';
  CPName.Text := '';
  CPNo.Text := '';
  CSHLB();
//  CPName.SetFocus;
end;

procedure TfrmCPTypeM.CSHLB();
begin
  CPTopName.Visible := false;
  CPName.Visible := false;
  GSCPName.Visible := false;
  CPNo.Visible := false;

  Label1.Visible := false;
  Label2.Visible := false;
  Label3.Visible := false;
  Label4.Visible := false;

  if PState = 11 then
  begin
    CPNo.Text := Trim(ADOQueryTree.fieldbyname('CPNo').AsString);

    if ADOQueryTree.FieldByName('CPLevel').Value = 1 then
    begin
      CPTopName.Visible := True;
      Label1.Visible := True;
    end
    else if ADOQueryTree.FieldByName('CPLevel').Value = 2 then
    begin
      CPTopName.Visible := True;
      CPName.Visible := True;
      GSCPName.Visible := True;
      CPNo.Visible := True;

      Label1.Visible := True;
      Label2.Visible := True;
      Label3.Visible := True;
      Label4.Visible := True;
    end;
//    if ADOQueryTree.FieldByName('CPLevel').Value > 1 then
//    begin
//      CPNo.Visible := True;
//      Label3.Visible := True;
//      if ADOQueryTree.FieldByName('CPLevel').Value > 2 then
//      begin
//        CPNo.Visible := false;
//        Label3.Visible := false;
//      end;
//    end
//    else
//    begin
//      CPNo.Visible := False;
//      Label3.Visible := False;
//    end;
  end
  else if PState = 22 then
  begin
    if ADOQueryTree.FieldByName('CPLevel').Value = 2 then
    begin
      CPTopName.Visible := True;
      Label1.Visible := True;
    end
    else if ADOQueryTree.FieldByName('CPLevel').Value = 3 then
    begin
      CPTopName.Visible := True;
      CPName.Visible := True;
      GSCPName.Visible := True;
      CPNo.Visible := True;

      Label1.Visible := True;
      Label2.Visible := True;
      Label3.Visible := True;
      Label4.Visible := True;
    end;

//    if ADOQueryTree.FieldByName('CPLevel').Value>2 then
//    begin
//      CPNo.Visible:=True;
//      Label3.Visible:=True;
//      if ADOQueryTree.FieldByName('CPLevel').Value>3 then
//      begin
//        CPNo.Visible:=false;
//        Label3.Visible:=false;
//      end;
//    end else
//    begin
//      CPNo.Visible:=False;
//      Label3.Visible:=False;
//    end;
  end;
end;

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

  if ADOQueryTree.FieldByName('CPLevel').Value = 2 then
  begin
    CPTopName.Text := Trim(ADOQueryTree.fieldbyname('CPName').AsString);
  end
  else if ADOQueryTree.FieldByName('CPLevel').Value = 3 then
  begin
    CPTopName.Text := Trim(ADOQueryTemp.fieldbyname('CPName').AsString);
    CPName.Text := Trim(ADOQueryTree.fieldbyname('CPName').AsString);
    CPNo.Text := Trim(ADOQueryTree.fieldbyname('CPNo').AsString);
  end;

  CSHLB();
end;

procedure TfrmCPTypeM.ToolButton1Click(Sender: TObject);
var
  maxId, FXCPID: string;
begin
  if Trim(CPName.Text) = '' then
  begin
    Application.MessageBox('类别名称不能为空！', '提示', 0);
    Exit;
  end;
  if PState = 11 then
  begin
    if ADOQueryTree.FieldByName('CPLevel').Value > 1 then
    begin
      if Trim(CPNo.Text) = '' then
      begin
        Application.MessageBox('类别编码不能为空！', '提示', 0);
        Exit;
      end;
    end;
  end
  else if PState = 22 then
  begin
    if ADOQueryTree.FieldByName('CPLevel').Value > 2 then
    begin
      if Trim(CPNo.Text) = '' then
      begin
        Application.MessageBox('类别编码不能为空！', '提示', 0);
        Exit;
      end;
    end;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if PState = 11 then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'CP', 'CP_TypeM', 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_TypeM 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;
        FXCPID := Trim(ADOQueryTree.fieldbyname('CPID').AsString);
        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;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_TypeM where CPNo=''' + Trim(CPNo.Text) + ''' and isnull(CPNO,'''')<>''''');
      SQL.Add(' and CPLevel<4');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox('类别编号重复', '提示');
      exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_TypeM where CPName=''' + Trim(CPName.Text) + '''');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox('类别名称重复', '提示');
      exit;
    end;
    if CPNo.Visible = true then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CP_TypeM Set CPNO=''' + Trim(CPNO.Text) + '''');
        sql.Add(' where CPNO=''' + Trim(ADOQueryTree.fieldbyname('CPNO').AsString) + '''');
        sql.Add(' and isnull(CPNo,'''')<>''''');
        ExecSQL;
      end;
    end;
    CPName.Text := '';
    CPNo.Text := '';

    TBRafresh.Click;
    ADOQueryTree.Locate('CPID', FXCPID, []);
    TBAdd.Click;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmCPTypeM.TBDelClick(Sender: TObject);
begin
  if ADOQueryTree.FieldByName('CPLevel').AsInteger = 0 then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_TypeM 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 M_ZDY where CPID=''' + 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_TypeM where CPID=''' + Trim(ADOQueryTree.fieldbyname('CPID').AsString) + '''');
    sql.Add('delete CP_TypeM where CPParent=''' + Trim(ADOQueryTree.fieldbyname('CPID').AsString) + '''');
    ExecSQL;
  end;
  InitTree();
end;

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

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

procedure TfrmCPTypeM.cxDBTreeList1DblClick(Sender: TObject);
begin
  if Panel1.Visible = False then
  begin
    ModalResult := 1;
  end;
end;

end.

