2021-09-11 14:04:09 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-27 21:02:48 +08:00
|
|
|
|
"cmp"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"context"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
"mayfly-go/internal/msg/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/msg/domain/repository"
|
2025-07-27 21:02:48 +08:00
|
|
|
|
"mayfly-go/internal/msg/msgx"
|
|
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/i18n"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2025-07-27 21:02:48 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2021-09-11 14:04:09 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Msg interface {
|
2025-07-27 21:02:48 +08:00
|
|
|
|
msgx.MsgSender
|
2021-09-11 14:04:09 +08:00
|
|
|
|
|
2025-07-27 21:02:48 +08:00
|
|
|
|
base.App[*entity.Msg]
|
2021-11-11 15:56:02 +08:00
|
|
|
|
|
2025-07-27 21:02:48 +08:00
|
|
|
|
GetPageList(condition *entity.Msg, pageParam model.PageParam, orderBy ...string) (*model.PageResult[*entity.Msg], error)
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 21:04:47 +08:00
|
|
|
|
var _ (Msg) = (*msgAppImpl)(nil)
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type msgAppImpl struct {
|
2025-07-27 21:02:48 +08:00
|
|
|
|
base.AppImpl[*entity.Msg, repository.Msg]
|
|
|
|
|
|
|
2024-12-16 23:29:18 +08:00
|
|
|
|
msgRepo repository.Msg `inject:"T"`
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 21:04:47 +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
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-27 21:02:48 +08:00
|
|
|
|
func (a *msgAppImpl) Send(ctx context.Context, channel *msgx.Channel, msg *msgx.Msg) error {
|
|
|
|
|
|
// 存在i18n msgId,content则使用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
|
2021-11-11 15:56:02 +08:00
|
|
|
|
}
|