service/library/contentaudit/imageaudit/task.go

119 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package imageaudit
import (
"service/api/consts"
imageauditproto "service/api/proto/imageaudit/proto"
imageaudittaskproto "service/api/proto/imageaudittask/proto"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"github.com/gin-gonic/gin"
goproto "google.golang.org/protobuf/proto"
)
func AddTasks(tasks []*dbstruct.ImageAuditTask) error {
for _, task := range tasks {
err := AddTask(task)
if err != nil {
return err
}
}
return nil
}
func AddTask(task *dbstruct.ImageAuditTask) error {
if task == nil || task.AuditedMedia == nil {
return nil
}
fragmentNum := len(task.AuditedMedia.GetImageIds())
if fragmentNum == 0 {
return nil
}
task.BatchId = goproto.String(defaultImageAuditTaskScheduler.batchId)
task.Status = goproto.Int64(consts.ImageAudit_Created)
if fragmentNum == 1 {
task.IsFragmented = goproto.Int64(0)
task.FragmentsNum = goproto.Int64(1)
// 写入图像审核表
if err := prepareNotFragmentedImageAuditTask(task); err != nil {
return err
}
} else {
task.IsFragmented = goproto.Int64(1)
task.FragmentsNum = goproto.Int64(int64(fragmentNum))
// 写入图像审核表
if err := prepareFragmentedImageAuditTask(task); err != nil {
return err
}
}
// 写入图像审核任务表
if err := _DefaultImageAuditTask.OpCreate(&gin.Context{}, &imageaudittaskproto.OpCreateReq{
ImageAuditTask: task,
}); err != nil {
logger.Error("Imageaudittask OpCreate failed: %v", err)
return err
}
return nil
}
// 根据任务信息创建单个图片的图片审核并存入数据库返回images
func prepareFragmentedImageAuditTask(task *dbstruct.ImageAuditTask) (err error) {
ctx := &gin.Context{}
fragmentsNum := util.DerefInt64(task.FragmentsNum)
imageAudits := make([]*dbstruct.ImageAudit, fragmentsNum)
// 将媒体拆分为单个图像并存入图片审核表
imageIds := task.AuditedMedia.GetImageIds()
for i, imageId := range imageIds {
image := &dbstruct.MediaComponent{
ImageIds: util.Int64Slice([]int64{imageId}),
}
imageAudits[i] = &dbstruct.ImageAudit{
AuditedMedia: image,
BatchId: task.BatchId,
Status: goproto.Int64(consts.ImageAudit_Created),
}
}
if err = _DefaultImageAudit.OpCreateFragmentGroup(ctx, &imageauditproto.OpCreateBatchReq{
ImageAudits: imageAudits,
FragmentsNum: fragmentsNum,
}); err != nil {
logger.Error("Imageaudit OpCreateFragmentGroup failed: %v", err)
return
}
auditFragmentIds := make([]string, fragmentsNum)
for i, imageaudit := range imageAudits {
auditFragmentIds[i] = util.DerefString(imageaudit.Id)
}
task.ImageAuditFragmentIds = &auditFragmentIds
return
}
func prepareNotFragmentedImageAuditTask(task *dbstruct.ImageAuditTask) (err error) {
ctx := &gin.Context{}
imageAudit := &dbstruct.ImageAudit{
AuditedMedia: task.AuditedMedia,
BatchId: task.BatchId,
Status: goproto.Int64(consts.ImageAudit_Created),
}
if err = _DefaultImageAudit.OpCreate(ctx, &imageauditproto.OpCreateReq{
ImageAudit: imageAudit,
}); err != nil {
logger.Error("Imageaudit OpCreate failed: %v", err)
return
}
task.ImageAuditId = imageAudit.Id
return
}