pg_order_base_dao.go
5.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package dao
import (
"fmt"
"github.com/go-pg/pg/v10/orm"
"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 OrderBaseDao struct {
transactionContext *transaction.TransactionContext
}
func NewOrderBaseDao(transactionContext *transaction.TransactionContext) (*OrderBaseDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrderBaseDao{
transactionContext: transactionContext,
}, nil
}
}
//OrderCodeExist 检查order_code是否重复
//
func (dao OrderBaseDao) OrderCodeExist(code string, partnerCategory int64, partnerId int64) (bool, error) {
tx := dao.transactionContext.GetDB()
m := &models.OrderBase{}
query := tx.Model(m).
Where("order_code=?", code).
Where("partner_id=?", partnerId).
Where(`partner_category @>'{"id":?}'`, partnerCategory)
ok, err := query.Exists()
return ok, err
}
func (dao OrderBaseDao) DeliveryCodeExist(code string, companyId int64, notId ...int64) (bool, error) {
tx := dao.transactionContext.GetDB()
m := &models.OrderBase{}
query := tx.Model(m).Where("delivery_code=?", code).Where("company_id=?", companyId)
if len(notId) > 0 {
query = query.WhereIn("id not in(?)", notId)
}
ok, err := query.Exists()
return ok, err
}
//OrderListByCondition 根据条件获取订单分红列表
//orderType 订单类型
//partnerOrCode 合伙人姓名或订单号或发货单号
func (dao OrderBaseDao) OrderBonusListByCondition(companyId int64, orderType int, partnerOrCode string, limit, offset int) ([]models.OrderBase, int, error) {
tx := dao.transactionContext.GetDB()
var orders []models.OrderBase
query := tx.Model(&orders).Where("order_base.company_id=?", companyId)
if orderType > 0 {
query = query.Where("order_base.order_type=?", orderType)
} else {
query = query.Where("order_base.order_type<>?", domain.OrderIntention)
}
if len(partnerOrCode) > 0 {
query = query.Join("LEFT JOIN partner_info as p ON order_base.partner_id=p.id").
WhereGroup(func(q *orm.Query) (*orm.Query, error) {
q = q.WhereOr("order_base.order_code like ? ", "%"+partnerOrCode+"%").
WhereOr("order_base.delivery_code like ? ", "%"+partnerOrCode+"%").
WhereOr("p.partner_name like ? ", "%"+partnerOrCode+"%")
return q, nil
})
}
query = query.Order("order_base.update_time DESC").
Offset(offset).
Limit(limit)
cnt, err := query.SelectAndCount()
return orders, cnt, err
}
//导出分红列表所用的sql
// SELECT
// ROW_NUMBER() over(ORDER BY t1.update_time DESC) AS num,
// t1.ID AS order_id,
// t1.order_type,
// t1.order_code,
// t1.bonus_status,
// to_char(t1.update_time at time zone 'CCT' ,'YYYY-MM-DD HH24:MI:SS') AS update_time,
// (CASE
// WHEN t1.use_partner_bonus>0
// THEN
// t1.use_partner_bonus
// ELSE
// t1.plan_partner_bonus
// END) AS partner_bonus,
// t1.partner_bonus_has,
// t1.partner_bonus_not,
// t1.partner_bonus_expense,
// tt1.has_bonus_percent,
// t2.partner_name
// FROM
// order_base AS t1
// LEFT JOIN partner_info AS t2 ON t1.partner_id = t2."id"
// LEFT JOIN (
// SELECT COUNT ( * ) AS has_bonus_percent, t3.order_id
// FROM "order_good" AS t3
// WHERE t3.partner_bonus_percent >= 0
// GROUP BY t3.order_id
// ) AS tt1 ON t1."id" = tt1.order_id
type CustomOrderBonusForExcel struct {
Num int
OrderId int64
OrderType int
OrderCode string
BonusStatus int
DeliveryCode string
UpdateTime string
PartnerBonus float64
PartnerBonusHas float64
PartnerBonusNot float64
PartnerBonusExpense float64
HasBonusPercent int
PartnerName string
}
//OrderBonusListForExcel 导出分红列表所用
//@param companyId 公司id
//@param orderType 订单类型
//@param partnerOrCode 合伙人姓名或订单号或发货单号
//@param partnerCategory 合伙人类型id
//@return result 处理后的数据,可直接用于导出数据到excel
//@return column 数据对应的键名,例 [0][英文键名,中文名称]
func (dao OrderBaseDao) OrderBonusListForExcel(companyId int64, orderType int, partnerOrCode string,
partnerCategory int) (result []CustomOrderBonusForExcel, err error) {
sqlStr := `SELECT
t1.ID AS order_id,
t1.order_type,
t1.order_code,
t1.delivery_code,
t1.bonus_status,
to_char(t1.update_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS') AS update_time,
(CASE
WHEN t1.use_partner_bonus>0
THEN
t1.use_partner_bonus
ELSE
t1.plan_partner_bonus
END) AS partner_bonus,
t1.partner_bonus_has,
t1.partner_bonus_not,
t1.partner_bonus_expense,
tt1.has_bonus_percent,
t2.partner_name
FROM
order_base AS t1
LEFT JOIN partner_info AS t2 ON t1.partner_id = t2."id"
LEFT JOIN (
SELECT COUNT ( * ) AS has_bonus_percent, t3.order_id
FROM "order_good" AS t3
WHERE t3.partner_bonus_percent >= 0
GROUP BY t3.order_id
) AS tt1 ON t1."id" = tt1.order_id
WHERE 1=1 `
sqlStr += ` AND t1.company_id = ? `
param := []interface{}{companyId}
if orderType > 0 {
param = append(param, orderType)
sqlStr += ` AND t1.order_type=? `
} else {
param = append(param, domain.OrderIntention)
sqlStr += ` AND t1.order_type<>? `
}
if len(partnerOrCode) > 0 {
sqlStr += ` AND (t1.order_code like ? OR t1.delivery_code like ? OR t2.partner_name like ? )`
likeParam := "%" + partnerOrCode + "%"
param = append(param, likeParam, likeParam, likeParam)
}
if partnerCategory > 0 {
sqlStr += ` AND t1.partner_category @>'{"id":?}'`
param = append(param, partnerCategory)
}
sqlStr += ` ORDER BY t1.update_time DESC limit 10 `
tx := dao.transactionContext.GetDB()
_, err = tx.Query(&result, sqlStr, param...)
if err != nil {
return result, err
}
return result, nil
}