Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec api_consumos
select * from apikey where producto >= 106
select * from productos where producto=40
*/
CREATE OR ALTER PROCEDURE dbo.API_consumos_MIS
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
--set @firstday = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
set @firstMonthday = DATEADD(DAY, 1, EOMONTH(getdate(), -1))
--set @lastday = select DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
set @lastMonthday = EOMONTH(getdate())
SET @lastMonthday = CONVERT(DATETIME, CONVERT(varchar(11),@lastMonthday, 111 ) + ' 23:59:59', 111)
set @StartOfYear = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
set @EndOfYear = DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0))
-- select @firstMonthday as FDAY,@lastMonthday as LDAY,@StartOfYear starty,@EndOfYear endy
-- Si es el primer dia del mes, guardo en la tabla apikey_consumo_mensual (si todavia no lo guardé)
-- Acá guardo el primer y último día del mes anterior
declare @hoy date = getdate()
/* PROBANDO */
--set @hoy = '2022-02-01'
/**/
declare @ant_lastday date = null
declare @ant_firstday date = null
if (convert(varchar,DAY(@hoy)) = '1')
begin
set @ant_lastday = DATEADD(dd, -1, @hoy)
set @ant_firstday = DATEADD(mm, -1, @hoy)
end
----
DECLARE C_api_consumo CURSOR FOR
/* select user_id,producto from apikey where producto is not null*/
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 >= 219
/**/
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
if (@ptipo_api!='P')
begin
select @pConsumoTotal = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey
--select @pConsumoAnual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between @StartOfYear and @EndOfYear
/* 10/02/2022: no se guarda desde el 1 de enero, sino, desde la vigencia_desde del registro */
select @pConsumoAnual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between convert(date,@vigencia_desde) and convert(date,GETDATE())
select @pConsumoAnual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey
/**/
select @pConsumoMensual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) =convert(date,getdate())
end
else
begin
if (@pProdKey = 106) -- API A PROVEEDOR - WORLDSYS
begin
select @pConsumoTotal = count(*) from LN_Fidecheck_ws where StatusCode = 200
select @pConsumoAnual = count(*) from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @StartOfYear and convert(date,GETDATE())
select @pConsumoMensual = count(*) from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) = convert(date,getdate())
end
if (@pProdKey = 107) -- API A PROVEEDOR - RENAPER
begin
select @pConsumoTotal = count(*) from RENAPER_INFO where codigoError = 99
select @pConsumoAnual = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) between @StartOfYear and convert(date,GETDATE())
select @pConsumoMensual = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) = convert(date,getdate())
end
if (@pProdKey = 108) -- API A PROVEEDOR - RENAPER FACE MATCH
begin
select @pConsumoTotal = count(*) from RENAPER_FACE_INFO where codigo_http = 200
select @pConsumoAnual = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) between @StartOfYear and convert(date,GETDATE())
select @pConsumoMensual = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) = convert(date,getdate())
end
--select @pUserKey as usuario, @pProdKey as producto, @pConsumoTotal total, @pConsumoAnual anual, @pConsumoMensual mensual, @pConsumoDiario diario, @StartOfYear anio, @firstMonthday mes, @lastMonthday mes2
end
update apikey set consumo_acumulado = @pConsumoTotal,
consumo_anual=@pConsumoAnual,
consumo_mes=@pConsumoMensual,
consumo_dia=@pConsumoDiario,
bloquea=@pbloquear
where user_id = @pUserKey and producto = @pProdKey
and activo = 1
/* 10/02/2022 :
Si estamos a principio de mes, tengo que guardar el congelado del mes anterior (si todavía no lo guardé)
*/
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 LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @ant_firstday and @ant_lastday
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
/*
update apikey set vigencia_desde = '2022-01-01 00:00:000' WHERE activo = 1
select * from apikey where id_api_key = 19
select * from apikey where user_id = 399154624 and producto = 27
select * from [apikey_consumo_mensual]
*/
GO
GO
/*
exec api_consumos
select * from apikey where producto >= 106
select * from productos where producto=40
*/
CREATE OR ALTER PROCEDURE dbo.API_consumos_MIS
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
--set @firstday = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
set @firstMonthday = DATEADD(DAY, 1, EOMONTH(getdate(), -1))
--set @lastday = select DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
set @lastMonthday = EOMONTH(getdate())
SET @lastMonthday = CONVERT(DATETIME, CONVERT(varchar(11),@lastMonthday, 111 ) + ' 23:59:59', 111)
set @StartOfYear = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
set @EndOfYear = DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0))
-- select @firstMonthday as FDAY,@lastMonthday as LDAY,@StartOfYear starty,@EndOfYear endy
-- Si es el primer dia del mes, guardo en la tabla apikey_consumo_mensual (si todavia no lo guardé)
-- Acá guardo el primer y último día del mes anterior
declare @hoy date = getdate()
/* PROBANDO */
--set @hoy = '2022-02-01'
/**/
declare @ant_lastday date = null
declare @ant_firstday date = null
if (convert(varchar,DAY(@hoy)) = '1')
begin
set @ant_lastday = DATEADD(dd, -1, @hoy)
set @ant_firstday = DATEADD(mm, -1, @hoy)
end
----
DECLARE C_api_consumo CURSOR FOR
/* select user_id,producto from apikey where producto is not null*/
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 >= 219
/**/
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
if (@ptipo_api!='P')
begin
select @pConsumoTotal = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey
--select @pConsumoAnual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between @StartOfYear and @EndOfYear
/* 10/02/2022: no se guarda desde el 1 de enero, sino, desde la vigencia_desde del registro */
select @pConsumoAnual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between convert(date,@vigencia_desde) and convert(date,GETDATE())
select @pConsumoAnual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey
/**/
select @pConsumoMensual = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from usuarios_consumos where user_id= @pUserKey and id_producto=@pProdKey and convert(date,created) =convert(date,getdate())
end
else
begin
if (@pProdKey = 106) -- API A PROVEEDOR - WORLDSYS
begin
select @pConsumoTotal = count(*) from LN_Fidecheck_ws where StatusCode = 200
select @pConsumoAnual = count(*) from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @StartOfYear and convert(date,GETDATE())
select @pConsumoMensual = count(*) from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) = convert(date,getdate())
end
if (@pProdKey = 107) -- API A PROVEEDOR - RENAPER
begin
select @pConsumoTotal = count(*) from RENAPER_INFO where codigoError = 99
select @pConsumoAnual = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) between @StartOfYear and convert(date,GETDATE())
select @pConsumoMensual = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from RENAPER_INFO where codigoError = 99 and convert(date,created) = convert(date,getdate())
end
if (@pProdKey = 108) -- API A PROVEEDOR - RENAPER FACE MATCH
begin
select @pConsumoTotal = count(*) from RENAPER_FACE_INFO where codigo_http = 200
select @pConsumoAnual = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) between @StartOfYear and convert(date,GETDATE())
select @pConsumoMensual = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) between @firstMonthday and @lastMonthday
select @pConsumoDiario = count(*) from RENAPER_FACE_INFO where codigo_http = 200 and convert(date,created) = convert(date,getdate())
end
--select @pUserKey as usuario, @pProdKey as producto, @pConsumoTotal total, @pConsumoAnual anual, @pConsumoMensual mensual, @pConsumoDiario diario, @StartOfYear anio, @firstMonthday mes, @lastMonthday mes2
end
update apikey set consumo_acumulado = @pConsumoTotal,
consumo_anual=@pConsumoAnual,
consumo_mes=@pConsumoMensual,
consumo_dia=@pConsumoDiario,
bloquea=@pbloquear
where user_id = @pUserKey and producto = @pProdKey
and activo = 1
/* 10/02/2022 :
Si estamos a principio de mes, tengo que guardar el congelado del mes anterior (si todavía no lo guardé)
*/
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 LN_Fidecheck_ws where StatusCode = 200 and convert(date,created) between @ant_firstday and @ant_lastday
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
/*
update apikey set vigencia_desde = '2022-01-01 00:00:000' WHERE activo = 1
select * from apikey where id_api_key = 19
select * from apikey where user_id = 399154624 and producto = 27
select * from [apikey_consumo_mensual]
*/
GO
Depends On
8Used By
No items found