mysql model initial commit

pull/4/head
away 4 years ago
parent 0d54547dd3
commit 772ecc7ab5

@ -0,0 +1,70 @@
package im_mysql_model
import (
"Open_IM/src/common/db"
_ "github.com/jinzhu/gorm/dialects/mysql"
"time"
)
func InsertToFriend(ownerId, friendId string, flag int32) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
toInsertFollow := Friend{
OwnerId: ownerId,
FriendId: friendId,
FriendFlag: flag,
CreateTime: time.Now(),
}
err = dbConn.Table("friend").Create(toInsertFollow).Error
if err != nil {
return err
}
return nil
}
func FindFriendRelationshipFromFriend(ownerId, friendId string) (*Friend, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var friend Friend
err = dbConn.Table("friend").Where("owner_id=? and friend_id=?", ownerId, friendId).Find(&friend).Error
if err != nil {
return nil, err
}
return &friend, err
}
func FindUserInfoFromFriend(ownerId string) ([]Friend, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
//dbConn.LogMode(true)
var friends []Friend
err = dbConn.Table("friend").Where("owner_id=?", ownerId).Find(&friends).Error
if err != nil {
return nil, err
}
return friends, nil
}
func UpdateFriendComment(ownerId, friendId, comment string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("update friend set comment=? where owner_id=? and friend_id=?", comment, ownerId, friendId).Error
return err
}
func DeleteSingleFriendInfo(ownerId, friendId string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Table("friend").Where("owner_id=? and friend_id=?", ownerId, friendId).Delete(Friend{}).Error
return err
}

@ -0,0 +1,57 @@
package im_mysql_model
import (
"Open_IM/src/common/db"
"time"
)
func InsertIntoFriendReq(reqId, userId string, flag int32, reqMessage string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("insert into friend_request(req_id,user_id,flag,req_message,create_time) values(?,?,?,?,?)", reqId, userId, flag, reqMessage, time.Now()).Error
if err != nil {
return err
}
return nil
}
func FindFriendsApplyFromFriendReq(userId string) ([]FriendRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var usersInfo []FriendRequest
//dbConn.LogMode(true)
err = dbConn.Table("friend_request").Where("user_id=?", userId).Find(&usersInfo).Error
if err != nil {
return nil, err
}
return usersInfo, nil
}
func FindFriendRelationshipFromFriendReq(reqId, userId string) (*FriendRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var friendRequest FriendRequest
err = dbConn.Table("friend_request").Where("req_id=? and user_id=?", reqId, userId).Find(&friendRequest).Error
if err != nil {
return nil, err
}
return &friendRequest, nil
}
func UpdateFriendRelationshipToFriendReq(reqId, userId string, flag int32) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("update friend_request set flag=? where req_id=? and user_id=?", flag, reqId, userId).Error
if err != nil {
return err
}
return nil
}

@ -0,0 +1,98 @@
package im_mysql_model
import "Open_IM/src/common/db"
func InsertIntoGroupMember(groupId, userId string, isAdmin int64) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("insert into `group_member`(group_id,user_id,is_admin) values(?,?,?)", groupId, userId, isAdmin).Error
if err != nil {
return err
}
return nil
}
func FindGroupMemberListByUserId(userId string) ([]GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMemberList []GroupMember
err = dbConn.Raw("select * from `group_member` where user_id=?", userId).Find(&groupMemberList).Error
if err != nil {
return nil, err
}
return groupMemberList, nil
}
func FindGroupMemberListByGroupId(groupId string) ([]GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMemberList []GroupMember
err = dbConn.Raw("select * from `group_member` where group_id=?", groupId).Find(&groupMemberList).Error
if err != nil {
return nil, err
}
return groupMemberList, nil
}
func FindGroupMemberInfoByGroupIdAndUserId(groupId, userId string) (*GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMember GroupMember
err = dbConn.Raw("select * from `group_member` where group_id=? and user_id=? limit 1", groupId, userId).Scan(&groupMember).Error
if err != nil {
return nil, err
}
return &groupMember, nil
}
func DeleteGroupMemberByGroupIdAndUserId(groupId, userId string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("delete from `group_member` where group_id=? and user_id=?", groupId, userId).Error
if err != nil {
return err
}
return nil
}
func UpdateOwnerGroupNickName(groupId, userId, groupNickName string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("update `group_member` set nickname=? where group_id=? and user_id=?", groupNickName, groupId, userId).Error
if err != nil {
return err
}
return nil
}
func SelectGroupList(groupID string) ([]string, error) {
var groupUserID string
var groupList []string
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return groupList, err
}
rows, err := dbConn.Model(&GroupMember{}).Where("group_id = ?", groupID).Select("user_id").Rows()
if err != nil {
return groupList, err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&groupUserID)
groupList = append(groupList, groupUserID)
}
return groupList, nil
}

@ -0,0 +1,67 @@
package im_mysql_model
import "Open_IM/src/common/db"
func InsertIntoGroup(groupId, name, groupHeadUrl string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
//Default group name
if name == "" {
name = "groupChat"
}
err = dbConn.Exec("insert into `group`(group_id,name,head_url) values(?,?,?)", groupId, name, groupHeadUrl).Error
if err != nil {
return err
}
return nil
}
func FindGroupInfoByGroupId(groupId string) (*Group, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupInfo Group
err = dbConn.Raw("select * from `group` where group_id=?", groupId).Scan(&groupInfo).Error
if err != nil {
return nil, err
}
return &groupInfo, nil
}
func UpdateGroupName(groupId, groupName string) (err error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("update `group` set name=? where group_id=?", groupName, groupId).Error
if err != nil {
return err
}
return nil
}
func UpdateGroupBulletin(groupId, bulletinContent string) (err error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("update `group` set bulletin=? where group_id=?", bulletinContent, groupId).Error
if err != nil {
return err
}
return nil
}
func UpdateGroupHeadImage(groupId, headImageUrl string) (err error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("update `group` set head_url=? where group_id=?", headImageUrl, groupId).Error
if err != nil {
return err
}
return nil
}

@ -0,0 +1,48 @@
package im_mysql_model
import "time"
type User struct {
UID string `gorm:"column:uid"`
Name string `gorm:"column:name"`
Icon string `gorm:"column:icon"`
Gender int32 `gorm:"column:gender"`
Mobile string `gorm:"column:mobile"`
Birth string `gorm:"column:birth"`
Email string `gorm:"column:email"`
Ex string `gorm:"column:ex"`
CreateTime time.Time `gorm:"column:create_time"`
}
type Friend struct {
OwnerId string `gorm:"column:owner_id"`
FriendId string `gorm:"column:friend_id"`
Comment string `gorm:"column:comment"`
FriendFlag int32 `gorm:"column:friend_flag"`
CreateTime time.Time `gorm:"column:create_time"`
}
type FriendRequest struct {
ReqId string `gorm:"column:req_id"`
UserId string `gorm:"column:user_id"`
Flag int32 `gorm:"column:flag"`
ReqMessage string `gorm:"column:req_message"`
CreateTime time.Time `gorm:"column:create_time"`
}
type BlackList struct {
OwnerId string `gorm:"column:owner_id"`
BlockId string `gorm:"column:block_id"`
CreateTime time.Time `gorm:"column:create_time"`
}
type Group struct {
GroupId string `gorm:"column:group_id"`
Name string `gorm:"column:name"`
HeadURL string `gorm:"column:head_url"`
Bulletin string `gorm:"column:bulletin"`
}
type GroupMember struct {
GroupId string `gorm:"column:group_id"`
UserId string `gorm:"column:user_id"`
NickName string `gorm:"column:nickname"`
IsAdmin int32 `gorm:"column:is_admin"`
}

@ -0,0 +1,48 @@
package im_mysql_model
import (
"Open_IM/src/common/db"
"time"
)
func InsertInToUserBlackList(ownerID, blockID string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
toInsertInfo := BlackList{OwnerId: ownerID, BlockId: blockID, CreateTime: time.Now()}
err = dbConn.Table("user_black_list").Create(toInsertInfo).Error
return err
}
func FindRelationshipFromBlackList(ownerID, blockID string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
var blackList BlackList
err = dbConn.Table("user_black_list").Where("owner_id=? and block_id=?", ownerID, blockID).Find(&blackList).Error
return err
}
func RemoveBlackList(ownerID, blockID string) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
err = dbConn.Exec("delete from user_black_list where owner_id=? and block_id=?", ownerID, blockID).Error
return err
}
func GetBlackListByUID(ownerID string) ([]BlackList, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var blackListUsersInfo []BlackList
err = dbConn.Table("user_black_list").Where("owner_id=?", ownerID).Find(&blackListUsersInfo).Error
if err != nil {
return nil, err
}
return blackListUsersInfo, nil
}

@ -0,0 +1,104 @@
package im_mysql_model
import (
"Open_IM/src/common/db"
pbAuth "Open_IM/src/proto/auth"
_ "github.com/jinzhu/gorm/dialects/mysql"
"time"
)
func UserRegister(pb *pbAuth.UserRegisterReq) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
addUser := User{
UID: pb.UID,
Name: pb.Name,
Icon: pb.Icon,
Gender: pb.Gender,
Mobile: pb.Mobile,
Birth: pb.Birth,
Email: pb.Email,
Ex: pb.Ex,
CreateTime: time.Now(),
}
err = dbConn.Table("user").Create(&addUser).Error
if err != nil {
return err
}
return nil
}
func FindUserByUID(uid string) (*User, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var user User
err = dbConn.Table("user").Where("uid=?", uid).First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func UpDateUserInfo(uid, name, headUrl, mobilePhoneNum, birth, email, extendInfo string, gender int32) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
}
if name != "" {
if err = dbConn.Exec("update user set name=? where uid=?", name, uid).Error; err != nil {
return err
}
}
if headUrl != "" {
if err = dbConn.Exec("update user set icon=? where uid=?", headUrl, uid).Error; err != nil {
return err
}
}
if mobilePhoneNum != "" {
if err = dbConn.Exec("update user set mobile=? where uid=?", mobilePhoneNum, uid).Error; err != nil {
return err
}
}
if birth != "" {
if err = dbConn.Exec("update user set birth=? where uid=?", birth, uid).Error; err != nil {
return err
}
}
if email != "" {
if err = dbConn.Exec("update user set email=? where uid=?", email, uid).Error; err != nil {
return err
}
}
if extendInfo != "" {
if err = dbConn.Exec("update user set ex=? where uid=?", extendInfo, uid).Error; err != nil {
return err
}
}
if gender != 0 {
if err = dbConn.Exec("update user set gender=? where uid=?", gender, uid).Error; err != nil {
return err
}
}
return nil
}
func SelectAllUID() ([]string, error) {
var uid []string
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return uid, err
}
rows, _ := dbConn.Raw("select uid from user").Rows()
defer rows.Close()
var strUID string
for rows.Next() {
rows.Scan(&strUID)
uid = append(uid, strUID)
}
return uid, nil
}
Loading…
Cancel
Save