unit ReportInteractInfo;

interface

uses
  Classes, ScFileArchiver, XmlDoc, XmlIntf;

Type
  TInteractInfo = class
  private
    FTemplateFile: string;
    FInteractFlag: Integer;
    FInteractSubFlag: Integer;

    FMemoryStream: TMemoryStream;
    FAudits: TStringList;
    FSql: string;
    FSpecialProjGatherTypes: TStringList;

    procedure LoadReportTemplate;

    procedure LoadAuditor(AXmlNode: IXMLNode);
    procedure LoadSql(AXmlNode: IXMLNode);
    procedure LoadSpecialProjGatherTypes(AXmlNode: IXMLNode);

    procedure LoadInteractInfo(AXmlNode: IXMLNode);
  public
    constructor Create(const AFileName: string);
    destructor Destroy; override;

    procedure LoadXmlFromStream;

    property Audits: TStringList read FAudits;
    property Sql: string read FSql;
    property SpecialProjGatherTypes: TStringList read FSpecialProjGatherTypes;
  end;

implementation

uses
  UtilMethods;

{ TInteractInfo }

constructor TInteractInfo.Create(const AFileName: string);
begin
  FTemplateFile := AFileName;
  FAudits := TStringList.Create;
  FSpecialProjGatherTypes := TStringList.Create;
  LoadReportTemplate;
  LoadXmlFromStream;
end;

destructor TInteractInfo.Destroy;
begin
  FMemoryStream.Free;
  inherited;
end;

procedure TInteractInfo.LoadAuditor(AXmlNode: IXMLNode);
var
  vAudits: IXMLNodeList;
  vAudit: IXMLNode;
  i: Integer;
begin
  if AXmlNode = nil then Exit;

  FAudits.Clear;
  vAudits := AXmlNode.ChildNodes;
  for i := 0 to vAudits.Count - 1 do
  begin
    vAudit := AXmlNode.ChildNodes.Nodes[i];
    if vAudit.HasAttribute('职位') then
      FAudits.Add(vAudit.Attributes['职位']);
  end;
end;

procedure TInteractInfo.LoadInteractInfo(AXmlNode: IXMLNode);
var
  XmlNode: IXMLNode;
begin
  XmlNode := AXmlNode.ChildNodes.FindNode('附加信息');
  LoadAuditor(XmlNode.ChildNodes.FindNode('审核意见'));
  LoadSql(XmlNode.ChildNodes.FindNode('查询语句'));
  LoadSpecialProjGatherTypes(XmlNode.ChildNodes.FindNode('特殊汇总项目'));
end;

procedure TInteractInfo.LoadReportTemplate;
var
  vArchiver: TReportArchiver;
begin
  vArchiver := TReportArchiver.Create;
  vArchiver.FileName := FTemplateFile;
  FMemoryStream := vArchiver.Extract;
end;

procedure TInteractInfo.LoadSpecialProjGatherTypes(AXmlNode: IXMLNode);
var
  vSpecialProjs: IXMLNodeList;
  vSpecialProj: IXMLNode;
  i: Integer;
begin
  if AXmlNode = nil then Exit;

  FSpecialProjGatherTypes.Clear;
  vSpecialProjs := AXmlNode.ChildNodes;
  for i := 0 to vSpecialProjs.Count - 1 do
  begin
    vSpecialProj := vSpecialProjs.Nodes[i];
    if vSpecialProj.HasAttribute('类型') then
      FSpecialProjGatherTypes.Add(vSpecialProj.Attributes['类型']);
  end;
end;

procedure TInteractInfo.LoadSql(AXmlNode: IXMLNode);
begin
  if AXmlNode = nil then Exit;
  if AXmlNode.HasAttribute('Sql语句') then
    FSql := AXmlNode.Attributes['Sql语句'];
end;

procedure TInteractInfo.LoadXmlFromStream;
var
  FXmlDocument: IXMLDocument;
begin
  FXmlDocument := TXMLDocument.Create(nil) as IXMLDocument;
  try
    FXmlDocument.Options := [doNodeAutoCreate,doNodeAutoIndent,doAutoPrefix,doNamespaceDecl];
    if not Assigned(FMemoryStream) then Exit;
    FXmlDocument.LoadFromStream(FMemoryStream);
    LoadInteractInfo(FXmlDocument.DocumentElement);
  finally
    FXmlDocument := nil;
  end;
end;

end.
