pg_company_reponsitory.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
111
112
113
114
package repository
import (
"errors"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type CompanyRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.CompanyRepository = (*CompanyRepository)(nil)
)
func NewCompanyRepository(transactionContext *transaction.TransactionContext) (*CompanyRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &CompanyRepository{transactionContext: transactionContext}, nil
}
func (repository CompanyRepository) transformPgModelToDomainModel(m *models.Company) (domain.Company, error) {
return domain.Company{
Id: m.Id,
Name: m.Name,
Phone: m.Phone,
Logo: m.Logo,
Remarks: m.Remarks,
AdminCompanyId: m.AdminCompanyId,
Enable: m.Enable,
CreateAt: m.CreateAt,
UpdateAt: m.UpdateAt,
Abbreviation: m.Abbreviation,
}, nil
}
func (reponsitory CompanyRepository) Add(m *domain.Company) error {
var (
err error
tx = reponsitory.transactionContext.PgTx
)
companyModel := models.Company{
Id: m.Id,
Name: m.Name,
Phone: m.Phone,
Logo: m.Logo,
Remarks: m.Remarks,
AdminCompanyId: m.AdminCompanyId,
Enable: m.Enable,
CreateAt: m.CreateAt,
UpdateAt: m.UpdateAt,
Abbreviation: m.Abbreviation,
}
_, err = tx.Model(&companyModel).Insert()
return err
}
func (reponsitory CompanyRepository) Edit(m *domain.Company) error {
var (
err error
tx = reponsitory.transactionContext.PgTx
)
companyModel := models.Company{
Id: m.Id,
Name: m.Name,
Phone: m.Phone,
Logo: m.Logo,
Remarks: m.Remarks,
AdminCompanyId: m.AdminCompanyId,
Enable: m.Enable,
CreateAt: m.CreateAt,
DeleteAt: m.DeleteAt,
UpdateAt: m.UpdateAt,
Abbreviation: m.Abbreviation,
}
_, err = tx.Model(&companyModel).WherePK().Update()
return err
}
func (reponsitory CompanyRepository) FindOne(queryOptions domain.CompanyFindOneOptions) (domain.Company, error) {
var (
err error
tx = reponsitory.transactionContext.PgTx
m models.Company
hasCondition bool
)
query := tx.Model(&m)
if queryOptions.Id > 0 {
hasCondition = true
query = query.Where("id=?", queryOptions.Id)
}
if queryOptions.AdminCompanyId > 0 {
hasCondition = true
query = query.Where("admin_company_id=?", queryOptions.Id)
}
if !hasCondition {
return domain.Company{}, errors.New("findOne 必须要有查询条件")
}
err = query.First()
if err != nil {
return domain.Company{}, err
}
return reponsitory.transformPgModelToDomainModel(&m)
}
func (reponsitory CompanyRepository) Find(queryOptions domain.CompanyFindOptions) (int64, []domain.Company, error) {
return 0, nil, nil
}