dbo.API_consumos_mensual

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@ant_lastday date 3
@ant_firstday date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec api_consumos_mensual
*/


CREATE OR ALTER PROCEDURE dbo.API_consumos_mensual
 @ant_lastday date = '2024-09-30',
 @ant_firstday date = '2024-09-01'
AS

BEGIN
    SET NOCOUNT ON;
 declare @ptipo_api char(1) = ''
 declare @pUserKey int = 0
 declare @pProdKey int = 0
 declare @pConsumoTotal int = 0
 declare @pConsumoAnual int = 0
 declare @pConsumoMensual int = 0
 declare @pConsumoDiario int = 0
 declare @plimiteConsumo int = 0
 declare @pbloquear bit = 0
 declare @vigencia_desde datetime 

 declare @firstMonthday datetime
 declare @lastMonthday datetime
 declare @StartOfYear datetime
 declare @EndOfYear datetime

 -- Borrar los registros de [apikey_consumo_mensual] del ese mes 
 delete from [apikey_consumo_mensual] where mes = @ant_lastday

 DECLARE C_api_consumo CURSOR FOR
  select distinct apikey.tipo_api, apikey.user_id,apikey.producto,
  /*apikey.api_key, */
  usuarios_productos.limite_consumo,usuarios_productos.bloquear_consumo, vigencia_desde 
  from apikey 
  inner join usuarios_productos on (apikey.user_id=usuarios_productos.user_id and apikey.producto=usuarios_productos.id_producto)
  left join clientes_productos on (usuarios_productos.cliente = clientes_productos.cliente and clientes_productos.id_producto=usuarios_productos.id_producto and usuarios_productos.id_seccion=0)
  where apikey.activo = 1
  /* PROBANDO */
  --and id_api_key = 222
  /**/
  order by usuarios_productos.limite_consumo desc

 OPEN C_api_consumo
 FETCH NEXT FROM C_api_consumo INTO @ptipo_api,@pUserKey,@pProdKey,@plimiteConsumo,@pbloquear,@vigencia_desde 
 WHILE @@fetch_status = 0
 BEGIN

  /* 
   Guardar el congelado del mes anterior  
   */

  IF (@ant_lastday is not null) 
  begin
   declare @id int 
   declare @consumo bigint 

   DECLARE C_congelado CURSOR FOR
    select distinct id_api_key from apikey where user_id = @pUserKey and producto = @pProdKey and activo = 1 
   OPEN C_congelado
   FETCH NEXT FROM C_congelado INTO @id 
   WHILE @@fetch_status = 0
   BEGIN
    if not exists (select * from [apikey_consumo_mensual] where id_api_key = @id and mes = @ant_lastday)
    begin 
     if (@ptipo_api!='P')
     begin
      select @consumo = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between @ant_firstday and @ant_lastday
      insert into apikey_consumo_mensual (id_api_key, mes, consumo_mes)
      values (@id, @ant_lastday, @consumo) 
     end
     else 
     begin
      if (@pProdKey = 106) -- API A PROVEEDOR - WORLDSYS
      begin
       select @consumo = count(*) FROM (SELECT distinct documentobuscado, NombreBuscado, convert(date,created) fecha 
       from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @ant_firstday and @ant_lastday) AS internalQuery
      end
      if (@pProdKey = 107) -- API A PROVEEDOR - RENAPER
      begin
       select @consumo = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) between @ant_firstday and @ant_lastday
      end
      if (@pProdKey = 108) -- API A PROVEEDOR - RENAPER FACE MATCH
      begin
       select @consumo = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) between @ant_firstday and @ant_lastday
      end
      insert into apikey_consumo_mensual (id_api_key, mes, consumo_mes)
      values (@id, @ant_lastday, @consumo) 
     end
     --select @id id, @ant_lastday lastday, @consumo consumo

    end
    FETCH NEXT FROM C_congelado INTO @id 
   END 
   CLOSE C_congelado
   DEALLOCATE C_congelado
  end  
  
  FETCH NEXT FROM C_api_consumo INTO @ptipo_api,@pUserKey,@pProdKey,@plimiteConsumo,@pbloquear,@vigencia_desde
 END  
 CLOSE C_api_consumo
 DEALLOCATE C_api_consumo

END

GO

Used By

No items found