pg_business_bonus.go
3.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
package dao
import (
"fmt"
"strings"
"time"
"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 BusinessBonusDao struct {
transactionContext *transaction.TransactionContext
}
func NewBusinessBonusDao(transactionContext *transaction.TransactionContext) (*BusinessBonusDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &BusinessBonusDao{
transactionContext: transactionContext,
}, nil
}
}
type CustomBusinessBonus struct {
Id int64
Bonus string
BonusNot string
BonusExpense string
BonusHas string
BonusStatus int8
PartnerName string
UpdateAt time.Time
}
func (dao BusinessBonusDao) SearchBusinessBonus(partnerId int64, partnerNameMatch string,
companyId int64, limit int, offset int) ([]CustomBusinessBonus, error) {
sql := `SELECT business_bonus.id, business_bonus.bonus,business_bonus.bonus_not
,business_bonus.bonus_expense,business_bonus.bonus_status,business_bonus.update_at
,partner_info.partner_name,business_bonus.bonus_has
FROM business_bonus
JOIN partner_info ON business_bonus.partner_info_id=partner_info.id
WHERE business_bonus.is_disable=? AND business_bonus.company_id = ? `
partnerCondition := []string{}
allParam := []interface{}{domain.BUSINESS_BONUS_ENABLE, companyId}
if partnerId > 0 {
partnerCondition = append(partnerCondition, ` business_bonus.partner_info_id=? `)
allParam = append(allParam, partnerId)
}
if len(partnerNameMatch) > 0 {
allParam = append(allParam, "%"+partnerNameMatch+"%")
partnerCondition = append(partnerCondition, ` partner_info.partner_name like ? `)
}
if len(partnerCondition) > 0 {
sql += fmt.Sprintf(" AND (%s)", strings.Join(partnerCondition, " OR "))
}
sql += `ORDER BY business_bonus.id DESC limit ? OFFSET ? `
allParam = append(allParam, limit, offset)
tx := dao.transactionContext.PgTx
var (
result []CustomBusinessBonus
err error
)
_, err = tx.Query(&result, sql, allParam...)
return result, err
}
func (dao BusinessBonusDao) CountBusinessBonus(partnerId int64, partnerNameMatch string,
companyId int64, limit int, offset int) (int, error) {
sql := `SELECT count(*)
FROM business_bonus
JOIN partner_info ON business_bonus.partner_info_id=partner_info.id
WHERE business_bonus.is_disable=? AND business_bonus.company_id = ? `
partnerCondition := []string{}
allParam := []interface{}{domain.BUSINESS_BONUS_ENABLE, companyId}
if partnerId > 0 {
partnerCondition = append(partnerCondition, ` business_bonus.partner_info_id=? `)
allParam = append(allParam, partnerId)
}
if len(partnerNameMatch) > 0 {
allParam = append(allParam, "%"+partnerNameMatch+"%")
partnerCondition = append(partnerCondition, ` partner_info.partner_name like ? `)
}
if len(partnerCondition) > 0 {
sql += fmt.Sprintf(" AND (%s)", strings.Join(partnerCondition, " OR "))
}
tx := dao.transactionContext.PgTx
var (
result int
err error
)
_, err = tx.Query(&result, sql, allParam...)
return result, err
}
func (dao BusinessBonusDao) ExistBusinessBonus(userId int64) (bool, error) {
tx := dao.transactionContext.PgTx
ok, err := tx.Model(&models.BusinessBonus{}).
Where("partner_info_id=?", userId).
Exists()
return ok, err
}