unit tpMainData;

interface

uses
  Classes, tpBaseGatherData, tpSelectTenderDm, sdIDTree, tpSelectTenderNode,
  tpPartTender, tpPartTenderSet;

type
  TtpMainData = class
  private
    FHistoryTenders: TList;
    FTenders: TList;
    // Memory DataSet -- For Display
    // Display FTenders
    FSelectTenderData: TtpSelectTenderData;

    FBaseGatherData: TtpBaseGatherData;
    FPartTrial: TtpPartTender;
    FPartTenderSet: TtpPartTenderSet;

    procedure ClearAllHistoryData;
  public
    constructor Create;
    destructor Destroy; override;

    procedure GatherProjects(AProjects: TList);
    procedure TrialPeg;
    procedure PricePart;
    procedure PegPart;

    property Tenders: TList read FTenders;                          
    property SelectTenderData: TtpSelectTenderData read FSelectTenderData;

    property BaseGatherData: TtpBaseGatherData read FBaseGatherData;
    property PartTrial: TtpPartTender read FPartTrial;
    property PartTenderSet: TtpPartTenderSet read FPartTenderSet;
  end;

implementation

uses
  ZhAPI, Globals, mPegFilter, tpTrialPegInputFrm, UtilMethods,
  Math;

{ TtpMainData }

procedure TtpMainData.ClearAllHistoryData;
begin
  FTenders.Clear;
  FPartTrial.Clear;
end;

constructor TtpMainData.Create;
begin
  FHistoryTenders := TList.Create;
  FTenders := TList.Create;
  FSelectTenderData := TtpSelectTenderData.Create(nil);

  FBaseGatherData := TtpBaseGatherData.Create;
  FPartTrial := TtpPartTender.Create;
  FPartTenderSet := TtpPartTenderSet.Create;
end;

destructor TtpMainData.Destroy;
begin
  FPartTenderSet.Free;
  FPartTrial.Free;
  FBaseGatherData.Free;

  FSelectTenderData.Free;
  ClearObjects(FTenders);
  FTenders.Free;
  FHistoryTenders.Free;
  inherited;
end;

procedure TtpMainData.GatherProjects(AProjects: TList);

  function SortTenders(AItem1, AItem2: Pointer): Integer;
  begin
    Result := TtpSelectTenderNode(AItem1).SerialNo - TtpSelectTenderNode(AItem2).SerialNo;
  end;

  procedure FilterTenders;
  var
    i: Integer;
    stnNode: TsdIDTreeNode;
    vTender: TtpSelectTenderNode;
  begin
    for i := 0 to AProjects.Count - 1 do
    begin
      stnNode := ProjectManager.ProjectsTree.FindNode(Integer(AProjects.Items[i]));
      if Assigned(stnNode) and (stnNode.Rec.ValueByName('Type').AsInteger = 1) then
      begin
        vTender := TtpSelectTenderNode.Create(stnNode);
        FTenders.Add(vTender);
        FTenders.Sort(@SortTenders);
      end;
    end;
  end;

  function CheckSameTenders: Boolean;
  var
    i: Integer;
    vOrg, vNew: TtpSelectTenderNode;
  begin
    if FHistoryTenders.Count = FTenders.Count then
    begin
      Result := True;
      for i := 0 to FHistoryTenders.Count - 1 do
      begin
        vOrg := TtpSelectTenderNode(FHistoryTenders.Items[i]);
        vNew := TtpSelectTenderNode(FTenders.Items[i]);
        if vOrg.ID <> vNew.ID then
        begin
          Result := False;
          Break;
        end;
      end;
    end
    else
      Result := False;
  end;

begin
  ClearAllHistoryData;
  FilterTenders;
  if not CheckSameTenders then
  begin
    FBaseGatherData.GatherTenders(FTenders);
    FSelectTenderData.AssignSelectTenders(FTenders);
    ClearObjects(FHistoryTenders);
    FHistoryTenders.Assign(FTenders);
  end;
end;

procedure TtpMainData.PegPart;
begin
  if FBaseGatherData.Pegs.Count > 0 then
    FPartTenderSet.PegPart(FBaseGatherData.Pegs)
  else
    WarningMessage('无桩号数据，无法划分');
end;

procedure TtpMainData.PricePart;
begin
  if FBaseGatherData.Pegs.TotalPrice > 0 then
    FPartTenderSet.PricePart(FBaseGatherData.Pegs)
  else
    WarningMessage('当前可划分的桩号节点金额为0，无法通过金额模式进行划分。');
end;

procedure TtpMainData.TrialPeg;
var
  sPeg: string;
begin
  if InputTrialPeg(sPeg) then
  begin
    PegFilter.Filter(sPeg);
    FPartTrial.FilterPegs(PegFilter, BaseGatherData.Pegs);
  end;
end;

end.
