You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB

package mysql
import (
"database/sql"
"main/models"
"main/utils"
)
func GetAllStudentInfo() (back *[]models.User, err error) {
sqlStr := `select
user_id, user_name, user_gender, user_score
from user
ORDER BY user_score ASC`
back = new([]models.User)
if err = db.Select(back, sqlStr); err != nil {
if err == sql.ErrNoRows {
err = utils.ErrorStuInfoEmpty
return
}
return
}
return
}
func GetOneRandomStudentInfo() (back *models.User, err error) {
sqlStr := `select
user_id, user_name, user_gender, user_score
from user
ORDER BY RAND() LIMIT 1`
back = new(models.User)
if err = db.Get(back, sqlStr); err != nil {
if err == sql.ErrNoRows {
err = utils.ErrorStuInfoEmpty
return
}
return
}
return
}
func GetTargetStudentInfo(user_id int64) (back *models.User, err error) {
sqlStr := `select
user_id, user_name, user_gender, user_score
from user
where user_id = ?`
back = new(models.User)
if err = db.Get(back, sqlStr, user_id); err != nil {
if err == sql.ErrNoRows {
err = utils.ErrorStuInfoEmpty
return
}
return
}
return
}
func ChangeStuScore(user *models.UserScore) (err error) {
sqlStr := `update user set
user_score = ?
where user_id=?`
r, err := db.Exec(sqlStr, user.User_score, user.User_id)
if n, err := r.RowsAffected(); err != nil || n == 0 {
return utils.ErrorInvalidStuId
}
return
}