github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/models/notification.go (about) 1 package models 2 3 import ( 4 "errors" 5 "fmt" 6 "time" 7 ) 8 9 type Notification struct { 10 Id int64 11 Uid int64 `xorm:"index"` 12 Tid int64 `xorm:"index"` 13 Rid int64 `xorm:"index"` 14 Ctype int64 `xorm:"index"` //0普通通知 1通知作者 -1忽略 15 Subject string `xorm:"text"` 16 Reply string `xorm:"text"` 17 Author string `xorm:"index"` 18 Avatar string `xorm:"index"` //200x200 19 AvatarLarge string `xorm:"index"` //100x100 20 AvatarMedium string `xorm:"index"` //48x48 21 AvatarSmall string `xorm:"index"` //32x32 22 Created int64 `xorm:"created index"` 23 } 24 25 type Notificationjuser struct { 26 Notification `xorm:"extends"` 27 User `xorm:"extends"` 28 } 29 30 type Notificationjtopic struct { 31 Notification `xorm:"extends"` 32 Topic `xorm:"extends"` 33 } 34 35 func GetNotificationsByUid(uid int64, ctype int64, offset int, limit int, field string) *[]Notification { 36 rp := &[]Notification{} 37 if field == "asc" { 38 39 if uid == 0 { 40 if ctype != 0 { 41 Engine.Table("notification").Where("notification.ctype=?", ctype).Limit(limit, offset).Asc("notification.id").Find(rp) 42 } else { 43 Engine.Table("notification").Limit(limit, offset).Asc("notification.id").Find(rp) 44 } 45 46 } else { 47 48 if ctype == 0 { 49 Engine.Table("notification").Where("notification.uid=?", uid).Limit(limit, offset).Asc("notification.id").Find(rp) 50 51 } else { 52 53 Engine.Table("notification").Where("notification.ctype=? and notification.uid=?", ctype, uid).Limit(limit, offset).Asc("notification.id").Find(rp) 54 } 55 } 56 } else { 57 58 if uid == 0 { 59 if ctype != 0 { 60 Engine.Table("notification").Where("notification.ctype=?", ctype).Limit(limit, offset).Desc("notification." + field).Find(rp) 61 } else { 62 Engine.Table("notification").Limit(limit, offset).Desc("notification." + field).Find(rp) 63 } 64 65 } else { 66 67 if ctype == 0 { 68 //Engine.Where("(ctype=-1 or ctype=1) and uid=?", uid).Limit(limit, offset).Desc(field).Find(rp) 69 Engine.Table("notification").Where("notification.uid=?", uid).Limit(limit, offset).Desc("notification." + field).Find(rp) 70 71 } else { 72 73 Engine.Table("notification").Where("notification.ctype=? and notification.uid=?", ctype, uid).Limit(limit, offset).Desc("notification." + field).Find(rp) 74 } 75 } 76 } 77 return rp 78 } 79 80 func GetNotificationsByUidJoinUser(uid int64, ctype int64, offset int, limit int, field string) *[]Notificationjuser { 81 rp := &[]Notificationjuser{} 82 if field == "asc" { 83 84 if uid == 0 { 85 if ctype != 0 { 86 Engine.Table("notification").Where("notification.ctype=?", ctype).Limit(limit, offset).Asc("notification.id").Join("LEFT", "user", "user.username = notification.author").Find(rp) 87 } else { 88 Engine.Table("notification").Limit(limit, offset).Asc("notification.id").Join("LEFT", "user", "user.username = notification.author").Find(rp) 89 } 90 91 } else { 92 93 if ctype == 0 { 94 Engine.Table("notification").Where("notification.uid=?", uid).Limit(limit, offset).Asc("notification.id").Join("LEFT", "user", "user.username = notification.author").Find(rp) 95 96 } else { 97 98 Engine.Table("notification").Where("notification.ctype=? and notification.uid=?", ctype, uid).Limit(limit, offset).Asc("notification.id").Join("LEFT", "user", "user.username = notification.author").Find(rp) 99 } 100 } 101 } else { 102 103 if uid == 0 { 104 if ctype != 0 { 105 Engine.Table("notification").Where("notification.ctype=?", ctype).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "user", "user.username = notification.author").Find(rp) 106 } else { 107 Engine.Table("notification").Limit(limit, offset).Desc("notification."+field).Join("LEFT", "user", "user.username = notification.author").Find(rp) 108 } 109 110 } else { 111 112 if ctype == 0 { 113 //Engine.Where("(ctype=-1 or ctype=1) and uid=?", uid).Limit(limit, offset).Desc(field).Find(rp) 114 Engine.Table("notification").Where("notification.uid=?", uid).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "user", "user.username = notification.author").Find(rp) 115 116 } else { 117 118 Engine.Table("notification").Where("notification.ctype=? and notification.uid=?", ctype, uid).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "user", "user.username = notification.author").Find(rp) 119 } 120 } 121 } 122 return rp 123 } 124 125 func GetNotificationsByUidUsernameJoinTopic(uid int64, author string, ctype int64, offset int, limit int, field string) *[]Notificationjtopic { 126 rp := &[]Notificationjtopic{} 127 128 if uid == 0 { 129 if ctype != 0 { 130 Engine.Table("notification").Where("notification.ctype=? and notification.author=?", ctype, author).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "topic", "notification.uid = topic.id").Find(rp) 131 } else { 132 Engine.Table("notification").Where("notification.author=?", author).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "topic", "notification.uid = topic.id").Find(rp) 133 } 134 135 } else { 136 137 if ctype == 0 { 138 Engine.Table("notification").Where("notification.uid=? and notification.author=?", uid, author).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "topic", "notification.uid = topic.id").Find(rp) 139 140 } else { 141 142 Engine.Table("notification").Where("notification.ctype=? and notification.uid=? and notification.author=?", ctype, uid, author).Limit(limit, offset).Desc("notification."+field).Join("LEFT", "topic", "notification.uid = topic.id").Find(rp) 143 } 144 } 145 return rp 146 } 147 148 func PostNotification(tid int64, rp *Notification) (int64, error) { 149 // 创建 Session 对象 150 sess := Engine.NewSession() 151 defer sess.Close() 152 // 启动事务 153 if err := sess.Begin(); err != nil { 154 return -1, err 155 } 156 157 //执行事务 158 notificationid := int64(0) 159 if _, err := sess.Insert(rp); err == nil && rp != nil { 160 if rp.Tid > 0 { 161 n, _ := sess.Where("tid=?", rp.Tid).Count(&Notification{}) 162 163 if row, err := sess.Table(&Topic{}).Where("id=?", rp.Tid).Update(&map[string]interface{}{"author": rp.Author, "notification_time": time.Now().Unix(), "notification_count": n, "notification_last_user_id": rp.Uid}); err != nil || row == 0 { 164 sess.Rollback() 165 return -1, errors.New(fmt.Sprint("PostNotification更新topic表相关信息时,执行:", row, "行变更,出现错误:", err)) 166 } 167 } 168 169 if rp.Uid > 0 { 170 n, _ := sess.Where("uid=?", rp.Uid).Count(&Notification{}) 171 172 if row, err := sess.Table(&User{}).Where("id=?", rp.Uid).Update(&map[string]interface{}{"notification_count": n}); err != nil || row == 0 { 173 sess.Rollback() 174 return -1, errors.New(fmt.Sprint("PostNotification更新user表话题相关信息时,执行:", row, "行变更,出现错误:", err)) 175 } 176 } 177 178 notificationid = rp.Id 179 } else { 180 sess.Rollback() 181 return -1, err 182 } 183 184 // 提交事务 185 return notificationid, sess.Commit() 186 } 187 188 func PutNotification(rid int64, rp *Notification) (int64, error) { 189 // 创建 Session 对象 190 sess := Engine.NewSession() 191 defer sess.Close() 192 // 启动事务 193 if err := sess.Begin(); err != nil { 194 return -1, err 195 } 196 197 //执行事务 198 notificationid := int64(0) 199 if row, err := sess.Update(rp, &Notification{Id: rid}); err != nil || row <= 0 { 200 sess.Rollback() 201 return -1, err 202 } else { 203 if rp.Uid > 0 { 204 n, _ := sess.Where("uid=?", rp.Uid).Count(&Notification{}) 205 206 if row, err := sess.Table(&User{}).Where("id=?", rp.Uid).Update(&map[string]interface{}{"notification_count": n}); err != nil || row == 0 { 207 sess.Rollback() 208 return -1, errors.New(fmt.Sprint("PutNotification更新user表话题相关信息时,执行:", row, "行变更,出现错误:", err)) 209 } 210 } 211 } 212 213 // 提交事务 214 return notificationid, sess.Commit() 215 } 216 217 func GetAllNotification() *[]Notification { 218 rps := &[]Notification{} 219 Engine.Desc("id").Find(rps) 220 return rps 221 } 222 223 func GetNotification(id int64) (*Notification, error) { 224 225 rpy := &Notification{} 226 has, err := Engine.Id(id).Get(rpy) 227 if has { 228 return rpy, err 229 } else { 230 231 return nil, err 232 } 233 } 234 235 func GetNotificationsByTid(tid int64, ctype int64, offset int, limit int, field string) *[]Notification { 236 rp := &[]Notification{} 237 238 //ctype等于-1为游客 ctype等于1为正常会员 这里ctype等于0的情况则返回两者 239 if tid == 0 { 240 if ctype != 0 { 241 Engine.Where("ctype=?", ctype).Limit(limit, offset).Desc(field).Find(rp) 242 } else { 243 Engine.Limit(limit, offset).Desc(field).Find(rp) 244 } 245 246 } else { 247 248 if ctype == 0 { 249 //Engine.Where("(ctype=-1 or ctype=1) and tid=?", tid).Limit(limit, offset).Desc(field).Find(rp) 250 Engine.Where("tid=?", tid).Limit(limit, offset).Desc(field).Find(rp) 251 252 } else { 253 254 Engine.Where("ctype=? and tid=?", ctype, tid).Limit(limit, offset).Desc(field).Find(rp) 255 } 256 } 257 return rp 258 } 259 260 func GetNotificationsByTidUid(tid int64, uid int64, ctype int64, offset int, limit int, field string) *[]Notification { 261 rp := &[]Notification{} 262 263 //ctype等于-1为游客 ctype等于1为正常会员 这里ctype等于0的情况则返回两者 264 if tid == 0 { 265 if ctype != 0 { 266 Engine.Where("ctype=? and uid=?", ctype, uid).Limit(limit, offset).Desc(field).Find(rp) 267 } else { 268 Engine.Where("uid=?", uid).Limit(limit, offset).Desc(field).Find(rp) 269 } 270 271 } else { 272 273 if ctype == 0 { 274 //Engine.Where("(ctype=-1 or ctype=1) and tid=?", tid).Limit(limit, offset).Desc(field).Find(rp) 275 Engine.Where("tid=? and uid=?", tid, uid).Limit(limit, offset).Desc(field).Find(rp) 276 277 } else { 278 279 Engine.Where("ctype=? and tid=? and uid=?", ctype, tid, uid).Limit(limit, offset).Desc(field).Find(rp) 280 } 281 } 282 return rp 283 } 284 285 func GetNotificationsByTidUsername(tid int64, author string, ctype int64, offset int, limit int, field string) *[]Notification { 286 rp := &[]Notification{} 287 288 if tid == 0 { 289 if ctype != 0 { 290 Engine.Where("ctype=? and author=?", ctype, author).Limit(limit, offset).Desc(field).Find(rp) 291 } else { 292 Engine.Where("author=?", author).Limit(limit, offset).Desc(field).Find(rp) 293 } 294 295 } else { 296 297 if ctype == 0 { 298 //Engine.Where("(ctype=-1 or ctype=1) and tid=?", tid).Limit(limit, offset).Desc(field).Find(rp) 299 Engine.Where("tid=? and author=?", tid, author).Limit(limit, offset).Desc(field).Find(rp) 300 301 } else { 302 303 Engine.Where("ctype=? and tid=? and author=?", ctype, tid, author).Limit(limit, offset).Desc(field).Find(rp) 304 } 305 } 306 return rp 307 } 308 309 func SetNotificationCountByUid(uid int64) (int64, error) { 310 n, _ := Engine.Where("uid=?", uid).Count(&Notification{Uid: uid}) 311 312 qs := &User{} 313 qs.NotificationCount = n 314 affected, err := Engine.Where("id=?", uid).Cols("notification_count").Update(qs) 315 return affected, err 316 } 317 318 func SetNotificationCount(uid int64, count int64) (int64, error) { 319 320 qs := &User{} 321 qs.NotificationCount = count 322 affected, err := Engine.Where("id=?", uid).Cols("notification_count").Update(qs) 323 return affected, err 324 } 325 326 func GetNotificationCountByUid(uid int64) int64 { 327 n, _ := Engine.Where("uid=?", uid).Count(&Notification{Uid: uid}) 328 return n 329 } 330 331 func AddNotification(tid, rid, uid, ctype int64, subject, reply, author, avatar, avatarLarge, avatarMedium, avatarSmall string) (int64, error) { 332 // 创建 Session 对象 333 sess := Engine.NewSession() 334 defer sess.Close() 335 // 启动事务 336 if err := sess.Begin(); err != nil { 337 return -1, err 338 } 339 340 // 执行事务 341 notificationid := int64(0) 342 { 343 nf := &Notification{} 344 nf.Tid = tid 345 nf.Rid = rid 346 nf.Uid = uid 347 nf.Ctype = ctype 348 nf.Subject = subject 349 nf.Reply = reply 350 nf.Author = author 351 nf.Avatar = avatar 352 nf.AvatarLarge = avatarLarge 353 nf.AvatarMedium = avatarMedium 354 nf.AvatarSmall = avatarSmall 355 nf.Created = time.Now().Unix() 356 357 if _, err := sess.Insert(nf); err == nil { 358 //更新用户中的通知数量记录 359 360 if nf.Uid > 0 { 361 n, _ := sess.Where("uid=?", nf.Uid).Count(&Notification{}) 362 if affected, err := sess.Table(&User{}).Where("id=?", nf.Uid).Update(&map[string]interface{}{"notification_count": n}); err != nil || affected <= 0 { 363 sess.Rollback() 364 return -1, errors.New(fmt.Sprint("AddNotification #", nf.Id, "更新user表相关信息出现错误,affected:", affected, "错误:", err)) 365 } 366 } 367 368 notificationid = nf.Id 369 } else { 370 return -1, err 371 } 372 373 } 374 375 // 提交事务 376 return notificationid, sess.Commit() 377 } 378 379 func SetNotificationContentByRid(rid int64, Content string) error { 380 if row, err := Engine.Table(&Notification{}).Where("id=?", rid).Update(&map[string]interface{}{"content": Content}); err != nil || row == 0 { 381 return err 382 } else { 383 return nil 384 } 385 386 } 387 388 func DelNotification(rid int64) error { 389 if row, err := Engine.Id(rid).Delete(new(Notification)); err != nil || row == 0 { 390 391 return errors.New("删除通知错误!") 392 } else { 393 return nil 394 } 395 396 } 397 398 func DelNotificationByRole(rid int64, uid int64, role int64) error { 399 allow := bool(false) 400 if anz, err := GetNotification(rid); err == nil && anz != nil { 401 if anz.Uid == uid { 402 allow = true 403 } else if role < 0 { 404 allow = true 405 } 406 if allow { 407 if row, err := Engine.Id(rid).Delete(new(Notification)); err != nil || row == 0 { 408 return errors.New("删除通知发生错误!") 409 } else { 410 return nil 411 } 412 } else { 413 return errors.New("你没有权限删除通知!") 414 } 415 416 } else { 417 return errors.New("没有办法删除根本不存在的通知!") 418 } 419 420 } 421 422 func DelNotificationsByPid(pid int64) error { 423 rpy := &[]Notification{} 424 if err := Engine.Where("pid=?", pid).Find(rpy); err == nil && rpy != nil { 425 for _, v := range *rpy { 426 if err := DelNotificationByRole(v.Id, v.Uid, -1000); err != nil { 427 fmt.Println("DelNotificationByRole:", err) 428 } 429 } 430 return nil 431 } else { 432 return err 433 } 434 435 } 436 437 func DelNotificationsByTid(tid int64) error { 438 439 rpy := &[]Notification{} 440 if err := Engine.Where("tid=?", tid).Find(rpy); err == nil && rpy != nil { 441 for _, v := range *rpy { 442 if err := DelNotificationByRole(v.Id, v.Uid, -1000); err != nil { 443 fmt.Println("DelNotificationByRole:", err) 444 } 445 } 446 return nil 447 } else { 448 return err 449 } 450 451 }