|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
-
- "xorm.io/xorm"
- )
-
- //UserNoticeByReceiverByTime is 获取改接收者的
- func UserNoticeByReceiverByTime(Db *xorm.Engine, uid interface{}, sort string) (*[]model.UserNotice, error) {
- switch sort {
- case "desc":
- // 降序
- var m []model.UserNotice
- err := Db.Where("receiver = ?", uid).Desc("create_at").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- case "asc":
- //升序
- var m []model.UserNotice
- err := Db.Where("receiver = ?", uid).Asc("create_at").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- default:
- //默认
- var m []model.UserNotice
- err := Db.Where("receiver = ?", uid).Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- }
-
- //UserNoticeByReceiverByTimeByType is 获取改接收者的By Type
- func UserNoticeByReceiverByTimeByType(Db *xorm.Engine, uid, t interface{}, sort string, limit, start int) (*[]model.UserNotice, error) {
- switch sort {
- case "desc":
- // 降序
- var m []model.UserNotice
- if limit == 0 && start == 0 {
- err := Db.Where("receiver = ? AND type = ?", uid, t).Desc("create_at").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- err := Db.Where("receiver = ? AND type = ?", uid, t).Desc("create_at").Limit(limit, start).Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- case "asc":
- //升序
- var m []model.UserNotice
- if limit == 0 && start == 0 {
- err := Db.Where("receiver = ? AND type = ?", uid, t).Asc("create_at").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- err := Db.Where("receiver = ? AND type = ?", uid, t).Asc("create_at").Limit(limit, start).Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- default:
- //默认
- var m []model.UserNotice
- if limit == 0 && start == 0 {
- err := Db.Where("receiver = ? AND type = ?", uid).Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- err := Db.Where("receiver = ? AND type = ?", uid, t).Limit(limit, start).Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- }
-
- //GetUserNoticeByReceiverByTimeByType is 获取改接收者的By Type
- func GetUserNoticeByReceiverByTimeByType(Db *xorm.Engine, uid, t interface{}, sort string) (*model.UserNotice, error) {
- var (
- has bool
- err error
- )
- switch sort {
- case "desc":
- // 降序
- var m model.UserNotice
- if t == 0 || t == 1 {
- // fmt.Println(t)
- has, err = Db.Where("receiver = '0' AND type = ?", t).Desc("create_at").Get(&m)
- } else {
- has, err = Db.Where("receiver = ? AND type = ?", uid, t).Desc("create_at").Get(&m)
- }
- if !has {
- return &m, logx.Warn("Not found")
- }
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- case "asc":
- //升序
- var m model.UserNotice
- if t == 0 || t == 1 {
- has, err = Db.Where("receiver = '0' AND type = ?", t).Asc("create_at").Get(&m)
- } else {
- has, err = Db.Where("receiver = ? AND type = ?", uid, t).Asc("create_at").Get(&m)
- }
- if !has {
- return &m, logx.Warn("Not found")
- }
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- default:
- //默认
- var m model.UserNotice
- if t == 0 || t == 1 {
- // fmt.Println(t)
- has, err = Db.Where("receiver = '0' AND type = ?", t).Get(&m)
- } else {
- has, err = Db.Where("receiver = ? AND type = ?", uid, t).Get(&m)
- }
- if !has {
- return &m, logx.Warn("Not found")
- }
- if err != nil {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
- }
-
- //GetUserUnReadCountByType is 获取
- func GetUserUnReadCountByType(Db *xorm.Engine, id, t interface{}) (int64, error) {
- var (
- count int64
- err error
- )
- n := new(model.UserNotice)
- if t == 0 || t == 1 {
- count, err = Db.Where("receiver = '0' AND type = ? AND status = '0'", t).Count(n)
- } else {
- count, err = Db.Where("receiver = ? AND type = ? AND status = '0'", id, t).Count(n)
- }
- if err != nil {
- return 0, nil
- }
- return count, nil
- }
-
- //BatchUpdateUserNoticeRead is 批量更新某种类型的通知
- func BatchUpdateUserNoticeRead(Db *xorm.Engine, uid, t interface{}) error {
- sql := "update user_notice set status = ? where receiver = ? and type = ?"
- _, err := Db.Exec(sql, 1, uid, t)
- if err != nil {
- return logx.Warn(err)
- }
- return nil
- }
-
- //UserNoticeInsertOne 插入一条通知数据
- func UserNoticeInsertOne(Db *xorm.Engine, m *model.UserNotice) error {
- _, err := Db.InsertOne(m)
- if err != nil {
- return logx.Warn(err)
- }
- return nil
- }
-
- //UserNoticeDeleteByUserDelete is 删除用户通知
- func UserNoticeDeleteByUserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
- return Db.Where("receiver = ? OR sender = ?", uid, uid).Delete(model.UserNotice{})
- }
|