unit stgGatherDm;
// 结果展示，内存表

interface

uses
  SysUtils, Classes, stgGatherCacheData, sdDB, sdProvider, stgGatherUtils;

type
  TstgGatherData = class(TDataModule)
    smpGatherTree: TsdMemoryProvider;
    sddGatherTree: TsdDataSet;
    sdvGatherTree: TsdDataView;
    smpSubTenders: TsdMemoryProvider;
    sddSubTenders: TsdDataSet;
    sdvSubTenders: TsdDataView;
    smpBillsDetail: TsdMemoryProvider;
    sddBillsDetail: TsdDataSet;
    sdvBillsDetail: TsdDataView;
    sdmErrorDetail: TsdMemoryProvider;
    sddErrorDetail: TsdDataSet;
    sdvErrorDetail: TsdDataView;
    procedure sdvBillsDetailGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
    procedure sdvBillsDetailFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvGatherTreeCurrentChanged(ARecord: TsdDataRecord);
    procedure sdvGatherTreeGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
    procedure sdvErrorDetailFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvSubTendersCurrentChanged(ARecord: TsdDataRecord);
    procedure sdvErrorDetailGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
  private
    procedure LoadSubTenders(ACacheData: TstgGatherCacheData);

    procedure LoadGatherTreeNodeDetail(ANode: TstgGatherTreeNode);
    procedure LoadGatherTreeNode(ANode: TstgGatherTreeNode);
    procedure LoadGatherTree(ACacheData: TstgGatherCacheData);

    procedure LoadError(AError: TstgErrorInfo);
    procedure LoadErrors(ACacheData: TstgGatherCacheData);
  public
    procedure LoadGatherData(ACacheData: TstgGatherCacheData);
  end;

implementation

uses
  Variants, DB, ConditionalDefines;

{$R *.dfm}

{ TstgGatherData }

procedure TstgGatherData.LoadGatherData(ACacheData: TstgGatherCacheData);
begin
  LoadSubTenders(ACacheData);
  LoadGatherTree(ACacheData);
  LoadErrors(ACacheData);

  sdvGatherTree.Active := True;
  sdvBillsDetail.Active := True;
  sdvBillsDetail.RefreshFilter;

  sdvSubTenders.Active := True;
  sdvErrorDetail.Active := True;
  sdvErrorDetail.RefreshFilter;
end;

procedure TstgGatherData.LoadGatherTree(ACacheData: TstgGatherCacheData);
var
  i: Integer;
  vNode:TstgGatherTreeNode;
begin
  if _IsDebugView then
    ACacheData.GatherTree.SaveTreeToFile('E:\stgGatherTree.txt');
  for i := 0 to ACacheData.GatherTree.CacheNodes.Count - 1 do
  begin
    vNode := TstgGatherTreeNode(ACacheData.GatherTree.CacheNodes.Items[i]);
    LoadGatherTreeNode(vNode);
  end;
end;

procedure TstgGatherData.LoadGatherTreeNode(ANode: TstgGatherTreeNode);
var
  Rec: TsdDataRecord;
begin
  Rec := sddGatherTree.Add;
  Rec.ValueByName('ID').AsInteger := ANode.ID;
  Rec.ValueByName('ParentID').AsInteger := ANode.ParentID;
  Rec.ValueByName('NextSiblingID').AsInteger := ANode.NextSiblingID;
  Rec.ValueByName('Code').AsString := ANode.Code;
  Rec.ValueByName('B_Code').AsString := ANode.B_Code;
  Rec.ValueByName('Name').AsString := ANode.Name;
  Rec.ValueByName('Units').AsString := ANode.Units;
  Rec.ValueByName('IsSubTender').AsBoolean := ANode.IsSubTender;
  Rec.ValueByName('IsSumBase').AsBoolean := ANode.IsSumBase;
  Rec.ValueByName('IsLeaf').AsBoolean := ANode.IsLeaf;

  Rec.ValueByName('DealQuantity').AsFloat := ANode.Gather.DealQuantity;
  Rec.ValueByName('DealTotalPrice').AsFloat := ANode.Gather.DealTotalPrice;
  Rec.ValueByName('QcQuantity').AsFloat := ANode.Gather.QcQuantity;
  Rec.ValueByName('QcTotalPrice').AsFloat := ANode.Gather.QcTotalPrice;
  Rec.ValueByName('QcBGLCode').AsString := ANode.Gather.QcBGLCode;
  Rec.ValueByName('QcBGLNum').AsString := ANode.Gather.QcBGLNum;
  if ANode.Children.Count = 0 then
    LoadGatherTreeNodeDetail(ANode);
end;

procedure TstgGatherData.LoadGatherTreeNodeDetail(
  ANode: TstgGatherTreeNode);
var
  iSub, iDetail: Integer;
  vSub: TstgSubTenderStageData;
  vDetail: TstgSubTenderDetailData;
  Rec: TsdDataRecord;
  sSubTenderName: string;
begin
  for iSub := 0 to ANode.SubTenderCount - 1 do
  begin
    vSub := ANode.SubTender[iSub];
    for iDetail := 0 to vSub.DetailCount - 1 do
    begin
      vDetail := vSub.Detail[iDetail];
      Rec := sddBillsDetail.Add;
      Rec.ValueByName('BillsID').AsInteger := ANode.ID;
      Rec.ValueByName('TenderID').AsInteger := vSub.SubTenderID;
      Rec.ValueByName('TenderName').AsString := '';
      Rec.ValueByName('SerialNo').AsInteger := vDetail.SerialNo;
      Rec.ValueByName('DealQuantity').AsFloat := vDetail.DetailStage.DealQuantity;
      Rec.ValueByName('DealTotalPrice').AsFloat := vDetail.DetailStage.DealTotalPrice;
      Rec.ValueByName('QcQuantity').AsFloat := vDetail.DetailStage.QcQuantity;
      Rec.ValueByName('QcTotalPrice').AsFloat := vDetail.DetailStage.QcTotalPrice;
      Rec.ValueByName('QcBGLCode').AsString := vDetail.DetailStage.QcBGLCode;
      Rec.ValueByName('QcBGLNum').AsString := vDetail.DetailStage.QcBGLNum;
    end;
  end;
end;

procedure TstgGatherData.LoadSubTenders(ACacheData: TstgGatherCacheData);
var
  i: Integer;
  vSubTender: TstgGatherSubTender;
  Rec: TsdDataRecord;
begin
  for i := 0 to ACacheData.SubTenderCount - 1 do
  begin
    vSubTender := ACacheData.SubTender[i];
    Rec := sddSubTenders.Add;
    Rec.ValueByName('ID').AsInteger := vSubTender.ID;
    Rec.ValueByName('Name').AsString := vSubTender.Rec.ValueByName('Name').AsString;
  end;
end;

procedure TstgGatherData.sdvBillsDetailGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);

  function GetTenderName(ATenderID: Integer): string;
  begin
    Result := VarToStrDef(sddSubTenders.Lookup('ID', ATenderID, 'Name'), '');
  end;

begin
  if DisplayText and (AColumn.FieldName = 'TenderName') then
    Text := GetTenderName(ARecord.ValueByName('TenderID').asInteger);
end;

procedure TstgGatherData.sdvBillsDetailFilterRecord(ARecord: TsdDataRecord;
  var Allow: Boolean);
begin
  if Assigned(sdvGatherTree.Current) then
    Allow := ARecord.ValueByName('BillsID').AsInteger = sdvGatherTree.Current.ValueByName('ID').AsInteger
  else
    Allow := False;
end;

procedure TstgGatherData.sdvGatherTreeCurrentChanged(
  ARecord: TsdDataRecord);
begin
  sdvBillsDetail.RefreshFilter;
end;

procedure TstgGatherData.sdvGatherTreeGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);
begin
  if (AValue.DataType = ftFloat) and (AValue.AsFloat = 0) then
  begin
    Text := '';
  end;
end;

procedure TstgGatherData.LoadErrors(ACacheData: TstgGatherCacheData);
var
  i: Integer;
begin
  for i := 0 to ACacheData.ErrorCount - 1 do
    LoadError(ACacheData.Error[i]);
end;

procedure TstgGatherData.sdvErrorDetailFilterRecord(ARecord: TsdDataRecord;
  var Allow: Boolean);
begin
  if Assigned(sdvSubTenders.Current) then
    Allow := ARecord.ValueByName('TenderID').AsInteger = sdvSubTenders.Current.ValueByName('ID').AsInteger
  else
    Allow := False;
end;

procedure TstgGatherData.LoadError(AError: TstgErrorInfo);

  function GetSubTenderSerialNo(ANode: TstgGatherTreeNode; ATenderID: Integer): Integer;
  var
    vSource: TstgSubTenderStageData;
  begin
    vSource := ANode.FindSubTender(ATenderID);
    if Assigned(vSource) then
      Result := vSource.Detail[0].SerialNo;
  end;

var
  i, j, k: Integer;
  vRelaDetail: TstgGatherTreeNode;
  vRelaDetailStage: TstgSubTenderStageData;
  Rec: TsdDataRecord;
begin
  for i := 0 to AError.DetailCount - 1 do
  begin
    vRelaDetail := AError.Detail[i];
    for j := 0 to vRelaDetail.SubTenderCount - 1 do
    begin
      vRelaDetailStage := vRelaDetail.SubTender[j];
      for k := 0 to vRelaDetailStage.DetailCount - 1 do
      begin
        Rec := sddErrorDetail.Add;
        Rec.ValueByName('TenderID').AsInteger := vRelaDetail.SubTender[j].SubTenderID;
        Rec.ValueByName('RelaCode').AsString := AError.RelaNode.Code + AError.RelaNode.B_Code;
        if AError.RelaNode.IsLeaf then
          Rec.ValueByName('RelaSerialNo').AsInteger := vRelaDetailStage.Detail[k].SerialNo
        else
          Rec.ValueByName('RelaSerialNo').AsInteger := GetSubTenderSerialNo(AError.RelaNode, vRelaDetailStage.SubTenderID);
        Rec.ValueByName('DetailCode').AsString := vRelaDetail.Code + vRelaDetail.B_Code;
        Rec.ValueByName('DetailSerialNo').AsInteger := vRelaDetailStage.Detail[k].SerialNo;
        Rec.ValueByName('ErrorType').AsInteger := AError.ErrorType;
      end;
    end;
  end;
end;

procedure TstgGatherData.sdvSubTendersCurrentChanged(
  ARecord: TsdDataRecord);
begin
  sdvErrorDetail.RefreshFilter;
end;

procedure TstgGatherData.sdvErrorDetailGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);

  function GetTenderName(ATenderID: Integer): string;
  begin
    Result := VarToStrDef(sddSubTenders.Lookup('ID', ATenderID, 'Name'), '');
  end;

begin
  if DisplayText then
  begin
    if (AColumn.FieldName = 'TenderName') then
      Text := GetTenderName(ARecord.ValueByName('TenderID').asInteger)
    else if (AColumn.FieldName = 'ErrorType') then
      Text := GetErrorTypeText(AValue.AsInteger);
  end;
end;

end.
