62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
// push.go Day 61(P0.5):cmpp-deliver 消费桥(gateway 进程)——回执终态事件
|
|
// →账号用户名反查→CMPP Server 会话注册表→PushDeliver 到客户活跃连接。
|
|
package access
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/logger"
|
|
"go-sms/internal/protocol/cmpp"
|
|
"go-sms/internal/service/report"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// DeliverPusher cmpp-deliver 事件→协议侧下发。
|
|
type DeliverPusher struct {
|
|
db *gorm.DB
|
|
sv *cmpp.Server
|
|
}
|
|
|
|
// NewDeliverPusher 构造(sv=nil 静默丢弃——CMPP 未启用场景)。
|
|
func NewDeliverPusher(db *gorm.DB, sv *cmpp.Server) *DeliverPusher {
|
|
return &DeliverPusher{db: db, sv: sv}
|
|
}
|
|
|
|
// HandlePayload Kafka 消费入口(载荷=report.DeliverEvent JSON)。
|
|
// 账号离线/无会话=正常路径(Warn 观测,事件消费完成不重试——离线补推属 P1)。
|
|
func (p *DeliverPusher) HandlePayload(_ context.Context, value []byte) error {
|
|
var ev report.DeliverEvent
|
|
if err := json.Unmarshal(value, &ev); err != nil {
|
|
return err // 非法载荷→死信(与 report 消费同口径)
|
|
}
|
|
if p.sv == nil {
|
|
return nil
|
|
}
|
|
var u entity.AccountUser
|
|
if err := p.db.WithContext(context.Background()).
|
|
Select("username").Where("id = ?", ev.AccountID).First(&u).Error; err != nil {
|
|
logger.L().Warn("cmpp_deliver_account_missing", zap.Int64("account_id", ev.AccountID), zap.Error(err))
|
|
return nil // 账号已删:丢弃(重试无意义)
|
|
}
|
|
d := &cmpp.Deliver{
|
|
MsgID: uint64(ev.DetailID), // 平台 MsgID 口径(=Submit_Resp 应答值)
|
|
DestID: "", ServiceID: "", MsgFmt: 8, // MsgFmt=UCS2 占位(状态报告体不涉内容编码)
|
|
SrcTerminal: ev.Mobile,
|
|
IsReport: true,
|
|
Report: &cmpp.ReportInfo{
|
|
MsgID: uint64(ev.DetailID), Stat: ev.Stat,
|
|
SubmitTime: ev.SubmitAt, DoneTime: ev.DoneAt,
|
|
DestTerminalID: ev.Mobile,
|
|
},
|
|
}
|
|
sent := p.sv.PushDeliver(u.Username, d)
|
|
logger.L().Info("cmpp_deliver_pushed",
|
|
zap.Int64("account_id", ev.AccountID), zap.String("username", u.Username),
|
|
zap.Int64("detail_id", ev.DetailID), zap.String("stat", ev.Stat),
|
|
zap.Int("connections", sent))
|
|
return nil
|
|
}
|