declare @retailstoreid varchar(18);
declare @taskId numeric(10,0);
set @retailstoreid = 'S1688';
select @taskId = MIN(taskid) from tm.Task where ActionId = 14 and PriceChangeDate > GETDATE() - 1;
--total de items para a loja na staging
select count(distinct itemid) as "Total de Items Integrados" from stra.itempricechange with(nolock) where pricechangedate > getdate() - 1 and retailstoreid = @retailstoreid;
--total de items com alerta configurado
select count(distinct i.itemid) as "Total de Items Ativos e com Alerta Configurado"
from ra.item i with(nolock)
join ra.itemretailstore irs with(nolock) on irs.itemid = i.itemid
where irs.retailstoreid = @retailstoreid
and irs.status in ('A','D')
and i.itemid in (select itemid from stra.itempricechange with(nolock) where retailstoreid = @retailstoreid and pricechangedate > getdate() -1 and IntegrationStatus = 'P') and hierarchicalstructureid in (SELECT DISTINCT hsp.HierarchicalStructureId
FROM ra.HierarchicalStructurePath hsp with(nolock)
INNER JOIN ra.BusinessStructureItem bsi with(nolock) ON bsi.HierarchicalStructureId = hsp.HierarchicalAncestorId
WHERE bsi.BusinessStructureListId in (select a.businessstrucutureid from tm.alertconfig a with(nolock) join tm.locationitem li with(nolock) on li.locationlistid = a.locationlistid where li.retailstoreid = @retailstoreid and a.status in ('A','W')));
-- total de items sem alerta configurado
select count(distinct itemid) as "Total de Items sem Alerta Configurado"
from ra.item with(nolock) where itemid in (select itemid from stra.itempricechange with(nolock) where retailstoreid = @retailstoreid and pricechangedate > getdate() -1) and hierarchicalstructureid not in (SELECT DISTINCT hsp.HierarchicalStructureId
FROM ra.HierarchicalStructurePath hsp with(nolock)
INNER JOIN ra.BusinessStructureItem bsi with(nolock) ON bsi.HierarchicalStructureId = hsp.HierarchicalAncestorId
WHERE bsi.BusinessStructureListId in (select a.businessstrucutureid from tm.alertconfig a with(nolock) join tm.locationitem li with(nolock) on li.locationlistid = a.locationlistid where li.retailstoreid = @retailstoreid and a.[status] IN ('A','W')));
-- total de items na tarefa
select count(distinct itemid) as "Total de Items nas Tarefas" from tm.taskitem with(nolock) where taskid in(select TaskId from tm.Task with(nolock) where TaskId >= @taskId and actionid = 14 and retailstoreid = @retailstoreid)
-- Valdia se a loja tem alertas
select * from tm.alertconfig ac
inner join tm.Locationlist ll on ac.LocationListId = ll.LocationListId
inner join tm.locationitem li on li.locationlistid=ll.locationlistid
where li.retailstoreid='S1688'