unit uSvcCustom;

interface

uses
  System.Classes,
  System.JSON,
  System.Generics.Collections,
  Winapi.ActiveX,
  System.DateUtils,
  System.SysUtils,
  JsonDataObjects,
  uSvc_Base,
  uCommons, // kvuli EServiceException
  helTabsBIDs,
  uHeoObj_Base,
  uHeOObj_Custom,
  uHeoObj_Vyroba,
  uSvc_ObehZbozi,
  uSvc_Vyroba;


const
  CRLF = #13#10;



type
  TWestraService = class(TServiceBase)
   public
    procedure ZapisJsonDoHeliosu (jsonData: string; var respString: string);
   end;



implementation
uses
  System.StrUtils,
  FireDAC.Stan.Option,
  FireDAC.Comp.Client,
  FireDAC.Stan.Param,
  MVCFramework.FireDAC.Utils,
  MVCFramework.DataSet.Utils,
  MVCFramework.Serializer.Commons;




{ TWestraService }

 procedure TWestraService.ZapisJsonDoHeliosu (jsonData: string; var respString: string);
  var lSQL, taskStr, strTmp, strTmp2, code, rs, guidIdent: string;
      guid: TGuid;
      akce: string;
      j, t, a, v: JsonDataObjects.TJSONObject;
      i, ii, taskDZId, taskId, intTemp, idJSON: integer;
      jeOld: boolean;
      lQry: TFDQuery;
      w: TStreamWriter;
      jo: System.JSON.TJsonObject;
  begin
   idJSON:= 0;
   respString:= '[';

   jo:= System.JSON.TJsonObject.Create;

   jsonData:= jsonData.Trim;
   if (jsonData<>'') then
     begin
      lQry:= TFDQuery.Create(nil);
      try
        lQry.Connection:= FDM.sqlConn;
        try
          CoCreateGuid(guid);
          if (System.SysUtils.CreateGUID(guid)=S_OK) then
            guidIdent:= System.SysUtils.GUIDToString(guid)
           else
            guidIdent:= self.NewUUID32;
          lSQL:= 'INSERT ' + tblPrijataJsonData + ' (IdPHIdent, GUIDIdent, JSONData) SELECT 0, CONVERT(uniqueidentifier, N' + guidIdent.QuotedString + '), N' + jsonData.QuotedString;
          FDM.sqlConn.ExecSQL(lSQL);
          lSQL:= 'SELECT ID FROM ' + tblPrijataJsonData + ' WHERE GUIDIdent=CONVERT(uniqueidentifier, N' + guidIdent.QuotedString + ')';
          lQry.Open(lSQL);
          if (lQry.RecordCount=1) then
           begin
            idJSON:= lQry.FieldByName('ID').AsInteger;
            respString:= 'OK';
            jo.AddPair('heliosid', idJSON.ToString);
           end;
         except on E:Exception do
          begin
           respString:= 'NOT OK';
           raise EServiceException.Create('Chyba zápisu JSON dat: ' + E.Message);
          end;
        end;
       finally
        FreeAndNil(lQry);
      end;


      CoInitialize(nil);
      j:= TJsonObject.Parse(jsonData) as JsonDataObjects.TJsonObject;
      try
          if (j<>nil) then
           if (j.Contains('akce')) then
            begin
             akce:= j.S['akce'].Trim;
             if (idJSON>0) and (akce<>'') then
              FDM.sqlConn.ExecSQL('UPDATE ' + tblPrijataJsonData + ' SET Akce=N' + akce.QuotedString + ' WHERE ID=' + idJSON.ToString);
{
             for i:=0 to j['tasks'].Count-1 do
              begin
               try
                 t:= j['tasks'].Items[i];
                 taskDZId:= t.I['id']; // j['tasks'].Items[i].I['id'];
                 taskStr:= j['tasks'].Items[i].ObjectValue.ToString;
                except on E:Exception do
               end;
              end;
}
            end;
       finally
        j.Free;
      end;
      CoUninitialize;

     end
    else
     respString:= 'NO input data';

   jo.AddPair('status', respString);
   respString:= '[' +respString + ']';

   respString:= jo.ToString;


  end;



end.
