service/library/contentaudit/imageaudit/control_block.go

116 lines
4.9 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 (
"fmt"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"service/library/mediafiller"
"github.com/gin-gonic/gin"
)
// 批次图像审核任务控制块
type ImageAuditTaskBatchControlBlock struct {
BatchId string // 批次号
TaskCtrlBlocks []*ImageAuditTaskControlBlock // 任务控制块
Images []*dbstruct.MediaComponent // 送审图像序列
ImageAuditIds []string // 送审图像对应的图像审核表id
Img2taskIndexMap map[int]int // 图像审核序列下标->任务序列下标的映射map
ActionMap map[string]*ImageAuditAction // 动作Id号-action的map
}
func NewImageAuditTaskBatchControlBlock(tasks []*dbstruct.ImageAuditTask, batchId string) *ImageAuditTaskBatchControlBlock {
if len(tasks) == 0 {
return nil
}
ctrlBlock := &ImageAuditTaskBatchControlBlock{
BatchId: batchId,
TaskCtrlBlocks: make([]*ImageAuditTaskControlBlock, 0),
Images: make([]*dbstruct.MediaComponent, 0),
ImageAuditIds: make([]string, 0),
Img2taskIndexMap: make(map[int]int),
ActionMap: make(map[string]*ImageAuditAction),
}
imageIndex := 0
taskIndex := 0
mediaFillables := make([]mediafiller.MediaFillable, 0)
for _, task := range tasks {
taskCtrlBlock := NewImageAuditTaskControlBlock(task)
ctrlBlock.TaskCtrlBlocks = append(ctrlBlock.TaskCtrlBlocks, taskCtrlBlock)
ctrlBlock.RecordAction(taskCtrlBlock)
imageIndex, taskIndex = ctrlBlock.RecordImage(taskCtrlBlock, &mediaFillables, imageIndex, taskIndex)
}
mediafiller.FillList(&gin.Context{}, mediaFillables)
return ctrlBlock
}
func (ctrlBlock *ImageAuditTaskBatchControlBlock) RecordAction(taskCtrlBlock *ImageAuditTaskControlBlock) {
ctx := &gin.Context{}
// 写map记录
if ctrlBlock.ActionMap[taskCtrlBlock.ActionId] == nil { // 写入ctrlBlock.ActionMap
ctrlBlock.ActionMap[taskCtrlBlock.ActionId] = NewImageAuditAction()
// 将此前批次对该元素未成功,待处理的审核全部置为已失效
if err := _DefaultImageAuditTask.OpHandleOverdue(ctx, taskCtrlBlock.ImageAuditTask, util.DerefString(taskCtrlBlock.ImageAuditTask.BatchId)); err != nil {
logger.Error("_DefaultImageAuditTask OpHandleOverdue fail :%v", err)
}
}
ctrlBlock.ActionMap[taskCtrlBlock.ActionId].Record(taskCtrlBlock)
}
func (ctrlBlock *ImageAuditTaskBatchControlBlock) RecordImage(taskCtrlBlock *ImageAuditTaskControlBlock, mediaFillables *[]mediafiller.MediaFillable, imageIndex int, taskIndex int) (newImageIndex int, newTaskIndex int) {
// 未分片
if util.DerefInt64(taskCtrlBlock.ImageAuditTask.IsFragmented) == 0 {
ctrlBlock.Images = append(ctrlBlock.Images, taskCtrlBlock.ImageAuditTask.AuditedMedia)
*mediaFillables = append(*mediaFillables, taskCtrlBlock.ImageAuditTask.AuditedMedia)
ctrlBlock.Img2taskIndexMap[imageIndex] = taskIndex
ctrlBlock.ImageAuditIds = append(ctrlBlock.ImageAuditIds, util.DerefString(taskCtrlBlock.ImageAuditTask.ImageAuditId))
imageIndex++
} else { // 已分片
imageIds := taskCtrlBlock.ImageAuditTask.AuditedMedia.GetImageIds()
imageAuditIds := util.DerefStringSlice(taskCtrlBlock.ImageAuditTask.ImageAuditFragmentIds)
for i, imageId := range imageIds {
image := &dbstruct.MediaComponent{
ImageIds: util.Int64Slice([]int64{imageId}),
}
ctrlBlock.Images = append(ctrlBlock.Images, image)
*mediaFillables = append(*mediaFillables, image)
ctrlBlock.ImageAuditIds = append(ctrlBlock.ImageAuditIds, imageAuditIds[i])
ctrlBlock.Img2taskIndexMap[imageIndex] = taskIndex
imageIndex++
}
}
taskIndex++
return imageIndex, taskIndex
}
// 图像审核任务控制块
// ActionId设计初衷由于图像审核是定时任务触发的批量作业如果在一次作业间隔有针对同一个图像媒体的多次更新则会提交关于它的多次审核需要保证数据一致性
type ImageAuditTaskControlBlock struct {
// 静态元素
ActionId string // 审核动作id号由图像审核实体数据库四要素拼接而成用于指示对数据库-表-单条数据-图像字段的审核动作
ImageAuditTask *dbstruct.ImageAuditTask // 审核任务
// 动态元素
IsTaskPassed bool // 任务状态,仅当任务已确定完成,即已完成审核任务的分片数 = 审核任务分片数时,才有意义
AuditedFragmentsNum int // 已完成审核任务的分片数
}
// 新建图像审核任务块
func NewImageAuditTaskControlBlock(task *dbstruct.ImageAuditTask) (tcb *ImageAuditTaskControlBlock) {
tcb = &ImageAuditTaskControlBlock{
ActionId: fmt.Sprintf("%v%v%v%v", util.DerefString(task.AssociativeDatabase), util.DerefString(task.AssociativeTableName),
util.DerefInt64(task.AssociativeTableId), util.DerefString(task.AssociativeTableColumn)),
ImageAuditTask: task,
IsTaskPassed: true,
AuditedFragmentsNum: 0,
}
return
}