172 lines
5.0 KiB
Go
172 lines
5.0 KiB
Go
package dao
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
)
|
|
|
|
func GetUserBill(db *DaoDB, userID, billID string) (userBill *model.UserBill, err error) {
|
|
sql := `
|
|
SELECT * FROM user_bill WHERE deleted_at = ?
|
|
`
|
|
sqlParams := []interface{}{utils.DefaultTimeValue}
|
|
if userID != "" {
|
|
sql += ` AND user_id = ?`
|
|
sqlParams = append(sqlParams, userID)
|
|
}
|
|
if billID != "" {
|
|
sql += ` AND bill_id = ?`
|
|
sqlParams = append(sqlParams, billID)
|
|
}
|
|
err = GetRow(db, &userBill, sql, sqlParams)
|
|
return userBill, err
|
|
}
|
|
|
|
func GetBillExpend(db *DaoDB, userID string, billType int, fromTime, toTime time.Time) (billExpends []*model.BillExpend, err error) {
|
|
sql := `
|
|
SELECT b.*
|
|
FROM user_bill a
|
|
LEFT JOIN bill_expend b ON b.bill_id = a.bill_id
|
|
WHERE a.deleted_at = ?
|
|
`
|
|
sqlParams := []interface{}{utils.DefaultTimeValue}
|
|
if userID != "" {
|
|
sql += ` AND a.user_id = ?`
|
|
sqlParams = append(sqlParams, userID)
|
|
}
|
|
if billType != 0 {
|
|
sql += ` AND b.type = ?`
|
|
sqlParams = append(sqlParams, billType)
|
|
}
|
|
if fromTime != utils.ZeroTimeValue {
|
|
sql += ` AND b.created_at >= ?`
|
|
sqlParams = append(sqlParams, fromTime)
|
|
}
|
|
if toTime != utils.ZeroTimeValue {
|
|
sql += ` AND b.created_at <= ?`
|
|
sqlParams = append(sqlParams, toTime)
|
|
}
|
|
err = GetRows(db, &billExpends, sql, sqlParams)
|
|
return billExpends, err
|
|
}
|
|
|
|
func GetBillIncome(db *DaoDB, jobID int, billID int64) (billIncomes []*model.BillIncome, err error) {
|
|
sql := `
|
|
SELECT b.*
|
|
FROM user_bill a
|
|
LEFT JOIN bill_income b ON b.bill_id = a.bill_id
|
|
WHERE a.deleted_at = ?
|
|
`
|
|
sqlParams := []interface{}{utils.DefaultTimeValue}
|
|
if jobID != 0 {
|
|
sql += ` AND b.job_id = ?`
|
|
sqlParams = append(sqlParams, jobID)
|
|
}
|
|
if billID != 0 {
|
|
sql += ` AND b.bill_id = ?`
|
|
sqlParams = append(sqlParams, billID)
|
|
}
|
|
err = GetRows(db, &billIncomes, sql, sqlParams)
|
|
return billIncomes, 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)
|
|
txDB, _ := Begin(db)
|
|
defer Commit(db, txDB)
|
|
if err = GetRowsTx(txDB, &userBillDetails, sql, sqlParams...); err == nil {
|
|
pagedInfo = &model.PagedInfo{
|
|
TotalCount: GetLastTotalRowCountTx(txDB),
|
|
Data: userBillDetails,
|
|
}
|
|
}
|
|
return pagedInfo, err
|
|
}
|
|
|
|
// UpdateUserBill 修改用户余额
|
|
func UpdateUserBill(userId string, money int) error {
|
|
_, err := ExecuteSQL(GetDB(), `UPDATE user_bill SET account_balance = ? WHERE user_id = ? `, []interface{}{money, userId}...)
|
|
return err
|
|
}
|
|
|
|
type MixPayDetail struct {
|
|
CreatedAt time.Time `json:"created_at"`
|
|
LastOperator string `json:"lastOperator"`
|
|
OrderID string `json:"order_id"`
|
|
BalancePrice int `json:"balance_price"`
|
|
TotalPrice int `json:"total_price"`
|
|
WxPrice int `json:"wx_price"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
//获取混合支付 余额部分信息
|
|
func GetMixPayDetail(orderID string) (details *model.MixPay, err error) {
|
|
var info []*model.MixPay
|
|
sqlParams := []interface{}{}
|
|
sql := ""
|
|
if orderID != "" {
|
|
sql += "SELECT order_id,balance_price,total_price,wx_price,status FROM mix_pay WHERE order_id = ?"
|
|
sqlParams = append(sqlParams, orderID)
|
|
}
|
|
err = GetRow(GetDB(), &info, sql, sqlParams)
|
|
if err != nil {
|
|
return nil, err
|
|
//details = info{
|
|
// OrderID: info[0].OrderID,
|
|
// BalancePrice: info[0].BalancePrice,
|
|
// TotalPrice: info[0].TotalPrice,
|
|
// WxPrice: info[0].WxPrice,
|
|
// Status: info[0].Status,
|
|
//}
|
|
}
|
|
//globals.SugarLogger.Debug("输出mix pay信息", info.OrderID)
|
|
return info[0], err
|
|
}
|