package services import ( "github.com/jinzhu/gorm" "lzyd-message-api/common" "lzyd-message-api/models" "sort" "strings" ) type ChatroomList []models.ChatroomInfo func GetUser(db *gorm.DB, userId string) *models.User { var user models.User db.Where("user_id = ?", userId).Find(&user) return &user } func DeleteRoom(db *gorm.DB,userId string,roomId string) bool { sql := "UPDATE lzyd_live_user SET status = 0 WHERE room_id = ? && user_id = ? " err := db.Exec(sql, roomId, userId).Error if err != nil { return false } return true } func FindUser(db *gorm.DB, userId string) []models.ChatroomInfo { //var sql string var Chatroom []models.ChatroomInfo var messageContent []models.MessageList var messageList []models.MessageList var userInfo models.User db.Table("lzyd_live_chatroom").Select("*").Where("creator_id = ? || b_creator_id = ?",userId, userId).Find(&Chatroom) for i := 0; i < len(Chatroom); i++ { if Chatroom[i].CreatorId == userId { creatorName := Chatroom[i].CreatorName creatorAvatar := Chatroom[i].CreatorAvatar creatorId := Chatroom[i].CreatorId Chatroom[i].CreatorName = Chatroom[i].BCreatorName Chatroom[i].CreatorAvatar = Chatroom[i].BCreatorAvatar Chatroom[i].CreatorId = Chatroom[i].BCreatorId Chatroom[i].BCreatorName = creatorName Chatroom[i].BCreatorAvatar = creatorAvatar Chatroom[i].BCreatorId = creatorId } sql := `SELECT content from lzyd_live_message WHERE target_id = ? && status = ? && from_uid != ?` common.DB.Raw(sql,Chatroom[i].RoomId,0,userId).Find(&messageList) Chatroom[i].Count = len(messageList) sql2 := `SELECT user_id, user_avatar, username from lzyd_user WHERE user_id = ?` common.DB.Raw(sql2,Chatroom[i].CreatorId).Find(&userInfo) Chatroom[i].CreatorAvatar = userInfo.UserAvatar Chatroom[i].CreatorName = userInfo.UserName sql3 := `SELECT user_id, user_avatar, username from lzyd_user WHERE user_id = ?` common.DB.Raw(sql3,Chatroom[i].BCreatorId).Find(&userInfo) Chatroom[i].BCreatorAvatar = userInfo.UserAvatar Chatroom[i].BCreatorName = userInfo.UserName sql1 := `SELECT content,updated_at from lzyd_live_message WHERE target_id = ?` common.DB.Raw(sql1,Chatroom[i].RoomId).Find(&messageContent) //fmt.Println(len(messageContent)) if len(messageContent) > 0 { //fmt.Println(messageContent) Chatroom[i].NewContent = messageContent[(len(messageContent)-1)].Content Chatroom[i].MsgTime = messageContent[(len(messageContent)-1)].UpdatedAt } var isShow models.IsShow sql4 := `SELECT status from lzyd_live_user WHERE room_id = ? && user_id = ?` common.DB.Raw(sql4,Chatroom[i].RoomId,userId).Find(&isShow) //fmt.Println(Chatroom[i].RoomId,userId,isShow) Chatroom[i].IsShow = isShow.IsShow } var chatrooms []models.ChatroomInfo sort.Sort(ChatroomList(Chatroom)) for i :=0;i list[j].UpdatedAt } func (list ChatroomList) Swap(i, j int) { list[i], list[j] = list[j], list[i] } //func Login(db *gorm.DB, userId string) (err error, data *models.UserInfoRes) { // var user models.UserInfoRes // err = db.Raw("SELECT * FROM live_user WHERE user_id = ? AND is_delete=0", userId).Find(&user).Error // return err, &user //} //func Register(db *gorm.DB, userInfo *utils.WxUserInfoDetail) (err error, data *models.UserInfoRes) { // var user models.UserInfoRes // NotRecord := db.Raw("SELECT * FROM live_user WHERE wx_id = ? AND is_delete=0", userInfo.OpenId).Find(&user).RecordNotFound() // if !NotRecord { // return err, &user // } else { // UId, _ := uuid.NewV4() // UserId := fmt.Sprintf("%s", UId) // userName := utils.GenerateSubString() // passWord := utils.MD5V([]byte("888888")) // sql := `INSERT INTO live_user(user_id, username, password, nickname, avatar, wx_id) VALUES(?,?,?,?,?,?)` // err = db.Exec(sql, UserId, userName, passWord, userInfo.NickName, userInfo.HeadImgUrl, userInfo.OpenId).Error // if err != nil { // return err, data // } // user.UserId = UserId // user.Nickname = userInfo.NickName // user.Avatar = userInfo.HeadImgUrl // return err, &user // } //}