user_message_service.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package user
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/domian/message"
  7. productDao "eta/eta_mini_ht_api/models/merchant"
  8. messageDao "eta/eta_mini_ht_api/models/message"
  9. userDao "eta/eta_mini_ht_api/models/user"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "text/template"
  14. )
  15. type MessageDTO struct {
  16. From int `gorm:"column:from"`
  17. To int `gorm:"column:to"`
  18. Message string `gorm:"column:message"`
  19. Type string `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO')"`
  20. Status string `gorm:"column:status;type:enum('UNREAD','READ')"`
  21. }
  22. type MyMessage struct {
  23. Id int `json:"messageId"`
  24. SourceId int `json:"SourceId"`
  25. ProductId int `json:"ProductId"`
  26. Type string `json:"Type"`
  27. Message string `json:"Message"`
  28. }
  29. const (
  30. ReportMessageTemplate = "您关注的研究员%v更新了一篇报告"
  31. VideoMessageTemplate = "您关注的研究员%v更新了一个视频"
  32. AudioMessageTemplate = "您关注的研究员%v更新了一个音频"
  33. RefundMessageTemplate = "订单:{{.ProductOrderNo}},姓名:{{.RealName}},退款{{.Result}}!"
  34. )
  35. var (
  36. transMap = map[messageDao.SourceType]productDao.MerchantProductType{
  37. messageDao.ReportSourceType: productDao.Report,
  38. messageDao.VideoSourceType: productDao.Video,
  39. messageDao.AudioSourceType: productDao.Audio,
  40. }
  41. )
  42. type MessageInfo struct {
  43. analyst string
  44. }
  45. func createRefundMessage(meta message.MetaInfoDTO) (err error) {
  46. var content messageDao.RefundMetaData
  47. err = json.Unmarshal([]byte(meta.Meta), &content)
  48. messageInfo, _ := generateMessage(content, RefundMessageTemplate)
  49. userId, _ := strconv.Atoi(meta.To)
  50. userMessage := userDao.UserMessage{
  51. UserId: userId,
  52. Message: messageInfo,
  53. UserType: userDao.Admin,
  54. Type: messageDao.RefundSourceType,
  55. Status: userDao.UnReadStatus,
  56. }
  57. if err = userDao.CreateMessage(userMessage); err == nil {
  58. messageDao.FinishMetaInfo(meta.Id)
  59. } else {
  60. logger.Error("生成消息信息失败:%v", err)
  61. messageDao.FailedMetaInfo(meta.Id)
  62. }
  63. return
  64. }
  65. func CreateMessage(meta message.MetaInfoDTO) (err error) {
  66. if messageDao.SourceType(meta.SourceType) == messageDao.RefundSourceType {
  67. return createRefundMessage(meta)
  68. }
  69. messageType := messageDao.SourceType(meta.SourceType)
  70. var messageList []userDao.UserMessage
  71. users := strings.Split(meta.To, ",")
  72. var messageInfo string
  73. var content messageDao.MetaData
  74. err = json.Unmarshal([]byte(meta.Meta), &content)
  75. if err != nil {
  76. logger.Error("生成消息信息失败:%v", err)
  77. messageDao.FailedMetaInfo(meta.Id)
  78. }
  79. switch messageDao.SourceType(meta.SourceType) {
  80. case messageDao.ReportSourceType:
  81. messageInfo = fmt.Sprintf(ReportMessageTemplate, content.AuthorName)
  82. messageType = messageDao.ReportSourceType
  83. case messageDao.VideoSourceType:
  84. messageInfo = fmt.Sprintf(VideoMessageTemplate, content.AuthorName)
  85. case messageDao.AudioSourceType:
  86. messageInfo = fmt.Sprintf(AudioMessageTemplate, content.AuthorName)
  87. }
  88. for _, user := range users {
  89. id, _ := strconv.Atoi(user)
  90. userMessage := userDao.UserMessage{
  91. AnalystId: content.AuthorId,
  92. UserId: id,
  93. UserType: userDao.Customer,
  94. Message: messageInfo,
  95. SourceId: content.SourceId,
  96. Type: messageType,
  97. Status: userDao.UnReadStatus,
  98. }
  99. messageList = append(messageList, userMessage)
  100. }
  101. if userDao.BatchInsertMessage(messageList) {
  102. messageDao.FinishMetaInfo(meta.Id)
  103. } else {
  104. logger.Error("生成消息信息失败:%v", err)
  105. messageDao.FailedMetaInfo(meta.Id)
  106. }
  107. return
  108. }
  109. func generateMessage(data interface{}, tmpl string) (message string, err error) {
  110. t := template.Must(template.New("messageTemplate").Parse(tmpl))
  111. var buffer bytes.Buffer
  112. err = t.Execute(&buffer, data)
  113. if err != nil {
  114. logger.Error("生成消息模板失败:%v", err)
  115. return
  116. }
  117. message = buffer.String()
  118. return
  119. }
  120. func NeedNotice(userId int, analystId int) bool {
  121. return userDao.NeedNotice(userId, analystId)
  122. }
  123. func ReadMessage(userId int, messageId int) bool {
  124. return userDao.ReadMessage(userId, messageId)
  125. }
  126. func ReadMessages(userId int, analystId int) bool {
  127. return userDao.ReadMessages(userId, analystId)
  128. }
  129. func GetUnReadMessageList(userId int) (messageList []MyMessage, err error) {
  130. list, err := userDao.GetUnReadMessageList(userId)
  131. if err != nil {
  132. logger.Error("获取我的未读消息失败:%v", err)
  133. return
  134. }
  135. for _, messageDTO := range list {
  136. myMessage := convertToMyMessage(messageDTO)
  137. product, productErr := productDao.GetMerchantProductBySourceId(messageDTO.SourceId, transMap[messageDTO.Type])
  138. if productErr != nil {
  139. logger.Error("获取产品信息失败:%v", productErr)
  140. } else {
  141. myMessage.ProductId = product.Id
  142. }
  143. messageList = append(messageList, myMessage)
  144. }
  145. return
  146. }
  147. func convertToMyMessage(message userDao.UserMessage) MyMessage {
  148. return MyMessage{
  149. Id: message.Id,
  150. SourceId: message.SourceId,
  151. Type: string(message.Type),
  152. Message: message.Message,
  153. }
  154. }