user.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package services
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "lzyd-message-api/common"
  5. "lzyd-message-api/models"
  6. "sort"
  7. "strings"
  8. )
  9. type ChatroomList []models.ChatroomInfo
  10. func GetUser(db *gorm.DB, userId string) *models.User {
  11. var user models.User
  12. db.Where("user_id = ?", userId).Find(&user)
  13. return &user
  14. }
  15. func DeleteRoom(db *gorm.DB,userId string,roomId string) bool {
  16. sql := "UPDATE lzyd_live_user SET status = 0 WHERE room_id = ? && user_id = ? "
  17. err := db.Exec(sql, roomId, userId).Error
  18. if err != nil {
  19. return false
  20. }
  21. return true
  22. }
  23. func FindUser(db *gorm.DB, userId string) []models.ChatroomInfo {
  24. //var sql string
  25. var Chatroom []models.ChatroomInfo
  26. var messageContent []models.MessageList
  27. var messageList []models.MessageList
  28. var userInfo models.User
  29. db.Table("lzyd_live_chatroom").Select("*").Where("creator_id = ? || b_creator_id = ?",userId, userId).Find(&Chatroom)
  30. for i := 0; i < len(Chatroom); i++ {
  31. if Chatroom[i].CreatorId == userId {
  32. creatorName := Chatroom[i].CreatorName
  33. creatorAvatar := Chatroom[i].CreatorAvatar
  34. creatorId := Chatroom[i].CreatorId
  35. Chatroom[i].CreatorName = Chatroom[i].BCreatorName
  36. Chatroom[i].CreatorAvatar = Chatroom[i].BCreatorAvatar
  37. Chatroom[i].CreatorId = Chatroom[i].BCreatorId
  38. Chatroom[i].BCreatorName = creatorName
  39. Chatroom[i].BCreatorAvatar = creatorAvatar
  40. Chatroom[i].BCreatorId = creatorId
  41. }
  42. sql := `SELECT content from lzyd_live_message WHERE target_id = ? && status = ? && from_uid != ?`
  43. common.DB.Raw(sql,Chatroom[i].RoomId,0,userId).Find(&messageList)
  44. Chatroom[i].Count = len(messageList)
  45. sql2 := `SELECT user_id, user_avatar, username from lzyd_user WHERE user_id = ?`
  46. common.DB.Raw(sql2,Chatroom[i].CreatorId).Find(&userInfo)
  47. Chatroom[i].CreatorAvatar = userInfo.UserAvatar
  48. Chatroom[i].CreatorName = userInfo.UserName
  49. sql3 := `SELECT user_id, user_avatar, username from lzyd_user WHERE user_id = ?`
  50. common.DB.Raw(sql3,Chatroom[i].BCreatorId).Find(&userInfo)
  51. Chatroom[i].BCreatorAvatar = userInfo.UserAvatar
  52. Chatroom[i].BCreatorName = userInfo.UserName
  53. sql1 := `SELECT content,updated_at from lzyd_live_message WHERE target_id = ?`
  54. common.DB.Raw(sql1,Chatroom[i].RoomId).Find(&messageContent)
  55. //fmt.Println(len(messageContent))
  56. if len(messageContent) > 0 {
  57. //fmt.Println(messageContent)
  58. Chatroom[i].NewContent = messageContent[(len(messageContent)-1)].Content
  59. Chatroom[i].MsgTime = messageContent[(len(messageContent)-1)].UpdatedAt
  60. }
  61. var isShow models.IsShow
  62. sql4 := `SELECT status from lzyd_live_user WHERE room_id = ? && user_id = ?`
  63. common.DB.Raw(sql4,Chatroom[i].RoomId,userId).Find(&isShow)
  64. //fmt.Println(Chatroom[i].RoomId,userId,isShow)
  65. Chatroom[i].IsShow = isShow.IsShow
  66. }
  67. var chatrooms []models.ChatroomInfo
  68. sort.Sort(ChatroomList(Chatroom))
  69. for i :=0;i<len(Chatroom);i++ {
  70. if Chatroom[i].IsShow == 1 {
  71. chatrooms = append(chatrooms,Chatroom[i])
  72. }
  73. }
  74. for i :=0;i<len(chatrooms);i++ {
  75. timeRes := strings.Split(chatrooms[i].UpdatedAt,"Z")
  76. timeRes = strings.Split(timeRes[0],"T")
  77. var timeRes3 string
  78. timeRes3 = timeRes[0]+" "+timeRes[1]
  79. chatrooms[i].UpdatedAt = timeRes3
  80. }
  81. //fmt.Println(chatrooms)
  82. return chatrooms
  83. }
  84. func (list ChatroomList) Len() int {
  85. return len(list)
  86. }
  87. func (list ChatroomList) Less(i, j int) bool {
  88. return list[i].UpdatedAt > list[j].UpdatedAt
  89. }
  90. func (list ChatroomList) Swap(i, j int) {
  91. list[i], list[j] = list[j], list[i]
  92. }
  93. //func Login(db *gorm.DB, userId string) (err error, data *models.UserInfoRes) {
  94. // var user models.UserInfoRes
  95. // err = db.Raw("SELECT * FROM live_user WHERE user_id = ? AND is_delete=0", userId).Find(&user).Error
  96. // return err, &user
  97. //}
  98. //func Register(db *gorm.DB, userInfo *utils.WxUserInfoDetail) (err error, data *models.UserInfoRes) {
  99. // var user models.UserInfoRes
  100. // NotRecord := db.Raw("SELECT * FROM live_user WHERE wx_id = ? AND is_delete=0", userInfo.OpenId).Find(&user).RecordNotFound()
  101. // if !NotRecord {
  102. // return err, &user
  103. // } else {
  104. // UId, _ := uuid.NewV4()
  105. // UserId := fmt.Sprintf("%s", UId)
  106. // userName := utils.GenerateSubString()
  107. // passWord := utils.MD5V([]byte("888888"))
  108. // sql := `INSERT INTO live_user(user_id, username, password, nickname, avatar, wx_id) VALUES(?,?,?,?,?,?)`
  109. // err = db.Exec(sql, UserId, userName, passWord, userInfo.NickName, userInfo.HeadImgUrl, userInfo.OpenId).Error
  110. // if err != nil {
  111. // return err, data
  112. // }
  113. // user.UserId = UserId
  114. // user.Nickname = userInfo.NickName
  115. // user.Avatar = userInfo.HeadImgUrl
  116. // return err, &user
  117. // }
  118. //}