pg_order_base_dao.go
4.6 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
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"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"
"time"
)
type OrderBaseDao struct {
transactionContext *transaction.TransactionContext
}
//订单统计
func (dao *OrderBaseDao) OrderStatics(option *domain.OrderStaticQuery) (count int, amount float64, err error) {
tx := dao.transactionContext.PgTx
order := new(models.OrderBase)
var orderAmount float64
q := tx.Model(order)
q.ColumnExpr("count(*) count")
//q.ColumnExpr("sum(case when use_order_amount >0 THEN use_order_amount else plan_order_amount END) total_order_actual_amount")
q.ColumnExpr("sum(plan_order_amount) total_order_actual_amount")
q.ColumnExpr("sum(use_order_amount) total_order_amount")
if option.PartnerId > 0 {
q.Where(`"order_base".partner_id =?`, option.PartnerId)
}
if option.OrderType > 0 {
q.Where(`"order_base".order_type =?`, option.OrderType)
}
if option.BeginTime > 0 {
q.Where(`"order_base".create_time >=?`, time.Unix(option.BeginTime/1000, 0))
}
if option.EndTime > 0 {
q.Where(`"order_base".create_time <?`, time.Unix(option.EndTime/1000, 0))
}
//q.Where(`"order_base".is_disable =?`, 0)
err = q.Select(&count, &amount, &orderAmount)
if option.OrderType == domain.OrderIntention { //意向订单值只有 order_amount order_actual_amount还未赋值
amount = orderAmount
}
return
}
//订单分红统计
func (dao *OrderBaseDao) OrderBonusStatics(option domain.OrderBonusQuery) (rsp domain.OrderBonusResponse, err error) {
rsp = domain.OrderBonusResponse{}
if option.PartnerId == 0 && option.CompanyId == 0 && len(option.InPartnerIds) == 0 {
return
}
tx := dao.transactionContext.PgTx
q := tx.Model(new(models.OrderBase))
q.ColumnExpr("count(*) count")
q.ColumnExpr("sum(plan_partner_bonus) bonus")
q.ColumnExpr("sum(partner_bonus_expense) bonus_expense")
if option.PartnerId > 0 {
q.Where(`"order_base".partner_id =?`, option.PartnerId)
}
if option.CompanyId > 0 {
q.Where(`"order_base".company_id =?`, option.CompanyId)
}
if len(option.InPartnerIds) > 0 {
q.Where(`"order_base".partner_id in (?)`, pg.In(option.InPartnerIds))
}
err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense)
return
}
//分红订单
func (dao *OrderBaseDao) DividendOrders(option *domain.DividendOrdersQueryOption) (count int, domainOrders []*domain.OrderBase, err error) {
tx := dao.transactionContext.PgTx
var orders []*models.OrderBase
q := tx.Model(&orders)
//q.Column(`order_base.id`, `order_base.order_code`, `order_base.order_actual_amount`, `order_base.partner_bonus_percent`)
if option.OrderType > 0 {
q.Where(`"order_base".order_type=?`, option.OrderType)
}
if option.PartnerId > 0 {
q.Where(`"order_base".partner_id=?`, option.PartnerId)
}
if option.StartTime > 0 {
q.Where(`"order_base".create_time >=?`, time.Unix(option.StartTime/1000, 0))
}
if option.EndTime > 0 {
q.Where(`"order_base".create_time <?`, time.Unix(option.EndTime/1000, 0))
}
//if len(option.IsDisable) > 0 {
// value, _ := strconv.Atoi(option.IsDisable)
// q.Where(`"order_base".is_disable =?`, value)
//}
if option.DividendAction == 0 { //累计分红
if option.DetailAction == 0 { //已收明细
q.Where(`"order_base".partner_bonus_has>0`)
} else if option.DetailAction == 1 { //未收明细 //实际金额>已支付金额
q.Where(`"order_base".partner_bonus_not>0`)
}
} else if option.DividendAction == 1 { //分红支出
q.Where(`"order_base".partner_bonus_expense>0`)
}
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_base.id %v", option.SortByUpdateTime))
}
count, err = q.Distinct().SelectAndCount()
for i := range orders {
var domainOrder *domain.OrderBase
err = utils.GobModelTransform(&domainOrder, &orders[i])
if err != nil {
return
}
domainOrders = append(domainOrders, domainOrder)
}
return
}
func (dao *OrderBaseDao) UpdateLastViewTime(id int64, lastViewTime time.Time) (err error) {
tx := dao.transactionContext.PgTx
order := new(models.OrderBase)
q := tx.Model(order).Set("last_view_time = ?", lastViewTime)
q.Where("id=?", id)
_, err = q.Update()
return
}
func NewOrderBaseDao(transactionContext *transaction.TransactionContext) (*OrderBaseDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrderBaseDao{
transactionContext: transactionContext,
}, nil
}
}