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 }