unit U_ClothType;

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, U_BaseList, cxLookAndFeels, cxLookAndFeelPainters,
  cxTLdxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters, cxContainer,
  cxEdit, cxFilter, dxScrollbarAnnotations, cxProgressBar;

type
  TfrmClothType = class(TfrmBaseList)
    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;
    CTTopName: TEdit;
    CTName: TEdit;
    CTNo: 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 CTNameKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    PState: Integer;
    FCTID, FTopID: string;
    procedure InitTree();

  public
    FCTType: string;
    { Public declarations }
  end;

var
  frmClothType: TfrmClothType;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmClothType.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree10 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_Type  ');
    SQL.Add(' where CTType=' + QuotedStr(FCTType));
    SQL.Add(' order by CTlevel,CTOrder,CTName');
    Open;
  end;
  SCreateCDS(ADOQueryTree10, ADOQueryTree);
  SInitCDSData(ADOQueryTree10, ADOQueryTree);
  if ADOQueryTree.IsEmpty then
    Exit;
  cxDBTreeList1.Items[0].Expand(True);
end;

procedure TfrmClothType.FormDestroy(Sender: TObject);
begin
  inherited;
  frmClothType := nil;
end;

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

procedure TfrmClothType.FormCreate(Sender: TObject);
begin
  inherited;
  FCTType := Trim(self.fParameters2);
end;

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

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

procedure TfrmClothType.TBAddClick(Sender: TObject);
begin
  PState := 11;
  CTTopName.Text := Trim(ADOQueryTree.fieldbyname('CTName').AsString);
  FCTID := '';
  CTName.Text := '';
  CTNo.Text := '';
  CTName.SetFocus;
end;

procedure TfrmClothType.cxDBTreeList1Click(Sender: TObject);
begin
  PState := 22;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Cloth_Type where CTID=''' + Trim(ADOQueryTree.fieldbyname('CTParent').AsString) + '''');
    Open;
  end;
  FCTID := Trim(ADOQueryTree.fieldbyname('CTID').AsString);
  CTTopName.Text := Trim(ADOQueryTemp.fieldbyname('CTName').AsString);
  CTName.Text := Trim(ADOQueryTree.fieldbyname('CTName').AsString);
  CTNo.Text := Trim(ADOQueryTree.fieldbyname('CTNo').AsString);
end;

procedure TfrmClothType.ToolButton1Click(Sender: TObject);
var
  maxId, FLMainId: string;
begin
  if Trim(CTName.Text) = '' then
  begin
    Application.MessageBox('类别名称不能为空！', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    if PState = 11 then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'CT', 'BS_Cloth_Type', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxId := Trim(FCTID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Cloth_Type where CTID=''' + Trim(FCTID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if PState = 11 then
      begin
        Append;
        FieldByName('CTType').Value := Trim(FCTType);
        FieldByName('CTID').Value := Trim(maxId);
        FieldByName('CTName').Value := Trim(CTName.Text);
        FieldByName('CTNo').Value := Trim(CTNo.Text);
        FieldByName('CTParent').Value := Trim(ADOQueryTree.fieldbyname('CTID').AsString);
        FieldByName('CTLevel').Value := ADOQueryTree.fieldbyname('CTLevel').AsInteger + 1;
        Post;
      end
      else if PState = 22 then
      begin
        Edit;
        FieldByName('CTID').Value := Trim(maxId);
        FieldByName('CTName').Value := Trim(CTName.Text);
        FieldByName('CTNo').Value := Trim(CTNo.Text);
        //FieldByName('CTParent').Value:=Trim(ADOQueryTree.fieldbyname('CTID').AsString);
        //FieldByName('CPOrder').Value:=ADOQueryTree.fieldbyname('CPOrder').AsInteger+1;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
    FLMainId := Trim(ADOQueryTree.fieldbyname('CTID').AsString);
    InitTree();
    ADOQueryTree.Locate('CTID', FLMainId, []);

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmClothType.TBDelClick(Sender: TObject);
begin
  if ADOQueryTree.FieldByName('CTLevel').AsInteger = 0 then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Cloth_Type where CTParent=''' + Trim(ADOQueryTree.fieldbyname('CTID').AsString) + '''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经定义子类不能删除！', '提示', 0);
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Cloth_Info where CTID=''' + Trim(ADOQueryTree.fieldbyname('CTID').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 BS_Cloth_Type where CTID=''' + Trim(ADOQueryTree.fieldbyname('CTID').AsString) + '''');
    sql.Add('delete BS_Cloth_Type where CTParent=''' + Trim(ADOQueryTree.fieldbyname('CTID').AsString) + '''');
    ExecSQL;
  end;
  InitTree();
end;

procedure TfrmClothType.CTNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    CTNo.SetFocus;
end;

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

end.

