package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type UserDb struct { Db *xorm.Engine `json:"db"` CompanyId int `json:"company_id"` } func (userDb *UserDb) Set() { // set方法 userDb.Db = Db } func (userDb *UserDb) GetUser(id int) (m *model.User, err error) { m = new(model.User) has, err := userDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (userDb *UserDb) GetUserByName(userName string) (m *model.User, err error) { m = new(model.User) has, err := userDb.Db.Where("name =?", userName).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (userDb *UserDb) GetUserByPhone(phone string) (m *model.User, err error) { m = new(model.User) has, err := userDb.Db.Where("phone =?", phone).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (userDb *UserDb) FindUser(limit, start int) (*[]model.User, error) { var m []model.User if limit == 0 || start == 0 { if err := userDb.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := userDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (userDb *UserDb) UserInsert(m *model.User) (int, error) { _, err := userDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (userDb *UserDb) UpdateUser(m *model.User, columns ...string) (int64, error) { affected, err := userDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (userDb *UserDb) DeleteUser(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return userDb.Db.In("enterprise_id", id).Delete(model.User{}) } else { return userDb.Db.Where("enterprise_id = ?", id).Delete(model.User{}) } } type UserWithUserIdentity struct { model.User `xorm:"extends"` model.UserIdentity `xorm:"extends"` } func (UserWithUserIdentity) TableName() string { return "user" }