unit UMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, Buttons, Inifiles, dbtables;

type
  TMain = class(TForm)
    GroupBox1: TGroupBox;
    Label2: TLabel;
    ProgressBar1: TProgressBar;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
    function getInputField(Src:String;var Tail:String):String;
    procedure Init_Delete(tSessionName, tDatabaseName: String);
    procedure Init_Syspazon(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syspalvl(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syspasct(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syspascv(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syspatrx(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syspabcp(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syspatel(tSessionName, tDatabaseName, FileName:String);
    procedure Init_Syscjnl1(tSessionName, tDatabaseName: String);
    procedure Init_Syssjnl1(tSessionName, tDatabaseName: String);
  public
    { Public declarations }
    function CalcPassword(Password: String):Longint;
  end;

var
  Main: TMain;

implementation

{$R *.DFM}

uses
   i_SYSCJNL1, i_SYSCJNL2, i_SYSPABCP, i_SYSPALVL, i_SYSPASCT, i_SYSPASCV,
   i_SYSPATEL, i_SYSPATRX, i_SYSPAZON, i_SYSSJNL1, i_SYSSJNL2, i_ebss;

procedure TMain.Init_Delete(tSessionName, tDatabaseName: String);
begin
  admin_SYSPASCV.Empty(tSessionName, tDatabaseName);
  admin_SYSPABCP.Empty(tSessionName, tDatabaseName);
  admin_SYSPATRX.Empty(tSessionName, tDatabaseName);
  admin_SYSPATEL.Empty(tSessionName, tDatabaseName);
  admin_SYSPAZON.Empty(tSessionName, tDatabaseName);
  admin_SYSPALVL.Empty(tSessionName, tDatabaseName);
  admin_SYSPASCT.Empty(tSessionName, tDatabaseName);
  admin_SYSCJNL1.Empty(tSessionName, tDatabaseName);
  admin_SYSCJNL2.Empty(tSessionName, tDatabaseName);
  admin_SYSSJNL1.Empty(tSessionName, tDatabaseName);
  admin_SYSSJNL2.Empty(tSessionName, tDatabaseName);
end;

function TMain.CalcPassword(Password: String):Longint;
var
   i,n: Integer;
begin
   Result := 0;
   n := Length(Password);
   For i := 1 to n do
   begin
      Result := Result +
                11 * Ord(Password[i])*Ord(Password[i])*i +
                 3 * Ord(Password[i])*i*i + i*1997;
   end;
end;

function TMain.getInputField(Src:String;var Tail:String):String;
var
   p: Integer;
begin
   p := Pos(',', Src);
   If p > 0 then
   begin
      Result := Copy(Src, 1, p-1);
      Tail := Copy(Src, p+1, Length(Src)-p);
   end else begin
      Result := Src;
      Tail := '';
   end;
end;

procedure TMain.Init_Syscjnl1(tSessionName, tDatabaseName: String);
begin
   admin_SysCJNL1.Empty(tSessionName, tDatabaseName);
end;

procedure TMain.Init_Syssjnl1(tSessionName, tDatabaseName: String);
begin
   admin_SysSJNL1.Empty(tSessionName, tDatabaseName);
end;

procedure TMain.Init_Syspasct(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   WorkDate, LogID, TrxSeqno: String;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPASCT',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         WorkDate := getInputField(tmpLine, tmpLine);
         LogId := getInputField(tmpLine, tmpLine);
         TrxSeqno := getInputField(tmpLine, tmpLine);

         admin_SYSPASCT.Insert(
                               tSessionName,
                               tDatabaseName,
                               VarArrayOf([
                                     ''''+WorkDate+'''', LogID , TrxSeqno
                                          ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;
      Ini.Free;
      Buf.Free;
end;

procedure TMain.Init_Syspazon(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   ZONENO, BRNO, BRNOTYPE, STATUS, NOTE: String;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPAZON',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         ZONENO := IntToStr(StrToInt(getInputField(tmpLine, tmpLine)));
         BRNO := IntToStr(StrToInt(getInputField(tmpLine, tmpLine)));
         BRNOTYPE := IntToStr(StrToInt(getInputField(tmpLine, tmpLine)));
         STATUS := IntToStr(StrToInt(getInputField(tmpLine, tmpLine)));
         NOTE := getInputField(tmpLine, tmpLine);

         admin_SYSPAZON.Insert(
                               tSessionName,
                               tDatabaseName,
                               VarArrayOf([
                                          ZONENO,
                                          BRNO,
                                          BRNOTYPE,
                                          STATUS,
                                     ''''+NOTE+''''
                                          ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;
      Ini.Free;
      Buf.Free;
end;

procedure TMain.Init_Syspalvl(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   LEVELNO, NOTE: String;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPALVL',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         LEVELNO := IntToStr(StrToInt(getInputField(tmpLine, tmpLine)));
         NOTE := getInputField(tmpLine, tmpLine);

         admin_SYSPALVL.Insert(
                               tSessionName,
                               tDatabaseName,
                               VarArrayOf([
                                          LEVELNO,
                                     ''''+NOTE+''''
                                          ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;
      Ini.Free;
      Buf.Free;
end;

procedure TMain.Init_Syspascv(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   ZONENO, BRNO, APPCODE, APPNAME, APPSQNO: String;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPASCV',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         ZONENO := getInputField(tmpLine, tmpLine);
         BRNO := getInputField(tmpLine, tmpLine);
         APPCODE := getInputField(tmpLine, tmpLine);
         APPNAME := getInputField(tmpLine, tmpLine);
         APPSQNO := getInputField(tmpLine, tmpLine);

         admin_SYSPASCV.Insert(
                               tSessionName, tDatabaseName,
                               VarArrayOf([
                                          ZONENO,
                                          BRNO,
                                          APPCODE,
                                     ''''+APPNAME+'''',
                                          APPSQNO
                                          ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;

      Ini.Free;
      Buf.Free;
end;

procedure TMain.Init_Syspatrx(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   ZONENO, BRNO, STATUS, TRXCODE, TRXNAME, TRXRANK, TRXPOSI, TRXGWID, TRXSRVC: String;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPATRX',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         TRXCODE := getInputField(tmpLine, tmpLine);
         TRXRANK := getInputField(tmpLine, tmpLine);
         ZONENO := getInputField(tmpLine, tmpLine);
         BRNO := getInputField(tmpLine, tmpLine);
         TRXNAME := getInputField(tmpLine, tmpLine);
         STATUS := getInputField(tmpLine, tmpLine);
         TRXPOSI := getInputField(tmpLine, tmpLine);
         TRXGWID := getInputField(tmpLine, tmpLine);
         TRXSRVC := getInputField(tmpLine, tmpLine);

         admin_SYSPATRX.Insert(
                               tSessionName, tDatabaseName,
                               VarArrayOf([
                                     ''''+TRXCODE+'''',
                                          TRXRANK,
                                          ZONENO,
                                          BRNO,
                                     ''''+TRXNAME+'''',
                                          STATUS,
                                     ''''+TRXPOSI+'''',
                                     ''''+TRXGWID+'''',
                                     ''''+TRXSRVC+''''
                                          ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;

      Ini.Free;
      Buf.Free;
end;

procedure TMain.Init_Syspabcp(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   ZONENO, BRNO, NAME, SNAME, STATUS, KIND, ADDR, TELE, FAX, CEO, IDCODE, MTELE, NOTE: String;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPABCP',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         ZONENO := getInputField(tmpLine, tmpLine);
         BRNO := getInputField(tmpLine, tmpLine);
         NAME := getInputField(tmpLine, tmpLine);
         SNAME  := getInputField(tmpLine, tmpLine);
         KIND  := getInputField(tmpLine, tmpLine);
         STATUS := getInputField(tmpLine, tmpLine);
         ADDR := getInputField(tmpLine, tmpLine);
         TELE := getInputField(tmpLine, tmpLine);
         FAX := getInputField(tmpLine, tmpLine);
         CEO := getInputField(tmpLine, tmpLine);
         IDCODE := getInputField(tmpLine, tmpLine);
         MTELE := getInputField(tmpLine, tmpLine);
         NOTE := getInputField(tmpLine, tmpLine);

         admin_SYSPABCP.Insert(
                               tSessionName, tDatabaseName,
                               VarArrayOf([
                                          ZONENO,
                                          BRNO,
                                     ''''+NAME+'''',
                                     ''''+SNAME+'''',
                                          STATUS,
                                          KIND,
                                     ''''+ADDR+'''',
                                     ''''+TELE+'''',
                                     ''''+FAX+'''',
                                     ''''+CEO+'''',
                                     ''''+IDCODE+'''',
                                     ''''+MTELE+'''',
                                     ''''+NOTE+''''
                                          ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;

      Ini.Free;
      Buf.Free;
end;

procedure TMain.Init_Syspatel(tSessionName, tDatabaseName, FileName:String);
var
   Buf: TStringList;
   Ini: TInifile;
   i, n: Integer;
   trueFileName, tmpLine: String;
   ZONENO, TELL, BRNO, NAME, IDCODE, STATUS, RANK, PSW, SIGNDATE, EXPIREDDATE, NOTE: String;
   NPSW: Longint;
begin
      Buf := TStringList.Create;
      trueFileName := ExtractFilePath(Application.ExeName)+FileName;
      Ini := TIniFile.Create(trueFileName);

      Ini.ReadSection('SYSPATEL',Buf);

      ProgressBar1.Position := 0;
      ProgressBar1.Min := 0;
      ProgressBar1.Max := Buf.Count;

      n := Buf.Count - 1;
      For i := 0 to n do
      begin
         tmpLine := Buf[i];

         TELL := getInputField(tmpLine, tmpLine);
         RANK := getInputField(tmpLine, tmpLine);
         ZONENO := getInputField(tmpLine, tmpLine);
         BRNO := getInputField(tmpLine, tmpLine);
         NAME := getInputField(tmpLine, tmpLine);
         IDCODE := getInputField(tmpLine, tmpLine);
         STATUS := getInputField(tmpLine, tmpLine);

         PSW := getInputField(tmpLine, tmpLine);
         NPSW := CalcPassword(PSW);
         PSW := IntToStr(NPSW);

         SIGNDATE := getInputField(tmpLine, tmpLine);
         EXPIREDDATE := getInputField(tmpLine, tmpLine);
         NOTE := getInputField(tmpLine, tmpLine);

         admin_SYSPATEL.Insert(
                               tSessionName, tDatabaseName,
                               VarArrayOf([
                                  ''''+TELL+'''',
                                       RANK,
                                       ZONENO,
                                       BRNO,
                                  ''''+NAME+'''',
                                  ''''+IDCODE+'''',
                                       STATUS,
                                       PSW,
                                  ''''+SIGNDATE+'''',
                                  ''''+EXPIREDDATE+'''',
                                  ''''+NOTE+''''
                                       ])
                              );
         ProgressBar1.Position := ProgressBar1.Position + 1;
      end;

      Ini.Free;
      Buf.Free;
end;

procedure TMain.BitBtn1Click(Sender: TObject);
var
   txDatabase: TDatabase;
   txSession: TSession;
   tDatabaseName, tSessionName: String;
begin
   If BitBtn1.Caption = '开始' then
   begin
      If MessageDlg('如果进行本操作，系统将删除数据库的所有信息，同时使数据库将恢复到初始状态。' +
                    Chr(13) + Chr(10) + '进行本操作吗？',
                     mtWarning, [mbOK, mbCancel], 0) = mrOK then
      begin
         Self.Caption := '系统初始化 - 正在连接数据库……';

         openDatabase(txSession, txDatabase, tSessionName, tDatabaseName);

         TransactionStart(txDatabase);
         try
            BitBtn1.Enabled := False;

            Self.Caption := '系统初始化 - 清理参数表';
            Self.Init_Delete(tSessionName, tDatabaseName);

            Self.Caption := '系统初始化 - 初始化地区号表';
            Init_Syspazon(tSessionName, tDatabaseName, 'Initial.ini');

            Self.Caption := '系统初始化 - 初始化级别号表';
            Init_Syspalvl(tSessionName, tDatabaseName, 'Initial.ini');

//            Self.Caption := '系统初始化 - 清空控制交易日志一';
//            Init_Syscjnl1(tSessionName, tDatabaseName);

//            Self.Caption := '系统初始化 - 清空应用交易日志一';
//            Init_Syssjnl1(tSessionName, tDatabaseName);

            Self.Caption := '系统初始化 - 初始通用序号表';
            Init_Syspascv(tSessionName, tDatabaseName, 'Initial.ini');

            Self.Caption := '系统初始化 - 初始网点控制表';
            Init_Syspabcp(tSessionName, tDatabaseName, 'Initial.ini');

            Self.Caption := '系统初始化 - 初始交易表';
            Init_Syspatrx(tSessionName, tDatabaseName, 'Initial.ini');

            Self.Caption := '系统初始化 - 初始柜员表';
            Init_Syspatel(tSessionName, tDatabaseName, 'Initial.ini');

            Self.Caption := '系统初始化 - 初始系统控制表';
            Init_Syspasct(tSessionName, tDatabaseName, 'Initial.ini');
         except
            TransactionRollBack(txDatabase);
            closeDatabase(txSession, txDatabase);
            BitBtn1.Caption := '开始';
            BitBtn1.Enabled := True;
            Self.Caption := '系统初始化 - 全部撤消，恢复原状';
            Label2.Caption := '系统初始化失败';
            Exit;
         end;
         TransactionCommit(txDatabase);
         closeDatabase(txSession, txDatabase);

         BitBtn1.Caption := '完成';
         BitBtn1.Enabled := True;
         Self.Caption := '系统初始化 - 全部成功完成';
         Label2.Caption := '系统初始化完成';
      end else
         Close;
   end else If BitBtn1.Caption = '完成' then
   begin
      Close;
   end;
end;

end.
