pg_order_dao.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"
"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"
"time"
)
type OrderDao struct {
transactionContext *transaction.TransactionContext
}
//订单统计
func (dao *OrderDao) OrderStatics(option *domain.OrderStaticQuery) (count int, amount float64, err error) {
tx := dao.transactionContext.PgTx
order := new(models.Order)
var orderAmount float64
q := tx.Model(order)
q.ColumnExpr("count(*) count")
q.ColumnExpr("sum(order_actual_amount) total_order_actual_amount")
q.ColumnExpr("sum(order_amount) total_order_amount")
if option.PartnerId > 0 {
q.Where(`"order".partner_id =?`, option.PartnerId)
}
if option.OrderType > 0 {
q.Where(`"order".order_type =?`, option.OrderType)
}
if option.OrderStatus > 0 {
q.Where(`"order".order_status =?`, option.OrderStatus)
}
if option.BeginTime > 0 {
q.Where(`"order".create_at >=?`, time.Unix(option.BeginTime/1000, 0))
}
if option.EndTime > 0 {
q.Where(`"order".create_at <?`, time.Unix(option.EndTime/1000, 0))
}
err = q.Select(&count, &amount, &orderAmount)
if option.OrderType == domain.OrderIntention { //意向订单值只有 order_amount order_actual_amount还未赋值
amount = orderAmount
}
return
}
//分红订单
func (dao *OrderDao) DividendOrders(option *domain.DividendOrdersQueryOption) (count int, orders []*models.Order, err error) {
tx := dao.transactionContext.PgTx
q := tx.Model(&orders)
q.Column(`order.id`, `order.order_code`, `order.order_actual_amount`, `order.partner_bonus_percent`)
//q.Where(`"order".order_status >=?`, domain.OrderStatusDeliverSome) //已经发货
if option.OrderType > 0 {
q.Where(`"order".order_type=?`, option.OrderType)
}
if option.PartnerId > 0 {
q.Where(`"order".partner_id=?`, option.PartnerId)
}
if option.StartTime > 0 {
q.Where(`"order".create_at >=?`, time.Unix(option.StartTime/1000, 0))
}
if option.EndTime > 0 {
q.Where(`"order".create_at <?`, time.Unix(option.EndTime/1000, 0))
}
if option.DividendAction == 0 { //累计分红
if option.DetailAction == 0 { //已收明细
q.Join(`JOIN order_payment as a ON a.order_id="order".id`)
q.Where(`"a".bonus_status=?`, domain.BonusPaid)
} else if option.DetailAction == 1 { //未收明细 //实际金额>已支付金额
q.Where(`"order".order_actual_amount>"order".order_payment_amount`)
}
} else if option.DividendAction == 1 { //分红支出
q.Where(`"order".order_amount>"order".order_actual_amount`)
}
if option.Limit > 0 {
q.Limit(option.Limit)
}
if option.Offset > 0 {
q.Offset(option.Offset)
}
if len(option.SortByUpdateTime) > 0 {
q.Order(fmt.Sprintf("order.id %v", option.SortByUpdateTime))
}
count, err = q.Distinct().SelectAndCount()
return
}
func (dao *OrderDao) UpdateLastViewTime(id int64, lastViewTime time.Time) (err error) {
tx := dao.transactionContext.PgTx
order := new(models.Order)
q := tx.Model(order).Set("last_view_time = ?", lastViewTime)
q.Where("id=?", id)
_, err = q.Update()
return
}
func NewOrderDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrderDao{
transactionContext: transactionContext,
}, nil
}
}