pg_business_bonus_repository.go
4.3 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 BusinessBonusRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *BusinessBonusRepository) transformPgModelToDomainModel(m *models.BusinessBonus) (domain.BusinessBonus, error) {
dm := domain.BusinessBonus{
Id: m.Id,
CompanyId: m.CompanyId,
PartnerInfoId: m.PartnerInfoId,
Bonus: m.Bonus,
BonusNot: m.BonusNot,
BonusHas: m.BonusHas,
BonusExpense: m.BonusExpense,
IsDisable: m.IsDisable,
BonusStatus: m.BonusStatus,
CreateAt: m.CreateAt,
UpdateAt: m.UpdateAt,
}
switch m.BonusStatus {
case domain.BUSINESS_BONUS_WAIT_PAY:
dm.BusinessBonusPayStatus = domain.BusinessBonusWaitPay{}
case domain.BUSINESS_BONUS_HAS_PAY:
dm.BusinessBonusPayStatus = domain.BusinessBonusHasPay{}
default:
dm.BusinessBonusPayStatus = domain.BusinessBonusNullPay{}
}
return dm, nil
}
func NewBusinessBonusRepository(transactionContext *transaction.TransactionContext) (*BusinessBonusRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &BusinessBonusRepository{transactionContext: transactionContext}, nil
}
func (repository *BusinessBonusRepository) Add(dm *domain.BusinessBonus) error {
tx := repository.transactionContext.PgTx
bonusModel := models.BusinessBonus{
Id: dm.Id,
CompanyId: dm.CompanyId,
PartnerInfoId: dm.PartnerInfoId,
Bonus: dm.Bonus,
BonusNot: dm.Bonus,
BonusHas: dm.BonusHas,
BonusExpense: dm.BonusExpense,
IsDisable: dm.IsDisable,
BonusStatus: dm.BonusStatus,
}
_, err := tx.Model(&bonusModel).
Insert()
dm.CreateAt = bonusModel.CreateAt
dm.UpdateAt = bonusModel.UpdateAt
dm.Id = bonusModel.Id
return err
}
func (repository *BusinessBonusRepository) Edit(dm *domain.BusinessBonus) error {
tx := repository.transactionContext.PgTx
bonusModel := models.BusinessBonus{
Id: dm.Id,
CompanyId: dm.CompanyId,
PartnerInfoId: dm.PartnerInfoId,
Bonus: dm.Bonus,
BonusNot: dm.BonusNot,
BonusExpense: dm.BonusExpense,
BonusHas: dm.BonusHas,
IsDisable: dm.IsDisable,
BonusStatus: dm.BonusStatus,
CreateAt: dm.CreateAt,
UpdateAt: dm.UpdateAt,
}
_, err := tx.Model(&bonusModel).WherePK().Update()
return err
}
func (repository *BusinessBonusRepository) FindOne(queryOptions domain.BusinessBonusFindOneQuery) (*domain.BusinessBonus, error) {
tx := repository.transactionContext.PgTx
BusinessBonusModel := new(models.BusinessBonus)
var hasCondition bool
query := tx.Model(BusinessBonusModel).Where("delete_at ISNULL")
if queryOptions.Id > 0 {
hasCondition = true
query = query.Where("id=?", queryOptions.Id)
}
if queryOptions.PartnerId > 0 {
hasCondition = true
query = query.Where("partner_info_id=?", queryOptions.PartnerId)
}
if queryOptions.CompanyId > 0 {
hasCondition = true
query = query.Where("company_id=?", queryOptions.CompanyId)
}
if !hasCondition {
return nil, errors.New("没有查询条件")
}
if err := query.First(); err != nil {
return nil, err
}
data, err := repository.transformPgModelToDomainModel(BusinessBonusModel)
return &data, err
}
func (repository *BusinessBonusRepository) Find(queryOptions domain.BusinessBonusFindQuery) (int, []domain.BusinessBonus, error) {
tx := repository.transactionContext.PgTx
var (
err error
cnt int
businessBonuss []domain.BusinessBonus
businessBonusModels []models.BusinessBonus
)
query := tx.Model(&businessBonusModels)
if queryOptions.Offset > -1 {
query = query.Offset(queryOptions.Offset)
}
if queryOptions.Limit > 0 {
query = query.Limit(queryOptions.Limit)
} else {
query = query.Limit(1000)
}
query = query.Order("business_bonus.id DESC")
if cnt, err = query.SelectAndCount(); err != nil {
return 0, businessBonuss, err
}
for i := range businessBonusModels {
businessBonus, err := repository.transformPgModelToDomainModel(&businessBonusModels[i])
if err != nil {
return 0, businessBonuss, err
}
businessBonuss = append(businessBonuss, businessBonus)
}
return cnt, businessBonuss, nil
}