增加SendResult

This commit is contained in:
白茶清欢 2023-02-02 17:23:41 +08:00
parent 04fed6ffbd
commit 8b73672fa9
2 changed files with 26 additions and 13 deletions

View File

@ -100,4 +100,10 @@ type StructInfo struct {
//
// Date : 15:27 2023/2/2
type SendResult struct {
IsNeedSend bool `json:"is_need_send"` // 是否需要发送
NoSendDetail map[string]interface{} `json:"no_send_detail"` // 未发送详细原因
Data interface{} `json:"data"` // 发送的数据
IsSuccess bool `json:"is_success"` // 是否处理成功
FailReason string `json:"fail_reason"` // 失败原因
SendResult map[string]interface{} `json:"send_result"` // 发送结果
}

33
send.go
View File

@ -17,7 +17,7 @@ import (
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:16 2023/2/2
func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) {
func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) SendResult {
if nil == preSendHandler {
preSendHandler = &DefaultPreSendHandler{}
}
@ -25,26 +25,33 @@ func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSe
if nil == sendEventHandler {
sendEventHandler = &DefaultSendEventHandler{}
}
// noSendReason, isNeedSend := preSendHandler.NeedSend()
_, isNeedSend := preSendHandler.NeedSend()
res := SendResult{}
if !isNeedSend {
if res.NoSendDetail, res.IsNeedSend = preSendHandler.NeedSend(); !res.IsNeedSend {
sendEventHandler.NoSendCallback(data, res)
return
return res
}
// 通过反射填充数据
ReflectValueInstance.Do(dataFlag, data, preSendHandler)
if byteData, err := json.Marshal(data); nil != err {
res.Data = data
var (
byteData []byte
err error
)
if byteData, err = json.Marshal(data); nil != err {
res.IsSuccess = false
res.FailReason = err.Error()
sendEventHandler.FailCallback(map[string]interface{}{
"err": err.Error(),
"reason": "data marshal fail",
}, err)
} else {
sendResult, sendErr := sendEventHandler.Send(byteData)
if nil != sendErr {
sendEventHandler.FailCallback(sendResult, sendErr)
return
}
sendEventHandler.SuccessCallback(sendResult)
return res
}
if res.SendResult, err = sendEventHandler.Send(byteData); nil != err {
sendEventHandler.FailCallback(res.SendResult, err)
return res
}
sendEventHandler.SuccessCallback(res.SendResult)
return res
}