service/library/contentaudit/imageaudit/task.go

119 lines
3.1 KiB
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
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"
)
2024-01-18 18:19:31 +08:00
func AddTasks(tasks []*dbstruct.ImageAuditTask) error {
for _, task := range tasks {
err := AddTask(task)
if err != nil {
return err
}
}
return nil
2023-12-21 22:17:40 +08:00
}
2024-01-18 18:19:31 +08:00
func AddTask(task *dbstruct.ImageAuditTask) error {
2023-12-21 22:17:40 +08:00
if task == nil || task.AuditedMedia == nil {
2024-01-18 18:19:31 +08:00
return nil
2023-12-21 22:17:40 +08:00
}
2024-01-20 06:33:10 +08:00
fragmentNum := len(task.AuditedMedia.GetImageIds())
2023-12-21 22:17:40 +08:00
if fragmentNum == 0 {
2024-01-18 18:19:31 +08:00
return nil
2023-12-21 22:17:40 +08:00
}
task.BatchId = goproto.String(defaultImageAuditTaskScheduler.batchId)
task.Status = goproto.Int64(consts.ImageAudit_Created)
2024-01-18 18:19:31 +08:00
2023-12-21 22:17:40 +08:00
if fragmentNum == 1 {
task.IsFragmented = goproto.Int64(0)
task.FragmentsNum = goproto.Int64(1)
2024-01-18 18:19:31 +08:00
// 写入图像审核表
2024-01-20 18:48:38 +08:00
if err := prepareNotFragmentedImageAuditTask(task); err != nil {
2023-12-21 22:17:40 +08:00
return err
}
} else {
2024-01-18 18:19:31 +08:00
task.IsFragmented = goproto.Int64(1)
task.FragmentsNum = goproto.Int64(int64(fragmentNum))
// 写入图像审核表
2024-01-20 18:48:38 +08:00
if err := prepareFragmentedImageAuditTask(task); err != nil {
2023-12-21 22:17:40 +08:00
return err
}
}
2024-01-18 18:19:31 +08:00
// 写入图像审核任务表
2023-12-21 22:17:40 +08:00
if err := _DefaultImageAuditTask.OpCreate(&gin.Context{}, &imageaudittaskproto.OpCreateReq{
2024-01-18 18:19:31 +08:00
ImageAuditTask: task,
2023-12-21 22:17:40 +08:00
}); err != nil {
logger.Error("Imageaudittask OpCreate failed: %v", err)
return err
}
return nil
}
// 根据任务信息创建单个图片的图片审核并存入数据库返回images
2024-01-18 18:19:31 +08:00
func prepareFragmentedImageAuditTask(task *dbstruct.ImageAuditTask) (err error) {
2023-12-21 22:17:40 +08:00
ctx := &gin.Context{}
fragmentsNum := util.DerefInt64(task.FragmentsNum)
imageAudits := make([]*dbstruct.ImageAudit, fragmentsNum)
// 将媒体拆分为单个图像并存入图片审核表
2024-01-20 06:33:10 +08:00
imageIds := task.AuditedMedia.GetImageIds()
2023-12-21 22:17:40 +08:00
for i, imageId := range imageIds {
2024-01-18 18:19:31 +08:00
image := &dbstruct.MediaComponent{
2023-12-21 22:17:40 +08:00
ImageIds: util.Int64Slice([]int64{imageId}),
}
imageAudits[i] = &dbstruct.ImageAudit{
2024-01-18 18:19:31 +08:00
AuditedMedia: image,
2023-12-21 22:17:40 +08:00
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
}