unit SignReports;

interface

uses
  Classes, CslJson, superobject;

type
  TSignReport = class
  private
    FPhaseNo: Integer;
    FName: string;
    FOwnerID: Integer;
  public
    constructor Create(APhaseNo: Integer; const AName: string; AOwnerID: Integer);

    property PhaseNo: Integer read FPhaseNo;
    property Name: string read FName;
    property OwnerID: Integer read FOwnerID;
  end;

  TSignReports = class
  private
    FList: TList;

    procedure LoadSignReport(AStr: string);
    procedure LoadSignReports(AInfos: TStrings);
  public
    constructor Create;
    destructor Destroy; override;

    function AddSignReport(APhaseNo: Integer; const AName: string; AOwnerID: Integer): TSignReport;
    function LoadAllSignReports(const AUrl: string; AProjWebID, APhaseNo: Integer): Boolean;

    function FindSignReport(APhaseNo: Integer; const AName: string): TSignReport;
  end;

implementation

uses
  PHPWebDm, UtilMethods, ZhAPI, SysUtils;

{ TSignReports }

function TSignReports.AddSignReport(APhaseNo: Integer; const AName: string;
  AOwnerID: Integer): TSignReport;
var
  vSignReport: TSignReport;
begin
  vSignReport := TSignReport.Create(APhaseNo, AName, AOwnerID);
  FList.Add(vSignReport);
end;

constructor TSignReports.Create;
begin
  FList := TList.Create;
end;

destructor TSignReports.Destroy;
begin
  FList.Free;
  inherited;
end;

function TSignReports.FindSignReport(APhaseNo: Integer;
  const AName: string): TSignReport;
var
  i: Integer;
  vSignReport: TSignReport;
begin
  Result := nil;
  for i := 0 to FList.Count - 1 do
  begin
    vSignReport := TSignReport(FList.Items[i]);
    if (vSignReport.PhaseNo = APhaseNo) and (vSignReport.Name = AName) then
    begin
      Result := vSignReport;
      Break;
    end;
  end;
end;

function TSignReports.LoadAllSignReports(const AUrl: string; AProjWebID,
  APhaseNo: Integer): Boolean;
var
  iResult: Integer;
  sgsUrlResult: TStrings;
begin
  sgsUrlResult := TStringList.Create;
  try
    sgsUrlResult.Add(Format('tender=%d', [AProjWebID]));
    sgsUrlResult.Add(Format('phaseno=%d', [APhaseNo]));
    iResult := PHPWeb.UrlGet(AUrl, sgsUrlResult, sgsUrlResult);
    case iResult of
      1: LoadSignReports(sgsUrlResult);
      0: WarningMessage('网络错误：' + sgsUrlResult[0]);
      -1: WarningMessage('网络错误：无法连接到云端');
    end;
    Result := iResult = 1;
  finally
    sgsUrlResult.Free;
  end;
end;

procedure TSignReports.LoadSignReport(AStr: string);
var
  vJ: TCslJson;
  sPhaseNo, sName, sOwnerID: string;
  iPhaseNo, iOwnerID: Integer;
  //iSO: ISuperObject;
begin
  {*
  try
    iSO := SO(AStr);
    sPhaseNo := iSO['phaseno'].AsString;
    sName := iSO['name'].AsString;
    sOwnerID := iSO['ownuid'].AsString;
    if (sPhaseNo <> '') and TryStrToInt(sPhaseNo, iPhaseNo) and (sName <> '') and (sOwnerID <> '') and TryStrToInt(sOwnerID, iOwnerID) then
      AddSignReport(iPhaseNo, sName, iOwnerID);
  finally
    iSO := nil;
  end;
  *}
  vJ := TCslJson.Create;
  try
    vJ.Text := AStr;
    sPhaseNo := vJ.Value['phaseno'];
    sName := vJ.Value['name'];
    sOwnerID := vJ.Value['ownuid'];
    if (sPhaseNo <> '') and TryStrToInt(sPhaseNo, iPhaseNo) and (sName <> '') and (sOwnerID <> '') and TryStrToInt(sOwnerID, iOwnerID) then
      AddSignReport(iPhaseNo, sName, iOwnerID);
  finally
    vJ.Free;
  end;
end;

procedure TSignReports.LoadSignReports(AInfos: TStrings);
var
  i: Integer;
begin
  ClearObjects(FList);
  for i := 0 to AInfos.Count - 1 do
    LoadSignReport(AInfos[i]);
end;

{ TSignReport }

constructor TSignReport.Create(APhaseNo: Integer; const AName: string;
  AOwnerID: Integer);
begin
  FPhaseNo := APhaseNo;
  FName := AName;
  FOwnerID := AOwnerID;
end;

end.
