user_message_service.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package user
  2. import (
  3. "encoding/json"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. userDao "eta/eta_mini_ht_api/models/user"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. type MessageDTO struct {
  11. From int `gorm:"column:from"`
  12. To int `gorm:"column:to"`
  13. Message string `gorm:"column:message"`
  14. Type string `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO')"`
  15. Status string `gorm:"column:status;type:enum('UNREAD','READ')"`
  16. }
  17. const (
  18. ReportMessageTemplate = "您关注的研究员%v更新了一篇报告"
  19. VideoMessageTemplate = "您关注的研究员%v更新了一个视频"
  20. AudioMessageTemplate = "您关注的研究员%v更新了一个音频"
  21. )
  22. type MessageInfo struct {
  23. analyst string
  24. }
  25. func CreateMessage(meta MetaInfoDTO) (err error) {
  26. messageType := userDao.SourceType(meta.SourceType)
  27. var messageList []userDao.UserMessage
  28. users := strings.Split(meta.To, ",")
  29. if err != nil {
  30. logger.Error("unmarshal meta info error")
  31. return
  32. }
  33. var message string
  34. var content MetaData
  35. err = json.Unmarshal([]byte(meta.Meta), &content)
  36. switch userDao.SourceType(meta.SourceType) {
  37. case userDao.ReportSourceType:
  38. message = fmt.Sprintf(ReportMessageTemplate, content.AuthorName)
  39. messageType = userDao.ReportSourceType
  40. case userDao.VideoSourceType:
  41. message = fmt.Sprintf(VideoMessageTemplate, content.AuthorName)
  42. case userDao.AudioSourceType:
  43. message = fmt.Sprintf(AudioMessageTemplate, content.AuthorName)
  44. }
  45. for _, user := range users {
  46. id, _ := strconv.Atoi(user)
  47. userMessage := userDao.UserMessage{
  48. From: content.AuthorId,
  49. To: id,
  50. Message: message,
  51. SourceId: content.SourceId,
  52. Type: messageType,
  53. Status: userDao.UnReadStatus,
  54. }
  55. messageList = append(messageList, userMessage)
  56. }
  57. return userDao.BatchInsertMessage(messageList)
  58. }