pg_position_repository.go
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package repository
import (
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)
type PositionRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func NewPositionRepository(tx *pgTransaction.TransactionContext) *PositionRepository {
return &PositionRepository{
transactionContext: tx,
}
}
// Insert 新增职位
func (repository *PositionRepository) Insert(position *domain.Position) (*domain.Position, error) {
model := &models.Position{
Id: position.Id,
CompanyId: position.CompanyId,
Name: position.Name,
ParentId: position.ParentId,
Path: position.Path,
Level: position.Level,
}
_, err := repository.transactionContext.PgTx.Model(model).Insert()
return position, err
}
// Update 更新
func (repository *PositionRepository) Update(position *domain.Position) (*domain.Position, error) {
model := &models.Position{
Id: position.Id,
CompanyId: position.CompanyId,
Name: position.Name,
ParentId: position.ParentId,
Path: position.Path,
Level: position.Level,
UpdatedAt: time.Now(),
}
_, err := repository.transactionContext.PgTx.Model(model).WherePK().Update()
return position, err
}
// Remove 删除
func (repository *PositionRepository) Remove(ids []int64) error {
_, err := repository.transactionContext.PgTx.Model(&models.Position{}).Where("id in (?)", pg.In(ids)).Delete()
return err
}
func (repository *PositionRepository) FindOne(queryOption map[string]interface{}) (*domain.Position, error) {
position := &models.Position{}
query := repository.transactionContext.PgTx.Model(position)
if id, ok := queryOption["id"]; ok {
query.Where("id = ?", id)
}
err := query.First()
if err != nil {
return nil, err
}
return repository.transferToDomain(position), nil
}
func (repo *PositionRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Position, error) {
tx := repo.transactionContext.PgTx
positionModel := []models.Position{}
query := tx.Model(&positionModel)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
if v, ok := queryOptions["ids"]; ok {
query.Where("id in(?)", pg.In(v))
}
if v, ok := queryOptions["companyId"]; ok {
query.Where("company_id=?", v)
}
if v, ok := queryOptions["offset"]; ok {
query.Offset(v.(int))
}
if v, ok := queryOptions["limit"]; ok {
query.Limit(v.(int))
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
arrays := make([]*domain.Position, 0)
for i := range positionModel {
result := repo.transferToDomain(&positionModel[i])
arrays = append(arrays, result)
}
return cnt, arrays, nil
}
func (repository *PositionRepository) transferToDomain(model *models.Position) *domain.Position {
return &domain.Position{
Id: model.Id,
CompanyId: model.CompanyId,
Name: model.Name,
ParentId: model.ParentId,
Path: model.Path,
Level: model.Level,
}
}