pg_partner_dao.go
5.1 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
147
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"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"
"strings"
)
type PartnerInfoDao struct {
transactionContext *transaction.TransactionContext
}
func (dao *PartnerInfoDao) Update(queryOptions map[string]interface{}) error {
tx := dao.transactionContext.PgTx
m := new(models.PartnerInfo)
query := NewQuery(tx.Model(m), queryOptions)
query.SetUpdate(`account=?`, "Account")
query.SetUpdate(`password=?`, "Password")
query.SetWhere(`id=?`, "Id")
query.SetWhere(`account=?`, "oldAccount")
if _, ok := queryOptions["Id"]; !ok {
if _, ok := queryOptions["oldAccount"]; !ok {
return fmt.Errorf(`error: miss param "Id"`)
}
}
_, err := query.Update()
return err
}
func (dao *PartnerInfoDao) PartnerStatic(queryOptions map[string]interface{}) (count int, err error) {
tx := dao.transactionContext.PgTx
m := new(models.PartnerInfo)
query := NewQuery(tx.Model(m), queryOptions)
query.ColumnExpr("count(*) count")
if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
}
if inPartnerCategory, ok := queryOptions["inPartnerCategory"]; ok {
query.Where(`partner_info.partner_category_infos @>'[{"id":?}]'`, inPartnerCategory)
}
// 多个合伙人类型
if inPartnerCategory, ok := queryOptions["inPartnerCategories"]; ok {
query.Query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
if arrayInPartnerCategory, ok := inPartnerCategory.([]int64); ok {
for i := range arrayInPartnerCategory {
q.WhereOr(`partner_info.partner_category_infos @>'[{"id":?}]')`, arrayInPartnerCategory[i])
}
}
return q, nil
})
}
err = query.Select(&count)
return
}
func (dao *PartnerInfoDao) Partners(partnerIds []int64, queryOptions map[string]interface{}) (statics []*domain.PartnerStatics, err error) {
tx := dao.transactionContext.PgDd
var sql = strings.Builder{}
var filterPartners = strings.Builder{}
if districts, ok := queryOptions["districts"]; ok {
districts, ok := districts.([]string)
if ok && len(districts) > 0 {
var filterDistricts = strings.Builder{}
for i := range districts {
filterDistricts.WriteString(fmt.Sprintf(` region_info @>'{"regionName":"%v"}'`, districts[i]))
if i != (len(districts) - 1) {
filterDistricts.WriteString(" or ")
}
}
filterPartners.WriteString(fmt.Sprintf(" and (%v)", filterDistricts.String()))
}
}
if joinWays, ok := queryOptions["joinWays"]; ok {
joinWays, ok := joinWays.([]int64)
if ok && len(joinWays) > 0 {
var filterJoinWays = strings.Builder{}
for i := range joinWays {
filterJoinWays.WriteString(fmt.Sprintf(` partner_category_infos @>'[{"id":%v}]'`, joinWays[i]))
if i != (len(joinWays) - 1) {
filterJoinWays.WriteString(" or ")
}
}
filterPartners.WriteString(fmt.Sprintf(" and (%v)", filterJoinWays.String()))
}
}
if startTime, ok := queryOptions["startTime"]; ok {
filterPartners.WriteString(fmt.Sprintf(" and cooperate_time>=to_timestamp(%v)", startTime))
}
if endTime, ok := queryOptions["endTime"]; ok {
filterPartners.WriteString(fmt.Sprintf(" and cooperate_time<to_timestamp(%v)", endTime))
}
if keyword, ok := queryOptions["keyword"]; ok && keyword.(string) != "" {
filterPartners.WriteString(fmt.Sprintf(` and partner_name LIKE '%v'`, fmt.Sprintf("%%%s%%", "%%"+keyword.(string)+"%")))
}
sql.WriteString(fmt.Sprintf(`
SELECT A.*,B.total,B.amount,COALESCE(B.bonus,0) bonus,B.bonus_expense
FROM
(
SELECT * FROM partner_info
WHERE (id in (?)) %v
)
A left join
(
SELECT partner_id,count(*) total,sum(amount) amount,sum(bonus) bonus,sum(bonus_expense) bonus_expense FROM
(
SELECT partner_id,
plan_order_amount amount,
(case when use_order_count>=0 then use_partner_bonus else plan_partner_bonus end) bonus,
partner_bonus_expense bonus_expense FROM "order_base" AS "order_base"
WHERE (partner_id in (?)) and (order_type in (?))
UNION ALL
SELECT partner_info_id partner_id,
0 amount, bonus bonus, bonus_expense bonus_expense FROM business_bonus
WHERE (partner_info_id in (?)) and is_disable=1
) B
GROUP BY partner_id
) B on A."id" = B.partner_id`, filterPartners.String()))
if sortByBonus, ok := queryOptions["sortByBonus"]; ok {
sql.WriteString(fmt.Sprintf(" \norder by bonus %v", sortByBonus))
}
if limit, ok := queryOptions["limit"]; ok {
sql.WriteString(fmt.Sprintf(" \nLIMIT %v", limit))
if offset, ok := queryOptions["offset"]; ok {
sql.WriteString(fmt.Sprintf(" \nOFFSET %v", offset))
}
}
_, err = tx.Query(&statics, sql.String(), pg.In(partnerIds), pg.In(partnerIds), pg.In(domain.UserOrderTypes(domain.Career)), pg.In(partnerIds))
return
}
func NewPartnerInfoDao(transactionContext *transaction.TransactionContext) (*PartnerInfoDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PartnerInfoDao{
transactionContext: transactionContext,
}, nil
}
}