|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "fmt"
- "xorm.io/xorm"
- )
-
- // UserisExistByUsernameAndPassword is usernameAndPassword exist
- func UserisExistByUsernameAndPassword(Db *xorm.Engine, username, password, zone string) (bool, error) {
- sess := Db.Where("(username = ? or phone=?) AND password = ?", username, username, password)
- if zone != "" && zone != "86" {
- sess = sess.And("zone=?", zone)
- }
- has, err := sess.Exist(&model.User{})
-
- if err != nil {
- return false, err
- }
- return has, nil
- }
-
- // UserisExistByMobile is exist
- func UserisExistByMobile(Db *xorm.Engine, n string) (bool, error) {
- has, err := Db.Where("phone = ? and phone<>''", n).Exist(&model.User{})
-
- if err != nil {
- return false, err
- }
- return has, nil
- }
-
- // UserInByUIDByLevel is In查询 以及是否是有效用户
- func UserInByUIDByLevel(Db *xorm.Engine, ids []int, levelID interface{}) (*[]model.User, error) {
- var m []model.User
- if err := Db.In("uid", ids).Where("level = ?", levelID).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UserFindByMobile search user by mobile
- func UserFindByMobile(Db *xorm.Engine, mobile string) (*model.User, error) {
- var m model.User
- if has, err := Db.Where("(phone = ? OR uid = ?) AND delete_at = 0", mobile, mobile).
- Get(&m); err != nil || has == false {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UserFindExistByMobile search user by mobile
- func UserFindExistByMobile(Db *xorm.Engine, mobile string) (*model.User, bool, error) {
- var m model.User
- has, err := Db.Where("(phone = ? OR uid = ?) AND delete_at = 0", mobile, mobile).Get(&m)
- if err != nil {
- logx.Infof("UserFindExistByMobile err")
- return nil, false, logx.Warn(err)
- }
- return &m, has, nil
- }
-
- // UserFindByMobile search user by mobile
- func UserFindByMobileAll(Db *xorm.Engine, mobile string) (*model.User, error) {
- var m model.User
- if has, err := Db.Where("(phone = ? OR uid = ?)", mobile, mobile).
- Get(&m); err != nil || has == false {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UserGetByMobileIgnoreDelete search user by mobile ignore delete
- func UserGetByMobileIgnoreDelete(Db *xorm.Engine, mobile, zone string) (*model.User, bool, error) {
- m := new(model.User)
- sess := Db.Where("phone = ?", mobile)
- if zone != "" && zone != "86" {
- sess = sess.And("zone=?", zone)
- }
- has, err := sess.Get(m)
- if err != nil {
- return nil, false, logx.Warn(err)
- }
- return m, has, nil
- }
-
- // UsersFindByMobileLike search users by mobile
- func UsersFindByMobileLike(Db *xorm.Engine, mobile string) (*[]model.User, error) {
- var m []model.User
- if err := Db.Where("phone like ?", "%"+mobile+"%").
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UsersFindByNickNameLike search users by nickname
- func UsersFindByNickNameLike(Db *xorm.Engine, nickname string) (*[]model.User, error) {
- var m []model.User
- if err := Db.Where("nickname like ?", "%"+nickname+"%").
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- //UsersInByIds is 根据ids 查找users
- func UsersInByIds(Db *xorm.Engine, ids []int, limit, start int) (*[]model.User, error) {
- var m []model.User
- if limit == 0 && start == 0 {
- if err := Db.In("uid", ids).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- if err := Db.In("uid", ids).Limit(limit, start).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- //UsersInByIdsWhereLv is 根据ids和 lv会员等级 查找users
- func UsersInByIdsWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int) (*[]model.User, error) {
- var m []model.User
- if limit == 0 && start == 0 {
- if err := Db.Where("level = ?", lv).In("uid", ids).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- if err := Db.Where("level = ?", lv).In("uid", ids).Limit(limit, start).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- //UsersInByIdsByAscWhereLv is 根据ids和 lv会员等级 查找users 升排序
- func UsersInByIdsByAscWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
- var m []model.User
- if limit == 0 && start == 0 {
- if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).Limit(limit, start).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- //UsersInByIdsByDescWhereLv is 根据ids和 lv会员等级 查找users 降排序
- func UsersInByIdsByDescWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
- var m []model.User
- if limit == 0 && start == 0 {
- if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).Limit(limit, start).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UserFindByArkidUserName search user by mobile
- func UserFindByArkidUserName(Db *xorm.Engine, name string) (*model.User, error) {
- var m model.User
- if has, err := Db.Where("username = ?", name).
- Get(&m); err != nil || has == false {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UserFindByID is find user byid
- func UserFindByID(Db *xorm.Engine, id interface{}) (*model.User, error) {
- var m model.User
- if _, err := Db.Where("uid = ?", id).
- Get(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- func UserFindByIDWithSession(sess *xorm.Session, id interface{}) (*model.User, error) {
- var m model.User
- if has, err := sess.Where("uid = ?", id).
- Get(&m); err != nil || has == false {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- func UserFindByIDs(Db *xorm.Engine, uids []int) (*[]model.User, error) {
- var m []model.User
- if err := Db.In("uid", uids).Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UsersInByIdsByDesc is 根据某列 降序
- func UsersInByIdsByDesc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
- var m []model.User
- if limit == 0 && start == 0 {
- if err := Db.In("uid", ids).Desc(c).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- if err := Db.In("uid", ids).Desc(c).Limit(limit, start).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- // UsersInByIdsByAsc is 根据某列 升序
- func UsersInByIdsByAsc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
- var m []model.User
- if limit == 0 && start == 0 {
- if err := Db.In("uid", ids).Asc(c).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- if err := Db.In("uid", ids).Asc(c).Limit(limit, start).
- Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- //UserInsert is insert user
- func UserInsert(Db *xorm.Engine, user *model.User) (int64, error) {
- affected, err := Db.Insert(user)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- // UserIsExistByMobile is mobile exist
- func UserIsExistByMobile(Db *xorm.Engine, mobile string) (bool, error) {
- //fmt.Println(mobile)
- has, err := Db.Where("phone = ? OR uid = ?", mobile, mobile).Exist(&model.User{})
- fmt.Println(has, mobile)
- if err != nil {
- return false, err
- }
- return has, nil
- }
-
- // UserIsExistByID is mobile exist by id
- func UserIsExistByID(Db *xorm.Engine, id string) (bool, error) {
- has, err := Db.Where("uid = ?", id).Exist(&model.User{})
- if err != nil {
- return false, err
- }
- return has, nil
- }
-
- // UserUpdate is update user
- func UserUpdate(Db *xorm.Engine, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
- var (
- affected int64
- err error
- )
- if forceColums != nil {
- affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
- } else {
- affected, err = Db.Where("uid=?", uid).Update(user)
- }
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func UserUpdateWithSession(Db *xorm.Session, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
- var (
- affected int64
- err error
- )
- if forceColums != nil {
- affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
- } else {
- affected, err = Db.Where("uid=?", uid).Update(user)
- }
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func UpdateUserFinValid() {
-
- }
-
- // UserDelete is delete user
- func UserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
- return Db.Where("uid = ?", uid).Delete(model.User{})
- }
-
- func UserDeleteWithSess(sess *xorm.Session, uid interface{}) (int64, error) {
- return sess.Where("uid = ?", uid).Delete(model.User{})
- }
|