package gim import ( "applet/app/db/gim/model" "applet/app/utils" "applet/app/utils/logx" "errors" "fmt" "reflect" "xorm.io/xorm" ) // BatchSelectGroupUsers 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `GroupUserFindByParams` 方法 func BatchSelectGroupUsers(Db *xorm.Engine, params map[string]interface{}) (*[]model.GroupUser, error) { var GroupUserData []model.GroupUser if err := Db.In(utils.AnyToString(params["key"]), params["value"]). Find(&GroupUserData); err != nil { return nil, logx.Warn(err) } return &GroupUserData, nil } // GroupUserInsert 插入单条数据 func GroupUserInsert(Db *xorm.Engine, GroupUser *model.GroupUser) (int64, error) { _, err := Db.InsertOne(GroupUser) if err != nil { return 0, err } return GroupUser.Id, nil } // BatchAddGroupUsers 批量新增数据 func BatchAddGroupUsers(Db *xorm.Engine, GroupUserData []*model.GroupUser) (int64, error) { affected, err := Db.Insert(GroupUserData) if err != nil { return 0, err } return affected, nil } func GetGroupUserCount(Db *xorm.Engine) int { var GroupUser model.GroupUser session := Db.Where("") count, err := session.Count(&GroupUser) if err != nil { return 0 } return int(count) } // GroupUserDelete 删除记录 func GroupUserDelete(Db *xorm.Engine, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return Db.In("id", id).Delete(model.GroupUser{}) } else { return Db.Where("id = ?", id).Delete(model.GroupUser{}) } } // GroupUserUpdate 更新记录 func GroupUserUpdate(Db *xorm.Engine, id interface{}, GroupUser *model.GroupUser, forceColums ...string) (int64, error) { var ( affected int64 err error ) if forceColums != nil { affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(GroupUser) } else { affected, err = Db.Where("id=?", id).Update(GroupUser) } if err != nil { return 0, err } return affected, nil } // GroupUserGetOneByParams 通过传入的参数查询数据(单条) func GroupUserGetOneByParams(Db *xorm.Engine, memberType int, params map[string]interface{}) (*model.GroupUser, error) { var m model.GroupUser var query = fmt.Sprintf("%s =?", params["key"]) has, err := Db.Where(query, params["value"]).And("member_type = ?", memberType).Get(&m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return &m, nil } // GroupUserFindByParams 通过传入的参数查询数据(多条) func GroupUserFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.GroupUser, error) { var m []model.GroupUser 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 } } }