message.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. "gopkg"
  6. "lzyd-message-api/common"
  7. "lzyd-message-api/models"
  8. "sort"
  9. "time"
  10. )
  11. type MessageSql struct {
  12. Content string `json:"content" gorm:"column:content"`
  13. MsgType string `json:"msg_type" gorm:"column:msg_type"`
  14. FromUid string `json:"from_uid" gorm:"column:from_uid"`
  15. TargetId string `json:"target_id" gorm:"column:target_id"`
  16. TargetType string `json:"target_type" gorm:"column:target_type"`
  17. ReviewState int `json:"review_state" gorm:"column:review_state"`
  18. CreatedAt string `json:"created_at" gorm:"column:created_at"`
  19. MsgId string `json:"msg_id" gorm:"column:msg_id"`
  20. }
  21. type NoticeResp struct {
  22. Content string `json:"content" gorm:"column:content"`
  23. NoticeId string `json:"notice_id" gorm:"column:notice_id"`
  24. AppId string `json:"app_id" gorm:"column:app_id"`
  25. Title string `json:"title" gorm:"column:title"`
  26. UpdateAt string `json:"update_at" gorm:"column:update_at"`
  27. DeleteAt string `json:"delete_at" gorm:"column:delete_at"`
  28. NoticeSkipType int `json:"notice_skip_type" gorm:"column:notice_skip_type"`
  29. NoticeTypeId string `json:"notice_type_id" gorm:"column:notice_type_id"`
  30. LinkUrl string `json:"link_url" gorm:"column:link_url"`
  31. }
  32. type InteractRespList []models.InteractResp
  33. func (p * MessageSql) SaveMessage(db *gorm.DB, userCode string,roomId string) MessageSql {
  34. var roomUser models.RoomUser
  35. var messageData MessageSql
  36. now := time.Now()
  37. sql1 := `SELECT creator_id,b_creator_id from lzyd_live_chatroom WHERE room_id = ?`
  38. common.DB.Raw(sql1,roomId).Find(&roomUser)
  39. var userId string
  40. if roomUser.CreatorId == userCode {
  41. userId = roomUser.BCreatorId
  42. }else if roomUser.BCreatorId == userCode {
  43. userId = roomUser.CreatorId
  44. }
  45. sql2 := `SELECT leave_time from lzyd_live_chatroom_user WHERE user_id = ? AND id = (select max(id) from lzyd_live_chatroom_user)`
  46. common.DB.Raw(sql2,userId).Last(&roomUser)
  47. fmt.Println("leave_time",roomUser)
  48. sql3 := "UPDATE lzyd_live_chatroom SET updated_at = ? WHERE room_id = ?"
  49. err3 := db.Exec(sql3, now, roomId).Error
  50. if err3 != nil {
  51. }
  52. if len(roomUser.LeaveTime) <= 0 {
  53. fmt.Println("status=1",roomUser.LeaveTime)
  54. msgId := gopkg.GenUUID()
  55. sql := `INSERT INTO lzyd_live_message(msg_id,content, msg_type, from_uid, target_id,target_type, review_state,status) VALUES (?,?,?,?,?,?,?,?)`
  56. err := db.Exec(sql,msgId, p.Content, p.MsgType, p.FromUid, p.TargetId,p.TargetType,p.ReviewState,1).Error
  57. sql1 := `SELECT created_at,msg_id FROM lzyd_live_message where msg_id = ?`
  58. common.DB.Raw(sql1,msgId).Find(&messageData)
  59. fmt.Println(err)
  60. return messageData
  61. }else {
  62. fmt.Println("status=0",roomUser.LeaveTime)
  63. msgId := gopkg.GenUUID()
  64. sql := `INSERT INTO lzyd_live_message(msg_id,content, msg_type, from_uid, target_id,target_type, review_state,status) VALUES (?,?,?,?,?,?,?,?)`
  65. err := db.Exec(sql, msgId,p.Content, p.MsgType, p.FromUid, p.TargetId,p.TargetType,p.ReviewState,0).Error
  66. sql1 := `SELECT created_at,msg_id FROM lzyd_live_message where msg_id = ?`
  67. common.DB.Raw(sql1,msgId).Find(&messageData)
  68. fmt.Println(err)
  69. return messageData
  70. }
  71. }
  72. func QueryNoticeList(noticeCode string) []models.NoticeResp{
  73. var notice []models.NoticeResp
  74. 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 = ?`
  75. common.DB.Raw(sql,noticeCode).Find(&notice)
  76. //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error
  77. return notice
  78. }
  79. func QueryNoticeType(userCode string) models.NoticeTypeResp{
  80. var noticeType models.NoticeTypeResp
  81. var noticeList []models.NoticeResp
  82. var noticeReadList []models.NoticeReadResp
  83. //var newContent models.NewContent
  84. sqlType := `SELECT notice_type_id, notice_avatar, notice_name,update_at, delete_at FROM lzyd_notice_genre`
  85. common.DB.Raw(sqlType).Find(&noticeType)
  86. 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`
  87. common.DB.Raw(sqlList).Find(&noticeList)
  88. sqlReadList := `SELECT user_id, notice_id FROM lzyd_notice_user where user_id = ?`
  89. common.DB.Raw(sqlReadList,userCode).Find(&noticeReadList)
  90. sqlContent := `SELECT title,content,update_at FROM lzyd_notice WHERE is_delete = 0 AND notice_type_id = ? order by id desc limit 1`
  91. common.DB.Raw(sqlContent,"111").Find(&noticeType)
  92. for k := 0; k < len(noticeList); k++ {
  93. if noticeType.NoticeTypeId == noticeList[k].NoticeTypeId {
  94. //var temp = noticeList[k]
  95. //noticeType[i].List = append(noticeType[i].List, temp)
  96. for q := 0; q < len(noticeReadList); q++{
  97. //fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId)
  98. if noticeList[k].NoticeId == noticeReadList[q].NoticeId {
  99. fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId)
  100. noticeList[k].Status = 1
  101. var temp = noticeList[k]
  102. noticeType.List = append(noticeType.List, temp)
  103. //noticeType[i].list =
  104. }else {
  105. noticeList[k].Status = 0
  106. }
  107. }
  108. }
  109. }
  110. //noticeType[i].List = noticeList
  111. //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error
  112. return noticeType
  113. }
  114. func QueryNoticeType1(userCode string) []models.NoticeTypeResp{
  115. var noticeType []models.NoticeTypeResp
  116. var noticeList []models.NoticeResp
  117. var noticeReadList []models.NoticeReadResp
  118. sqlType := `SELECT notice_type_id, notice_avatar, notice_name,update_at, delete_at FROM lzyd_notice_genre`
  119. common.DB.Raw(sqlType).Find(&noticeType)
  120. sqlList := `SELECT content, notice_id, app_id, title,update_at, delete_at,notice_skip_type,notice_type_id,link_url FROM lzyd_notice`
  121. common.DB.Raw(sqlList).Find(&noticeList)
  122. sqlReadList := `SELECT user_id, notice_id FROM lzyd_notice_user where user_id = ?`
  123. common.DB.Raw(sqlReadList,userCode).Find(&noticeReadList)
  124. for i := 0; i < len(noticeType); i++ {
  125. for k := 0; k < len(noticeList); k++ {
  126. if noticeType[i].NoticeTypeId == noticeList[k].NoticeTypeId {
  127. //var temp = noticeList[k]
  128. //noticeType[i].List = append(noticeType[i].List, temp)
  129. for q := 0; q < len(noticeReadList); q++{
  130. //fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId)
  131. if noticeList[k].NoticeId == noticeReadList[q].NoticeId {
  132. fmt.Println(noticeList[k].NoticeId , noticeReadList[q].NoticeId)
  133. noticeList[k].Status = 1
  134. var temp = noticeList[k]
  135. noticeType[i].List = append(noticeType[i].List, temp)
  136. //noticeType[i].list =
  137. }else {
  138. noticeList[k].Status = 0
  139. }
  140. }
  141. }
  142. }
  143. //noticeType[i].List = noticeList
  144. }
  145. //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error
  146. return noticeType
  147. }
  148. func QueryInteract(userCode string) []models.InteractResp{
  149. fmt.Println("QueryInteract.inargs is:",userCode)
  150. var interact1 models.InteractRespList
  151. var interact2 models.InteractRespList
  152. var interact3 models.InteractRespList
  153. var interact4 models.InteractRespList
  154. var interact5 models.InteractRespList
  155. var interact6 models.InteractRespList
  156. var totalList = make(models.InteractRespList, 0)
  157. // 评论点赞
  158. 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
  159. FROM lzyd_comment_record u
  160. LEFT JOIN lzyd_user v ON u.user_id = v.user_id
  161. LEFT JOIN lzyd_video_comment x ON x.comment_id = u.comment_id
  162. LEFT JOIN lzyd_video_master n ON n.video_id = x.resource_id
  163. WHERE u.comment_user_id = ? && u.type = 0 && u.status = 1`
  164. common.DB.Raw(sqlType1,userCode).Find(&interact1)
  165. // 评论
  166. 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
  167. FROM lzyd_comment_record u
  168. LEFT JOIN lzyd_user v ON u.user_id = v.user_id
  169. LEFT JOIN lzyd_video_comment x ON x.comment_id = u.comment_id
  170. LEFT JOIN lzyd_video_master n ON n.video_id = x.resource_id
  171. WHERE u.comment_user_id = ? && u.type = 1 && u.comment_type = 1 && u.status = 1`
  172. common.DB.Raw(sqlType2,userCode).Find(&interact2)
  173. // 关注
  174. 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
  175. FROM lzyd_video_attention u
  176. LEFT JOIN lzyd_user v ON u.user_id = v.user_id
  177. WHERE u.attention_user_id = ? && u.status = 0 && is_show = 1`
  178. common.DB.Raw(sqlType3,userCode).Find(&interact3)
  179. // 评论回复
  180. 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
  181. FROM lzyd_comment_record u
  182. LEFT JOIN lzyd_user v ON u.user_id = v.user_id
  183. LEFT JOIN lzyd_video_reply_comment x ON x.comment_id = u.comment_id
  184. LEFT JOIN lzyd_video_comment n ON n.comment_id = x.comment_id
  185. LEFT JOIN lzyd_video_master k ON k.video_id = n.resource_id
  186. WHERE u.comment_user_id = ? && u.type = 1 && u.comment_type = 2 && u.status = 1`
  187. common.DB.Raw(sqlType4,userCode).Find(&interact4)
  188. // 视频点赞
  189. 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
  190. FROM lzyd_video_record u
  191. LEFT JOIN lzyd_user v ON u.user_id = v.user_id
  192. LEFT JOIN lzyd_video_master n ON n.video_id = u.video_id
  193. WHERE u.video_user_id = ? && u.status = 1 && u.is_delete = 0 && u.type = 0`
  194. common.DB.Raw(sqlType5,userCode).Find(&interact5)
  195. // 视频转发
  196. 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
  197. FROM lzyd_video_record u
  198. LEFT JOIN lzyd_user v ON u.user_id = v.user_id
  199. LEFT JOIN lzyd_video_master n ON n.video_id = u.video_id
  200. WHERE u.video_user_id = ? && u.status = 1 && u.is_delete = 0 && u.type = 1`
  201. common.DB.Raw(sqlType6,userCode).Find(&interact6)
  202. //fmt.Println(interact1,interact2)
  203. // 1评论点赞// 2评论// 3关注// 4评论回复// 5视频点赞// 6视频转发
  204. //totalList = append(totalList, interact1...)
  205. for _, resp := range interact1 {
  206. resp.IntType = "1"
  207. totalList = append(totalList, resp)
  208. }
  209. for _, resp := range interact2 {
  210. resp.IntType = "2"
  211. totalList = append(totalList, resp)
  212. }
  213. //totalList = append(totalList, interact3...)
  214. for _, resp := range interact3 {
  215. resp.IntType = "3"
  216. totalList = append(totalList, resp)
  217. }
  218. for _, resp := range interact4 {
  219. resp.IntType = "4"
  220. totalList = append(totalList, resp)
  221. }
  222. for _, resp := range interact5 {
  223. resp.IntType = "5"
  224. totalList = append(totalList, resp)
  225. }
  226. for _, resp := range interact6 {
  227. resp.IntType = "6"
  228. totalList = append(totalList, resp)
  229. }
  230. //for i:=0;i<len(totalList);i++ {
  231. // timeRes := strings.Split(totalList[i].UpdateAt,"")
  232. // timeRes = strings.Split(timeRes[0],"T")
  233. // var timeRes3 string
  234. // timeRes3 = timeRes[0]+" "+timeRes[1]
  235. // totalList[i].UpdateAt = timeRes3
  236. //}
  237. //interact := append(append(interact1,interact2...),interact3...)
  238. //for i := 0; i < len(interact2); i++ {
  239. //
  240. //}
  241. //err := db.Exec(sql, notice.Content, p.NoticeId, p.AppId, p.Title,p.UpdateAt,p.DeleteAt).Error
  242. sort.Sort(InteractRespList(totalList))
  243. fmt.Println(totalList)
  244. return totalList
  245. }
  246. func UpdateInteract(id string,intType string) bool{
  247. var (
  248. upDateInfoSql string
  249. upDateInfoErr error
  250. )
  251. //fmt.Println(p)
  252. if intType == "1" || intType == "2" || intType == "4" {
  253. upDateInfoSql = "UPDATE lzyd_comment_record SET status = 0 WHERE id = ?"
  254. upDateInfoErr = common.DB.Exec(upDateInfoSql, id).Error
  255. }else if intType == "3" {
  256. upDateInfoSql = "UPDATE lzyd_video_attention SET is_show = 0 WHERE id = ?"
  257. upDateInfoErr = common.DB.Exec(upDateInfoSql, id).Error
  258. }else if intType == "5" || intType == "6" {
  259. upDateInfoSql = "UPDATE lzyd_video_record SET status = 0 WHERE id = ?"
  260. upDateInfoErr = common.DB.Exec(upDateInfoSql, id).Error
  261. }
  262. if upDateInfoErr != nil{
  263. return false
  264. }
  265. return true
  266. }
  267. func QueryReadNotice(noticeCode string,userCode string,appCode string) models.NoticeReadResp{
  268. var noticeRead models.NoticeReadResp
  269. sql := `SELECT notice_id, app_id, user_id FROM lzyd_notice_user where notice_id = ? AND user_id = ? AND app_id = ?`
  270. common.DB.Raw(sql,noticeCode,userCode,appCode).Find(&noticeRead)
  271. fmt.Println(len(noticeRead.NoticeId))
  272. if len(noticeRead.NoticeId) == 0 {
  273. sqlRead := "INSERT INTO lzyd_notice_user(user_id, app_id, notice_id) VALUES (?,?,?)"
  274. errRead := common.DB.Exec(sqlRead,userCode,appCode,noticeCode).Error
  275. fmt.Println(errRead)
  276. //insertReadSql = db.Exec(insertReadSql, cus.UserId, cus.UserName, cus.UserAvatar, cus.MobilePhone, cus.Gender, cus.Birthday, now, now, cus.AppId).Error
  277. }else {
  278. return noticeRead
  279. }
  280. return noticeRead
  281. }
  282. func (list InteractRespList) Len() int {
  283. return len(list)
  284. }
  285. func (list InteractRespList) Less(i, j int) bool {
  286. return list[i].UpdateAt > list[j].UpdateAt
  287. }
  288. func (list InteractRespList) Swap(i, j int) {
  289. list[i], list[j] = list[j], list[i]
  290. }
  291. func UploadLiveUser (roomId string,creatorId string,bCreatorId string ) bool {
  292. var (
  293. upDateInfoSql1 string
  294. upDateInfoErr1 error
  295. upDateInfoSql2 string
  296. upDateInfoErr2 error
  297. )
  298. //fmt.Println(p)
  299. upDateInfoSql1 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?"
  300. upDateInfoErr1 = common.DB.Exec(upDateInfoSql1, creatorId, roomId).Error
  301. upDateInfoSql2 = "UPDATE lzyd_live_user SET status = 1 WHERE user_id = ? && room_id = ?"
  302. upDateInfoErr2 = common.DB.Exec(upDateInfoSql2, bCreatorId, roomId).Error
  303. if upDateInfoErr1 != nil && upDateInfoErr2 != nil {
  304. return false
  305. }
  306. return true
  307. }
  308. func UserRelation(creatorId string,bCreatorId string,userId string) bool{
  309. var noticeRead1 models.UserRelationResp
  310. var noticeRead2 models.UserRelationResp
  311. //var noticeRead3 models.UserRelationResp
  312. //var noticeRead4 models.UserRelationResp
  313. // sql1与sql2 查询用户之前互相关注
  314. sql1 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0`
  315. common.DB.Raw(sql1,creatorId,bCreatorId).Find(&noticeRead1)
  316. sql2 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0`
  317. common.DB.Raw(sql2,bCreatorId,creatorId).Find(&noticeRead2)
  318. // sql3与sql4 查询用户之前互相拉黑
  319. //sql3 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2`
  320. //common.DB.Raw(sql3,bCreatorId,creatorId).Find(&noticeRead3)
  321. //sql4 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2`
  322. //common.DB.Raw(sql4,bCreatorId,creatorId).Find(&noticeRead4)
  323. if noticeRead1.AttentionUserId == bCreatorId && noticeRead2.AttentionUserId == creatorId {
  324. return true
  325. }
  326. return false
  327. }
  328. func UserBlack(creatorId string,bCreatorId string) bool{
  329. var noticeRead3 models.UserRelationResp
  330. var noticeRead4 models.UserRelationResp
  331. // sql1与sql2 查询用户之前互相关注
  332. //sql1 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0`
  333. //common.DB.Raw(sql1,creatorId,bCreatorId).Find(&noticeRead1)
  334. //sql2 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0`
  335. //common.DB.Raw(sql2,bCreatorId,creatorId).Find(&noticeRead2)
  336. // sql3与sql4 查询用户之前互相拉黑
  337. sql3 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2`
  338. common.DB.Raw(sql3,bCreatorId,creatorId).Find(&noticeRead3)
  339. sql4 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 2`
  340. common.DB.Raw(sql4,bCreatorId,creatorId).Find(&noticeRead4)
  341. if noticeRead3.AttentionUserId == bCreatorId || noticeRead4.AttentionUserId == creatorId {
  342. return true
  343. }
  344. return false
  345. }
  346. func UserSendNum(roomId string,userId string) int{
  347. var messageRead []models.UserSendResp
  348. sql1 := `SELECT target_id FROM lzyd_live_message where from_uid = ? AND target_id=?`
  349. common.DB.Raw(sql1,userId,roomId).Find(&messageRead)
  350. return len(messageRead)
  351. }