pg_company_repository.go
2.7 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
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 CompanyRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *CompanyRepository) Save(dm *domain.Company) (*domain.Company, error) {
var (
err error
m = &models.Company{}
tx = repository.transactionContext.PgTx
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Id == 0 {
if err = tx.Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if err = tx.Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *CompanyRepository) Remove(Company *domain.Company) (*domain.Company, error) {
var (
tx = repository.transactionContext.PgTx
CompanyModel = &models.Company{Id: Company.Id}
)
if _, err := tx.Model(CompanyModel).Where("id = ?", Company.Id).Delete(); err != nil {
return Company, err
}
return Company, nil
}
func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
tx := repository.transactionContext.PgTx
CompanyModel := new(models.Company)
query := NewQuery(tx.Model(CompanyModel), queryOptions)
query.SetWhere(`"company".id = ?`, "id")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
}
if CompanyModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(CompanyModel)
}
func (repository *CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Company, error) {
tx := repository.transactionContext.PgTx
var CompanyModels []*models.Company
Companys := make([]*domain.Company, 0)
query := NewQuery(tx.Model(&CompanyModels), queryOptions)
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, Companys, err
}
for _, CompanyModel := range CompanyModels {
if Company, err := repository.transformPgModelToDomainModel(CompanyModel); err != nil {
return 0, Companys, err
} else {
Companys = append(Companys, Company)
}
}
return int64(query.AffectRow), Companys, nil
}
func (repository *CompanyRepository) transformPgModelToDomainModel(CompanyModel *models.Company) (*domain.Company, error) {
m := &domain.Company{}
err := GobModelTransform(m, CompanyModel)
return m, err
}
func NewCompanyRepository(transactionContext *transaction.TransactionContext) (*CompanyRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &CompanyRepository{transactionContext: transactionContext}, nil
}