This commit is contained in:
苏尹岚
2021-01-04 15:14:02 +08:00
parent 18b17e846b
commit 742ee01346

View File

@@ -191,32 +191,33 @@ type GetPayStatisticsResult struct {
} }
func getWhereSql(sqlP []interface{}, fromTime, toTime time.Time, orderTypes []int) (sql string) { func getWhereSql(sqlP []interface{}, fromTime, toTime time.Time, orderTypes []int) (sql string) {
if len(orderTypes) > 0 {
sql += ` AND b.order_type IN ` + GenQuestionMarks(len(orderTypes)) + `)`
sqlP = append(sqlP, orderTypes)
}
if fromTime != utils.ZeroTimeValue {
sql += ` AND b.created_at > ?`
sqlP = append(sqlP, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND b.created_at < ?`
sqlP = append(sqlP, toTime)
}
return sql return sql
} }
func getFromSql(sqlP []interface{}, orderType, status int, alies string, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (sql string) { func getFromSql(orderType, status int, alies string, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (sql string, sqlParams []interface{}) {
sql += ` sql += `
(SELECT SUM(IFNULL(b.pay_price,0)) total_pay (SELECT SUM(IFNULL(b.pay_price,0)) total_pay
FROM user a FROM user a
JOIN ` + "`order` b " + `ON b.user_id = a.user_id AND b.type = ? AND b.status = ? ` + getWhereSql(sqlP, fromTime, toTime, orderTypes) + ` JOIN ` + "`order` b " + `ON b.user_id = a.user_id AND b.type = ? AND b.status = ?
` `
sqlP = append(sqlP, orderType, status) sqlParams = append(sqlParams, orderType, status)
if len(orderTypes) > 0 {
sql += ` AND b.order_type IN ` + GenQuestionMarks(len(orderTypes)) + `)`
sqlParams = append(sqlParams, orderTypes)
}
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)
}
if mobile != "" { if mobile != "" {
if pop == 1 { if pop == 1 {
sql += " JOIN user e ON e.moblie = ? AND a.pop_user = e.user_id" sql += " JOIN user e ON e.moblie = ? AND a.pop_user = e.user_id"
sqlP = append(sqlP, mobile) sqlParams = append(sqlParams, mobile)
} }
} }
sql += ` sql += `
@@ -225,33 +226,39 @@ func getFromSql(sqlP []interface{}, orderType, status int, alies string, userID
if userID != "" { if userID != "" {
if pop == 1 { if pop == 1 {
sql += " AND a.pop_user = ?" sql += " AND a.pop_user = ?"
sqlP = append(sqlP, userID) sqlParams = append(sqlParams, userID)
} else { } else {
sql += " AND a.user_id = ?" sql += " AND a.user_id = ?"
sqlP = append(sqlP, userID) sqlParams = append(sqlParams, userID)
} }
} }
if mobile != "" { if mobile != "" {
if pop == 0 { if pop == 0 {
sql += " AND a.mobile = ?" sql += " AND a.mobile = ?"
sqlP = append(sqlP, mobile) sqlParams = append(sqlParams, mobile)
} }
} }
if len(cityCodes) > 0 { if len(cityCodes) > 0 {
sql += ` AND a.city_code IN ` + GenQuestionMarks(len(cityCodes)) + `)` sql += ` AND a.city_code IN ` + GenQuestionMarks(len(cityCodes)) + `)`
sqlP = append(sqlP, cityCodes) sqlParams = append(sqlParams, cityCodes)
} }
sql += `) ` + alies sql += `) ` + alies
return sql return sql, sqlParams
} }
func GetPayStatistics(db *DaoDB, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (getPayStatisticsResult *GetPayStatisticsResult, err error) { func GetPayStatistics(db *DaoDB, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (getPayStatisticsResult *GetPayStatisticsResult, err error) {
var sqlParams []interface{} sqlParams := []interface{}{}
sql := `SELECT t1.total_pay, t2.total_pay submit_cash, t3.total_pay cashed, t4.account_balance, t4.account_balance + t2.total_pay can_cash, t3.total_pay / 10 cash_income sql := `SELECT t1.total_pay, t2.total_pay submit_cash, t3.total_pay cashed, t4.account_balance, t4.account_balance + t2.total_pay can_cash, t3.total_pay / 10 cash_income
FROM ` FROM `
sql += getFromSql(sqlParams, model.OrderTypePay, model.OrderStatusFinished, "t1", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes) + "," rSQL1, rSQLParams1 := getFromSql(model.OrderTypePay, model.OrderStatusFinished, "t1", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes)
sql += getFromSql(sqlParams, model.OrderTypeCash, model.OrderStatusWait4Pay, "t2", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes) + "," sql += rSQL1 + ","
sql += getFromSql(sqlParams, model.OrderTypeCash, model.OrderStatusFinished, "t3", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes) + "," sqlParams = append(sqlParams, rSQLParams1...)
rSQL2, rSQLParams2 := getFromSql(model.OrderTypeCash, model.OrderStatusWait4Pay, "t2", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes)
sql += rSQL2 + ","
sqlParams = append(sqlParams, rSQLParams2...)
rSQL3, rSQLParams3 := getFromSql(model.OrderTypeCash, model.OrderStatusFinished, "t3", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes)
sql += rSQL3 + ","
sqlParams = append(sqlParams, rSQLParams3...)
sql += `(SELECT SUM(IFNULL(b.account_balance,0)) account_balacne sql += `(SELECT SUM(IFNULL(b.account_balance,0)) account_balacne
FROM user a FROM user a
JOIN user_bill b ON a.user_id = b.user_id JOIN user_bill b ON a.user_id = b.user_id