pg_order_base_dao.go
5.4 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
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 len(option.OrderTypes) > 0 {
q.Where(`"order_base".order_type in (?)`, pg.In(option.OrderTypes))
}
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((case when use_order_count>=0 then use_partner_bonus else plan_partner_bonus end)) bonus")
q.ColumnExpr("sum(partner_bonus_expense) bonus_expense")
q.ColumnExpr("sum(plan_order_amount) total_order_amount")
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))
}
if option.OrderType > 0 {
q.Where(`"order_base".order_type =?`, option.OrderType)
}
if len(option.OrderTypes) > 0 {
q.Where(`"order_base".order_type in (?)`, pg.In(option.OrderTypes))
}
err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense, &rsp.TotalOrderAmount)
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 len(option.OrderTypes) > 0 {
q.Where(`"order_base".order_type in (?)`, pg.In(option.OrderTypes))
}
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(`(partner_bonus_has>0 or (partner_bonus_has=0 and coalesce(partner_bonus_not,0)=0 and bonus_status=2)) `)
} else if option.DetailAction == 1 { //未收明细 //实际金额>已支付金额 如果应收分红为0,根据支付状态判断是已收(2)还是未收(1)
q.Where(`(partner_bonus_not>0 or (partner_bonus_has=0 and coalesce(partner_bonus_not,0)=0 and bonus_status=1))`)
}
} 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
}
}