pg_customer_service_repository.go
3.4 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
package repository
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type ImCustomerServiceRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *ImCustomerServiceRepository) Save(dm *domain.ImCustomerService) (*domain.ImCustomerService, error) {
var (
err error
m = &models.ImCustomerService{}
tx = repository.transactionContext.PgTx
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if _, err = tx.Model(m).Returning("id").Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if _, err = tx.Model(m).Where("id=?", m.Id).Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *ImCustomerServiceRepository) Remove(ImCustomerService *domain.ImCustomerService) (*domain.ImCustomerService, error) {
var (
tx = repository.transactionContext.PgTx
ImCustomerServiceModel = &models.ImCustomerService{Id: ImCustomerService.Identify().(int64)}
)
if _, err := tx.Model(ImCustomerServiceModel).Where("id = ?", ImCustomerService.Id).Delete(); err != nil {
return ImCustomerService, err
}
return ImCustomerService, nil
}
func (repository *ImCustomerServiceRepository) FindOne(queryOptions map[string]interface{}) (*domain.ImCustomerService, error) {
tx := repository.transactionContext.PgTx
ImCustomerServiceModel := new(models.ImCustomerService)
query := NewQuery(tx.Model(ImCustomerServiceModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("user_id = ?", "user_id")
if err := query.First(); err != nil {
return nil, domain.QueryNoRow
}
if ImCustomerServiceModel.Id == 0 {
return nil, domain.QueryNoRow
}
return repository.transformPgModelToDomainModel(ImCustomerServiceModel)
}
func (repository *ImCustomerServiceRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ImCustomerService, error) {
tx := repository.transactionContext.PgTx
var ImCustomerServiceModels []*models.ImCustomerService
ImCustomerServices := make([]*domain.ImCustomerService, 0)
query := NewQuery(tx.Model(&ImCustomerServiceModels), queryOptions).
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime").
SetOrder("id", "sortById")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, ImCustomerServices, err
}
for _, ImCustomerServiceModel := range ImCustomerServiceModels {
if ImCustomerService, err := repository.transformPgModelToDomainModel(ImCustomerServiceModel); err != nil {
return 0, ImCustomerServices, err
} else {
ImCustomerServices = append(ImCustomerServices, ImCustomerService)
}
}
return int64(query.AffectRow), ImCustomerServices, nil
}
func (repository *ImCustomerServiceRepository) transformPgModelToDomainModel(ImCustomerServiceModel *models.ImCustomerService) (*domain.ImCustomerService, error) {
m := &domain.ImCustomerService{}
err := GobModelTransform(m, ImCustomerServiceModel)
return m, err
}
func NewImCustomerServiceRepository(transactionContext *transaction.TransactionContext) (*ImCustomerServiceRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &ImCustomerServiceRepository{transactionContext: transactionContext}, nil
}