unit uCtrlCustom;

interface
{$I 'GlobalDefs.inc'}

uses
  MVCframework,
  MVCframework.Commons,
  MVCframework.Serializer.Commons,
//  MVCframework.Serializer.Intf,
  System.Generics.Collections, // kvuli TDictionary
  System.RegularExpressions,
//  JsonDataObjects,
  uCommons, // kvuli EServiceException
  uCtrlBase,
//  uSvc_Base,
  helTabsBIDs,
  uHeOObj_Base,
  uHeOObj_Custom,
  uSvcCustom,
  MVCFramework.Swagger.Commons; // kvuli dokumentaci

type

  [MVCPath('/inco')]
  TINCOsysController = class(TBaseController)
{
   strict private
    FSelfSvc: TINCOSysService;
   strict protected
    function GetINCOSysService: TINCOSysService;
}
   public
    destructor Destroy; override;

    [MVCPath('/zapisjson')] // zapis obecnych json data do Heliosu
    [MVCHTTPMethod([httpPOST])]
    [MVCSwagSummary('INCOSystems', 'Zápis JSON dat do db Heliosu', 'INCOZapisJSONDoHeliosu')]
    [MVCConsumes(TMVCMediaType.APPLICATION_JSON)]
    procedure ZapisJSONDoHeliosu (CTX: TWebContext);

    [MVCPath('/poslednievidence')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Skupina karet', 'Vrací data skupiny karet dle jejího kódu')]
    [MVCSwagParam(plQuery, 'idprikaz', 'ID vyrobniho prikazu', ptString, false, '1')]
    procedure PosledniEvidOperacePrikazu ([MVCFromQueryString('idprikaz', '')] sIdPrikaz: string=''
                                         );



  end;



implementation
uses
 FireDAC.Stan.Option,
 FireDAC.Comp.Client,
 FireDAC.Stan.Param,
 MVCFramework.FireDAC.Utils,
 MVCFramework.DataSet.Utils,
 System.StrUtils,
 System.SysUtils,
 System.JSON,
 JsonDataObjects,
 Winapi.ActiveX,
 uDataMod,
 Quick.Logger;

const
 selSloupce = '';
{$I globalConsts.inc}




{ TINCOSysController }
 destructor TINCOSysController.Destroy;
  begin
//   FSelfSvc.Free;
   inherited;
  end;


{
 function TINCOSysController.GetINCOSysService: TINCOSysService;
  begin
   inherited;
   if not Assigned(FSelfSvc) then
    FSelfSvc:= TINCOSysService.Create (datMod);
   result:= FSelfSvc;
  end;
}



 procedure TINCOSysController.ZapisJSONDoHeliosu (CTX: TWebContext);
  var respData, jsonData: string;
//      o: System.JSON.TJSONObject;
  begin
   respData:= '';
   try
     jsonData:= CTX.Request.Body.Trim;
     if (jsonData<>'') then
      GetINCOSysService.ZapisJsonDoHeliosu (jsonData, respData);
     ResponseStatus(HTTP_STATUS.OK, 'OK');
     Render(respData);
    except
{$IFDEF NORENDER400}
     RenderStatusMessage (200);
{$ELSE}
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 200);
      end
     else
       raise;
{$ENDIF}
   end;
  end;




  procedure TINCOSysController.PosledniEvidOperacePrikazu (sIdPrikaz: string='');
   var idPrikaz: integer;
       params: TDictionary<string, string>;
   begin
    params:= TDictionary<string, string>.Create;

    idPrikaz:= 0;
    sIdPrikaz:= sanitizeSQLString(sIdPrikaz);
    if not(TryStrToInt(sIdPrikaz, idPrikaz)) then
     idPrikaz:= 0;

    if (idPrikaz>0) then
     params.Add('idPrikaz', idPrikaz.ToString);

    params.Add('jenPosledni', '1');


    try
      Render(ObjectDict().Add('data', GetVyrobaEvidenceOperaciService.GetByParams (params)));
     except
{$IFDEF NORENDER400}
     RenderStatusMessage (200);
{$ELSE}
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 200);
      end
     else
       raise;
{$ENDIF}
   end;

   end;


end.
