USE [master]
GO
/****** Object:  Database [LevelcodeSGI]    Script Date: 22/03/2026 23:26:54 ******/
CREATE DATABASE [LevelcodeSGI]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'LevelcodeSGI', FILENAME = N'/var/opt/mssql/data/LevelcodeSGI.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'LevelcodeSGI_log', FILENAME = N'/var/opt/mssql/data/LevelcodeSGI_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
 WITH CATALOG_COLLATION = DATABASE_DEFAULT, LEDGER = OFF
GO
ALTER DATABASE [LevelcodeSGI] SET COMPATIBILITY_LEVEL = 170
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [LevelcodeSGI].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [LevelcodeSGI] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET ARITHABORT OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [LevelcodeSGI] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [LevelcodeSGI] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET  ENABLE_BROKER 
GO
ALTER DATABASE [LevelcodeSGI] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [LevelcodeSGI] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET RECOVERY FULL 
GO
ALTER DATABASE [LevelcodeSGI] SET  MULTI_USER 
GO
ALTER DATABASE [LevelcodeSGI] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [LevelcodeSGI] SET DB_CHAINING OFF 
GO
ALTER DATABASE [LevelcodeSGI] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [LevelcodeSGI] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [LevelcodeSGI] SET DELAYED_DURABILITY = DISABLED 
GO
ALTER DATABASE [LevelcodeSGI] SET ACCELERATED_DATABASE_RECOVERY = OFF  
GO
ALTER DATABASE [LevelcodeSGI] SET OPTIMIZED_LOCKING = OFF 
GO
EXEC sys.sp_db_vardecimal_storage_format N'LevelcodeSGI', N'ON'
GO
ALTER DATABASE [LevelcodeSGI] SET QUERY_STORE = ON
GO
ALTER DATABASE [LevelcodeSGI] SET QUERY_STORE (OPERATION_MODE = READ_WRITE, CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30), DATA_FLUSH_INTERVAL_SECONDS = 900, INTERVAL_LENGTH_MINUTES = 60, MAX_STORAGE_SIZE_MB = 1000, QUERY_CAPTURE_MODE = AUTO, SIZE_BASED_CLEANUP_MODE = AUTO, MAX_PLANS_PER_QUERY = 200, WAIT_STATS_CAPTURE_MODE = ON)
GO
USE [LevelcodeSGI]
GO
/****** Object:  Table [dbo].[Pessoas]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Pessoas](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[NomeCompleto] [nvarchar](150) NOT NULL,
	[Email] [nvarchar](150) NULL,
	[Telefone] [nvarchar](20) NULL,
	[DataNascimento] [date] NULL,
	[Sexo] [nvarchar](10) NULL,
	[CPF] [nvarchar](20) NULL,
	[RG] [nvarchar](20) NULL,
	[DataCadastro] [datetime] NULL,
	[Ativo] [bit] NULL,
	[Cidade] [nvarchar](100) NULL,
	[Cargo] [nvarchar](100) NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[vw_CadastrosHoje]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vw_CadastrosHoje]
AS
SELECT COUNT(*) AS TotalHoje
FROM Pessoas
WHERE CAST(DataCadastro AS DATE) = CAST(GETDATE() AS DATE);
GO
/****** Object:  View [dbo].[vw_ResumoCadastros]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vw_ResumoCadastros]
AS
SELECT 
    COUNT(*) AS TotalCadastros,

    SUM(CASE 
        WHEN CAST(DataCadastro AS DATE) = CAST(GETDATE() AS DATE)
        THEN 1 ELSE 0 END) AS CadastrosHoje,

    SUM(CASE 
        WHEN CAST(DataCadastro AS DATE) = CAST(DATEADD(DAY,-1,GETDATE()) AS DATE)
        THEN 1 ELSE 0 END) AS CadastrosOntem,

    CASE 
        WHEN SUM(CASE 
            WHEN CAST(DataCadastro AS DATE) = CAST(DATEADD(DAY,-1,GETDATE()) AS DATE)
            THEN 1 ELSE 0 END) = 0
        THEN 100
        ELSE
            (
                (SUM(CASE 
                    WHEN CAST(DataCadastro AS DATE) = CAST(GETDATE() AS DATE)
                    THEN 1 ELSE 0 END) * 100.0)
                /
                SUM(CASE 
                    WHEN CAST(DataCadastro AS DATE) = CAST(DATEADD(DAY,-1,GETDATE()) AS DATE)
                    THEN 1 ELSE 0 END)
            )
    END AS CrescimentoPercentual

FROM Pessoas;
GO
/****** Object:  View [dbo].[vw_StatusPessoas]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vw_StatusPessoas]
AS
SELECT
    SUM(CASE WHEN Ativo = 1 THEN 1 ELSE 0 END) AS Ativos,
    SUM(CASE WHEN Ativo = 0 THEN 1 ELSE 0 END) AS Inativos
FROM Pessoas;
GO
/****** Object:  View [dbo].[vw_DistribuicaoSexo]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vw_DistribuicaoSexo]
AS
SELECT
    SUM(CASE WHEN Sexo = 'Masculino' THEN 1 ELSE 0 END) AS Masculino,
    SUM(CASE WHEN Sexo = 'Feminino' THEN 1 ELSE 0 END) AS Feminino,
    SUM(CASE WHEN Sexo NOT IN ('Masculino','Feminino') OR Sexo IS NULL THEN 1 ELSE 0 END) AS Outros,

    COUNT(*) AS Total,

    CAST(
        SUM(CASE WHEN Sexo = 'Masculino' THEN 1 ELSE 0 END) * 100.0 /
        NULLIF(COUNT(*),0)
    AS DECIMAL(5,2)) AS PercentMasculino,

    CAST(
        SUM(CASE WHEN Sexo = 'Feminino' THEN 1 ELSE 0 END) * 100.0 /
        NULLIF(COUNT(*),0)
    AS DECIMAL(5,2)) AS PercentFeminino,

    CAST(
        SUM(CASE WHEN Sexo NOT IN ('Masculino','Feminino') OR Sexo IS NULL THEN 1 ELSE 0 END) * 100.0 /
        NULLIF(COUNT(*),0)
    AS DECIMAL(5,2)) AS PercentOutros

FROM Pessoas;
GO
/****** Object:  Table [dbo].[Configuracoes]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Configuracoes](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Chave] [nvarchar](100) NULL,
	[Valor] [nvarchar](500) NULL,
	[Descricao] [nvarchar](200) NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Empresas]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Empresas](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[RazaoSocial] [nvarchar](200) NULL,
	[NomeFantasia] [nvarchar](200) NULL,
	[CNPJ] [nvarchar](20) NULL,
	[Telefone] [nvarchar](20) NULL,
	[Email] [nvarchar](150) NULL,
	[DataCadastro] [datetime] NULL,
	[Ativo] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Enderecos]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Enderecos](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[PessoaId] [int] NOT NULL,
	[CEP] [nvarchar](10) NULL,
	[Rua] [nvarchar](200) NULL,
	[Numero] [nvarchar](20) NULL,
	[Bairro] [nvarchar](100) NULL,
	[Cidade] [nvarchar](100) NULL,
	[Estado] [nvarchar](50) NULL,
	[Complemento] [nvarchar](200) NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Estatisticas]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Estatisticas](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [nvarchar](100) NULL,
	[Valor] [int] NULL,
	[DataReferencia] [date] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[LogsSistema]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[LogsSistema](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[UsuarioId] [int] NULL,
	[Acao] [nvarchar](200) NULL,
	[Descricao] [nvarchar](500) NULL,
	[DataHora] [datetime] NULL,
	[IP] [nvarchar](50) NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Notificacoes]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Notificacoes](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[UsuarioId] [int] NULL,
	[Mensagem] [nvarchar](300) NULL,
	[Lida] [bit] NULL,
	[DataCriacao] [datetime] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[PerfilPermissoes]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PerfilPermissoes](
	[PerfilId] [int] NOT NULL,
	[PermissaoId] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[PerfilId] ASC,
	[PermissaoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Perfis]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Perfis](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [nvarchar](100) NOT NULL,
	[Descricao] [nvarchar](200) NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Permissoes]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Permissoes](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [nvarchar](100) NOT NULL,
	[Descricao] [nvarchar](200) NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Relatorios]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Relatorios](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [nvarchar](150) NULL,
	[Categoria] [nvarchar](100) NULL,
	[CaminhoArquivo] [nvarchar](300) NULL,
	[GeradoPor] [int] NULL,
	[DataGeracao] [datetime] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Usuarios]    Script Date: 22/03/2026 23:26:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Usuarios](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [nvarchar](150) NOT NULL,
	[Email] [nvarchar](150) NOT NULL,
	[SenhaHash] [nvarchar](255) NOT NULL,
	[TipoConta] [nvarchar](50) NOT NULL,
	[EmailVerificado] [bit] NULL,
	[TelefoneVerificado] [bit] NULL,
	[UltimoLogin] [datetime] NULL,
	[CriadoEm] [datetime] NULL,
	[AtualizadoEm] [datetime] NULL,
	[Status] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
	[Email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Empresas] ADD  DEFAULT (getdate()) FOR [DataCadastro]
GO
ALTER TABLE [dbo].[Empresas] ADD  DEFAULT ((1)) FOR [Ativo]
GO
ALTER TABLE [dbo].[LogsSistema] ADD  DEFAULT (getdate()) FOR [DataHora]
GO
ALTER TABLE [dbo].[Notificacoes] ADD  DEFAULT ((0)) FOR [Lida]
GO
ALTER TABLE [dbo].[Notificacoes] ADD  DEFAULT (getdate()) FOR [DataCriacao]
GO
ALTER TABLE [dbo].[Pessoas] ADD  DEFAULT (getdate()) FOR [DataCadastro]
GO
ALTER TABLE [dbo].[Pessoas] ADD  DEFAULT ((1)) FOR [Ativo]
GO
ALTER TABLE [dbo].[Relatorios] ADD  DEFAULT (getdate()) FOR [DataGeracao]
GO
ALTER TABLE [dbo].[Usuarios] ADD  DEFAULT ((0)) FOR [EmailVerificado]
GO
ALTER TABLE [dbo].[Usuarios] ADD  DEFAULT ((0)) FOR [TelefoneVerificado]
GO
ALTER TABLE [dbo].[Usuarios] ADD  DEFAULT (getdate()) FOR [CriadoEm]
GO
ALTER TABLE [dbo].[Usuarios] ADD  DEFAULT ((1)) FOR [Status]
GO
ALTER TABLE [dbo].[Enderecos]  WITH CHECK ADD FOREIGN KEY([PessoaId])
REFERENCES [dbo].[Pessoas] ([Id])
GO
ALTER TABLE [dbo].[LogsSistema]  WITH CHECK ADD FOREIGN KEY([UsuarioId])
REFERENCES [dbo].[Usuarios] ([Id])
GO
ALTER TABLE [dbo].[Notificacoes]  WITH CHECK ADD FOREIGN KEY([UsuarioId])
REFERENCES [dbo].[Usuarios] ([Id])
GO
ALTER TABLE [dbo].[PerfilPermissoes]  WITH CHECK ADD FOREIGN KEY([PerfilId])
REFERENCES [dbo].[Perfis] ([Id])
GO
ALTER TABLE [dbo].[PerfilPermissoes]  WITH CHECK ADD FOREIGN KEY([PermissaoId])
REFERENCES [dbo].[Permissoes] ([Id])
GO
ALTER TABLE [dbo].[Relatorios]  WITH CHECK ADD FOREIGN KEY([GeradoPor])
REFERENCES [dbo].[Usuarios] ([Id])
GO
USE [master]
GO
ALTER DATABASE [LevelcodeSGI] SET  READ_WRITE 
GO
