{----------------------------------------------------------------
交互报表 -- 审核人选择窗口

交互报表中，所有须选择审核人的报表，均可使用该界面，其他情况禁止使用
即 InteractFlag[交互类型]为1时 可用。

将交互报表中的角色名填入第一列，用户在第二列下拉选择审核人

[项目数据库Proj, 当前期数据库Phase, 交互表内容保存至RD数据表]
其中Proj和Phase中均存有1份RD

整个流程可大致分解为4步：
1. 用户切换至报表界面时，将[Phase]RD -拷贝-> [Proj]RD
2. 用户点击该报表时，加载[Proj]RD数据至该窗口
3. 用户可在该窗口更改选择的审核人，用户确认后，将数据保存至[Proj]RD中
4. 用户关闭报表界面时，将[Proj]RD -拷贝-> [Phase]RD
-----------------------------------------------------------------}

unit AuditSelectFrm;

interface

uses
  ProjectData, ReportManager, ADODB, Checker,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ZJGrid, ZJEdits, ZJCells;

const
  AuditSelectSql = 'Select SerialNo, Select1, Result1, Result2' +
                   '  From P_ReportData' +
                   '  Where (Flag = %d) and (SubFlag = %d)' +
                   '  Order By SerialNo';

  AuditDeleteSql = 'Delete From %s' +
                   '  Where (Flag = %d) and (SubFlag = %d)';

  AuditInsertSql = 'Insert Into %s' +
                   '  (Flag, SubFlag, SerialNo, Select1, Result1, Result2)' +
                   '  Values (%d, %d, %d, ''%s'', ''%s'', ''%s'')';

type
  TAuditSelctForm = class(TForm)
    zgAuditSelect: TZJGrid;
    pnlBottom: TPanel;
    btnOk: TButton;
    btnCancel: TButton;
    pnlSelectAudit: TPanel;
    cbbAuditors: TZjComboBox;
    procedure zgAuditSelectGetCellEditor(Sender: TObject; ACoord: TPoint;
      var AControl: TWinControl);
    procedure zgAuditSelectEditorLoadCell(Sender: TObject; ACoord: TPoint;
      AControl: TWinControl);
    procedure zgAuditSelectEditorSaveCell(Sender: TObject; ACoord: TPoint;
      AControl: TWinControl);
    procedure zgAuditSelectGetCellText(Sender: TObject;
      const ACoord: TPoint; var Value: String; DisplayText: Boolean);
  private
    FProjectData: TProjectData;
    FReportTemplate: TTemplateNode;

    FProjQuery: TADOQuery;
    FPhaseQuery: TADOQuery;

    procedure LoadReportAudits;
    procedure LoadHistorySelectAudits;
    procedure LoadPhaseAuditors;
    procedure InitAuditSelectGrid;
  public
    constructor Create(AProjectData: TProjectData; ATemplateNode: TTemplateNode);
    destructor Destroy; override;

    procedure SaveAuditData;
  end;

implementation

uses ReportInteractInfo, PhaseData;

{$R *.dfm}

{ TAuditSelctForm }

constructor TAuditSelctForm.Create(AProjectData: TProjectData;
  ATemplateNode: TTemplateNode);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
  FReportTemplate := ATemplateNode;

  FProjQuery := TADOQuery.Create(nil);
  FProjQuery.Connection := FProjectData.ADOConnection;
  FPhaseQuery := TADOQuery.Create(nil);
  FPhaseQuery.Connection := FProjectData.PhaseData.ADOConnection;

  InitAuditSelectGrid;

  LoadReportAudits;
  LoadPhaseAuditors;
  LoadHistorySelectAudits;
end;

destructor TAuditSelctForm.Destroy;
begin
  inherited;
end;

procedure TAuditSelctForm.InitAuditSelectGrid;
begin
  zgAuditSelect.RowCount := 1;
  zgAuditSelect.Cells[1, 0].Text := '需选择信息';
  zgAuditSelect.ColWidths[1] := 150;
  zgAuditSelect.Cells[2, 0].Text := '审核人';
  zgAuditSelect.ColWidths[2] := 200;

  zgAuditSelect.CellClass.Cols[2] := TZjComboCell;
end;

procedure TAuditSelctForm.LoadHistorySelectAudits;
var
  i, iRow: Integer;
begin
  FProjQuery.Close;
  FProjQuery.SQL.Clear;
  FProjQuery.SQL.Add(Format(AuditSelectSql, [FReportTemplate.InteractFlag, FReportTemplate.InteractSubFlag]));
  FProjQuery.Open;

  FProjQuery.First;
  while not FProjQuery.Eof do
  begin
    iRow := FProjQuery.FieldByName('SerialNo').AsInteger;
    zgAuditSelect.Cells[2, iRow].Data := Pointer(StrToIntDef(FProjQuery.FieldByName('Select1').AsString, -1));
    FProjQuery.Next;
  end;
end;

procedure TAuditSelctForm.LoadPhaseAuditors;
var
  i: Integer;
  Auditor: TChecker;
begin
  cbbAuditors.Clear;
  for i := 0 to FProjectData.Checkers.Count - 1 do
  begin
    Auditor := FProjectData.Checkers.Item[i];
    if Auditor.Role = '' then
      cbbAuditors.Items.Add(Auditor.Name)
    else
      cbbAuditors.Items.Add(Format('%-10s[%s]', [Auditor.Name, Auditor.Role]));
  end;
end;

procedure TAuditSelctForm.LoadReportAudits;
var
  i: Integer;
begin
  if Assigned(FReportTemplate.InteractInfo) then
  begin
    with FReportTemplate.InteractInfo do
    begin
      zgAuditSelect.RowCount := Audits.Count + 1;
      for i := 0 to Audits.Count - 1 do
      begin
        zgAuditSelect.Cells[1, i+1].Text := Audits.Strings[i];
        zgAuditSelect.Cells[1, i+1].Align := gaCenterLeft;
        zgAuditSelect.Cells[2, i+1].Data := Pointer(-1);
        zgAuditSelect.Cells[2, i+1].Align := gaCenterLeft;
      end;
    end;
  end;
end;

procedure TAuditSelctForm.SaveAuditData;

  procedure UpdateDataBase(const ATableName: string; AQuery: TADOQuery);

    procedure ExecuteSql(const ASql: string);
    begin
      AQuery.Close;
      AQuery.SQL.Clear;
      AQuery.SQL.Add(ASql);
      AQuery.ExecSQL;
    end;

    procedure DeleteOldData;
    var
      sSql: string;
    begin
      sSql := Format(AuditDeleteSql, [ATableName, FReportTemplate.InteractFlag, FReportTemplate.InteractSubFlag]);
      ExecuteSql(sSql);
    end;

    procedure InsertNewData;
    var
      sSql: string;
      iRow, iAuditor: Integer;
      Auditor: TChecker;
    begin
      for iRow := 1 to zgAuditSelect.RowCount - 1 do
      begin
        iAuditor := Integer(zgAuditSelect.Cells[2, iRow].Data);
        if iAuditor <> -1 then
        begin
          Auditor := FProjectData.Checkers.Item[iAuditor];
          sSql := Format(AuditInsertSql, [ATableName, FReportTemplate.InteractFlag,
            FReportTemplate.InteractSubFlag, iRow, IntToStr(Auditor.No), Auditor.Memo, Auditor.Name])
        end
        else
          sSql := Format(AuditInsertSql, [ATableName, FReportTemplate.InteractFlag,
            FReportTemplate.InteractSubFlag, iRow, '-1', '', '']);
        ExecuteSql(sSql);
      end;
    end;

  begin
    DeleteOldData;
    InsertNewData;
  end;

begin
  UpdateDataBase('P_ReportData', FProjQuery);
  UpdateDataBase('ReportData', FPhaseQuery);
end;

procedure TAuditSelctForm.zgAuditSelectGetCellEditor(Sender: TObject;
  ACoord: TPoint; var AControl: TWinControl);
begin
  if (ACoord.X = 2) and (ACoord.Y > 0) then
    AControl := cbbAuditors;
end;

procedure TAuditSelctForm.zgAuditSelectEditorLoadCell(Sender: TObject;
  ACoord: TPoint; AControl: TWinControl);
var
  iAuditorIndex: Integer;
begin
 if AControl = cbbAuditors then
 begin
   iAuditorIndex := Integer(zgAuditSelect.Cells[ACoord.X, ACoord.Y].Data);
   if iAuditorIndex < cbbAuditors.Items.Count then
     cbbAuditors.ItemIndex := iAuditorIndex
   else
     cbbAuditors.ItemIndex := -1;
 end;
end;

procedure TAuditSelctForm.zgAuditSelectEditorSaveCell(Sender: TObject;
  ACoord: TPoint; AControl: TWinControl);
begin
  if AControl = cbbAuditors then
    zgAuditSelect.Cells[ACoord.X, ACoord.Y].Data := Pointer(cbbAuditors.ItemIndex);
end;

procedure TAuditSelctForm.zgAuditSelectGetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);
var
  iAuditorIndex: Integer;
begin
  if (ACoord.X = 2) and (ACoord.Y > 0) then
  begin
   iAuditorIndex := Integer(zgAuditSelect.Cells[ACoord.X, ACoord.Y].Data);
   if iAuditorIndex < cbbAuditors.Items.Count then
     Value := cbbAuditors.Items.Strings[iAuditorIndex]
   else
     Value := '';
  end;
end;

end.
