billdrtail

This commit is contained in:
苏尹岚
2020-10-22 15:10:06 +08:00
parent 777bdc6d93
commit b9c49f75a5
7 changed files with 105 additions and 166 deletions

View File

@@ -4,6 +4,7 @@ import (
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/model"
)
@@ -51,3 +52,60 @@ func GetBillExpend(db *DaoDB, userID string, billType int, fromTime, toTime time
err = GetRow(db, &billExpends, sql, sqlParams)
return billExpends, err
}
type UserBillDetail struct {
CreatedAt time.Time `json:"created_at"`
LastOperator string `json:"lastOperator"`
BillType int `json:"billType"`
Price int `json:"price"`
Type string `json:"type"`
}
func GetUserBillDetail(db *DaoDB, userID string, fromTime, toTime time.Time, pageSize, offset int) (pagedInfo *model.PagedInfo, err error) {
var userBillDetails []*UserBillDetail
sql := `
SELECT SQL_CALC_FOUND_ROWS k.* FROM (
SELECT a.created_at, a.last_operator, a.type bill_type, a.income_price price, 'income' type
FROM bill_income a
JOIN user_bill b ON a.bill_id = b.bill_id AND b.user_id = ?
`
sqlParams := []interface{}{userID}
if fromTime != utils.ZeroTimeValue {
sql += ` AND a.created_at >= ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND a.created_at <= ?`
sqlParams = append(sqlParams, toTime)
}
sql += `
UNION ALL
SELECT a.created_at, a.last_operator, a.type bill_type, a.expend_price price, 'expend' type
FROM bill_expend a
JOIN user_bill b ON a.bill_id = b.bill_id AND b.user_id = ?
`
sqlParams = append(sqlParams, userID)
if fromTime != utils.ZeroTimeValue {
sql += ` AND a.created_at >= ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND a.created_at <= ?`
sqlParams = append(sqlParams, toTime)
}
sql += `
)k
ORDER BY k.created_at LIMIT ? OFFSET ?
`
pageSize = jxutils.FormalizePageSize(pageSize)
sqlParams = append(sqlParams, pageSize, offset)
Begin(db)
defer Commit(db)
if err = GetRows(db, &userBillDetails, sql, sqlParams...); err == nil {
pagedInfo = &model.PagedInfo{
TotalCount: GetLastTotalRowCount(db),
Data: userBillDetails,
}
}
return pagedInfo, err
}