unit MeasureGatherZJJL;

interface

uses
  Classes, BillsTree;

type
  TmgZJJLDetail = class
  private
    FRelaNode: TBillsIDTreeNode;
  public
    constructor Create(ADetailNode: TBillsIDTreeNode);
    destructor destroy; override;

    property RelaNode: TBillsIDTreeNode read FRelaNode;
  end;

  TmgZJJL = class
  private
    FB_Code: string;
    FName: string;
    FUnits: string;
    FPrice: Double;
    FGatherNode: TBillsIDTreeNode;
    FDetails: TList;
    FPegName: string;
    FBeginPeg: string;
    FBGLCode: string;
    FEndPeg: string;
    FUnitName: string;
    FDrawingCode: string;
    FFBFXName: string;

    function FindDetail(ADetailNode: TBillsIDTreeNode): TmgZJJLDetail;
    function NewDetail(ADetailNode: TMeasureBillsIDTreeNode): TmgZJJLDetail;

    function GetDrawingCode(ANode: TBillsIDTreeNode): string;
    function GetPeg(ANode: TBillsIDTreeNode): string;
    procedure FilterPeg;

    function GetDetail(AIndex: Integer): TmgZJJLDetail;
    function GetDetailCount: Integer;
  public
    constructor Create(ADetailNode, AGatherNode: TBillsIDTreeNode);
    destructor Destroy; override;

    function AddDetail(ADetailNode: TBillsIDTreeNode): TmgZJJLDetail;

    property B_Code: string read FB_Code;
    property Name: string read FName;
    property Units: string read FUnits;
    property Price: Double read FPrice;
    property GatherNode: TBillsIDTreeNode read FGatherNode;

    property BGLCode: string read FBGLCode;
    property BeginPeg: string read FBeginPeg;
    property EndPeg: string read FEndPeg;
    property DrawingCode: string read FDrawingCode;

    property DetailCount: Integer read GetDetailCount;
    property Detail[AIndex: Integer]: TmgZJJLDetail read GetDetail;
  end;

  TmgZJJLManager = class
  private
    FZJJLs: TList;

    function FindZJJL(ADetailNode, AGatherNode: TBillsIDTreeNode): TmgZJJL;
    function NewZJJL(ADetailNode, AGatherNode: TBillsIDTreeNode): TmgZJJL;

    function GetZJJL(AIndex: Integer): TmgZJJL;
    function GetZJJLCount: Integer;
  public
    constructor Create;
    destructor Destroy; override;

    function AddZJJL(ADetailNode, AGatherNode: TBillsIDTreeNode): TmgZJJL;
    function AddZJJLAndDetail(ADetailNode, AGatherNode: TBillsIDTreeNode): TmgZJJLDetail;

    property ZJJLCount: Integer read GetZJJLCount;
    property ZJJL[AIndex: Integer]: TmgZJJL read GetZJJL;
  end;

implementation

uses
  sdDB, mDataRecord, ZhAPI, UtilMethods, mPegFilter;

{ TmgZJJL }

function TmgZJJL.AddDetail(ADetailNode: TBillsIDTreeNode): TmgZJJLDetail;
begin
  Result := FindDetail(ADetailNode);
  if not Assigned(Result) then
    Result := NewDetail(TMeasureBillsIDTreeNode(ADetailNode));
end;

constructor TmgZJJL.Create(ADetailNode, AGatherNode: TBillsIDTreeNode);
begin
  FB_Code := ADetailNode.Rec.B_Code.AsString;
  FName := ADetailNode.Rec.Name.AsString;
  FUnits := ADetailNode.Rec.Units.AsString;
  FPrice := ADetailNode.Rec.Price.AsFloat;
  FGatherNode := AGatherNode;
  FDetails := TList.Create;
  AddDetail(ADetailNode);

  FDrawingCode := GetDrawingCode(AGatherNode);
  FilterPeg;
end;

destructor TmgZJJL.Destroy;
begin
  ClearObjects(FDetails);
  FDetails.Free;
  inherited;
end;

procedure TmgZJJL.FilterPeg;
var
  vPegFilter: TPegStrFilter;
begin
  vPegFilter := TPegStrFilter.Create;
  try
    vPegFilter.PegStr := GetPeg(FGatherNode);
    FBeginPeg := vPegFilter.BeginPeg;
    FEndPeg := vPegFilter.EndPeg;
  finally
    vPegFilter.Free;
  end;
end;

function TmgZJJL.FindDetail(ADetailNode: TBillsIDTreeNode): TmgZJJLDetail;
var
  i: Integer;
begin
  Result := nil;
  for i := 0 to DetailCount - 1 do
  begin
    if Detail[i].RelaNode = ADetailNode then
    begin
      Result := Detail[i];
      Break;
    end;
  end;
end;

function TmgZJJL.GetDetail(AIndex: Integer): TmgZJJLDetail;
begin
  Result := TmgZJJLDetail(FDetails.Items[AIndex]);
end;

function TmgZJJL.GetDetailCount: Integer;
begin
  Result := FDetails.Count;
end;

function TmgZJJL.GetDrawingCode(ANode: TBillsIDTreeNode): string;
begin
  Result := '';
  if Assigned(ANode) then
  begin
    Result := ANode.Rec.DrawingCode.AsString;
    if Result = '' then
      Result := GetDrawingCode(TBillsIDTreeNode(ANode.Parent));
  end;
end;

function TmgZJJL.GetPeg(ANode: TBillsIDTreeNode): string;
begin
  Result := '';
  if Assigned(ANode) then
  begin
    if CheckPeg(ANode.Rec.Name.AsString) then
      Result := ANode.Rec.Name.AsString
    else
      Result := GetPeg(TBillsIDTreeNode(ANode.Parent));
  end;
end;

function TmgZJJL.NewDetail(ADetailNode: TMeasureBillsIDTreeNode): TmgZJJLDetail;
begin
  Result := TmgZJJLDetail.Create(ADetailNode);
  FDetails.Add(Result);
  if Assigned(ADetailNode.StageRec) then
    FBGLCode := MergeRelaBGL(FBGLCode, ADetailNode.StageRec.QcBGLCode.AsString);
end;

{ TmgZJJLDetail }

constructor TmgZJJLDetail.Create(ADetailNode: TBillsIDTreeNode);
begin
  FRelaNode := ADetailNode;
end;

destructor TmgZJJLDetail.destroy;
begin

  inherited;
end;

{ TmgZJJLManager }

function TmgZJJLManager.AddZJJL(ADetailNode,
  AGatherNode: TBillsIDTreeNode): TmgZJJL;
begin
  Result := FindZJJL(ADetailNode, AGatherNode);
  if not Assigned(Result) then
    Result := NewZJJL(ADetailNode, AGatherNode);
end;

function TmgZJJLManager.AddZJJLAndDetail(ADetailNode,
  AGatherNode: TBillsIDTreeNode): TmgZJJLDetail;
var
  vZJJL: TmgZJJL;
begin
  vZJJL := AddZJJL(ADetailNode, AGatherNode);
  Result := vZJJL.AddDetail(ADetailNode);
end;

constructor TmgZJJLManager.Create;
begin
  FZJJLs := TList.Create;
end;

destructor TmgZJJLManager.Destroy;
begin
  ClearObjects(FZJJLs);
  FZJJLs.Free;
  inherited;
end;

function TmgZJJLManager.FindZJJL(ADetailNode,
  AGatherNode: TBillsIDTreeNode): TmgZJJL;
var
  i: Integer;
begin
  Result := nil;
  for i := 0 to ZJJLCount - 1 do
  begin
    if (ZJJL[i].B_Code = ADetailNode.Rec.B_Code.AsString) and
       (ZJJL[i].Name = ADetailNode.Rec.Name.AsString) and
       (ZJJL[i].Units = ADetailNode.Rec.Units.AsString) and
       (ZJJL[i].Price = ADetailNode.Rec.Price.AsFloat) and
       (ZJJL[i].GatherNode = AGatherNode) then
    begin
      Result := ZJJL[i];
      Break;
    end;
  end;
end;

function TmgZJJLManager.GetZJJL(AIndex: Integer): TmgZJJL;
begin
  Result := TmgZJJL(FZJJLs.Items[AIndex]);
end;

function TmgZJJLManager.GetZJJLCount: Integer;
begin
  Result := FZJJLs.Count;
end;

function TmgZJJLManager.NewZJJL(ADetailNode,
  AGatherNode: TBillsIDTreeNode): TmgZJJL;
begin
  Result := TmgZJJL.Create(ADetailNode, AGatherNode);
  FZJJLs.Add(Result);
end;

end.
