Skip to content

Commit

Permalink
retornando média por membro
Browse files Browse the repository at this point in the history
  • Loading branch information
joellensilva committed Sep 9, 2024
1 parent f290546 commit c09f3aa
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 42 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ func (c *Client) GetAllAgencyCollection(agency string) ([]models.AgencyMonthlyIn
}
return collections, nil
}

func (c *Client) GetAveragePerCapita(agency string, year int) (*models.AveragePerCapita, error) {
avg, err := c.Db.GetAveragePerCapita(agency, year)
if err != nil {
return nil, fmt.Errorf("GetAveragePerCapita() error: %w", err)
}
return avg, nil
}
10 changes: 10 additions & 0 deletions models/averagePerCapita.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type AveragePerCapita struct {
ID string `json:"orgao,omitempty"`
Year int `json:"ano,omitempty"`
BaseRemunerationPerCapita float64 `json:"remuneracao_base,omitempty"`
OtherRemunerationsPerCapita float64 `json:"outras_remuneracoes,omitempty"`
DiscountsPerCapita float64 `json:"descontos,omitempty"`
RemunerationsPerCapita float64 `json:"remuneracoes,omitempty"`
}
24 changes: 14 additions & 10 deletions models/monthlyInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,20 @@ type GeneralMonthlyInfo struct {
}

type AnnualSummary struct {
Year int `json:"year,omitempty"` // Year of the data
AverageCount int `json:"average_count,omitempty"` // Average number of employees
TotalCount int `json:"total_count,omitempty"` // Total number of employees
BaseRemuneration float64 `json:"base_remuneration,omitempty"` // Statistics (Max, Min, Median, Total)
OtherRemunerations float64 `json:"other_remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
Discounts float64 `json:"discounts,omitempty"` // Statistics (Max, Min, Median, Total)
Remunerations float64 `json:"remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
NumMonthsWithData int `json:"months_with_data,omitempty"`
Package *Backup `json:"package,omitempty"`
ItemSummary ItemSummary `json:"item_summary,omitempty"`
Year int `json:"year,omitempty"` // Year of the data
AverageCount int `json:"average_count,omitempty"` // Average number of employees
TotalCount int `json:"total_count,omitempty"` // Total number of employees
BaseRemuneration float64 `json:"base_remuneration,omitempty"` // Statistics (Max, Min, Median, Total)
OtherRemunerations float64 `json:"other_remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
BaseRemunerationPerCapita float64 `json:"base_remuneration_member,omitempty"`
OtherRemunerationsPerCapita float64 `json:"other_remunerations_member,omitempty"`
DiscountsPerCapita float64 `json:"discounts_member,omitempty"`
RemunerationsPerCapita float64 `json:"remunerations_member,omitempty"`
Discounts float64 `json:"discounts,omitempty"` // Statistics (Max, Min, Median, Total)
Remunerations float64 `json:"remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
NumMonthsWithData int `json:"months_with_data,omitempty"`
Package *Backup `json:"package,omitempty"`
ItemSummary ItemSummary `json:"item_summary,omitempty"`
}

type RemmunerationSummary struct {
Expand Down
15 changes: 15 additions & 0 deletions repo/database/database_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 37 additions & 25 deletions repo/database/dto/annuaISummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@ import (
)

type AnnualSummaryDTO struct {
Year int `gorm:"column:ano"`
AverageCount int `gorm:"column:media_num_membros"`
TotalCount int `gorm:"column:total_num_membros"`
BaseRemuneration float64 `gorm:"column:remuneracao_base"`
OtherRemunerations float64 `gorm:"column:outras_remuneracoes"`
Discounts float64 `gorm:"column:descontos"`
Remunerations float64 `gorm:"column:remuneracoes"`
NumMonthsWithData int `gorm:"column:meses_com_dados"`
ItemSummary ItemSummary `gorm:"embedded"`
Year int `gorm:"column:ano"`
AverageCount int `gorm:"column:media_num_membros"`
TotalCount int `gorm:"column:total_num_membros"`
BaseRemuneration float64 `gorm:"column:remuneracao_base"`
OtherRemunerations float64 `gorm:"column:outras_remuneracoes"`
Discounts float64 `gorm:"column:descontos"`
Remunerations float64 `gorm:"column:remuneracoes"`
BaseRemunerationPerCapita float64 `gorm:"column:remuneracao_base_membro"`
OtherRemunerationsPerCapita float64 `gorm:"column:outras_remuneracoes_membro"`
DiscountsPerCapita float64 `gorm:"column:descontos_membro"`
RemunerationsPerCapita float64 `gorm:"column:remuneracoes_membro"`
NumMonthsWithData int `gorm:"column:meses_com_dados"`
ItemSummary ItemSummary `gorm:"embedded"`
}

func NewAnnualSummaryDTO(ami models.AnnualSummary) *AnnualSummaryDTO {
return &AnnualSummaryDTO{
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
BaseRemunerationPerCapita: ami.BaseRemunerationPerCapita,
OtherRemunerationsPerCapita: ami.OtherRemunerationsPerCapita,
DiscountsPerCapita: ami.DiscountsPerCapita,
RemunerationsPerCapita: ami.RemunerationsPerCapita,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
ItemSummary: ItemSummary{
FoodAllowance: ami.ItemSummary.FoodAllowance,
BonusLicense: ami.ItemSummary.BonusLicense,
Expand All @@ -41,14 +49,18 @@ func NewAnnualSummaryDTO(ami models.AnnualSummary) *AnnualSummaryDTO {

func (ami *AnnualSummaryDTO) ConvertToModel() *models.AnnualSummary {
return &models.AnnualSummary{
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
BaseRemunerationPerCapita: ami.BaseRemunerationPerCapita,
OtherRemunerationsPerCapita: ami.OtherRemunerationsPerCapita,
DiscountsPerCapita: ami.DiscountsPerCapita,
RemunerationsPerCapita: ami.RemunerationsPerCapita,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
ItemSummary: models.ItemSummary{
FoodAllowance: ami.ItemSummary.FoodAllowance,
BonusLicense: ami.ItemSummary.BonusLicense,
Expand Down
27 changes: 27 additions & 0 deletions repo/database/dto/averagePerCapitaDTO.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dto

import "github.com/dadosjusbr/storage/models"

type AveragePerCapita struct {
ID string `gorm:"column:orgao"`
Year int `gorm:"column:ano"`
BaseRemunerationPerCapita float64 `gorm:"column:salario"`
OtherRemunerationsPerCapita float64 `gorm:"column:beneficios"`
DiscountsPerCapita float64 `gorm:"column:descontos"`
RemunerationsPerCapita float64 `gorm:"column:remuneracao"`
}

func (AveragePerCapita) TableName() string {
return "media_por_membro"
}

func (a *AveragePerCapita) ConvertToModel() *models.AveragePerCapita {
return &models.AveragePerCapita{
ID: a.ID,
Year: a.Year,
BaseRemunerationPerCapita: a.BaseRemunerationPerCapita,
OtherRemunerationsPerCapita: a.OtherRemunerationsPerCapita,
DiscountsPerCapita: a.DiscountsPerCapita,
RemunerationsPerCapita: a.RemunerationsPerCapita,
}
}
22 changes: 22 additions & 0 deletions repo/database/init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ create table remuneracoes
constraint pk_remuneracoes primary key (id, id_contracheque, orgao, mes, ano),
constraint fk_remuneracoes foreign key (id_contracheque, orgao, mes, ano) references contracheques(id, orgao, mes, ano) on delete cascade
);

CREATE MATERIALIZED VIEW public.media_por_membro
TABLESPACE pg_default
AS SELECT media_por_membro.orgao,
media_por_membro.ano,
avg(media_por_membro.salario) AS salario,
avg(media_por_membro.beneficios) AS beneficios,
avg(media_por_membro.descontos) AS descontos,
avg(media_por_membro.remuneracao) AS remuneracao
FROM ( SELECT c.orgao,
c.ano,
c.nome_sanitizado,
count(*) AS num_meses,
avg(c.salario) AS salario,
avg(c.beneficios) AS beneficios,
avg(c.descontos) AS descontos,
avg(c.remuneracao) AS remuneracao
FROM contracheques c
GROUP BY c.orgao, c.ano, c.nome_sanitizado) media_por_membro
WHERE media_por_membro.num_meses > 1
GROUP BY media_por_membro.orgao, media_por_membro.ano
WITH DATA;
1 change: 1 addition & 0 deletions repo/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ type Interface interface {
GetAllAgencyCollection(agency string) ([]models.AgencyMonthlyInfo, error)
GetPaychecks(agency models.Agency, year int) ([]models.Paycheck, error)
GetPaycheckItems(agency models.Agency, year int) ([]models.PaycheckItem, error)
GetAveragePerCapita(agency string, year int) (*models.AveragePerCapita, error)
}
26 changes: 22 additions & 4 deletions repo/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (p *PostgresDB) GetAnnualSummary(agency string) ([]models.AnnualSummary, er
var dtoAmis []dto.AnnualSummaryDTO
agency = strings.ToLower(agency)
query := `
ano,
coletas.ano,
id_orgao,
TRUNC(AVG((sumario -> 'membros')::text::int)) AS media_num_membros,
SUM((sumario -> 'membros')::text::int) AS total_num_membros,
Expand All @@ -323,10 +323,16 @@ func (p *PostgresDB) GetAnnualSummary(agency string) ([]models.AnnualSummary, er
SUM(CAST(sumario -> 'resumo_rubricas' ->> 'auxilio_saude' AS DECIMAL)) AS auxilio_saude,
SUM(CAST(sumario -> 'resumo_rubricas' ->> 'outras' AS DECIMAL)) AS outras,
SUM(CAST(sumario -> 'resumo_rubricas' ->> 'ferias' AS DECIMAL)) AS ferias,
COUNT(*) AS meses_com_dados`
m := p.db.Model(&dtoAgmi).Select(query)
COUNT(*) AS meses_com_dados,
MAX(media_por_membro.salario) AS remuneracao_base_membro,
MAX(media_por_membro.beneficios) AS outras_remuneracoes_membro,
MAX(media_por_membro.descontos) AS descontos_membro,
MAX(media_por_membro.remuneracao) AS remuneracoes_membro`

join := `LEFT JOIN media_por_membro on coletas.ano = media_por_membro.ano and coletas.id_orgao = media_por_membro.orgao`
m := p.db.Model(&dtoAgmi).Select(query).Joins(join)
m = m.Where("id_orgao = ? AND atual = TRUE AND (procinfo::text = 'null' OR procinfo IS NULL) ", agency)
m = m.Group("ano, id_orgao").Order("ano ASC")
m = m.Group("coletas.ano, id_orgao").Order("coletas.ano ASC")
if err := m.Scan(&dtoAmis).Error; err != nil {
return nil, fmt.Errorf("error getting annual monthly info: %q", err)
}
Expand Down Expand Up @@ -549,3 +555,15 @@ func (p *PostgresDB) GetPaycheckItems(agency models.Agency, year int) ([]models.
}
return results, nil
}

func (p *PostgresDB) GetAveragePerCapita(agency string, ano int) (*models.AveragePerCapita, error) {
var dtoAvg dto.AveragePerCapita
//Pegando todas as coletas atuais de um determinado órgão.
m := p.db.Model(&dto.AveragePerCapita{})
m = m.Where("orgao = ? AND ano = ?", agency, ano)
if err := m.Find(&dtoAvg).Error; err != nil {
return nil, fmt.Errorf("error getting average per capita: %q", err)
}
avg := dtoAvg.ConvertToModel()
return avg, nil
}
Loading

0 comments on commit c09f3aa

Please sign in to comment.