@@ -0,0 +1,170 @@ | |||
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 | |||
} | |||
} | |||
} |
@@ -0,0 +1,302 @@ | |||
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 LIKE ? AND delete_at = 0", "%"+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 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 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{}) | |||
} | |||
func UserFindByLevel(eg *xorm.Engine, level int) []model.User { | |||
var data []model.User | |||
eg.Where("level=?", level).Find(&data) | |||
return data | |||
} |
@@ -0,0 +1,121 @@ | |||
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 | |||
} | |||
} | |||
} |
@@ -0,0 +1,117 @@ | |||
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 | |||
} | |||
} | |||
} |
@@ -0,0 +1,16 @@ | |||
package model | |||
import "time" | |||
type Group struct { | |||
Id int64 // 群组id | |||
Name string // 组名 | |||
AvatarUrl string // 头像 | |||
Introduction string // 群简介 | |||
UserNum int32 // 群组人数 | |||
IsAllMemberBanned int32 // 是否全员禁言(1:是 2:否) | |||
MasterId int64 // 站长id | |||
Extra string // 附加字段 | |||
CreateTime time.Time // 创建时间 | |||
UpdateTime time.Time // 更新时间 | |||
} |
@@ -0,0 +1,11 @@ | |||
package model | |||
type GroupUser struct { | |||
Id int64 `json:"id"` | |||
GroupId int64 `json:"group_id"` | |||
UserId int64 `json:"user_id"` | |||
MemberType int `json:"member_type"` | |||
Status int `json:"status"` | |||
CreateTime string `json:"create_time"` | |||
UpdateTime string `json:"update_time"` | |||
} |
@@ -0,0 +1,19 @@ | |||
package model | |||
import "time" | |||
type Message struct { | |||
Id int64 // 自增主键 | |||
UserId int64 // 所属类型id | |||
RequestId int64 // 请求id | |||
SenderType int32 // 发送者类型 | |||
SenderId int64 // 发送者账户id | |||
ReceiverType int32 // 接收者账户id | |||
ReceiverId int64 // 接收者id,如果是单聊信息,则为user_id,如果是群组消息,则为group_id | |||
ToUserIds string // 需要@的用户id列表,多个用户用,隔开 | |||
Type int // 消息类型 | |||
Content []byte // 消息内容 | |||
Seq int64 // 消息同步序列 | |||
SendTime time.Time // 消息发送时间 | |||
Status int32 // 创建时间 | |||
} |
@@ -0,0 +1,8 @@ | |||
package model | |||
type SysCfg struct { | |||
Key string // 键 | |||
Val string // 值 | |||
Memo string // 备注 | |||
MasterId int64 // 站长id | |||
} |
@@ -0,0 +1,12 @@ | |||
package model | |||
type User struct { | |||
Id int64 `json:"id"` | |||
PhoneNumber string `json:"phone_number"` | |||
Nickname string `json:"nickname"` | |||
AvatarUrl string `json:"avatar_url"` | |||
Sex int `json:"sex"` | |||
CreateTime string `json:"create_time"` | |||
UpdateTime string `json:"update_time"` | |||
IsAutoAddedFriends int `json:"is_auto_added_friends"` | |||
} |
@@ -0,0 +1,14 @@ | |||
package model | |||
import "time" | |||
type ImGroup struct { | |||
Id int `json:"id"` | |||
Kind int `json:"kind"` | |||
Uid int `json:"uid"` | |||
GroupId int `json:"group_id"` | |||
IsFull int `json:"is_full"` | |||
Name string `json:"name" ` | |||
CreateTime time.Time `json:"create_time"` | |||
UpdateTime time.Time `json:"update_time" ` | |||
} |
@@ -0,0 +1,28 @@ | |||
package model | |||
type User struct { | |||
Id int64 `json:"id" xorm:"pk autoincr BIGINT(20)"` | |||
Phone string `json:"phone" xorm:"not null default '' comment('手机号') VARCHAR(255)"` | |||
UnionId string `json:"union_id" xorm:"not null default '' comment('微信用户id') VARCHAR(255)"` | |||
OpenId string `json:"open_id" xorm:"not null default '' comment('微信openid') VARCHAR(255)"` | |||
Nickname string `json:"nickname" xorm:"not null default '' comment('昵称') VARCHAR(255)"` | |||
Avatar string `json:"avatar" xorm:"not null default '' comment('头像') VARCHAR(255)"` | |||
Password string `json:"password" xorm:"not null default '' comment('密码') CHAR(50)"` | |||
Passcode string `json:"passcode" xorm:"not null default '' comment('支付密码') CHAR(50)"` | |||
Level int `json:"level" xorm:"not null default 0 comment('用户等级id') INT(11)"` | |||
InviteTotal int `json:"invite_total" xorm:"not null default 0 comment('直推邀请总人数') INT(11)"` | |||
State int `json:"state" xorm:"not null default 1 comment('1正常,2冻结 3删除中 4进入回收站') TINYINT(1)"` | |||
LastLoginIp string `json:"last_login_ip" xorm:"not null default '' comment('最后登录IP') CHAR(50)"` | |||
Sex int `json:"sex" xorm:"not null default 0 comment('性别(0:未知 1:男 2:女)') TINYINT(1)"` | |||
ParentUid int64 `json:"parent_uid" xorm:"not null default 0 comment('父级id') BIGINT(20)"` | |||
SystemInviteCode string `json:"system_invite_code" xorm:"not null default '' comment('系统邀请码') CHAR(50)"` | |||
CustomInviteCode string `json:"custom_invite_code" xorm:"not null default '' comment('自定义邀请码') CHAR(50)"` | |||
Memo string `json:"memo" xorm:"not null default '' comment('备注信息') VARCHAR(244)"` | |||
IsRealName int `json:"is_real_name" xorm:"not null default 0 comment('是否实名(0:未实名 1.已实名)') TINYINT(1)"` | |||
RegisterType int `json:"register_type" xorm:"not null default 1 comment('注册类型(1:APP注册、2:H5注册)') TINYINT(1)"` | |||
LastLoginAt string `json:"last_login_at" xorm:"not null default CURRENT_TIMESTAMP comment('最近登录时间') DATETIME"` | |||
CreateAt string `json:"create_at" xorm:"not null default CURRENT_TIMESTAMP DATETIME"` | |||
UpdateAt string `json:"update_at" xorm:"not null default CURRENT_TIMESTAMP DATETIME"` | |||
RecycleParentUid int `json:"recycle_parent_uid" xorm:"default 0 comment('删除到回收站时候的上级') INT(11)"` | |||
WechatAccount string `json:"wechat_account" xorm:"comment('微信号') VARCHAR(255)"` | |||
} |
@@ -0,0 +1,15 @@ | |||
package md | |||
type UserRegisterMessageStructForOfficial struct { | |||
Phone int64 `json:"phone"` | |||
Uid int64 `json:"uid"` | |||
} | |||
type UserRegisterMessageStructForMyFans struct { | |||
Phone int64 `json:"phone"` | |||
Uid int64 `json:"uid"` | |||
} | |||
type UserRegisterMessageStructForMyRecommender struct { | |||
Phone int64 `json:"phone"` | |||
Uid int64 `json:"uid"` | |||
RecommenderUid int64 `json:"recommender_uid"` | |||
} |
@@ -0,0 +1,59 @@ | |||
package utils | |||
import ( | |||
"applet/pkg/pb" | |||
"context" | |||
"fmt" | |||
"google.golang.org/grpc" | |||
"google.golang.org/grpc/metadata" | |||
"strconv" | |||
"time" | |||
) | |||
func GetBusinessIntClient(url, port string) pb.BusinessIntClient { | |||
target := fmt.Sprintf("%s:%s", url, port) | |||
conn, err := grpc.Dial(target, grpc.WithInsecure()) | |||
if err != nil { | |||
fmt.Println(err) | |||
return nil | |||
} | |||
return pb.NewBusinessIntClient(conn) | |||
} | |||
func GetBusinessExtClient(url, port string) pb.BusinessExtClient { | |||
target := fmt.Sprintf("%s:%s", url, port) | |||
conn, err := grpc.Dial(target, grpc.WithInsecure()) | |||
//defer conn.Close() | |||
if err != nil { | |||
fmt.Println(err) | |||
return nil | |||
} | |||
return pb.NewBusinessExtClient(conn) | |||
} | |||
func GetLogicExtClient(url, port string) pb.LogicExtClient { | |||
target := fmt.Sprintf("%s:%s", url, port) | |||
conn, err := grpc.Dial(target, grpc.WithInsecure()) | |||
if err != nil { | |||
fmt.Println(err) | |||
return nil | |||
} | |||
return pb.NewLogicExtClient(conn) | |||
} | |||
func GetCtx(token, userId, deviceId string) context.Context { | |||
if userId == "" { | |||
userId = "1" | |||
} | |||
if deviceId == "" { | |||
deviceId = "1" | |||
} | |||
if token == "" { | |||
token = "0" | |||
} | |||
return metadata.NewOutgoingContext(context.TODO(), metadata.Pairs( | |||
"user_id", userId, | |||
"device_id", deviceId, | |||
"token", token, | |||
"request_id", strconv.FormatInt(time.Now().UnixNano(), 10))) | |||
} |
@@ -3,6 +3,7 @@ package consume | |||
import ( | |||
"applet/app/cfg" | |||
"applet/app/db" | |||
model2 "applet/app/db/model" | |||
utils2 "applet/app/utils" | |||
"applet/app/utils/logx" | |||
"applet/consume/md" | |||
@@ -85,7 +86,7 @@ func handleUserDeleteConsume(backEg *xorm.Engine, ch *rabbit.Channel, msgData [] | |||
} | |||
eg := db.Db | |||
//1.用户信息 | |||
var user model.User | |||
var user model2.User | |||
exist, err := eg.Where("id=?", msg.Uid).Get(&user) | |||
if exist { | |||
backEg.Insert(&user) | |||
@@ -146,7 +147,7 @@ func handleUserDeleteConsume(backEg *xorm.Engine, ch *rabbit.Channel, msgData [] | |||
} | |||
} | |||
} | |||
eg.Where("parent_uid=?", msg.Uid).Cols("parent_uid").Update(&model.User{ParentUid: utils2.StrToInt64(msg.ParentUid)}) | |||
eg.Where("parent_uid=?", msg.Uid).Cols("parent_uid").Update(&model2.User{ParentUid: utils2.StrToInt64(msg.ParentUid)}) | |||
_, err = eg.Where("parent_uid=?", msg.Uid).Delete(&model.UserRelate{}) | |||
_, err = eg.Where("uid=?", msg.Uid).Delete(&model.UserRelate{}) | |||
@@ -0,0 +1,185 @@ | |||
package consume | |||
import ( | |||
"applet/app/cfg" | |||
"applet/app/db" | |||
"applet/app/db/gim" | |||
imModel "applet/app/db/gim/model" | |||
"applet/app/db/model" | |||
md2 "applet/app/md" | |||
utils2 "applet/app/utils" | |||
"applet/app/utils/logx" | |||
utils "applet/app/utils/rpc" | |||
"applet/consume/md" | |||
"applet/pkg/pb" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" | |||
"encoding/json" | |||
"errors" | |||
"fmt" | |||
"github.com/streadway/amqp" | |||
"strconv" | |||
"time" | |||
) | |||
func UserRegisterConsumeForOfficial(queue md.MqQueue) { | |||
fmt.Println(">>>>>>>>>>>>UserRegisterConsumeForOfficial>>>>>>>>>>>>") | |||
ch, err := rabbit.Cfg.Pool.GetChannel() | |||
if err != nil { | |||
logx.Error(err) | |||
return | |||
} | |||
defer ch.Release() | |||
//1、将自己绑定到交换机上 | |||
ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) | |||
//2、取出数据进行消费 | |||
ch.Qos(1) | |||
delivery := ch.Consume(queue.Name, false) | |||
var res amqp.Delivery | |||
var ok bool | |||
for { | |||
res, ok = <-delivery | |||
if ok == true { | |||
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") | |||
err = handleUserRegisterConsumeForOfficial(res.Body) | |||
if err != nil { | |||
fmt.Println("!!!!!!!err!!!!!!!", err) | |||
_ = res.Reject(false) | |||
utils2.FilePutContents("UserRegisterConsumeForOfficial_err", "[err]:"+err.Error()) | |||
if err.Error() == "用户暂未注册im系统" { | |||
//TODO::重新推回队列末尾,避免造成队列堵塞 | |||
var msg *md2.UserRegisterMessageStructForOfficial | |||
json.Unmarshal(res.Body, &msg) | |||
ch.Publish(queue.ExchangeName, msg, queue.RoutKey) | |||
} | |||
} else { | |||
_ = res.Ack(true) | |||
} | |||
} else { | |||
panic(errors.New("error getting message")) | |||
} | |||
} | |||
fmt.Println("get msg done") | |||
} | |||
func handleUserRegisterConsumeForOfficial(msgData []byte) error { | |||
time.Sleep(time.Duration(5) * time.Second) | |||
//1、解析mq中queue的数据结构体 | |||
var msg *md2.UserRegisterMessageStructForOfficial | |||
err := json.Unmarshal(msgData, &msg) | |||
if err != nil { | |||
return err | |||
} | |||
//2、查找用户对应im系统中的数据 | |||
gimUser, err := gim.UserGetOneByParams(db.DbIm, map[string]interface{}{ | |||
"key": "phone_number", | |||
"value": msg.Phone, | |||
}) | |||
if err != nil { | |||
return err | |||
} | |||
if gimUser == nil { | |||
fmt.Println("===========================", msg.Phone) | |||
user, err := db.UserFindByMobile(db.Db, strconv.FormatInt(msg.Phone, 10)) | |||
if err != nil { | |||
return err | |||
} | |||
if user == nil { | |||
return nil | |||
} | |||
gimUserId, err := gim.UserInsert(db.DbIm, &imModel.User{ | |||
PhoneNumber: strconv.FormatInt(msg.Phone, 10), | |||
Nickname: user.Nickname, | |||
AvatarUrl: user.Avatar, | |||
Sex: user.Sex, | |||
CreateTime: time.Now().Format("2006-01-02 15:04:05"), | |||
UpdateTime: time.Now().Format("2006-01-02 15:04:05"), | |||
IsAutoAddedFriends: 0, | |||
}) | |||
if err != nil { | |||
return errors.New("用户暂未注册im系统") | |||
} | |||
gimUser, err = gim.UserGetOneByParams(db.DbIm, map[string]interface{}{ | |||
"key": "id", | |||
"value": gimUserId, | |||
}) | |||
if gimUser == nil { | |||
return errors.New("用户暂未注册im系统") | |||
} | |||
} | |||
//2、查找是否有群 | |||
var officialGroup model.ImGroup | |||
officialGroups, err := db.ImGroupFindByParams(db.Db, map[string]interface{}{ | |||
"key": "kind", | |||
"value": 1, | |||
}) | |||
if err != nil { | |||
return err | |||
} | |||
for _, group := range *officialGroups { | |||
if officialGroup.GroupId != 0 { | |||
continue | |||
} | |||
//统计当前群有多少人 | |||
userGroups, _ := gim.GroupUserFindByParams(db.DbIm, map[string]interface{}{ | |||
"key": "group_id", | |||
"value": group.GroupId, | |||
}) | |||
if len(*userGroups) < 500 { | |||
officialGroup = group | |||
} | |||
} | |||
if officialGroup.GroupId == 0 { | |||
groupName := "官方【" + utils2.AnyToString(len(*officialGroups)+1) + "】群" | |||
//3、创建群 | |||
resp, err := utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).CreateGroup(utils.GetCtx("", strconv.FormatInt(gimUser.Id, 10), "0"), &pb.CreateGroupReq{ | |||
Name: groupName, | |||
AvatarUrl: "", | |||
//Introduction: "官方群", | |||
Introduction: "", | |||
Extra: "", | |||
MemberIds: []int64{}, | |||
}) | |||
if err != nil { | |||
return err | |||
} | |||
//插入 dou_shen_im_group 记录 | |||
now := time.Now() | |||
_, err = db.ImGroupInsert(db.Db, &model.ImGroup{ | |||
Kind: 1, | |||
Uid: int(msg.Uid), | |||
GroupId: int(resp.GroupId), | |||
IsFull: 0, | |||
Name: groupName, | |||
CreateTime: now, | |||
UpdateTime: now, | |||
}) | |||
if err != nil { | |||
return err | |||
} | |||
} else { | |||
userGroup, err2 := gim.GroupUserGetOneByParams(db.DbIm, 1, map[string]interface{}{ | |||
"key": "group_id", | |||
"value": officialGroup.GroupId, | |||
}) | |||
if err2 != nil { | |||
return err2 | |||
} | |||
if userGroup == nil { | |||
return errors.New("当前官方群暂未设置群主,请联系管理员!!!") | |||
} | |||
//加入群 | |||
_, err = utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).AddGroupMembers( | |||
utils.GetCtx("", strconv.FormatInt(userGroup.UserId, 10), ""), | |||
&pb.AddGroupMembersReq{ | |||
GroupId: int64(officialGroup.GroupId), | |||
UserIds: []int64{gimUser.Id}, | |||
}) | |||
} | |||
return nil | |||
} |