{*******************************************************************************
  单元名称：  Checker.pas

  单元说明：  审核人类、管理类。

  作者时间：  Chenshilong, 2015-04-20
*******************************************************************************}

unit Checker;

interface

uses
  Classes, SysUtils;

type

  TChecker = class(TObject)			// 审核人类
  private
    FID: Integer;
    FRole: string;
    FMemo: string;
    FName: string;
    FNo: Integer;
    FCheckedDateTime: string;
    procedure SetID(const Value: Integer);
    procedure SetMemo(const Value: string);
    procedure SetName(const Value: string);
    procedure SetRole(const Value: string);
    procedure SetNo(const Value: Integer);
  public
    property No: Integer read FNo write SetNo;         // 序号（审核人1、审核人2...）
    property ID: Integer read FID write SetID;         // 审核人ID
    property Name: string read FName write SetName;    // 审核人姓名
    property Role: string read FRole write SetRole;    // 审核人职务
    property Memo: string read FMemo write SetMemo;    // 审核人审核意见
    property CheckedDateTime: string read FCheckedDateTime write FCheckedDateTime;
    constructor Create;
    destructor Destroy; override;
  end;

  TCheckers = class(TObject)			// 审核人管理类
  private
    FList: TList;
    FLastChecker: TChecker;
    function GetItem(Index: Integer): TChecker;
    function GetCount: Integer;
  public
    constructor Create;
    destructor Destroy; override;
    function Add(AChecker: TChecker): Integer; overload;
    function Add(AID: Integer; AName, ARole, AMemo, ATime: string): Integer; overload;
    procedure Delete(AIndex: Integer); overload;
    procedure Delete(AChecker: TChecker); overload;
    procedure Clear;
    procedure Exchange(AChecker1, AChecker2: TChecker);
    function FindByID(AID: Integer): TChecker;
    property Item[Index: Integer]: TChecker read GetItem; default;
    property Count: Integer read GetCount;
    property LastChecker: TChecker read FLastChecker write FLastChecker;
  end;

implementation

uses UtilMethods;


{ TCheckers }

function TCheckers.Add(AID: Integer; AName, ARole, AMemo, ATime: string): Integer;
var vCM: TChecker;
begin
  vCM := TChecker.Create;
  Result := Add(vCM);
  vCM.No := Result;
  vCM.ID := AID;
  vCM.Name := AName;
  vCM.Role := ARole;
  vCM.Memo := RecoverCharsFromJson(AMemo);
  vCM.CheckedDateTime := ATime;
end;

function TCheckers.Add(AChecker: TChecker): Integer;
begin
  Result := FList.Add(AChecker);
end;

procedure TCheckers.Clear;
var 
  i: Integer;
begin
  for i := 0 to FList.Count - 1 do
    TChecker(FList[i]).Free;

  FList.Clear;
end;

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

procedure TCheckers.Delete(AIndex: Integer);
begin
  TChecker(FList[AIndex]).Free;
  FList.Delete(AIndex);
end;

procedure TCheckers.Delete(AChecker: TChecker);
begin
  FList.Remove(AChecker);
end;

destructor TCheckers.Destroy;
begin
  Clear;
  FList.Free;
  inherited;
end;

procedure TCheckers.Exchange(AChecker1,
  AChecker2: TChecker);
var
  idx1, idx2: Integer;
begin
  idx1 := FList.IndexOf(AChecker1);
  idx2 := FList.IndexOf(AChecker2);
  if idx1 < 0 then Exit;
  if idx2 < 0 then Exit;

  FList.Exchange(idx1, idx2);
end;

function TCheckers.FindByID(AID: Integer): TChecker;
var i: Integer;
begin
  Result := nil;
  for i := 0 to FList.Count - 1 do
  begin
    if TChecker(FList[i]).ID = AID then
    begin
      Result := TChecker(FList[i]);
      Break;
    end;
  end;
end;

function TCheckers.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TCheckers.GetItem(Index: Integer): TChecker;
begin
  Result := nil;
  if (Index > -1) and (Index < FList.Count) then
    Result := TChecker(FList[Index]);
end;

{ TChecker }

constructor TChecker.Create;
begin

end;

destructor TChecker.Destroy;
begin

  inherited;
end;

procedure TChecker.SetID(const Value: Integer);
begin
  FID := Value;
end;

procedure TChecker.SetMemo(const Value: string);
begin
  FMemo := StringReplace(Value, '\r\n', '|', [rfReplaceAll, rfIgnoreCase]);
end;

procedure TChecker.SetName(const Value: string);
begin
  FName := Value;
end;

procedure TChecker.SetNo(const Value: Integer);
begin
  FNo := Value;
end;

procedure TChecker.SetRole(const Value: string);
begin
  FRole := Value;
end;

end.
