123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package models
- import (
- "fmt"
- "github.com/jinzhu/gorm"
- "lzyd-message-api/common"
- "time"
- )
- type JoinChatroomParam struct {
- RoomId string `json:"roomId"`
- UserId string `json:"userId"`
- }
- type LeaveChatroomParam struct {
- RoomId string `json:"roomId"`
- UserId string `json:"userId"`
- }
- type CreateChatroomReq struct {
- CreatorId string `json:"creatorId"`
- BCreatorId string `json:"bCreatorId"`
- UserCount int `json:"userCount"`
- }
- type JoinChatroomReq struct {
- CreatorId string `json:"creatorId"`
- BCreatorId string `json:"bCreatorId"`
- UserCount int `json:"userCount"`
- }
- type ChatroomInfo struct {
- RoomId string `gorm:"column:room_id" json:"room_id"`
- Name string `gorm:"column:name" json:"name"`
- CreatorId string `gorm:"column:creator_id" json:"creator_id"`
- BCreatorId string `gorm:"column:b_creator_id" json:"b_creator_id"`
- UserCount int `gorm:"column:user_count" json:"user_count"`
- CreatorName string `gorm:"column:creator_name" json:"creator_name"`
- BCreatorName string `gorm:"column:b_creator_name" json:"b_creator_name"`
- CreatorAvatar string `gorm:"column:creator_avatar" json:"creator_avatar"`
- BCreatorAvatar string `gorm:"column:b_creator_avatar" json:"b_creator_avatar"`
- Count int `gorm:"column:Count" json:"count"`
- NewContent string `gorm:"column:new_content" json:"new_content"`
- MsgTime string `gorm:"column:msg_time" json:"msg_time"`
- UpdatedAt string `gorm:"column:updated_at" json:"updated_at"`
- IsShow int `gorm:"column:is_show" json:"is_show"`
- }
- type ChatroomUser struct {
- ID int64 `gorm:"column:id" json:"id"`
- RoomId string `gorm:"column:room_id" json:"room_id"`
- UserId string `gorm:"column:user_id" json:"user_id"`
- JoinTime *JSONTime `gorm:"column:join_time" json:"join_time"`
- LeaveTime *JSONTime `gorm:"column:leave_time" json:"leave_time"`
- }
- type UsersInRoom struct {
- RoomId string `gorm:"column:room_id" json:"room_id"`
- }
- type RoomUsers struct {
- CreatorId string `json:"creator_id" gorm:"column:creator_id"`
- BCreatorId string `json:"b_creator_id" gorm:"column:b_creator_id"`
- LeaveTime string `json:"leave_time" gorm:"column:leave_time"`
- }
- type Chatroom struct {
- Model
- RoomId string `gorm:"column:room_id" json:"room_id"`
- Name string `gorm:"column:name" json:"name"`
- CreatorId string `gorm:"column:creator_id" json:"creator_id"`
- BCreatorId string `gorm:"column:b_creator_id" json:"b_creator_id"`
- }
- func (Chatroom) TableName() string {
- return "lzyd_live_chatroom"
- }
- func (ChatroomUser) TableName() string {
- return "lzyd_live_chatroom_user"
- }
- func (p ChatroomUser) Join(db *gorm.DB) error {
- sql := "INSERT INTO lzyd_live_chatroom_user(room_id, user_id, join_time) VALUES(?,?,NOW())"
- err := db.Exec(sql, p.RoomId, p.UserId).Error
- var roomUser RoomUsers
- sql1 := "SELECT creator_id, b_creator_id from lzyd_live_chatroom WHERE room_id=?"
- common.DB.Raw(sql1, p.RoomId).Find(&roomUser)
- var user string
- if roomUser.CreatorId == p.UserId {
- user = roomUser.BCreatorId
- }else {
- user = roomUser.CreatorId
- }
- //fmt.Println(user)
- sql2 := "UPDATE lzyd_live_message SET status = 1 WHERE target_id = ? && from_uid = ? "
- err2 := db.Exec(sql2, p.RoomId, user).Error
- fmt.Println(err2)
- return err
- }
- func (ChatroomUser) Leave(db *gorm.DB, UserId string) error {
- fmt.Println(111,UserId)
- var user ChatroomUser
- err := db.Where("user_id=?", UserId).Last(&user).Update("leave_time", time.Now()).Error
- //sql := "UPDATE lzyd_live_chatroom_user SET leave_time = NOW() WHERE user_id = ?"
- //err := db.Exec(sql, UserId).Error
- return err
- }
- func (p ChatroomUser) IsInChatroom(db *gorm.DB) bool {
- var count int
- sql := "SELECT COUNT(id) AS count FROM lzyd_live_chatroom_user WHERE room_id = ? AND user_id = ? AND leave_time IS NULL"
- db.Raw(sql, p.RoomId, p.UserId).Count(&count)
- fmt.Println(count)
- return count > 0
- }
- func (p ChatroomInfo) CreateRoom(db *gorm.DB)error {
- now := time.Now()
- tx := db.Begin()
- //fmt.Println(111,now)
- var (
- insertInfoSql string
- insertInfoErr error
- insertRoom1Sql string
- insertRoom1Err error
- insertRoom2Sql string
- insertRoom2Err error
- )
- insertInfoSql = "INSERT INTO lzyd_live_chatroom(room_id, creator_id, b_creator_id, creator_name, b_creator_name,creator_avatar,b_creator_avatar, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?)"
- insertInfoErr = db.Exec(insertInfoSql, p.RoomId, p.CreatorId, p.BCreatorId, p.CreatorName, p.BCreatorName, p.CreatorAvatar, p.BCreatorAvatar, now, now).Error
- insertRoom1Sql = "INSERT INTO lzyd_live_user(room_id, user_id, status, created_at, updated_at) VALUES (?,?,?,?,?)"
- insertRoom1Err = db.Exec(insertRoom1Sql, p.RoomId, p.CreatorId,1, now, now).Error
- insertRoom2Sql = "INSERT INTO lzyd_live_user(room_id, user_id, status, created_at, updated_at) VALUES (?,?,?,?,?)"
- insertRoom2Err = db.Exec(insertRoom2Sql, p.RoomId, p.BCreatorId,1, now, now).Error
- if insertInfoErr != nil && insertRoom1Err != nil && insertRoom2Err != nil {
- tx.Rollback()
- //return errors.New(common.ErrorInfo[common.DbInsertErr])
- }
- tx.Commit()
- return nil
- }
- func (p ChatroomInfo) UploadLiveUser(db *gorm.DB) error {
- tx := db.Begin()
- var (
- upDateInfoSql1 string
- upDateInfoErr1 error
- //upDateInfoSql2 string
- //upDateInfoErr2 error
- )
- fmt.Println(p)
- upDateInfoSql1 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?"
- upDateInfoErr1 = db.Exec(upDateInfoSql1, p.CreatorId, p.RoomId).Error
- //upDateInfoSql2 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?"
- //upDateInfoErr2 = db.Exec(upDateInfoSql2, p.BCreatorId, p.RoomId).Error
- //if upDateInfoErr1 != nil && upDateInfoErr2 != nil {
- // tx.Rollback()
- //}
- if upDateInfoErr1 != nil {
- tx.Rollback()
- }
- tx.Commit()
- return nil
- }
|