191 lines
6.5 KiB
Go
191 lines
6.5 KiB
Go
package dataengine
|
||
|
||
import (
|
||
consts "cid/consts/dataengine"
|
||
dao "cid/dao/dataengine"
|
||
entity "cid/model/entity/dataengine"
|
||
yidunService "cid/service/yidun"
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
)
|
||
|
||
// TencentContentCallbackService 腾讯内容检测回调处理服务
|
||
type TencentContentCallbackService struct{}
|
||
|
||
// TencentContentCallback 回调处理服务单例
|
||
var TencentContentCallback = new(TencentContentCallbackService)
|
||
|
||
// ProcessImageCallback 处理图片检测回调
|
||
func (s *TencentContentCallbackService) ProcessImageCallback(ctx context.Context, callbackData string) error {
|
||
g.Log().Infof(ctx, "处理图片检测回调, data: %s", callbackData)
|
||
|
||
var callback yidunService.ImageCallbackData
|
||
if err := json.Unmarshal([]byte(callbackData), &callback); err != nil {
|
||
g.Log().Errorf(ctx, "解析图片回调数据失败: %v", err)
|
||
return fmt.Errorf("解析回调数据失败: %w", err)
|
||
}
|
||
|
||
if callback.Antispam == nil {
|
||
return fmt.Errorf("回调数据格式错误:缺少antispam字段")
|
||
}
|
||
|
||
antispam := callback.Antispam
|
||
g.Log().Infof(ctx, "处理图片检测结果 - taskId: %s, suggestion: %d, resultType: %d",
|
||
antispam.TaskId, antispam.Suggestion, antispam.ResultType)
|
||
|
||
// 根据 taskId 查找送检日志
|
||
log, err := dao.TencentContentCheckLog.GetByTaskID(ctx, antispam.TaskId)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "查找送检日志失败, taskId=%s: %v", antispam.TaskId, err)
|
||
return fmt.Errorf("查找送检日志失败: %w", err)
|
||
}
|
||
|
||
if log == nil {
|
||
g.Log().Warningf(ctx, "未找到送检日志, taskId=%s", antispam.TaskId)
|
||
return nil
|
||
}
|
||
|
||
// 更新送检日志
|
||
checkTime := antispam.CensorTime
|
||
|
||
err = dao.TencentContentCheckLog.UpdateCheckResult(ctx, log.Id,
|
||
antispam.Suggestion, antispam.Label, antispam.ResultType, checkTime)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "更新送检日志检测结果失败: %v", err)
|
||
return err
|
||
}
|
||
|
||
g.Log().Infof(ctx, "图片检测回调处理完成, taskId=%s, suggestion=%d", antispam.TaskId, antispam.Suggestion)
|
||
return nil
|
||
}
|
||
|
||
// ProcessVideoCallback 处理视频检测回调
|
||
func (s *TencentContentCallbackService) ProcessVideoCallback(ctx context.Context, callbackData string) error {
|
||
g.Log().Infof(ctx, "处理视频检测回调, data: %s", callbackData)
|
||
|
||
var callback yidunService.VideoCallbackData
|
||
if err := json.Unmarshal([]byte(callbackData), &callback); err != nil {
|
||
g.Log().Errorf(ctx, "解析视频回调数据失败: %v", err)
|
||
return fmt.Errorf("解析回调数据失败: %w", err)
|
||
}
|
||
|
||
if callback.Antispam == nil {
|
||
return fmt.Errorf("回调数据格式错误:缺少antispam字段")
|
||
}
|
||
|
||
antispam := callback.Antispam
|
||
g.Log().Infof(ctx, "处理视频检测结果 - taskId: %s, suggestion: %d, resultType: %d, censorSource: %d",
|
||
antispam.TaskID, antispam.Suggestion, antispam.ResultType, antispam.CensorSource)
|
||
|
||
// 根据 taskId 查找送检日志
|
||
log, err := dao.TencentContentCheckLog.GetByTaskID(ctx, antispam.TaskID)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "查找送检日志失败, taskId=%s: %v", antispam.TaskID, err)
|
||
return fmt.Errorf("查找送检日志失败: %w", err)
|
||
}
|
||
|
||
if log == nil {
|
||
g.Log().Warningf(ctx, "未找到送检日志, taskId=%s", antispam.TaskID)
|
||
return nil
|
||
}
|
||
|
||
// 更新送检日志
|
||
checkTime := antispam.CensorTime
|
||
if checkTime == 0 {
|
||
checkTime = antispam.CheckTime
|
||
}
|
||
|
||
err = dao.TencentContentCheckLog.UpdateCheckResult(ctx, log.Id,
|
||
antispam.Suggestion, antispam.Label, antispam.ResultType, checkTime)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "更新送检日志检测结果失败: %v", err)
|
||
return err
|
||
}
|
||
|
||
g.Log().Infof(ctx, "视频检测回调处理完成, taskId=%s, suggestion=%d", antispam.TaskID, antispam.Suggestion)
|
||
return nil
|
||
}
|
||
|
||
// ProcessImageResult 手动处理图片检测结果(轮询模式)
|
||
func (s *TencentContentCallbackService) ProcessImageResult(ctx context.Context, taskID string) error {
|
||
g.Log().Infof(ctx, "查询图片检测结果, taskId: %s", taskID)
|
||
|
||
// 查找送检日志
|
||
log, err := dao.TencentContentCheckLog.GetByTaskID(ctx, taskID)
|
||
if err != nil || log == nil {
|
||
return fmt.Errorf("未找到送检日志, taskId=%s", taskID)
|
||
}
|
||
|
||
// 调用易盾查询结果
|
||
result, err := yidunService.ImageDetection.GetImageResult(ctx, taskID)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "查询图片检测结果失败: %v", err)
|
||
return err
|
||
}
|
||
|
||
// 更新日志
|
||
err = dao.TencentContentCheckLog.UpdateCheckResult(ctx, log.Id,
|
||
result.Suggestion, result.Label, result.ResultType, result.CensorTime)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "更新送检日志检测结果失败: %v", err)
|
||
return err
|
||
}
|
||
|
||
g.Log().Infof(ctx, "图片检测结果处理完成, taskId=%s, suggestion=%d", taskID, result.Suggestion)
|
||
return nil
|
||
}
|
||
|
||
// ProcessVideoResult 手动处理视频检测结果(轮询模式)
|
||
func (s *TencentContentCallbackService) ProcessVideoResult(ctx context.Context, taskID string) error {
|
||
g.Log().Infof(ctx, "查询视频检测结果, taskId: %s", taskID)
|
||
|
||
// 查找送检日志
|
||
log, err := dao.TencentContentCheckLog.GetByTaskID(ctx, taskID)
|
||
if err != nil || log == nil {
|
||
return fmt.Errorf("未找到送检日志, taskId=%s", taskID)
|
||
}
|
||
|
||
// 调用易盾查询结果
|
||
result, err := yidunService.VideoDetection.GetVideoResult(ctx, taskID)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "查询视频检测结果失败: %v", err)
|
||
return err
|
||
}
|
||
|
||
// 更新日志
|
||
err = dao.TencentContentCheckLog.UpdateCheckResult(ctx, log.Id,
|
||
result.Suggestion, result.Label, result.ResultType, result.CensorTime)
|
||
if err != nil {
|
||
g.Log().Errorf(ctx, "更新送检日志检测结果失败: %v", err)
|
||
return err
|
||
}
|
||
|
||
g.Log().Infof(ctx, "视频检测结果处理完成, taskId=%s, suggestion=%d", taskID, result.Suggestion)
|
||
return nil
|
||
}
|
||
|
||
// GetCheckLogsByImageID 根据图片ID获取送检日志
|
||
func (s *TencentContentCallbackService) GetCheckLogsByImageID(ctx context.Context, imageID string) ([]entity.TencentContentCheckLog, error) {
|
||
// 先获取图片数据
|
||
image, err := dao.TencentImage.GetByImageID(ctx, imageID)
|
||
if err != nil || image == nil {
|
||
return nil, fmt.Errorf("未找到图片数据")
|
||
}
|
||
|
||
return dao.TencentContentCheckLog.GetBySourceID(ctx, consts.SourceTableTencentImage, image.Id)
|
||
}
|
||
|
||
// GetCheckLogsByVideoID 根据视频ID获取送检日志
|
||
func (s *TencentContentCallbackService) GetCheckLogsByVideoID(ctx context.Context, videoID string) ([]entity.TencentContentCheckLog, error) {
|
||
// 先获取视频数据
|
||
video, err := dao.TencentVideo.GetByVideoID(ctx, videoID)
|
||
if err != nil || video == nil {
|
||
return nil, fmt.Errorf("未找到视频数据")
|
||
}
|
||
|
||
return dao.TencentContentCheckLog.GetBySourceID(ctx, consts.SourceTableTencentVideo, video.Id)
|
||
}
|