package user import ( "bytes" "encoding/json" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/domian/message" productDao "eta/eta_mini_ht_api/models/merchant" messageDao "eta/eta_mini_ht_api/models/message" userDao "eta/eta_mini_ht_api/models/user" "fmt" "strconv" "strings" "text/template" ) 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')"` } type MyMessage struct { Id int `json:"messageId"` SourceId int `json:"SourceId"` ProductId int `json:"ProductId"` Type string `json:"Type"` Message string `json:"Message"` } const ( ReportMessageTemplate = "您关注的研究员%v更新了一篇报告" VideoMessageTemplate = "您关注的研究员%v更新了一个视频" AudioMessageTemplate = "您关注的研究员%v更新了一个音频" RefundMessageTemplate = "订单:{{.ProductOrderNo}},姓名:{{.RealName}},退款{{.Result}}!" ) var ( transMap = map[messageDao.SourceType]productDao.MerchantProductType{ messageDao.ReportSourceType: productDao.Report, messageDao.VideoSourceType: productDao.Video, messageDao.AudioSourceType: productDao.Audio, } ) type MessageInfo struct { analyst string } func createRefundMessage(meta message.MetaInfoDTO) (err error) { var content messageDao.RefundMetaData err = json.Unmarshal([]byte(meta.Meta), &content) messageInfo, _ := generateMessage(content, RefundMessageTemplate) userId, _ := strconv.Atoi(meta.To) userMessage := userDao.UserMessage{ UserId: userId, Message: messageInfo, UserType: userDao.Admin, Type: messageDao.RefundSourceType, Status: userDao.UnReadStatus, } if err = userDao.CreateMessage(userMessage); err == nil { messageDao.FinishMetaInfo(meta.Id) } else { logger.Error("生成消息信息失败:%v", err) messageDao.FailedMetaInfo(meta.Id) } return } func CreateMessage(meta message.MetaInfoDTO) (err error) { if messageDao.SourceType(meta.SourceType) == messageDao.RefundSourceType { return createRefundMessage(meta) } messageType := messageDao.SourceType(meta.SourceType) var messageList []userDao.UserMessage users := strings.Split(meta.To, ",") var messageInfo string var content messageDao.MetaData err = json.Unmarshal([]byte(meta.Meta), &content) if err != nil { logger.Error("生成消息信息失败:%v", err) messageDao.FailedMetaInfo(meta.Id) } switch messageDao.SourceType(meta.SourceType) { case messageDao.ReportSourceType: messageInfo = fmt.Sprintf(ReportMessageTemplate, content.AuthorName) messageType = messageDao.ReportSourceType case messageDao.VideoSourceType: messageInfo = fmt.Sprintf(VideoMessageTemplate, content.AuthorName) case messageDao.AudioSourceType: messageInfo = fmt.Sprintf(AudioMessageTemplate, content.AuthorName) } for _, user := range users { id, _ := strconv.Atoi(user) userMessage := userDao.UserMessage{ AnalystId: content.AuthorId, UserId: id, UserType: userDao.Customer, Message: messageInfo, SourceId: content.SourceId, Type: messageType, Status: userDao.UnReadStatus, } messageList = append(messageList, userMessage) } if userDao.BatchInsertMessage(messageList) { messageDao.FinishMetaInfo(meta.Id) } else { logger.Error("生成消息信息失败:%v", err) messageDao.FailedMetaInfo(meta.Id) } return } func generateMessage(data interface{}, tmpl string) (message string, err error) { t := template.Must(template.New("messageTemplate").Parse(tmpl)) var buffer bytes.Buffer err = t.Execute(&buffer, data) if err != nil { logger.Error("生成消息模板失败:%v", err) return } message = buffer.String() return } func NeedNotice(userId int, analystId int) bool { return userDao.NeedNotice(userId, analystId) } func ReadMessage(userId int, messageId int) bool { return userDao.ReadMessage(userId, messageId) } func ReadMessages(userId int, analystId int) bool { return userDao.ReadMessages(userId, analystId) } func GetUnReadMessageList(userId int) (messageList []MyMessage, err error) { list, err := userDao.GetUnReadMessageList(userId) if err != nil { logger.Error("获取我的未读消息失败:%v", err) return } for _, messageDTO := range list { myMessage := convertToMyMessage(messageDTO) product, productErr := productDao.GetMerchantProductBySourceId(messageDTO.SourceId, transMap[messageDTO.Type]) if productErr != nil { logger.Error("获取产品信息失败:%v", productErr) } else { myMessage.ProductId = product.Id } messageList = append(messageList, myMessage) } return } func convertToMyMessage(message userDao.UserMessage) MyMessage { return MyMessage{ Id: message.Id, SourceId: message.SourceId, Type: string(message.Type), Message: message.Message, } }