package services import ( "fmt" "github.com/jinzhu/gorm" "gopkg" "lzyd-message-api/common" "lzyd-message-api/models" "sort" "time" ) type MessageSql struct { Content string `json:"content" gorm:"column:content"` MsgType string `json:"msg_type" gorm:"column:msg_type"` FromUid string `json:"from_uid" gorm:"column:from_uid"` TargetId string `json:"target_id" gorm:"column:target_id"` TargetType string `json:"target_type" gorm:"column:target_type"` ReviewState int `json:"review_state" gorm:"column:review_state"` CreatedAt string `json:"created_at" gorm:"column:created_at"` MsgId string `json:"msg_id" gorm:"column:msg_id"` } type NoticeResp struct { Content string `json:"content" gorm:"column:content"` NoticeId string `json:"notice_id" gorm:"column:notice_id"` AppId string `json:"app_id" gorm:"column:app_id"` Title string `json:"title" gorm:"column:title"` UpdateAt string `json:"update_at" gorm:"column:update_at"` DeleteAt string `json:"delete_at" gorm:"column:delete_at"` NoticeSkipType int `json:"notice_skip_type" gorm:"column:notice_skip_type"` NoticeTypeId string `json:"notice_type_id" gorm:"column:notice_type_id"` LinkUrl string `json:"link_url" gorm:"column:link_url"` } type InteractRespList []models.InteractResp func (p * MessageSql) SaveMessage(db *gorm.DB, userCode string,roomId string) MessageSql { var roomUser models.RoomUser var messageData MessageSql now := time.Now() sql1 := `SELECT creator_id,b_creator_id from lzyd_live_chatroom WHERE room_id = ?` common.DB.Raw(sql1,roomId).Find(&roomUser) var userId string if roomUser.CreatorId == userCode { userId = roomUser.BCreatorId }else if roomUser.BCreatorId == userCode { userId = roomUser.CreatorId } sql2 := `SELECT leave_time from lzyd_live_chatroom_user WHERE user_id = ? AND id = (select max(id) from lzyd_live_chatroom_user)` common.DB.Raw(sql2,userId).Last(&roomUser) fmt.Println("leave_time",roomUser) sql3 := "UPDATE lzyd_live_chatroom SET updated_at = ? WHERE room_id = ?" err3 := db.Exec(sql3, now, roomId).Error if err3 != nil { } if len(roomUser.LeaveTime) <= 0 { fmt.Println("status=1",roomUser.LeaveTime) msgId := gopkg.GenUUID() sql := `INSERT INTO lzyd_live_message(msg_id,content, msg_type, from_uid, target_id,target_type, review_state,status) VALUES (?,?,?,?,?,?,?,?)` err := db.Exec(sql,msgId, p.Content, p.MsgType, p.FromUid, p.TargetId,p.TargetType,p.ReviewState,1).Error sql1 := `SELECT created_at,msg_id FROM lzyd_live_message where msg_id = ?` common.DB.Raw(sql1,msgId).Find(&messageData) fmt.Println(err) return messageData }else { fmt.Println("status=0",roomUser.LeaveTime) msgId := gopkg.GenUUID() sql := `INSERT INTO lzyd_live_message(msg_id,content, msg_type, from_uid, target_id,target_type, review_state,status) VALUES (?,?,?,?,?,?,?,?)` err := db.Exec(sql, msgId,p.Content, p.MsgType, p.FromUid, p.TargetId,p.TargetType,p.ReviewState,0).Error sql1 := `SELECT created_at,msg_id FROM lzyd_live_message where msg_id = ?` common.DB.Raw(sql1,msgId).Find(&messageData) fmt.Println(err) return messageData } } func QueryNoticeList(noticeCode string) []models.NoticeResp{ var notice []models.NoticeResp sql := `SELECT content, notice_id, app_id, title,update_at, delete_at,notice_skip_type,notice_type_id,link_url FROM lzyd_notice where notice_type_id = ?` common.DB.Raw(sql,noticeCode).Find(¬ice) //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error return notice } func QueryNoticeType(userCode string) models.NoticeTypeResp{ var noticeType models.NoticeTypeResp var noticeList []models.NoticeResp var noticeReadList []models.NoticeReadResp //var newContent models.NewContent sqlType := `SELECT notice_type_id, notice_avatar, notice_name,update_at, delete_at FROM lzyd_notice_genre` common.DB.Raw(sqlType).Find(¬iceType) sqlList := `SELECT content, notice_id, app_id, title,update_at,notice_skip_type,notice_type_id,link_url FROM lzyd_notice where is_delete = 0` common.DB.Raw(sqlList).Find(¬iceList) sqlReadList := `SELECT user_id, notice_id FROM lzyd_notice_user where user_id = ?` common.DB.Raw(sqlReadList,userCode).Find(¬iceReadList) sqlContent := `SELECT title,content,update_at FROM lzyd_notice WHERE is_delete = 0 AND notice_type_id = ? order by id desc limit 1` common.DB.Raw(sqlContent,"111").Find(¬iceType) for k := 0; k < len(noticeList); k++ { if noticeType.NoticeTypeId == noticeList[k].NoticeTypeId { //var temp = noticeList[k] //noticeType[i].List = append(noticeType[i].List, temp) for q := 0; q < len(noticeReadList); q++{ //fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId) if noticeList[k].NoticeId == noticeReadList[q].NoticeId { fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId) noticeList[k].Status = 1 var temp = noticeList[k] noticeType.List = append(noticeType.List, temp) //noticeType[i].list = }else { noticeList[k].Status = 0 } } } } //noticeType[i].List = noticeList //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error return noticeType } func QueryNoticeType1(userCode string) []models.NoticeTypeResp{ var noticeType []models.NoticeTypeResp var noticeList []models.NoticeResp var noticeReadList []models.NoticeReadResp sqlType := `SELECT notice_type_id, notice_avatar, notice_name,update_at, delete_at FROM lzyd_notice_genre` common.DB.Raw(sqlType).Find(¬iceType) sqlList := `SELECT content, notice_id, app_id, title,update_at, delete_at,notice_skip_type,notice_type_id,link_url FROM lzyd_notice` common.DB.Raw(sqlList).Find(¬iceList) sqlReadList := `SELECT user_id, notice_id FROM lzyd_notice_user where user_id = ?` common.DB.Raw(sqlReadList,userCode).Find(¬iceReadList) for i := 0; i < len(noticeType); i++ { for k := 0; k < len(noticeList); k++ { if noticeType[i].NoticeTypeId == noticeList[k].NoticeTypeId { //var temp = noticeList[k] //noticeType[i].List = append(noticeType[i].List, temp) for q := 0; q < len(noticeReadList); q++{ //fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId) if noticeList[k].NoticeId == noticeReadList[q].NoticeId { fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId) noticeList[k].Status = 1 var temp = noticeList[k] noticeType[i].List = append(noticeType[i].List, temp) //noticeType[i].list = }else { noticeList[k].Status = 0 } } } } //noticeType[i].List = noticeList } //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error return noticeType } func QueryInteract(userCode string) []models.InteractResp{ fmt.Println("QueryInteract.inargs is:",userCode) var interact1 models.InteractRespList var interact2 models.InteractRespList var interact3 models.InteractRespList var interact4 models.InteractRespList var interact5 models.InteractRespList var interact6 models.InteractRespList var totalList = make(models.InteractRespList, 0) // 评论点赞 sqlType1 := `SELECT u.id,u.comment_user_id,u.comment_id, v.user_id , v.username, v.user_avatar, x.content,u.comment_id,DATE_FORMAT(x.updated_at, '%Y-%m-%d %H:%i:%S' ) as updated_at,n.cover,n.video_id FROM lzyd_comment_record u LEFT JOIN lzyd_user v ON u.user_id = v.user_id LEFT JOIN lzyd_video_comment x ON x.comment_id = u.comment_id LEFT JOIN lzyd_video_master n ON n.video_id = x.resource_id WHERE u.comment_user_id = ? && u.type = 0 && u.status = 1` common.DB.Raw(sqlType1,userCode).Find(&interact1) // 评论 sqlType2 := `SELECT u.id,u.comment_user_id,u.comment_id, v.user_id , v.username, v.user_avatar, x.content,u.comment_id,DATE_FORMAT(x.updated_at, '%Y-%m-%d %H:%i:%S' ) as updated_at,n.cover,n.video_id FROM lzyd_comment_record u LEFT JOIN lzyd_user v ON u.user_id = v.user_id LEFT JOIN lzyd_video_comment x ON x.comment_id = u.comment_id LEFT JOIN lzyd_video_master n ON n.video_id = x.resource_id WHERE u.comment_user_id = ? && u.type = 1 && u.comment_type = 1 && u.status = 1` common.DB.Raw(sqlType2,userCode).Find(&interact2) // 关注 sqlType3 := `SELECT u.id,u.user_id, v.user_avatar, v.username,DATE_FORMAT(u.updated_at, '%Y-%m-%d %H-%i-%S' ) as updated_at,u.status FROM lzyd_video_attention u LEFT JOIN lzyd_user v ON u.user_id = v.user_id WHERE u.attention_user_id = ? && u.status = 0 && is_show = 1` common.DB.Raw(sqlType3,userCode).Find(&interact3) // 评论回复 sqlType4 := `SELECT u.id,u.comment_user_id,u.comment_id, v.user_id , v.username, v.user_avatar, x.content,u.comment_id,DATE_FORMAT(x.updated_at, '%Y-%m-%d %H:%i:%S' ) as updated_at,k.video_id,k.cover FROM lzyd_comment_record u LEFT JOIN lzyd_user v ON u.user_id = v.user_id LEFT JOIN lzyd_video_reply_comment x ON x.comment_id = u.comment_id LEFT JOIN lzyd_video_comment n ON n.comment_id = x.comment_id LEFT JOIN lzyd_video_master k ON k.video_id = n.resource_id WHERE u.comment_user_id = ? && u.type = 1 && u.comment_type = 2 && u.status = 1` common.DB.Raw(sqlType4,userCode).Find(&interact4) // 视频点赞 sqlType5 := `SELECT u.id,u.user_id, v.user_avatar, v.username,DATE_FORMAT(u.updated_at, '%Y-%m-%d %H:%i:%S' ) as updated_at,u.video_id,n.cover FROM lzyd_video_record u LEFT JOIN lzyd_user v ON u.user_id = v.user_id LEFT JOIN lzyd_video_master n ON n.video_id = u.video_id WHERE u.video_user_id = ? && u.status = 1 && u.is_delete = 0 && u.type = 0` common.DB.Raw(sqlType5,userCode).Find(&interact5) // 视频转发 sqlType6 := `SELECT u.id,u.user_id, v.user_avatar, v.username,DATE_FORMAT(u.updated_at,'%Y-%m-%d %H:%i:%S' ) as updated_at,u.video_id,n.cover FROM lzyd_video_record u LEFT JOIN lzyd_user v ON u.user_id = v.user_id LEFT JOIN lzyd_video_master n ON n.video_id = u.video_id WHERE u.video_user_id = ? && u.status = 1 && u.is_delete = 0 && u.type = 1` common.DB.Raw(sqlType6,userCode).Find(&interact6) //fmt.Println(interact1,interact2) // 1评论点赞// 2评论// 3关注// 4评论回复// 5视频点赞// 6视频转发 //totalList = append(totalList, interact1...) for _, resp := range interact1 { resp.IntType = "1" totalList = append(totalList, resp) } for _, resp := range interact2 { resp.IntType = "2" totalList = append(totalList, resp) } //totalList = append(totalList, interact3...) for _, resp := range interact3 { resp.IntType = "3" totalList = append(totalList, resp) } for _, resp := range interact4 { resp.IntType = "4" totalList = append(totalList, resp) } for _, resp := range interact5 { resp.IntType = "5" totalList = append(totalList, resp) } for _, resp := range interact6 { resp.IntType = "6" totalList = append(totalList, resp) } //for i:=0;i list[j].UpdateAt } func (list InteractRespList) Swap(i, j int) { list[i], list[j] = list[j], list[i] } func UploadLiveUser (roomId string,creatorId string,bCreatorId string ) bool { 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 = common.DB.Exec(upDateInfoSql1, creatorId, roomId).Error upDateInfoSql2 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?" upDateInfoErr2 = common.DB.Exec(upDateInfoSql2, bCreatorId, roomId).Error if upDateInfoErr1 != nil && upDateInfoErr2 != nil { return false } return true } func UserRelation(creatorId string,bCreatorId string,userId string) bool{ var noticeRead1 models.UserRelationResp var noticeRead2 models.UserRelationResp //var noticeRead3 models.UserRelationResp //var noticeRead4 models.UserRelationResp // sql1与sql2 查询用户之前互相关注 sql1 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0` common.DB.Raw(sql1,creatorId,bCreatorId).Find(¬iceRead1) sql2 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0` common.DB.Raw(sql2,bCreatorId,creatorId).Find(¬iceRead2) // sql3与sql4 查询用户之前互相拉黑 //sql3 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2` //common.DB.Raw(sql3,bCreatorId,creatorId).Find(¬iceRead3) //sql4 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2` //common.DB.Raw(sql4,bCreatorId,creatorId).Find(¬iceRead4) if noticeRead1.AttentionUserId == bCreatorId && noticeRead2.AttentionUserId == creatorId { return true } return false } func UserBlack(creatorId string,bCreatorId string) bool{ var noticeRead3 models.UserRelationResp var noticeRead4 models.UserRelationResp // sql1与sql2 查询用户之前互相关注 //sql1 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0` //common.DB.Raw(sql1,creatorId,bCreatorId).Find(¬iceRead1) //sql2 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0` //common.DB.Raw(sql2,bCreatorId,creatorId).Find(¬iceRead2) // sql3与sql4 查询用户之前互相拉黑 sql3 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2` common.DB.Raw(sql3,bCreatorId,creatorId).Find(¬iceRead3) sql4 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2` common.DB.Raw(sql4,bCreatorId,creatorId).Find(¬iceRead4) if noticeRead3.AttentionUserId == bCreatorId || noticeRead4.AttentionUserId == creatorId { return true } return false } func UserSendNum(roomId string,userId string) int{ var messageRead []models.UserSendResp sql1 := `SELECT target_id FROM lzyd_live_message where from_uid = ? AND target_id=?` common.DB.Raw(sql1,userId,roomId).Find(&messageRead) return len(messageRead) }