unit uSvc_Organizace;

interface

uses
  System.Generics.Collections,
  System.SysUtils,
  JsonDataObjects,
  uSvc_Base,
  uCommons,
  uHeoObj_Base;

const
{$I globalConsts.inc}

type
  TOrganizaceService = class(TServiceBase)
   public
    function GetAll: TObjectList<TOrganizace>; virtual;
    function GetByID(const AID: integer): TOrganizace; virtual;
    function GetByCislo(const ACislo: integer): TOrganizace; virtual;
    function GetByICO(const AICO: string): TObjectList<TOrganizace>; virtual;
    function GetByDIC(const ADIC: string): TObjectList<TOrganizace>; virtual;
    function GetByParams(params: TDictionary<string, string>): TObjectList<TOrganizace>; virtual;
    function GetMeta: TJSONObject; virtual;
  end;

implementation

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

const
  selSloupce = 'SELECT ID, CisloOrg, Nazev, ICO, DIC, Ulice, PopCislo, OrCislo, Misto, PSC, PravniForma, Stav';


{ TOrganizaceService }

 function TOrganizaceService.GetByParams (params: TDictionary<string, string>): TObjectList<TOrganizace>;
  var lSQL, where: string;
  begin
   result:= nil;

   lSQL:= selSloupce + ' FROM ' + tblCOrg;

   where:= '';

   if (params.ContainsKey('id')) then
    if (params.Items['id']<>'') then
     if (params.Items['id']<>'0') then
      where:= where + 'ID=' + params.Items['id'];

   if (params.ContainsKey('cislo')) then
    if (params.Items['cislo']<>'') then
     where:= where + IfThen(where<>'', ' AND ', '') + 'CisloOrg=' + params.Items['cislo'];

   if (params.ContainsKey('ico')) then
    if (params.Items['ico']<>'') then
     where:= where + IfThen(where<>'', ' AND ', '') + 'ICO LIKE N' + QuotedStr(params.Items['ico'] + '%');

   if (params.ContainsKey('dic')) then
    if (params.Items['dic']<>'') then
     where:= where + IfThen(where<>'', ' AND ', '') + 'DIC LIKE N' + QuotedStr(params.Items['dic'] + '%');

   if (where<>'') then
    lSQL:= lSQL + ' WHERE ' + where;
   lSQL:= lSQL + ' ORDER BY CisloOrg';

   try
     try
       FDM.sqlQry1.Open(lSQL);
       result:= FDM.sqlQry1.AsObjectList<TOrganizace>;
      except on E:Exception do
       raise EServiceException.Create('Chyba načítání organizací: ' + E.Message);
     end;
    finally
     FDM.sqlQry1.Close;
   end;
  end;



 function TOrganizaceService.GetAll: TObjectList<TOrganizace>;
  var lSQL: string;
  begin
   result:= nil;

   lSQL:= selSloupce + ' FROM ' + tblCOrg + ' ORDER BY CisloOrg';
   try
     try
       FDM.sqlQry1.Open(lSQL);
       result:= FDM.sqlQry1.AsObjectList<TOrganizace>;
      except on E:Exception do
       raise EServiceException.Create('Chyba načítání organizací: ' + E.Message);
     end;
    finally
     FDM.sqlQry1.Close;
   end;
  end;



 function TOrganizaceService.GetByID (const AID: Integer): TOrganizace;
  var lSQL: string;
  begin
   result:= nil;

   lSQL:= selSloupce + ' FROM ' + tblCOrg + ' WHERE ID=:ID';
   FDM.sqlQry1.Open(lSQL, [AID]);
   try
     if not(FDM.sqlQry1.EOF) then
       result:= FDM.sqlQry1.AsObject<TOrganizace>
     else
       raise EServiceException.Create('Organizace s ID ' + AID.ToString + ' nebyla nalezena.');
    finally
     FDM.sqlQry1.Close;
   end;
  end;


 function TOrganizaceService.GetByCislo (const ACislo: Integer): TOrganizace;
  var lSQL: string;
  begin
   result:= nil;

   lSQL:= selSloupce + ' FROM ' + tblCOrg + ' WHERE CisloOrg=:Cislo';
   FDM.sqlQry1.Open(lSQL, [ACislo]);
   try
     if not(FDM.sqlQry1.EOF) then
       result:= FDM.sqlQry1.AsObject<TOrganizace>
     else
       raise EServiceException.Create('Organizace s číslem ' + ACislo.ToString + ' nebyla nalezena.');
    finally
     FDM.sqlQry1.Close;
   end;
  end;



 function TOrganizaceService.GetByICO (const AICO: string): TObjectList<TOrganizace>;
  var lSQL: string;
  begin
   result:= nil;

   lSQL:= selSloupce + ' FROM ' + tblCOrg + ' WHERE ICO=:ICO ORDER BY CisloOrg';
   FDM.sqlQry1.Open(lSQL, [AICO]);
   try
     if not(FDM.sqlQry1.EOF) then
       result:= FDM.sqlQry1.AsObjectList<TOrganizace>
     else
       raise EServiceException.Create('Organizace s IČO ' + AICO + ' nebyla nalezena.');
    finally
     FDM.sqlQry1.Close;
   end;
  end;



 function TOrganizaceService.GetByDIC(const ADIC: string): TObjectList<TOrganizace>;
  var lSQL: string;
  begin
   result:= nil;

   lSQL:= selSloupce + ' FROM ' + tblCOrg + ' WHERE DIC=:DIC ORDER BY CisloOrg';
   FDM.sqlQry1.Open(lSQL, [ADIC]);
   try
     if not(FDM.sqlQry1.EOF) then
       result:= FDM.sqlQry1.AsObjectList<TOrganizace>
     else
       raise EServiceException.Create('Organizace s DIČ ' + ADIC + ' nebyla nalezena.');
    finally
     FDM.sqlQry1.Close;
   end;
  end;



 function TOrganizaceService.GetMeta: TJSONObject;
  var lSQL: string;
  begin
   try
   lSQL:= selSloupce + ' FROM ' + tblCOrg + ' WHERE 1=0';
     FDM.sqlQry1.Open(lSQL);
     Result := FDM.sqlQry1.MetadataAsJSONObject();
    finally
     FDM.sqlQry1.Close;
   end;
  end;


end.
