205 lines
5.9 KiB
Go
205 lines
5.9 KiB
Go
package dataengine
|
|
|
|
import (
|
|
consts "cid/consts/dataengine"
|
|
entity "cid/model/entity/dataengine"
|
|
yidunService "cid/service/yidun"
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
"github.com/bwmarrin/snowflake"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
// TencentContentCheckLogDAO 送检日志数据访问层
|
|
type TencentContentCheckLogDAO struct{}
|
|
|
|
// TencentContentCheckLog 日志DAO单例
|
|
var TencentContentCheckLog = new(TencentContentCheckLogDAO)
|
|
|
|
// Create 创建送检日志
|
|
func (d *TencentContentCheckLogDAO) Create(ctx context.Context, log *entity.TencentContentCheckLog) (id int64, err error) {
|
|
// GoFrame v2.10.0 pgsql 驱动不支持 RETURNING/LastInsertId
|
|
node, err := snowflake.NewNode(1)
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "创建Snowflake节点失败: %v", err)
|
|
return 0, err
|
|
}
|
|
snowflakeID := node.Generate().Int64()
|
|
|
|
_, err = g.DB("cid").Model(consts.TencentContentCheckLogTable).Data(g.Map{
|
|
"id": snowflakeID,
|
|
"source_table": log.SourceTable,
|
|
"source_id": log.SourceID,
|
|
"request_url": log.RequestURL,
|
|
"request_param": log.RequestParam,
|
|
"response_data": log.ResponseData,
|
|
"status": log.Status,
|
|
"check_time": log.CheckTime,
|
|
"fail_reason": log.FailReason,
|
|
"task_id": log.TaskID,
|
|
"suggestion": log.Suggestion,
|
|
"label": log.Label,
|
|
"result_type": log.ResultType,
|
|
"duration": log.Duration,
|
|
}).Insert()
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "创建送检日志失败: %v", err)
|
|
return 0, err
|
|
}
|
|
return snowflakeID, nil
|
|
}
|
|
|
|
// UpdateStatus 更新送检状态
|
|
func (d *TencentContentCheckLogDAO) UpdateStatus(ctx context.Context, id int64, status string, responseData string, failReason string) error {
|
|
_, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("id", id).
|
|
Data(g.Map{
|
|
"status": status,
|
|
"response_data": responseData,
|
|
"fail_reason": failReason,
|
|
}).Update()
|
|
return err
|
|
}
|
|
|
|
// UpdateCheckResult 更新检测结果
|
|
func (d *TencentContentCheckLogDAO) UpdateCheckResult(ctx context.Context, id int64, suggestion, label, resultType int, checkTime int64) error {
|
|
_, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("id", id).
|
|
Data(g.Map{
|
|
"status": consts.CheckStatusCompleted,
|
|
"suggestion": suggestion,
|
|
"label": label,
|
|
"result_type": resultType,
|
|
"check_time": checkTime,
|
|
}).Update()
|
|
return err
|
|
}
|
|
|
|
// GetByID 根据ID获取日志
|
|
func (d *TencentContentCheckLogDAO) GetByID(ctx context.Context, id int64) (*entity.TencentContentCheckLog, error) {
|
|
var result entity.TencentContentCheckLog
|
|
r, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("id", id).
|
|
One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.IsEmpty() {
|
|
return nil, nil
|
|
}
|
|
if err = r.Struct(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// GetBySourceID 根据来源ID获取日志
|
|
func (d *TencentContentCheckLogDAO) GetBySourceID(ctx context.Context, sourceTable string, sourceID int64) ([]entity.TencentContentCheckLog, error) {
|
|
var result []entity.TencentContentCheckLog
|
|
r, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("source_table", sourceTable).
|
|
Where("source_id", sourceID).
|
|
OrderDesc("created_at").
|
|
All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = r.Structs(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// GetByTaskID 根据任务ID获取日志
|
|
func (d *TencentContentCheckLogDAO) GetByTaskID(ctx context.Context, taskID string) (*entity.TencentContentCheckLog, error) {
|
|
var result entity.TencentContentCheckLog
|
|
r, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("task_id", taskID).
|
|
One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.IsEmpty() {
|
|
return nil, nil
|
|
}
|
|
if err = r.Struct(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// ListByStatus 根据状态获取日志列表
|
|
func (d *TencentContentCheckLogDAO) ListByStatus(ctx context.Context, status string, page, pageSize int) ([]entity.TencentContentCheckLog, int, error) {
|
|
var result []entity.TencentContentCheckLog
|
|
m := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable)
|
|
|
|
if status != "" {
|
|
m.Where("status", status)
|
|
}
|
|
|
|
total, err := m.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
r, err := m.
|
|
OrderDesc("created_at").
|
|
Page(page, pageSize).
|
|
All()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if err = r.Structs(&result); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return result, int(total), nil
|
|
}
|
|
|
|
// UpdateDuration 更新耗时
|
|
func (d *TencentContentCheckLogDAO) UpdateDuration(ctx context.Context, id int64, duration int64) error {
|
|
_, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("id", id).
|
|
Data("duration", duration).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
// UpdateTaskID 更新任务ID
|
|
func (d *TencentContentCheckLogDAO) UpdateTaskID(ctx context.Context, id int64, taskID string) error {
|
|
_, err := gfdb.DB(ctx, "cid").Model(ctx, consts.TencentContentCheckLogTable).
|
|
Where("id", id).
|
|
Data("task_id", taskID).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
// GetImageSubmitResult 获取图片提交结果
|
|
func (d *TencentContentCheckLogDAO) GetImageSubmitResult(ctx context.Context, id int64) (*yidunService.ImageSubmitResult, error) {
|
|
log, err := d.GetByID(ctx, id)
|
|
if err != nil || log == nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result yidunService.ImageSubmitResult
|
|
if err := json.Unmarshal([]byte(log.ResponseData), &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// GetVideoSubmitResult 获取视频提交结果
|
|
func (d *TencentContentCheckLogDAO) GetVideoSubmitResult(ctx context.Context, id int64) (*yidunService.VideoSubmitResult, error) {
|
|
log, err := d.GetByID(ctx, id)
|
|
if err != nil || log == nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result yidunService.VideoSubmitResult
|
|
if err := json.Unmarshal([]byte(log.ResponseData), &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|