|
- package gim
-
- import (
- "applet/app/db/gim/model"
- "applet/app/utils"
- "applet/app/utils/logx"
- "errors"
- "fmt"
- "reflect"
- "xorm.io/xorm"
- )
-
- // BatchSelectUsers 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `UserFindByParams` 方法
- func BatchSelectUsers(Db *xorm.Engine, params map[string]interface{}) (*[]model.User, error) {
- var UserData []model.User
- if err := Db.In(utils.AnyToString(params["key"]), params["value"]).
- Find(&UserData); err != nil {
- return nil, logx.Warn(err)
- }
- return &UserData, nil
- }
-
- // UserInsert 插入单条数据
- func UserInsert(Db *xorm.Engine, User *model.User) (int64, error) {
- _, err := Db.InsertOne(User)
- if err != nil {
- return 0, err
- }
- return User.Id, nil
- }
-
- // BatchAddUsers 批量新增数据
- func BatchAddUsers(Db *xorm.Engine, UserData []*model.User) (int64, error) {
- affected, err := Db.Insert(UserData)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func GetUserCount(Db *xorm.Engine) int {
- var User model.User
- session := Db.Where("")
- count, err := session.Count(&User)
- if err != nil {
- return 0
- }
- return int(count)
- }
-
- // UserDelete 删除记录
- func UserDelete(Db *xorm.Engine, id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.User{})
- } else {
- return Db.Where("id = ?", id).Delete(model.User{})
- }
- }
-
- // UserUpdate 更新记录
- func UserUpdate(Db *xorm.Engine, id interface{}, User *model.User, forceColums ...string) (int64, error) {
- var (
- affected int64
- err error
- )
- if forceColums != nil {
- affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(User)
- } else {
- affected, err = Db.Where("id=?", id).Update(User)
- }
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- // UserGetOneByParams 通过传入的参数查询数据(单条)
- func UserGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.User, error) {
- var m model.User
- var query = fmt.Sprintf("%s =?", params["key"])
- if has, err := Db.Where(query, params["value"]).Get(&m); err != nil || has == false {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- // UserFindByParams 通过传入的参数查询数据(多条)
- func UserFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.User, error) {
- var m []model.User
- if params["value"] == nil {
- return nil, errors.New("参数有误")
- }
- if params["key"] == nil {
- //查询全部数据
- err := Db.Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- } else {
- if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
- //指定In查询
- if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- } else {
- var query = fmt.Sprintf("%s =?", params["key"])
- err := Db.Where(query, params["value"]).Find(&m)
- if err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- }
- }
|