⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 操作数据库的例子
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    procedure smcmo; //SMGP MO处理 SMG--SP
    procedure smcmo0; //CMPP MO处理 SMG--SP
    procedure smcmt; //MT处理 SP--SMG
  end;

var
  MainForm: TMainForm;
  MonitorThread: TMonitorThreadObj;
  MtPrePrcThread: TMtPrePrcThreadObj;
  MtQfThread: TMtQfThread;
  LogThread: TLogThreadObj;
  OutReadThread: TOutReadSMGPThreadObj; //smgp 线程
  MtSendThread: TMtSendSMGPThreadObj;
  OutReadCMPPThread: TOutReadCMPPThreadObj; //cmpp 线程
  MtSendCMPPThread: TMtSendCMPPThreadObj;
  OutReadCMPP0Thread: TOutReadCMPP0ThreadObj; //cmpp0 线程
  MtSendCMPP0Thread: TMtSendCMPP0ThreadObj;
  OutReadSgipThread: TOutReadSgipThreadObj; //sgip 线程
  MtSendSgipThread: TMtSendSgipThreadObj;
implementation
uses
  WinSock, Md5unit, Cryptcon, IniFiles, DateUtils;
{$R *.dfm}

{ TMtQfThread }
procedure TMtQfThread.AddMsgToMemo(const Msg: string);
begin
  ErrMsg := Msg;
  Synchronize(ThAddMsgToMemo);
end;

constructor TMtQfThread.Create(CreateSuspended: boolean);
begin
  inherited;
  LastActiveTime := now;
  AdoConnection := TADOConnection.Create(nil);
  AdoQuery := TAdoQuery.Create(nil);
  AdoQuery.Connection := AdoConnection;
  Seqid := 1;
  HaveMc := false;
end;

destructor TMtQfThread.destroy;
begin
  AdoQuery.Free;
  AdoConnection.Free;
  LastActiveTime := 0;
  inherited;
end;

procedure TMtQfThread.Execute;
var
  iHour: integer;
  strSql: string;
  iDay: integer;
begin
  FreeOnTerminate := True;
  while not Terminated do
  begin
    try
      try
        if AdoConnection.Connected then
        begin
          iHour := hourof(now);
          iDay := Dayof(now);
          {if MainForm.HUADAN.Checked then
          begin
            if (iDay >= GGATECONFIG.MCSTART) and (iDay < GGATECONFIG.MCEND) then
            begin
              McQf;
            end;
          end;
        }
          //高优先级
          HighPriovityQf;

          //低优先级,有限制时段
          {if (iHour in [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) and (HaveMc = false) then
          begin
            if MainForm.QUNFA.Checked then
            begin
              LowPriorityQf;
            end;
          end; }

          //将已处理,且有状态报告的数据删除
          strSql := 'delete MTSEND where PrcEd = 1 and Gateid = ' + inttostr(GSMSCENTERCONFIG.GateId) + ' and status is not null';
          AdoConnection.Execute(strSql);
          //将已过72小时,都未有状态报告的,更新状态报告为2,发送失败
          strSql := 'update MTSEND set status = 2 where PrcEd = 1 and Gateid = ' + inttostr(GSMSCENTERCONFIG.GateId) + ' and getdate() - sendtime > 3';
          AdoConnection.Execute(strSql);
        end
        else
        begin
          ConnectDB;
          sleep(3000);
        end;
      except
        on e: exception do
        begin
          AddMsgToMemo('MT群发线程:' + e.Message);

          if e.Message = '连接失败' then
          begin
            AdoConnection.Close;
          end;

        end;
      end;
    finally
      LastActiveTime := now;
      sleep(1000);
    end;
  end;
end;

function TMtQfThread.GetInMsgId: string;
var
  str: string;
  i: integer;
begin
  //3位网关号 + 14 yyyymmddhhnnss + 3序号,共20
  str := copy(inttostr(GSMSCENTERCONFIG.GateId), 2, 3) + FormatDatetime('yyyymmddhhnnss', now);
  i := GetSeqid;
  if i >= 100 then
  begin
    str := str + inttostr(i);
  end
  else if i >= 10 then
  begin
    str := str + '0' + inttostr(i);
  end
  else
  begin
    str := str + '00' + inttostr(i);
  end;
  result := str;
end;

function TMtQfThread.GetSeqid: integer;
begin
  result := Seqid;
  inc(Seqid);
  if Seqid >= 1000 then
  begin
    Seqid := 1;
  end;
end;

procedure TMtQfThread.HighPriovityQf;
var
  MtBufferCount: integer;
  strSql: string;
  pac: TSPPO_PACKET;
  Autoid: integer;
  MtInMsgId: string;
begin
  MtBufferCount := mtbuffer.Count;
  if MtBufferCount < GGATECONFIG.Flux then
  begin
    strSql := 'select top ' + inttostr(GGATECONFIG.Flux) + ' * from mtsend where gateid = ' + inttostr(GSMSCENTERCONFIG.GateId) + ' and Prced = 0 and Priority = 9 order by autoid';
    ;
    AdoQuery.Close;
    AdoQuery.SQL.Text := strSql;
    AdoQuery.Open;
    while not AdoQuery.Eof do
    begin
      ZeroMemory(@pac, sizeof(TSPPO_PACKET));
      Autoid := AdoQuery.fieldbyname('Autoid').AsInteger;
      pac.Body.Mt.MtLogicId := 1; //群发
      pac.Body.Mt.MtMsgType := 1; //非MO引起的MT
      pac.Body.Mt.MoOutMsgId := '';
      pac.Body.Mt.MoInMsgId := '';
      MtInMsgId := GetInMsgId;
      HexToChar(MtInMsgId, pac.Body.Mt.MtInMsgId);
      pac.Body.Mt.MoLinkId := '';

      SetPchar(pac.Body.Mt.MtSpAddr, AdoQuery.fieldbyname('MtSpAddr').AsString, sizeof(pac.Body.Mt.MtSpAddr));
      SetPchar(pac.Body.Mt.MtUserAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtUserAddr));
      SetPchar(pac.Body.Mt.MtFeeAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtFeeAddr));
      SetPchar(pac.Body.Mt.MtServiceId, AdoQuery.fieldbyname('MtServiceId').AsString, sizeof(pac.Body.Mt.MtServiceId));
      pac.Body.Mt.MtMsgFmt := 15;
      pac.Body.Mt.MtMsgLenth := length(AdoQuery.fieldbyname('MtMsgContent').AsString);
      SetPchar(pac.Body.Mt.MtMsgContent, AdoQuery.fieldbyname('MtMsgContent').AsString, pac.Body.Mt.MtMsgLenth);

      mtbuffer.Add(pac);
      strSql := 'update MTSEND set PrcEd = 1, MtInMsgId = ' + Quotedstr(MtInMsgId) + ' where autoid = ' + inttostr(Autoid);
      AdoConnection.Execute(strSql);
      AdoQuery.MoveBy(1);
    end;
    AdoQuery.Close;
  end;
end;

procedure TMtQfThread.LowPriorityQf;
var
  MtBufferCount: integer;
  strSql: string;
  pac: TSPPO_PACKET;
  Autoid: integer;
  MtInMsgId: string;
  ValidTime: string;
begin
  MtBufferCount := mtbuffer.Count;
  if MtBufferCount < GGATECONFIG.Flux then
  begin
    strSql := 'select top ' + inttostr(GGATECONFIG.Flux * 3) + ' * from mtsend where gateid = ' + inttostr(GSMSCENTERCONFIG.GateId) + ' and Prced = 0 order by autoid';
    AdoQuery.Close;
    AdoQuery.SQL.Text := strSql;
    AdoQuery.Open;
    while not AdoQuery.Eof do
    begin
      ZeroMemory(@pac, sizeof(TSPPO_PACKET));
      Autoid := AdoQuery.fieldbyname('Autoid').AsInteger;
      pac.Body.Mt.MtLogicId := 1; //群发
      pac.Body.Mt.MtMsgType := 1; //非MO引起的MT
      pac.Body.Mt.MoOutMsgId := '';
      pac.Body.Mt.MoInMsgId := '';
      MtInMsgId := GetInMsgId;
      HexToChar(MtInMsgId, pac.Body.Mt.MtInMsgId);
      pac.Body.Mt.MoLinkId := '';

      SetPchar(pac.Body.Mt.MtSpAddr, AdoQuery.fieldbyname('MtSpAddr').AsString, sizeof(pac.Body.Mt.MtSpAddr));
      SetPchar(pac.Body.Mt.MtUserAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtUserAddr));
      SetPchar(pac.Body.Mt.MtFeeAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtFeeAddr));
      SetPchar(pac.Body.Mt.MtServiceId, AdoQuery.fieldbyname('MtServiceId').AsString, sizeof(pac.Body.Mt.MtServiceId));
      pac.Body.Mt.MtMsgFmt := 15;
      pac.Body.Mt.MtMsgLenth := length(AdoQuery.fieldbyname('MtMsgContent').AsString);
      SetPchar(pac.Body.Mt.MtMsgContent, AdoQuery.fieldbyname('MtMsgContent').AsString, pac.Body.Mt.MtMsgLenth);

      mtbuffer.Add(pac);
      strSql := 'update MTSEND set PrcEd = 1, MtInMsgId = ' + Quotedstr(MtInMsgId) + ' where autoid = ' + inttostr(Autoid);
      AdoConnection.Execute(strSql);
      AdoQuery.MoveBy(1);
    end;
    AdoQuery.Close;
  end;
end;

procedure TMtQfThread.McQf;
var
  MtBufferCount: integer;
  strSql: string;
  pac: TSPPO_PACKET;
  Autoid: integer;
  MtInMsgId: string;
begin
  //发送话单
  MtBufferCount := mtbuffer.Count;
  if MtBufferCount < GGATECONFIG.Flux then
  begin
    strSql := 'select top ' + inttostr(GGATECONFIG.Flux * 3) + ' * from mcsend where gateid = ' + inttostr(GSMSCENTERCONFIG.GateId) + ' and Prced = 0 and Priority = 9 order by autoid';
    ;
    AdoQuery.Close;
    AdoQuery.SQL.Text := strSql;
    AdoQuery.Open;
    if not AdoQuery.Eof then
    begin
      //有话单数据
      while not AdoQuery.Eof do
      begin
        ZeroMemory(@pac, sizeof(TSPPO_PACKET));
        Autoid := AdoQuery.fieldbyname('Autoid').AsInteger;
        pac.Body.Mt.MtLogicId := 2; //话单
        pac.Body.Mt.MtMsgType := AdoQuery.fieldbyname('InMtMsgType').AsInteger;
        pac.Body.Mt.MoOutMsgId := '';
        pac.Body.Mt.MoInMsgId := '';
        MtInMsgId := GetInMsgId;
        HexToChar(MtInMsgId, pac.Body.Mt.MtInMsgId);
        pac.Body.Mt.MoLinkId := '';
        SetPchar(pac.Body.Mt.MtSpAddr, AdoQuery.fieldbyname('MtSpAddr').AsString, sizeof(pac.Body.Mt.MtSpAddr));
        SetPchar(pac.Body.Mt.MtUserAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtUserAddr));
        SetPchar(pac.Body.Mt.MtFeeAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtFeeAddr));
        SetPchar(pac.Body.Mt.MtServiceId, AdoQuery.fieldbyname('MtServiceId').AsString, sizeof(pac.Body.Mt.MtServiceId));
        pac.Body.Mt.MtMsgFmt := 15;
        pac.Body.Mt.MtMsgLenth := length(AdoQuery.fieldbyname('MtMsgContent').AsString);
        SetPchar(pac.Body.Mt.MtMsgContent, AdoQuery.fieldbyname('MtMsgContent').AsString, pac.Body.Mt.MtMsgLenth);
        mtbuffer.Add(pac);
        strSql := 'update MCSEND set PrcEd = 1, MtInMsgId = ' + Quotedstr(MtInMsgId) + ', Sendtime = getdate() where autoid = ' + inttostr(Autoid);
        AdoConnection.Execute(strSql);
        AdoQuery.MoveBy(1);
      end;
      HaveMc := True;
    end
    else
    begin
      HaveMc := false;
    end;
    AdoQuery.Close;

    if HaveMc = false then
    begin
      //发送本月已经发送,但发送失败的话单
      strSql := 'select top 1 * from MCSEND where gateid = ' + inttostr(GSMSCENTERCONFIG.GateId) + ' and PrcEd = 1 and McMonth = ' + Quotedstr(FormatDatetime('yyyymm', now)) + ' and (Status <> 1 or status is null) and getdate() > sendtime + 1 order by autoid';
      AdoQuery.Close;
      AdoQuery.SQL.Text := strSql;
      AdoQuery.Open;
      if not AdoQuery.Eof then
      begin
        ZeroMemory(@pac, sizeof(TSPPO_PACKET));
        Autoid := AdoQuery.fieldbyname('Autoid').AsInteger;
        pac.Body.Mt.MtLogicId := 2; //话单
        pac.Body.Mt.MtMsgType := AdoQuery.fieldbyname('InMtMsgType').AsInteger;
        pac.Body.Mt.MoOutMsgId := '';
        pac.Body.Mt.MoInMsgId := '';
        MtInMsgId := GetInMsgId;
        HexToChar(MtInMsgId, pac.Body.Mt.MtInMsgId);
        pac.Body.Mt.MoLinkId := '';
        SetPchar(pac.Body.Mt.MtSpAddr, AdoQuery.fieldbyname('MtSpAddr').AsString, sizeof(pac.Body.Mt.MtSpAddr));
        SetPchar(pac.Body.Mt.MtUserAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtUserAddr));
        SetPchar(pac.Body.Mt.MtFeeAddr, AdoQuery.fieldbyname('MtUserAddr').AsString, sizeof(pac.Body.Mt.MtFeeAddr));
        SetPchar(pac.Body.Mt.MtServiceId, AdoQuery.fieldbyname('MtServiceId').AsString, sizeof(pac.Body.Mt.MtServiceId));
        pac.Body.Mt.MtMsgFmt := 15;
        pac.Body.Mt.MtMsgLenth := length(AdoQuery.fieldbyname('MtMsgContent').AsString);
        SetPchar(pac.Body.Mt.MtMsgContent, AdoQuery.fieldbyname('MtMsgContent').AsString, pac.Body.Mt.MtMsgLenth);
        mtbuffer.Add(pac);
        strSql := 'update MCSEND set PrcEd = 1, MtInMsgId = ' + Quotedstr(MtInMsgId) + ', Sendtime = getdate() where autoid = ' + inttostr(Autoid);
        AdoConnection.Execute(strSql);
        HaveMc := True;
      end
      else
      begin
        HaveMc := false;
      end;
    end;
    AdoQuery.Close;
  end;
end;

procedure TMtQfThread.ThAddMsgToMemo;
begin
  if MainForm <> nil then
  begin
    MainForm.ShowToMemo(ErrMsg, MainForm.MonitorMemo);
  end;
end;

{ TMtPrePrcThreadObj }
procedure TMtPrePrcThreadObj.AddMsgToMemo(const Msg: string);
begin
  ErrMsg := Msg;
  Synchronize(ThAddMsgToMemo);
end;

constructor TMtPrePrcThreadObj.Create(CreateSuspended: boolean);
begin
  inherited;
  LastActiveTime := now;
end;

destructor TMtPrePrcThreadObj.destroy;
begin
  LastActiveTime := 0;
  inherited;
end;

procedure TMtPrePrcThreadObj.Execute;
begin
  FreeOnTerminate := True;
  while not Terminated do
  begin
    try
      try
        MtPrePrc;
      except
        on e: exception do
        begin
          AddMsgToMemo('MO预处理:' + e.Message);
        end;
      end;
    finally
      LastActiveTime := now;
      sleep(10);
    end;
  end;
end;

procedure TMtPrePrcThreadObj.MtPrePrc;
var
  Seqid: integer;
  Mt: TMtBuffer;
  i: integer;
  j: integer;
  preprced: boolean;
begin
  if mtbuffer = nil then exit;
  Seqid := mtbuffer.GetPrePrc;
  if Seqid > 0 then
  begin
    Mt := mtbuffer.Read(Seqid);
    if Mt.Mt.MtLogicId = 2 then
    begin
      for j := 0 to high(Protocol) do
      begin
        if (Protocol[j].GateCode = Mt.Mt.MtServiceId) and (Protocol[j].MsgType = Mt.Mt.MtMsgType) then
        begin
          mtbuffer.UpdatePrePrced(Seqid, j);
        end;
      end;
    end
    else
    begin
      //先做业务代码校验和转换
      preprced := false;
      for i := 0 to high(ServiceCode) do
      begin
        if (UpperCase(Mt.Mt.MtServiceId) = ServiceCode[i].LogicCode) and (Mt.Mt.MtLogicId = ServiceCode[i].LogicId) then
        begin

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -