db model update

pull/4/head
away 3 years ago
parent 0cea353558
commit 34a67e07b6

@ -21,7 +21,7 @@ func key(dbAddress, dbName string) string {
func init() {
//mysql init
initMysqlDB()
// mongo init
mgoDailInfo := &mgo.DialInfo{
Addrs: config.Config.Mongo.DBAddress,

@ -11,6 +11,7 @@ import (
)
const cChat = "chat"
const cGroup = "group"
type MsgInfo struct {
SendTime int64
@ -22,7 +23,13 @@ type UserChat struct {
Msg []MsgInfo
}
type GroupMember struct {
GroupID string
UIDList []string
}
func (d *DataBases) GetUserChat(uid string, seqBegin, seqEnd int64) (SingleMsg []*pbMsg.MsgFormat, GroupMsg []*pbMsg.MsgFormat, MaxSeq int64, MinSeq int64, err error) {
count := 0
session := d.mgoSession.Clone()
if session == nil {
return nil, nil, MaxSeq, MinSeq, errors.New("session == nil")
@ -57,7 +64,7 @@ func (d *DataBases) GetUserChat(uid string, seqBegin, seqEnd int64) (SingleMsg [
if pChat.RecvSeq > MaxSeq {
MaxSeq = pChat.RecvSeq
}
if i == 0 {
if count == 0 {
MinSeq = pChat.RecvSeq
}
if pChat.RecvSeq < MinSeq {
@ -68,6 +75,7 @@ func (d *DataBases) GetUserChat(uid string, seqBegin, seqEnd int64) (SingleMsg [
} else {
GroupMsg = append(GroupMsg, temp)
}
count++
}
}
@ -155,3 +163,71 @@ func (d *DataBases) MgoSkipUID(count int) (string, error) {
c.Find(nil).Skip(count).Limit(1).One(&sChat)
return sChat.UID, nil
}
func (d *DataBases) GetGroupMember(groupID string) []string {
groupInfo := GroupMember{}
groupInfo.GroupID = groupID
groupInfo.UIDList = make([]string, 0)
session := d.mgoSession.Clone()
if session == nil {
return groupInfo.UIDList
}
defer session.Close()
c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
if err := c.Find(bson.M{"groupid": groupInfo.GroupID}).One(&groupInfo); err != nil {
return groupInfo.UIDList
}
return groupInfo.UIDList
}
func (d *DataBases) AddGroupMember(groupID, uid string) error {
session := d.mgoSession.Clone()
if session == nil {
return errors.New("session == nil")
}
defer session.Close()
c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
n, err := c.Find(bson.M{"groupid": groupID}).Count()
if err != nil {
return err
}
if n == 0 {
groupInfo := GroupMember{}
groupInfo.GroupID = groupID
groupInfo.UIDList = append(groupInfo.UIDList, uid)
err = c.Insert(&groupInfo)
if err != nil {
return err
}
} else {
err = c.Update(bson.M{"groupid": groupID}, bson.M{"$addToSet": bson.M{"uidlist": uid}})
if err != nil {
return err
}
}
return nil
}
func (d *DataBases) DelGroupMember(groupID, uid string) error {
session := d.mgoSession.Clone()
if session == nil {
return errors.New("session == nil")
}
defer session.Close()
c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
if err := c.Update(bson.M{"groupid": groupID}, bson.M{"$pull": bson.M{"uidlist": uid}}); err != nil {
return err
}
return nil
}

@ -14,6 +14,85 @@ type mysqlDB struct {
dbMap map[string]*gorm.DB
}
func initMysqlDB() {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
db, err := gorm.Open("mysql", dsn)
if err != nil {
panic(err)
}
//Check the database and table during initialization
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s ;", config.Config.Mysql.DBDatabaseName)
err = db.Exec(sql).Error
if err != nil {
panic(err)
}
db.Close()
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
db, err = gorm.Open("mysql", dsn)
if err != nil {
panic(err)
}
sqlTable := "CREATE TABLE IF NOT EXISTS `user` (\n `uid` varchar(64) NOT NULL,\n `name` varchar(64) DEFAULT NULL,\n `icon` varchar(1024) DEFAULT NULL,\n `gender` int(11) unsigned zerofill DEFAULT NULL,\n `mobile` varchar(32) DEFAULT NULL,\n `birth` varchar(16) DEFAULT NULL,\n `email` varchar(64) DEFAULT NULL,\n `ex` varchar(1024) DEFAULT NULL,\n `create_time` datetime DEFAULT NULL,\n PRIMARY KEY (`uid`),\n UNIQUE KEY `uk_uid` (`uid`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `friend` (\n `owner_id` varchar(255) NOT NULL,\n `friend_id` varchar(255) NOT NULL,\n `comment` varchar(255) DEFAULT NULL,\n `friend_flag` int(11) NOT NULL,\n `create_time` datetime NOT NULL,\n PRIMARY KEY (`owner_id`,`friend_id`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `friend_request` (\n `req_id` varchar(255) NOT NULL,\n `user_id` varchar(255) NOT NULL,\n `flag` int(11) NOT NULL DEFAULT '0',\n `req_message` varchar(255) DEFAULT NULL,\n `create_time` datetime NOT NULL,\n PRIMARY KEY (`user_id`,`req_id`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `black_list` (\n `uid` varchar(32) NOT NULL COMMENT 'uid',\n `begin_disable_time` datetime DEFAULT NULL,\n `end_disable_time` datetime DEFAULT NULL,\n `ex` varchar(1024) DEFAULT NULL,\n PRIMARY KEY (`uid`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `user_black_list` (\n `owner_id` varchar(255) NOT NULL,\n `block_id` varchar(255) NOT NULL,\n `create_time` datetime NOT NULL,\n PRIMARY KEY (`owner_id`,`block_id`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `group` (\n `group_id` varchar(255) NOT NULL,\n `name` varchar(255) DEFAULT NULL,\n `introduction` varchar(255) DEFAULT NULL,\n `notification` varchar(255) DEFAULT NULL,\n `face_url` varchar(255) DEFAULT NULL,\n `create_time` datetime DEFAULT NULL,\n `ex` varchar(255) DEFAULT NULL,\n PRIMARY KEY (`group_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `group_member` (\n `group_id` varchar(255) NOT NULL,\n `uid` varchar(255) NOT NULL,\n `nickname` varchar(255) DEFAULT NULL,\n `user_group_face_url` varchar(255) DEFAULT NULL,\n `administrator_level` int(11) NOT NULL,\n `join_time` datetime NOT NULL,\n PRIMARY KEY (`group_id`,`uid`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `group_request` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `group_id` varchar(255) NOT NULL,\n `from_user_id` varchar(255) NOT NULL,\n `to_user_id` varchar(255) NOT NULL,\n `flag` int(10) NOT NULL DEFAULT '0',\n `req_msg` varchar(255) DEFAULT '',\n `handled_msg` varchar(255) DEFAULT '',\n `create_time` datetime NOT NULL,\n `from_user_nickname` varchar(255) DEFAULT '',\n `to_user_nickname` varchar(255) DEFAULT NULL,\n `from_user_face_url` varchar(255) DEFAULT '',\n `to_user_face_url` varchar(255) DEFAULT '',\n `handled_user` varchar(255) DEFAULT '',\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8mb4;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
sqlTable = "CREATE TABLE IF NOT EXISTS `chat_log` (\n `msg_id` varchar(128) NOT NULL,\n `send_id` varchar(255) NOT NULL,\n `session_type` int(11) NOT NULL,\n `recv_id` varchar(255) NOT NULL,\n `content_type` int(11) NOT NULL,\n `msg_from` int(11) NOT NULL,\n `content` varchar(1000) NOT NULL,\n `remark` varchar(100) DEFAULT NULL,\n `sender_platform_id` int(11) NOT NULL,\n `send_time` datetime NOT NULL,\n PRIMARY KEY (`msg_id`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;"
err = db.Exec(sqlTable).Error
if err != nil {
panic(err)
}
}
func (m *mysqlDB) DefaultGormDB() (*gorm.DB, error) {
return m.GormDB(config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
}

@ -139,6 +139,23 @@ func GetOwnerManagerByGroupId(groupId string) ([]GroupMember, error) {
return groupMemberList, nil
}
func IsExistGroupMember(groupId, uid string) bool {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return false
}
var number int32
err = dbConn.Raw("select count(*) from `group_member` where group_id = ? and uid = ?", groupId, uid).Count(&number).Error
if err != nil {
return false
}
if number != 1 {
return false
}
return true
}
func RemoveGroupMember(groupId string, memberId string) error {
return DeleteGroupMemberByGroupIdAndUserId(groupId, memberId)
}

@ -95,7 +95,8 @@ func GetGroupApplicationList(uid string) (*group.GetGroupApplicationListResp, er
return &group.GetGroupApplicationListResp{}, nil
}
sql := "select group_id, from_user_id, to_user_id, flag, req_msg, handled_msg, create_time, from_user_nickname, from_user_face_url, handled_user from `group_request` where group_id in ( "
sql := "select id, group_id, from_user_id, to_user_id, flag, req_msg, handled_msg, create_time, " +
"from_user_nickname, to_user_nickname, from_user_face_url, to_user_face_url, handled_user from `group_request` where group_id in ( "
for i := 0; i < len(gIDs); i++ {
if i == len(gIDs)-1 {
sql = sql + "\"" + gIDs[i] + "\"" + " )"
@ -113,8 +114,9 @@ func GetGroupApplicationList(uid string) (*group.GetGroupApplicationListResp, er
return nil, err
}
for rows.Next() {
rows.Scan(&groupRequest.GroupID, &groupRequest.FromUserID, &groupRequest.ToUserID, &groupRequest.Flag, &groupRequest.ReqMsg,
&groupRequest.HandledMsg, &groupRequest.CreateTime, &groupRequest.FromUserNickname, &groupRequest.FromUserFaceUrl, &groupRequest.HandledUser)
rows.Scan(&groupRequest.ID, &groupRequest.GroupID, &groupRequest.FromUserID, &groupRequest.ToUserID, &groupRequest.Flag, &groupRequest.ReqMsg,
&groupRequest.HandledMsg, &groupRequest.CreateTime, &groupRequest.FromUserNickname, &groupRequest.ToUserNickname,
&groupRequest.FromUserFaceUrl, &groupRequest.ToUserFaceUrl, &groupRequest.HandledUser)
groupRequests = append(groupRequests, groupRequest)
}
@ -123,14 +125,19 @@ func GetGroupApplicationList(uid string) (*group.GetGroupApplicationListResp, er
reply.Data.Count = int32(len(groupRequests))
for i := 0; i < int(reply.Data.Count); i++ {
addUser := group.GetGroupApplicationList_Data_User{
ID: groupRequests[i].ID,
GroupID: groupRequests[i].GroupID,
FromUserID: groupRequests[i].FromUserID,
FromUserNickName: groupRequests[i].FromUserNickname,
FromUserNickname: groupRequests[i].FromUserNickname,
FromUserFaceUrl: groupRequests[i].FromUserFaceUrl,
ToUserID: groupRequests[i].ToUserID,
AddTime: groupRequests[i].CreateTime.Unix(),
RequestMsg: groupRequests[i].ReqMsg,
HandledMsg: groupRequests[i].HandledMsg,
Flag: groupRequests[i].Flag,
ToUserNickname: groupRequests[i].ToUserNickname,
ToUserFaceUrl: groupRequests[i].ToUserFaceUrl,
HandledUser: groupRequests[i].HandledUser,
Type: 0,
HandleStatus: 0,
HandleResult: 0,

@ -11,14 +11,14 @@ func InsertIntoGroupRequest(groupId, fromUserId, toUserId, reqMsg, fromUserNickN
return err
}
toInsertInfo := GroupRequest{GroupID: groupId, FromUserID: fromUserId, ToUserID: toUserId, ReqMsg: reqMsg, FromUserNickname: fromUserNickName, FromUserFaceUrl: fromUserFaceUrl, CreateTime: time.Now()}
err = dbConn.Table("group_request").Create(toInsertInfo).Error
err = dbConn.Table("group_request").Create(&toInsertInfo).Error
if err != nil {
return err
}
return nil
}
func FindGroupRequestUserInfoByUidAndGroupID(groupId, uid string) (*GroupRequest, error) {
func FindGroupRequestUserInfoByGroupIDAndUid(groupId, uid string) (*GroupRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err

@ -54,6 +54,7 @@ type GroupMember struct {
}
type GroupRequest struct {
ID string `gorm:"column:id"`
GroupID string `gorm:"column:group_id"`
FromUserID string `gorm:"column:from_user_id"`
ToUserID string `gorm:"column:to_user_id"`

Loading…
Cancel
Save