Files
mayfly-go/server/internal/msg/application/msg.go

61 lines
1.6 KiB
Go
Raw Normal View History

2021-09-11 14:04:09 +08:00
package application
import (
"cmp"
"context"
2023-07-03 21:42:04 +08:00
"mayfly-go/internal/msg/domain/entity"
"mayfly-go/internal/msg/domain/repository"
"mayfly-go/internal/msg/msgx"
"mayfly-go/pkg/base"
"mayfly-go/pkg/i18n"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/stringx"
2021-09-11 14:04:09 +08:00
)
type Msg interface {
msgx.MsgSender
2021-09-11 14:04:09 +08:00
base.App[*entity.Msg]
GetPageList(condition *entity.Msg, pageParam model.PageParam, orderBy ...string) (*model.PageResult[*entity.Msg], error)
2021-09-11 14:04:09 +08:00
}
var _ (Msg) = (*msgAppImpl)(nil)
2022-09-09 18:26:08 +08:00
type msgAppImpl struct {
base.AppImpl[*entity.Msg, repository.Msg]
msgRepo repository.Msg `inject:"T"`
2021-09-11 14:04:09 +08:00
}
func (a *msgAppImpl) GetPageList(condition *entity.Msg, pageParam model.PageParam, orderBy ...string) (*model.PageResult[*entity.Msg], error) {
return a.msgRepo.GetPageList(condition, pageParam)
2021-09-11 14:04:09 +08:00
}
func (a *msgAppImpl) Send(ctx context.Context, channel *msgx.Channel, msg *msgx.Msg) error {
// 存在i18n msgIdcontent则使用msgId翻译
if msgId := msg.TmplExtra.GetInt("msgId"); msgId != 0 {
msg.Content = i18n.TC(ctx, i18n.MsgId(msgId))
}
content, err := stringx.TemplateParse(msg.Content, msg.Params)
if err != nil {
return err
}
for _, receiver := range msg.Receivers {
msgEntity := &entity.Msg{
Msg: content,
RecipientId: int64(receiver.Id),
Type: entity.MsgType(msg.TmplExtra.GetInt("type")),
Subtype: entity.MsgSubtype(msg.TmplExtra.GetStr("subtype")),
Status: cmp.Or(entity.MsgStatus(msg.TmplExtra.GetInt("status")), entity.MsgStatusRead),
}
msgEntity.Extra = msg.Params
if err := a.Save(ctx, msgEntity); err != nil {
return err
}
}
return nil
}