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