123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package user
- import (
- "encoding/json"
- logger "eta/eta_mini_ht_api/common/component/log"
- userDao "eta/eta_mini_ht_api/models/user"
- "fmt"
- "strconv"
- "strings"
- )
- type MessageDTO struct {
- From int `gorm:"column:from"`
- To int `gorm:"column:to"`
- Message string `gorm:"column:message"`
- Type string `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO')"`
- Status string `gorm:"column:status;type:enum('UNREAD','READ')"`
- }
- const (
- ReportMessageTemplate = "您关注的研究员%v更新了一篇报告"
- VideoMessageTemplate = "您关注的研究员%v更新了一个视频"
- AudioMessageTemplate = "您关注的研究员%v更新了一个音频"
- )
- type MessageInfo struct {
- analyst string
- }
- func CreateMessage(meta MetaInfoDTO) (err error) {
- messageType := userDao.SourceType(meta.SourceType)
- var messageList []userDao.UserMessage
- users := strings.Split(meta.To, ",")
- if err != nil {
- logger.Error("unmarshal meta info error")
- return
- }
- var message string
- var content MetaData
- err = json.Unmarshal([]byte(meta.Meta), &content)
- switch userDao.SourceType(meta.SourceType) {
- case userDao.ReportSourceType:
- message = fmt.Sprintf(ReportMessageTemplate, content.AuthorName)
- messageType = userDao.ReportSourceType
- case userDao.VideoSourceType:
- message = fmt.Sprintf(VideoMessageTemplate, content.AuthorName)
- case userDao.AudioSourceType:
- message = fmt.Sprintf(AudioMessageTemplate, content.AuthorName)
- }
- for _, user := range users {
- id, _ := strconv.Atoi(user)
- userMessage := userDao.UserMessage{
- From: content.AuthorId,
- To: id,
- Message: message,
- SourceId: content.SourceId,
- Type: messageType,
- Status: userDao.UnReadStatus,
- }
- messageList = append(messageList, userMessage)
- }
- return userDao.BatchInsertMessage(messageList)
- }
|