面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

189 regels
4.7 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "xorm.io/xorm"
  6. )
  7. //UserNoticeByReceiverByTime is 获取改接收者的
  8. func UserNoticeByReceiverByTime(Db *xorm.Engine, uid interface{}, sort string) (*[]model.UserNotice, error) {
  9. switch sort {
  10. case "desc":
  11. // 降序
  12. var m []model.UserNotice
  13. err := Db.Where("receiver = ?", uid).Desc("create_at").Find(&m)
  14. if err != nil {
  15. return nil, logx.Warn(err)
  16. }
  17. return &m, nil
  18. case "asc":
  19. //升序
  20. var m []model.UserNotice
  21. err := Db.Where("receiver = ?", uid).Asc("create_at").Find(&m)
  22. if err != nil {
  23. return nil, logx.Warn(err)
  24. }
  25. return &m, nil
  26. default:
  27. //默认
  28. var m []model.UserNotice
  29. err := Db.Where("receiver = ?", uid).Find(&m)
  30. if err != nil {
  31. return nil, logx.Warn(err)
  32. }
  33. return &m, nil
  34. }
  35. }
  36. //UserNoticeByReceiverByTimeByType is 获取改接收者的By Type
  37. func UserNoticeByReceiverByTimeByType(Db *xorm.Engine, uid, t interface{}, sort string, limit, start int) (*[]model.UserNotice, error) {
  38. switch sort {
  39. case "desc":
  40. // 降序
  41. var m []model.UserNotice
  42. if limit == 0 && start == 0 {
  43. err := Db.Where("receiver = ? AND type = ?", uid, t).Desc("create_at").Find(&m)
  44. if err != nil {
  45. return nil, logx.Warn(err)
  46. }
  47. return &m, nil
  48. }
  49. err := Db.Where("receiver = ? AND type = ?", uid, t).Desc("create_at").Limit(limit, start).Find(&m)
  50. if err != nil {
  51. return nil, logx.Warn(err)
  52. }
  53. return &m, nil
  54. case "asc":
  55. //升序
  56. var m []model.UserNotice
  57. if limit == 0 && start == 0 {
  58. err := Db.Where("receiver = ? AND type = ?", uid, t).Asc("create_at").Find(&m)
  59. if err != nil {
  60. return nil, logx.Warn(err)
  61. }
  62. return &m, nil
  63. }
  64. err := Db.Where("receiver = ? AND type = ?", uid, t).Asc("create_at").Limit(limit, start).Find(&m)
  65. if err != nil {
  66. return nil, logx.Warn(err)
  67. }
  68. return &m, nil
  69. default:
  70. //默认
  71. var m []model.UserNotice
  72. if limit == 0 && start == 0 {
  73. err := Db.Where("receiver = ? AND type = ?", uid).Find(&m)
  74. if err != nil {
  75. return nil, logx.Warn(err)
  76. }
  77. return &m, nil
  78. }
  79. err := Db.Where("receiver = ? AND type = ?", uid, t).Limit(limit, start).Find(&m)
  80. if err != nil {
  81. return nil, logx.Warn(err)
  82. }
  83. return &m, nil
  84. }
  85. }
  86. //GetUserNoticeByReceiverByTimeByType is 获取改接收者的By Type
  87. func GetUserNoticeByReceiverByTimeByType(Db *xorm.Engine, uid, t interface{}, sort string) (*model.UserNotice, error) {
  88. var (
  89. has bool
  90. err error
  91. )
  92. switch sort {
  93. case "desc":
  94. // 降序
  95. var m model.UserNotice
  96. if t == 0 || t == 1 {
  97. // fmt.Println(t)
  98. has, err = Db.Where("receiver = '0' AND type = ?", t).Desc("create_at").Get(&m)
  99. } else {
  100. has, err = Db.Where("receiver = ? AND type = ?", uid, t).Desc("create_at").Get(&m)
  101. }
  102. if !has {
  103. return &m, logx.Warn("Not found")
  104. }
  105. if err != nil {
  106. return nil, logx.Warn(err)
  107. }
  108. return &m, nil
  109. case "asc":
  110. //升序
  111. var m model.UserNotice
  112. if t == 0 || t == 1 {
  113. has, err = Db.Where("receiver = '0' AND type = ?", t).Asc("create_at").Get(&m)
  114. } else {
  115. has, err = Db.Where("receiver = ? AND type = ?", uid, t).Asc("create_at").Get(&m)
  116. }
  117. if !has {
  118. return &m, logx.Warn("Not found")
  119. }
  120. if err != nil {
  121. return nil, logx.Warn(err)
  122. }
  123. return &m, nil
  124. default:
  125. //默认
  126. var m model.UserNotice
  127. if t == 0 || t == 1 {
  128. // fmt.Println(t)
  129. has, err = Db.Where("receiver = '0' AND type = ?", t).Get(&m)
  130. } else {
  131. has, err = Db.Where("receiver = ? AND type = ?", uid, t).Get(&m)
  132. }
  133. if !has {
  134. return &m, logx.Warn("Not found")
  135. }
  136. if err != nil {
  137. return nil, logx.Warn(err)
  138. }
  139. return &m, nil
  140. }
  141. }
  142. //GetUserUnReadCountByType is 获取
  143. func GetUserUnReadCountByType(Db *xorm.Engine, id, t interface{}) (int64, error) {
  144. var (
  145. count int64
  146. err error
  147. )
  148. n := new(model.UserNotice)
  149. if t == 0 || t == 1 {
  150. count, err = Db.Where("receiver = '0' AND type = ? AND status = '0'", t).Count(n)
  151. } else {
  152. count, err = Db.Where("receiver = ? AND type = ? AND status = '0'", id, t).Count(n)
  153. }
  154. if err != nil {
  155. return 0, nil
  156. }
  157. return count, nil
  158. }
  159. //BatchUpdateUserNoticeRead is 批量更新某种类型的通知
  160. func BatchUpdateUserNoticeRead(Db *xorm.Engine, uid, t interface{}) error {
  161. sql := "update user_notice set status = ? where receiver = ? and type = ?"
  162. _, err := Db.Exec(sql, 1, uid, t)
  163. if err != nil {
  164. return logx.Warn(err)
  165. }
  166. return nil
  167. }
  168. //UserNoticeInsertOne 插入一条通知数据
  169. func UserNoticeInsertOne(Db *xorm.Engine, m *model.UserNotice) error {
  170. _, err := Db.InsertOne(m)
  171. if err != nil {
  172. return logx.Warn(err)
  173. }
  174. return nil
  175. }
  176. //UserNoticeDeleteByUserDelete is 删除用户通知
  177. func UserNoticeDeleteByUserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  178. return Db.Where("receiver = ? OR sender = ?", uid, uid).Delete(model.UserNotice{})
  179. }