query.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package yb_message
  2. import (
  3. "hongze/hongze_yb/global"
  4. "hongze/hongze_yb/utils"
  5. )
  6. // GetUnreadByUserId 获取用户的未读数
  7. func GetUnreadByUserId(UserId uint64) (total int64, err error) {
  8. err = global.DEFAULT_MYSQL.Model(YbMessage{}).
  9. Where("user_id = ? and enabled = 1 and is_read=0", UserId).
  10. Count(&total).Error
  11. return
  12. }
  13. // GetListByType 根据消息类型查询用户消息列表
  14. func GetListByType(userId uint64, msgType int, offset, limit int) (list []*YbMessage, err error) {
  15. var pars []interface{}
  16. pars = append(pars, userId)
  17. sql := ` SELECT * FROM yb_message
  18. WHERE user_id = ? and enabled = 1 `
  19. if msgType > 0 {
  20. sql += " and type= ?"
  21. pars = append(pars, msgType)
  22. }
  23. sql += " GROUP BY create_time desc, msg_id desc LIMIT ?,? "
  24. pars = append(pars, offset, limit)
  25. err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&list).Error
  26. return
  27. }
  28. // GetListTotalByType 根据消息类型查询用户消息列表总数
  29. func GetListTotalByType(userId uint64, msgType int) (total int64, err error) {
  30. var pars []interface{}
  31. pars = append(pars, userId)
  32. sql := ` SELECT count(*) FROM yb_message
  33. WHERE user_id = ? and enabled = 1 `
  34. if msgType > 0 {
  35. sql += " and type= ?"
  36. pars = append(pars, msgType)
  37. }
  38. err = global.DEFAULT_MYSQL.Raw(sql, pars...).Count(&total).Error
  39. return
  40. }
  41. // GetMsgByMsgId 根据消息ID查询消息详情
  42. func GetMsgByMsgId(userId, msgId uint64) (item *YbMessage, err error) {
  43. err = global.DEFAULT_MYSQL.Model(YbMessage{}).
  44. Where("user_id = ? and msg_id = ?", userId, msgId).
  45. First(&item).Error
  46. if err == utils.ErrNoRow {
  47. err = nil
  48. }
  49. return
  50. }