DriveLinked/model/user.go

40 lines
909 B
Go
Raw Normal View History

2022-04-03 12:30:50 +08:00
package model
import (
"drive-linked/pkg/dao"
"fmt"
"github.com/jmoiron/sqlx"
"strings"
2022-04-03 12:30:50 +08:00
)
type User struct {
Id int64 `json:"id,string"`
Name string `json:"name"`
Nickname string `json:"nickname"`
Email string `json:"email"`
Password string `json:"-"`
Status int32 `json:"status"`
Avatar string `json:"avatar"`
Roles string `json:"roles"`
}
func (user *User) GetWithConditions(conditions *map[string]interface{}) error {
// 支持多条件查询
//TODO:分离多条件查询部分,有利于代码复用
var where []string
var values []interface{}
for k, v := range *conditions {
values = append(values, v)
where = append(where, fmt.Sprintf(`"%s" = %s`, k, "?"))
2022-04-03 12:30:50 +08:00
}
exec := sqlx.Rebind(sqlx.QUESTION, `SELECT * FROM users WHERE `+strings.Join(where, "AND")+" LIMIT 1")
err := dao.DB.Get(user, exec, values...)
2022-04-03 12:30:50 +08:00
if err != nil {
return err
}
2022-04-03 12:30:50 +08:00
return nil
}