pg_position_repository.go
2.2 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
package repository
import (
"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"
"time"
)
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 (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,
}
}