package services import ( "github.com/jinzhu/gorm" "lzyd-message-api/models" ) func ChatRoomList(db *gorm.DB, r *models.ChatRoomListReq) (err error, list interface{}, total int) { var roomList []models.ChatRoomListRes var count models.Count sql := `SELECT id,room_id,name,url,cover,intro,category,with_graphic,with_chatroom as with_chat_room,state, start_time,end_time,sender_name,sender_avatar,view_count,created_at,updated_at FROM live_entity WHERE name like ? AND if("" = ?, true, category = ?) AND is_delete=0 ORDER BY created_at desc` sqlCount := `SELECT COUNT(*) as count FROM live_entity WHERE name like ? AND if("" = ?, true, category = ?) AND is_delete=0` limit := r.PageSize offset := r.PageSize * (r.Page - 1) err = db.Raw(sql, "%" + r.Name+"%", r.Category, r.Category).Limit(limit).Offset(offset).Find(&roomList).Error if err != nil { return err, list, total } err = db.Raw(sqlCount, "%" + r.Name+ "%", r.Category, r.Category).Find(&count).Error if err != nil { return err, list, total } return err, roomList, count.Count } func ChatroomDetail(db *gorm.DB, roomId string) (err error, data interface{}) { var room []models.ChatRoomListRes sqlRoom := `SELECT id,room_id,name,url,cover,intro,category,with_graphic,with_chatroom as with_chat_room,state, start_time,end_time,sender_name,sender_avatar,view_count,created_at,updated_at FROM live_entity WHERE room_id = ?` err = db.Raw(sqlRoom, roomId).Find(&room).Error return err, room[0] } func ChatroomView(db *gorm.DB, roomId string) (err error) { var count models.ViewCount sqlRoom := `SELECT view_count FROM live_entity WHERE room_id = ?` err = db.Raw(sqlRoom, roomId).Find(&count).Error if err != nil { return err } sql := `UPDATE live_entity SET view_count=? WHERE room_id = ?` err = db.Exec(sql, count.ViewCount+1, roomId).Error return err } func ChatroomClassification(db *gorm.DB, t string) (err error, list interface{}) { var classification []*models.ChatRoomClassificationRes sql := `SELECT id,name,created_at,updated_at FROM live_classification WHERE is_delete=0 AND type =?` err = db.Raw(sql, t).Find(&classification).Error return err, classification } func GraphicList(db *gorm.DB, roomId string) (err error, list interface{}) { var GraphficListRes []models.GraphicListDataRes var GraphicListDetail []models.GraphicDetail sqlGraphicMaster := `SELECT lg.graphic_id,le.sender_name,le.sender_avatar,lg.created_at,lg.updated_at FROM live_graphictext lg LEFT JOIN live_entity le ON le.room_id = lg.live_id WHERE lg.live_id = ? AND lg.is_delete=0 ORDER BY created_at desc` sqlCraphicDetail := `SELECT lg.graphic_id,lf.id,lf.type,lf.content,lf.created_at,lf.updated_at FROM live_graphictext lg LEFT JOIN live_fragment lf ON lf.graphic_id = lg.graphic_id WHERE lg.live_id = ? AND lf.is_delete=0 ORDER BY created_at desc` err = db.Raw(sqlGraphicMaster, roomId).Find(&GraphficListRes).Error if err != nil { return err, list } err = db.Raw(sqlCraphicDetail, roomId).Find(&GraphicListDetail).Error if err != nil { return err, list } var data []interface{} for _, item := range GraphficListRes { for _, child := range GraphicListDetail { if item.GraphicId == child.GraphicId { // 判断 图文直播是否有oss视频,有需私有读取 //if strings.Contains(child.Content, "oss-cn-hangzhou.aliyuncs.com") && item.type == 3 { // objKey := strings.Replace(child.Content, "https://health-live.oss-cn-hangzhou.aliyuncs.com/", "", 1) // child.Content = utils.GetVideoSignedURL(objKey) // child.Content = child.Content + "," + utils.GetSnapVideoSignedURL(objKey) //} item.List = append(item.List, child) } } data = append(data, item) } return err, data }