91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package textaudit
|
||
|
||
import (
|
||
"fmt"
|
||
"service/api/consts"
|
||
textauditproto "service/api/proto/textaudit/proto"
|
||
textaudittaskproto "service/api/proto/textaudittask/proto"
|
||
"service/bizcommon/util"
|
||
"service/dbstruct"
|
||
"service/library/logger"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
goproto "google.golang.org/protobuf/proto"
|
||
)
|
||
|
||
// 文字审核任务控制块
|
||
// ActionId设计初衷:由于文字审核是定时任务触发的批量作业,如果在一次作业间隔有针对同一个文字媒体的多次更新,则会提交关于它的多次审核,需要配合乐观锁保证数据一致性
|
||
type TextAuditTaskControlBlock struct {
|
||
// 静态元素
|
||
ActionId string // 审核动作id号,由文字审核实体数据库四要素拼接而成,用于指示对数据库-表-单条数据-文字字段的审核动作
|
||
TextAuditTask *dbstruct.TextAuditTask // 审核任务
|
||
RollbackFunc func() error // 审核失败时的回退方法
|
||
|
||
// 动态元素
|
||
IsTaskPassed bool // 任务状态,仅当任务已完成时有效
|
||
IsGivingNoticeToBatch bool // 是否进行批处理
|
||
}
|
||
|
||
// 新建文字审核任务块
|
||
func NewTextAuditTaskControlBlock(task *dbstruct.TextAuditTask, rollbackFunc func() error) (tcb *TextAuditTaskControlBlock) {
|
||
if task == nil || task.AuditedText == nil {
|
||
return
|
||
}
|
||
|
||
task.BatchId = goproto.String(defaultTextAuditTaskScheduler.batchId)
|
||
task.Status = goproto.Int64(consts.TextAudit_Created)
|
||
|
||
tcb = &TextAuditTaskControlBlock{
|
||
ActionId: fmt.Sprintf("%v%v%v%v", util.DerefString(task.AssociativeDatabase), util.DerefString(task.AssociativeTableName),
|
||
util.DerefInt64(task.AssociativeTableId), util.DerefString(task.AssociativeTableColumn)),
|
||
TextAuditTask: task,
|
||
RollbackFunc: rollbackFunc,
|
||
IsTaskPassed: true,
|
||
IsGivingNoticeToBatch: false,
|
||
}
|
||
return
|
||
}
|
||
|
||
func AddTask(task *TextAuditTaskControlBlock) {
|
||
if err := task.Prepare(); err != nil {
|
||
logger.Error("TextAuditTaskPrepareException: %v", err)
|
||
return
|
||
}
|
||
defaultTextAuditTaskScheduler.AddTask(task)
|
||
}
|
||
|
||
// 将文字审核任务信息写入数据库,准备送审
|
||
func (task *TextAuditTaskControlBlock) Prepare() (err error) {
|
||
|
||
if task == nil {
|
||
return fmt.Errorf("task is nil, check your input")
|
||
}
|
||
|
||
// 1.写入文字审核表
|
||
ctx := &gin.Context{}
|
||
|
||
textAudit := &dbstruct.TextAudit{
|
||
AuditedText: task.TextAuditTask.AuditedText,
|
||
BatchId: task.TextAuditTask.BatchId,
|
||
Status: goproto.Int64(consts.TextAudit_Created),
|
||
}
|
||
if err = _DefaultTextAudit.OpCreate(ctx, &textauditproto.OpCreateReq{
|
||
TextAudit: textAudit,
|
||
}); err != nil {
|
||
logger.Error("Textaudit OpCreate failed: %v", err)
|
||
return
|
||
}
|
||
|
||
task.TextAuditTask.TextAuditId = textAudit.Id
|
||
|
||
// 2.写入文字审核任务表
|
||
if err := _DefaultTextAuditTask.OpCreate(&gin.Context{}, &textaudittaskproto.OpCreateReq{
|
||
TextAuditTask: task.TextAuditTask,
|
||
}); err != nil {
|
||
logger.Error("Textaudittask OpCreate failed: %v", err)
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|