msg.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package orchard
  2. import (
  3. "active/tools"
  4. "gorm.io/gorm"
  5. )
  6. type OrchardMsg struct {
  7. Id int64 `json:"id"`
  8. CreateTime int64 `json:"create_time"`
  9. PlayerId int64 `json:"player_id"`
  10. Msg string `json:"msg"`
  11. UserId int64 `json:"user_id"`
  12. IsRead bool `json:"is_read"`
  13. }
  14. func (this *OrchardMsg) TableName() string {
  15. return "orchard_msg"
  16. }
  17. func GetRecentMsgList(userid int64, open bool) (*[]OrchardMsg, int64) {
  18. var orchardMsgList []OrchardMsg
  19. db := tools.NewConn(MysqlExtDb)
  20. var odInt []int64
  21. db.Where("user_id = ?", userid).Order("create_time desc").Limit(OrchardConf.Limit).Find(&orchardMsgList)
  22. for i := range orchardMsgList {
  23. if !orchardMsgList[i].IsRead {
  24. odInt = append(odInt, orchardMsgList[i].Id)
  25. }
  26. }
  27. if len(odInt) > 0 && open {
  28. index := 0
  29. for index < len(odInt) {
  30. if index+20 < len(odInt) {
  31. db.Table(orchardMsgList[0].TableName()).Where("id in ?", odInt[index:index+20]).Update("is_read", true)
  32. } else {
  33. db.Table(orchardMsgList[0].TableName()).Where("id in ?", odInt[index:]).Update("is_read", true)
  34. }
  35. index += 20
  36. }
  37. }
  38. return &orchardMsgList, int64(len(odInt))
  39. }
  40. func (this *OrchardMsg) Insert(db *gorm.DB) error {
  41. //db := tools.NewConn(MysqlExtDb)
  42. return db.Create(this).Error
  43. }