chatroom.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. "lzyd-message-api/common"
  6. "time"
  7. )
  8. type JoinChatroomParam struct {
  9. RoomId string `json:"roomId"`
  10. UserId string `json:"userId"`
  11. }
  12. type LeaveChatroomParam struct {
  13. RoomId string `json:"roomId"`
  14. UserId string `json:"userId"`
  15. }
  16. type CreateChatroomReq struct {
  17. CreatorId string `json:"creatorId"`
  18. BCreatorId string `json:"bCreatorId"`
  19. UserCount int `json:"userCount"`
  20. }
  21. type JoinChatroomReq struct {
  22. CreatorId string `json:"creatorId"`
  23. BCreatorId string `json:"bCreatorId"`
  24. UserCount int `json:"userCount"`
  25. }
  26. type ChatroomInfo struct {
  27. RoomId string `gorm:"column:room_id" json:"room_id"`
  28. Name string `gorm:"column:name" json:"name"`
  29. CreatorId string `gorm:"column:creator_id" json:"creator_id"`
  30. BCreatorId string `gorm:"column:b_creator_id" json:"b_creator_id"`
  31. UserCount int `gorm:"column:user_count" json:"user_count"`
  32. CreatorName string `gorm:"column:creator_name" json:"creator_name"`
  33. BCreatorName string `gorm:"column:b_creator_name" json:"b_creator_name"`
  34. CreatorAvatar string `gorm:"column:creator_avatar" json:"creator_avatar"`
  35. BCreatorAvatar string `gorm:"column:b_creator_avatar" json:"b_creator_avatar"`
  36. Count int `gorm:"column:Count" json:"count"`
  37. NewContent string `gorm:"column:new_content" json:"new_content"`
  38. MsgTime string `gorm:"column:msg_time" json:"msg_time"`
  39. UpdatedAt string `gorm:"column:updated_at" json:"updated_at"`
  40. IsShow int `gorm:"column:is_show" json:"is_show"`
  41. }
  42. type ChatroomUser struct {
  43. ID int64 `gorm:"column:id" json:"id"`
  44. RoomId string `gorm:"column:room_id" json:"room_id"`
  45. UserId string `gorm:"column:user_id" json:"user_id"`
  46. JoinTime *JSONTime `gorm:"column:join_time" json:"join_time"`
  47. LeaveTime *JSONTime `gorm:"column:leave_time" json:"leave_time"`
  48. }
  49. type UsersInRoom struct {
  50. RoomId string `gorm:"column:room_id" json:"room_id"`
  51. }
  52. type RoomUsers struct {
  53. CreatorId string `json:"creator_id" gorm:"column:creator_id"`
  54. BCreatorId string `json:"b_creator_id" gorm:"column:b_creator_id"`
  55. LeaveTime string `json:"leave_time" gorm:"column:leave_time"`
  56. }
  57. type Chatroom struct {
  58. Model
  59. RoomId string `gorm:"column:room_id" json:"room_id"`
  60. Name string `gorm:"column:name" json:"name"`
  61. CreatorId string `gorm:"column:creator_id" json:"creator_id"`
  62. BCreatorId string `gorm:"column:b_creator_id" json:"b_creator_id"`
  63. }
  64. func (Chatroom) TableName() string {
  65. return "lzyd_live_chatroom"
  66. }
  67. func (ChatroomUser) TableName() string {
  68. return "lzyd_live_chatroom_user"
  69. }
  70. func (p ChatroomUser) Join(db *gorm.DB) error {
  71. sql := "INSERT INTO lzyd_live_chatroom_user(room_id, user_id, join_time) VALUES(?,?,NOW())"
  72. err := db.Exec(sql, p.RoomId, p.UserId).Error
  73. var roomUser RoomUsers
  74. sql1 := "SELECT creator_id, b_creator_id from lzyd_live_chatroom WHERE room_id=?"
  75. common.DB.Raw(sql1, p.RoomId).Find(&roomUser)
  76. var user string
  77. if roomUser.CreatorId == p.UserId {
  78. user = roomUser.BCreatorId
  79. }else {
  80. user = roomUser.CreatorId
  81. }
  82. //fmt.Println(user)
  83. sql2 := "UPDATE lzyd_live_message SET status = 1 WHERE target_id = ? && from_uid = ? "
  84. err2 := db.Exec(sql2, p.RoomId, user).Error
  85. fmt.Println(err2)
  86. return err
  87. }
  88. func (ChatroomUser) Leave(db *gorm.DB, UserId string) error {
  89. fmt.Println(111,UserId)
  90. var user ChatroomUser
  91. err := db.Where("user_id=?", UserId).Last(&user).Update("leave_time", time.Now()).Error
  92. //sql := "UPDATE lzyd_live_chatroom_user SET leave_time = NOW() WHERE user_id = ?"
  93. //err := db.Exec(sql, UserId).Error
  94. return err
  95. }
  96. func (p ChatroomUser) IsInChatroom(db *gorm.DB) bool {
  97. var count int
  98. sql := "SELECT COUNT(id) AS count FROM lzyd_live_chatroom_user WHERE room_id = ? AND user_id = ? AND leave_time IS NULL"
  99. db.Raw(sql, p.RoomId, p.UserId).Count(&count)
  100. fmt.Println(count)
  101. return count > 0
  102. }
  103. func (p ChatroomInfo) CreateRoom(db *gorm.DB)error {
  104. now := time.Now()
  105. tx := db.Begin()
  106. //fmt.Println(111,now)
  107. var (
  108. insertInfoSql string
  109. insertInfoErr error
  110. insertRoom1Sql string
  111. insertRoom1Err error
  112. insertRoom2Sql string
  113. insertRoom2Err error
  114. )
  115. 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 (?,?,?,?,?,?,?,?,?)"
  116. insertInfoErr = db.Exec(insertInfoSql, p.RoomId, p.CreatorId, p.BCreatorId, p.CreatorName, p.BCreatorName, p.CreatorAvatar, p.BCreatorAvatar, now, now).Error
  117. insertRoom1Sql = "INSERT INTO lzyd_live_user(room_id, user_id, status, created_at, updated_at) VALUES (?,?,?,?,?)"
  118. insertRoom1Err = db.Exec(insertRoom1Sql, p.RoomId, p.CreatorId,1, now, now).Error
  119. insertRoom2Sql = "INSERT INTO lzyd_live_user(room_id, user_id, status, created_at, updated_at) VALUES (?,?,?,?,?)"
  120. insertRoom2Err = db.Exec(insertRoom2Sql, p.RoomId, p.BCreatorId,1, now, now).Error
  121. if insertInfoErr != nil && insertRoom1Err != nil && insertRoom2Err != nil {
  122. tx.Rollback()
  123. //return errors.New(common.ErrorInfo[common.DbInsertErr])
  124. }
  125. tx.Commit()
  126. return nil
  127. }
  128. func (p ChatroomInfo) UploadLiveUser(db *gorm.DB) error {
  129. tx := db.Begin()
  130. var (
  131. upDateInfoSql1 string
  132. upDateInfoErr1 error
  133. //upDateInfoSql2 string
  134. //upDateInfoErr2 error
  135. )
  136. fmt.Println(p)
  137. upDateInfoSql1 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?"
  138. upDateInfoErr1 = db.Exec(upDateInfoSql1, p.CreatorId, p.RoomId).Error
  139. //upDateInfoSql2 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?"
  140. //upDateInfoErr2 = db.Exec(upDateInfoSql2, p.BCreatorId, p.RoomId).Error
  141. //if upDateInfoErr1 != nil && upDateInfoErr2 != nil {
  142. // tx.Rollback()
  143. //}
  144. if upDateInfoErr1 != nil {
  145. tx.Rollback()
  146. }
  147. tx.Commit()
  148. return nil
  149. }