feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package entity
type CopyDataTask struct {
TaskUniqKey string // 复制任务的唯一标志
OriginDataID int64
TargetDataID int64
OriginSpaceID int64
TargetSpaceID int64
OriginUserID int64
TargetUserID int64
OriginAppID int64
TargetAppID int64
Status DataCopyTaskStatus
DataType DataType
StartTime int64 // 任务开始时间ms
FinishTime int64 // 任务结束时间ms
ExtInfo string
ErrorMsg string // 复制失败的错误信息
}
type DataCopyTaskStatus int
const (
DataCopyTaskStatusCreate DataCopyTaskStatus = 1
DataCopyTaskStatusInProgress DataCopyTaskStatus = 2
DataCopyTaskStatusSuccess DataCopyTaskStatus = 3
DataCopyTaskStatusFail DataCopyTaskStatus = 4
)
type DataType int
const (
DataTypeKnowledge DataType = 1
DataTypeDatabase DataType = 2
DataTypeVariable DataType = 3
)

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package datacopy
import (
"context"
"gorm.io/gorm"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/entity"
)
type DataCopy interface {
CheckAndGenCopyTask(ctx context.Context, req *CheckAndGenCopyTaskReq) (*CheckAndGenCopyTaskResp, error)
UpdateCopyTask(ctx context.Context, req *UpdateCopyTaskReq) error
UpdateCopyTaskWithTX(ctx context.Context, req *UpdateCopyTaskReq, tx *gorm.DB) error
}
type CheckAndGenCopyTaskReq struct {
Task *entity.CopyDataTask
}
type CheckAndGenCopyTaskResp struct {
CopyTaskStatus entity.DataCopyTaskStatus
FailReason string
TargetID int64
}
type UpdateCopyTaskReq struct {
Task *entity.CopyDataTask
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package convert
import (
"github.com/coze-dev/coze-studio/backend/domain/datacopy/entity"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/internal/dal/model"
)
func ConvertToDataCopyTaskModel(task *entity.CopyDataTask) *model.DataCopyTask {
return &model.DataCopyTask{
MasterTaskID: task.TaskUniqKey,
OriginDataID: task.OriginDataID,
TargetDataID: task.TargetDataID,
OriginSpaceID: task.OriginSpaceID,
TargetSpaceID: task.TargetSpaceID,
OriginUserID: task.OriginUserID,
TargetUserID: task.TargetUserID,
OriginAppID: task.OriginAppID,
TargetAppID: task.TargetAppID,
DataType: int32(task.DataType),
Status: int32(task.Status),
StartTime: task.StartTime,
FinishTime: task.FinishTime,
ExtInfo: task.ExtInfo,
ErrorMsg: task.ErrorMsg,
// ID: auto_increment
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dao
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/internal/dal/model"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/internal/dal/query"
)
type DataCopyTaskRepo interface {
UpsertCopyTask(ctx context.Context, task *model.DataCopyTask) error
UpsertCopyTaskWithTX(ctx context.Context, task *model.DataCopyTask, tx *gorm.DB) error
GetCopyTask(ctx context.Context, taskID string, originDataID int64, dataType int32) (*model.DataCopyTask, error)
}
type dataCopyTaskDAO struct {
db *gorm.DB
query *query.Query
}
func NewDataCopyTaskDAO(db *gorm.DB) DataCopyTaskRepo {
return &dataCopyTaskDAO{db: db, query: query.Use(db)}
}
func (dao *dataCopyTaskDAO) UpsertCopyTask(ctx context.Context, task *model.DataCopyTask) error {
return dao.query.DataCopyTask.WithContext(ctx).Debug().Clauses(
clause.OnConflict{
UpdateAll: true,
},
).Create(task)
}
func (dao *dataCopyTaskDAO) GetCopyTask(ctx context.Context, taskID string, originDataID int64, dataType int32) (*model.DataCopyTask, error) {
q := dao.query.DataCopyTask
return q.WithContext(ctx).Debug().Where(q.MasterTaskID.Eq(taskID)).Where(q.OriginDataID.Eq(originDataID)).Where(q.DataType.Eq(dataType)).First()
}
func (dao *dataCopyTaskDAO) UpsertCopyTaskWithTX(ctx context.Context, task *model.DataCopyTask, tx *gorm.DB) error {
return tx.WithContext(ctx).Model(&model.DataCopyTask{}).Debug().Clauses(
clause.OnConflict{
// UpdateAll: true,
Columns: []clause.Column{},
},
).Create(task).Error
}

View File

@@ -0,0 +1,32 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
const TableNameDataCopyTask = "data_copy_task"
// DataCopyTask data方向复制任务记录表
type DataCopyTask struct {
MasterTaskID string `gorm:"column:master_task_id;not null;comment:复制任务ID" json:"master_task_id"` // 复制任务ID
OriginDataID int64 `gorm:"column:origin_data_id;not null;comment:源id" json:"origin_data_id"` // 源id
TargetDataID int64 `gorm:"column:target_data_id;not null;comment:目标id" json:"target_data_id"` // 目标id
OriginSpaceID int64 `gorm:"column:origin_space_id;not null;comment:源团队空间" json:"origin_space_id"` // 源团队空间
TargetSpaceID int64 `gorm:"column:target_space_id;not null;comment:目标团队空间" json:"target_space_id"` // 目标团队空间
OriginUserID int64 `gorm:"column:origin_user_id;not null;comment:源用户ID" json:"origin_user_id"` // 源用户ID
TargetUserID int64 `gorm:"column:target_user_id;not null;comment:目标用户ID" json:"target_user_id"` // 目标用户ID
OriginAppID int64 `gorm:"column:origin_app_id;not null;comment:源AppID" json:"origin_app_id"` // 源AppID
TargetAppID int64 `gorm:"column:target_app_id;not null;comment:目标AppID" json:"target_app_id"` // 目标AppID
DataType int32 `gorm:"column:data_type;not null;comment:数据类型 1:knowledge, 2:database" json:"data_type"` // 数据类型 1:knowledge, 2:database
ExtInfo string `gorm:"column:ext_info;not null;comment:存储额外信息" json:"ext_info"` // 存储额外信息
StartTime int64 `gorm:"column:start_time;not null;comment:任务开始时间" json:"start_time"` // 任务开始时间
FinishTime int64 `gorm:"column:finish_time;comment:任务结束时间" json:"finish_time"` // 任务结束时间
Status int32 `gorm:"column:status;not null;default:1;comment:1:创建 2:执行中 3:成功 4:失败" json:"status"` // 1:创建 2:执行中 3:成功 4:失败
ErrorMsg string `gorm:"column:error_msg;comment:错误信息" json:"error_msg"` // 错误信息
ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:ID" json:"id"` // ID
}
// TableName DataCopyTask's table name
func (*DataCopyTask) TableName() string {
return TableNameDataCopyTask
}

View File

@@ -0,0 +1,441 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen"
"gorm.io/gen/field"
"gorm.io/plugin/dbresolver"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/internal/dal/model"
)
func newDataCopyTask(db *gorm.DB, opts ...gen.DOOption) dataCopyTask {
_dataCopyTask := dataCopyTask{}
_dataCopyTask.dataCopyTaskDo.UseDB(db, opts...)
_dataCopyTask.dataCopyTaskDo.UseModel(&model.DataCopyTask{})
tableName := _dataCopyTask.dataCopyTaskDo.TableName()
_dataCopyTask.ALL = field.NewAsterisk(tableName)
_dataCopyTask.MasterTaskID = field.NewString(tableName, "master_task_id")
_dataCopyTask.OriginDataID = field.NewInt64(tableName, "origin_data_id")
_dataCopyTask.TargetDataID = field.NewInt64(tableName, "target_data_id")
_dataCopyTask.OriginSpaceID = field.NewInt64(tableName, "origin_space_id")
_dataCopyTask.TargetSpaceID = field.NewInt64(tableName, "target_space_id")
_dataCopyTask.OriginUserID = field.NewInt64(tableName, "origin_user_id")
_dataCopyTask.TargetUserID = field.NewInt64(tableName, "target_user_id")
_dataCopyTask.OriginAppID = field.NewInt64(tableName, "origin_app_id")
_dataCopyTask.TargetAppID = field.NewInt64(tableName, "target_app_id")
_dataCopyTask.DataType = field.NewInt32(tableName, "data_type")
_dataCopyTask.ExtInfo = field.NewString(tableName, "ext_info")
_dataCopyTask.StartTime = field.NewInt64(tableName, "start_time")
_dataCopyTask.FinishTime = field.NewInt64(tableName, "finish_time")
_dataCopyTask.Status = field.NewInt32(tableName, "status")
_dataCopyTask.ErrorMsg = field.NewString(tableName, "error_msg")
_dataCopyTask.ID = field.NewInt64(tableName, "id")
_dataCopyTask.fillFieldMap()
return _dataCopyTask
}
// dataCopyTask data方向复制任务记录表
type dataCopyTask struct {
dataCopyTaskDo
ALL field.Asterisk
MasterTaskID field.String // 复制任务ID
OriginDataID field.Int64 // 源id
TargetDataID field.Int64 // 目标id
OriginSpaceID field.Int64 // 源团队空间
TargetSpaceID field.Int64 // 目标团队空间
OriginUserID field.Int64 // 源用户ID
TargetUserID field.Int64 // 目标用户ID
OriginAppID field.Int64 // 源AppID
TargetAppID field.Int64 // 目标AppID
DataType field.Int32 // 数据类型 1:knowledge, 2:database
ExtInfo field.String // 存储额外信息
StartTime field.Int64 // 任务开始时间
FinishTime field.Int64 // 任务结束时间
Status field.Int32 // 1:创建 2:执行中 3:成功 4:失败
ErrorMsg field.String // 错误信息
ID field.Int64 // ID
fieldMap map[string]field.Expr
}
func (d dataCopyTask) Table(newTableName string) *dataCopyTask {
d.dataCopyTaskDo.UseTable(newTableName)
return d.updateTableName(newTableName)
}
func (d dataCopyTask) As(alias string) *dataCopyTask {
d.dataCopyTaskDo.DO = *(d.dataCopyTaskDo.As(alias).(*gen.DO))
return d.updateTableName(alias)
}
func (d *dataCopyTask) updateTableName(table string) *dataCopyTask {
d.ALL = field.NewAsterisk(table)
d.MasterTaskID = field.NewString(table, "master_task_id")
d.OriginDataID = field.NewInt64(table, "origin_data_id")
d.TargetDataID = field.NewInt64(table, "target_data_id")
d.OriginSpaceID = field.NewInt64(table, "origin_space_id")
d.TargetSpaceID = field.NewInt64(table, "target_space_id")
d.OriginUserID = field.NewInt64(table, "origin_user_id")
d.TargetUserID = field.NewInt64(table, "target_user_id")
d.OriginAppID = field.NewInt64(table, "origin_app_id")
d.TargetAppID = field.NewInt64(table, "target_app_id")
d.DataType = field.NewInt32(table, "data_type")
d.ExtInfo = field.NewString(table, "ext_info")
d.StartTime = field.NewInt64(table, "start_time")
d.FinishTime = field.NewInt64(table, "finish_time")
d.Status = field.NewInt32(table, "status")
d.ErrorMsg = field.NewString(table, "error_msg")
d.ID = field.NewInt64(table, "id")
d.fillFieldMap()
return d
}
func (d *dataCopyTask) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := d.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (d *dataCopyTask) fillFieldMap() {
d.fieldMap = make(map[string]field.Expr, 16)
d.fieldMap["master_task_id"] = d.MasterTaskID
d.fieldMap["origin_data_id"] = d.OriginDataID
d.fieldMap["target_data_id"] = d.TargetDataID
d.fieldMap["origin_space_id"] = d.OriginSpaceID
d.fieldMap["target_space_id"] = d.TargetSpaceID
d.fieldMap["origin_user_id"] = d.OriginUserID
d.fieldMap["target_user_id"] = d.TargetUserID
d.fieldMap["origin_app_id"] = d.OriginAppID
d.fieldMap["target_app_id"] = d.TargetAppID
d.fieldMap["data_type"] = d.DataType
d.fieldMap["ext_info"] = d.ExtInfo
d.fieldMap["start_time"] = d.StartTime
d.fieldMap["finish_time"] = d.FinishTime
d.fieldMap["status"] = d.Status
d.fieldMap["error_msg"] = d.ErrorMsg
d.fieldMap["id"] = d.ID
}
func (d dataCopyTask) clone(db *gorm.DB) dataCopyTask {
d.dataCopyTaskDo.ReplaceConnPool(db.Statement.ConnPool)
return d
}
func (d dataCopyTask) replaceDB(db *gorm.DB) dataCopyTask {
d.dataCopyTaskDo.ReplaceDB(db)
return d
}
type dataCopyTaskDo struct{ gen.DO }
type IDataCopyTaskDo interface {
gen.SubQuery
Debug() IDataCopyTaskDo
WithContext(ctx context.Context) IDataCopyTaskDo
WithResult(fc func(tx gen.Dao)) gen.ResultInfo
ReplaceDB(db *gorm.DB)
ReadDB() IDataCopyTaskDo
WriteDB() IDataCopyTaskDo
As(alias string) gen.Dao
Session(config *gorm.Session) IDataCopyTaskDo
Columns(cols ...field.Expr) gen.Columns
Clauses(conds ...clause.Expression) IDataCopyTaskDo
Not(conds ...gen.Condition) IDataCopyTaskDo
Or(conds ...gen.Condition) IDataCopyTaskDo
Select(conds ...field.Expr) IDataCopyTaskDo
Where(conds ...gen.Condition) IDataCopyTaskDo
Order(conds ...field.Expr) IDataCopyTaskDo
Distinct(cols ...field.Expr) IDataCopyTaskDo
Omit(cols ...field.Expr) IDataCopyTaskDo
Join(table schema.Tabler, on ...field.Expr) IDataCopyTaskDo
LeftJoin(table schema.Tabler, on ...field.Expr) IDataCopyTaskDo
RightJoin(table schema.Tabler, on ...field.Expr) IDataCopyTaskDo
Group(cols ...field.Expr) IDataCopyTaskDo
Having(conds ...gen.Condition) IDataCopyTaskDo
Limit(limit int) IDataCopyTaskDo
Offset(offset int) IDataCopyTaskDo
Count() (count int64, err error)
Scopes(funcs ...func(gen.Dao) gen.Dao) IDataCopyTaskDo
Unscoped() IDataCopyTaskDo
Create(values ...*model.DataCopyTask) error
CreateInBatches(values []*model.DataCopyTask, batchSize int) error
Save(values ...*model.DataCopyTask) error
First() (*model.DataCopyTask, error)
Take() (*model.DataCopyTask, error)
Last() (*model.DataCopyTask, error)
Find() ([]*model.DataCopyTask, error)
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.DataCopyTask, err error)
FindInBatches(result *[]*model.DataCopyTask, batchSize int, fc func(tx gen.Dao, batch int) error) error
Pluck(column field.Expr, dest interface{}) error
Delete(...*model.DataCopyTask) (info gen.ResultInfo, err error)
Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
Updates(value interface{}) (info gen.ResultInfo, err error)
UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
UpdateFrom(q gen.SubQuery) gen.Dao
Attrs(attrs ...field.AssignExpr) IDataCopyTaskDo
Assign(attrs ...field.AssignExpr) IDataCopyTaskDo
Joins(fields ...field.RelationField) IDataCopyTaskDo
Preload(fields ...field.RelationField) IDataCopyTaskDo
FirstOrInit() (*model.DataCopyTask, error)
FirstOrCreate() (*model.DataCopyTask, error)
FindByPage(offset int, limit int) (result []*model.DataCopyTask, count int64, err error)
ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
Scan(result interface{}) (err error)
Returning(value interface{}, columns ...string) IDataCopyTaskDo
UnderlyingDB() *gorm.DB
schema.Tabler
}
func (d dataCopyTaskDo) Debug() IDataCopyTaskDo {
return d.withDO(d.DO.Debug())
}
func (d dataCopyTaskDo) WithContext(ctx context.Context) IDataCopyTaskDo {
return d.withDO(d.DO.WithContext(ctx))
}
func (d dataCopyTaskDo) ReadDB() IDataCopyTaskDo {
return d.Clauses(dbresolver.Read)
}
func (d dataCopyTaskDo) WriteDB() IDataCopyTaskDo {
return d.Clauses(dbresolver.Write)
}
func (d dataCopyTaskDo) Session(config *gorm.Session) IDataCopyTaskDo {
return d.withDO(d.DO.Session(config))
}
func (d dataCopyTaskDo) Clauses(conds ...clause.Expression) IDataCopyTaskDo {
return d.withDO(d.DO.Clauses(conds...))
}
func (d dataCopyTaskDo) Returning(value interface{}, columns ...string) IDataCopyTaskDo {
return d.withDO(d.DO.Returning(value, columns...))
}
func (d dataCopyTaskDo) Not(conds ...gen.Condition) IDataCopyTaskDo {
return d.withDO(d.DO.Not(conds...))
}
func (d dataCopyTaskDo) Or(conds ...gen.Condition) IDataCopyTaskDo {
return d.withDO(d.DO.Or(conds...))
}
func (d dataCopyTaskDo) Select(conds ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.Select(conds...))
}
func (d dataCopyTaskDo) Where(conds ...gen.Condition) IDataCopyTaskDo {
return d.withDO(d.DO.Where(conds...))
}
func (d dataCopyTaskDo) Order(conds ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.Order(conds...))
}
func (d dataCopyTaskDo) Distinct(cols ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.Distinct(cols...))
}
func (d dataCopyTaskDo) Omit(cols ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.Omit(cols...))
}
func (d dataCopyTaskDo) Join(table schema.Tabler, on ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.Join(table, on...))
}
func (d dataCopyTaskDo) LeftJoin(table schema.Tabler, on ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.LeftJoin(table, on...))
}
func (d dataCopyTaskDo) RightJoin(table schema.Tabler, on ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.RightJoin(table, on...))
}
func (d dataCopyTaskDo) Group(cols ...field.Expr) IDataCopyTaskDo {
return d.withDO(d.DO.Group(cols...))
}
func (d dataCopyTaskDo) Having(conds ...gen.Condition) IDataCopyTaskDo {
return d.withDO(d.DO.Having(conds...))
}
func (d dataCopyTaskDo) Limit(limit int) IDataCopyTaskDo {
return d.withDO(d.DO.Limit(limit))
}
func (d dataCopyTaskDo) Offset(offset int) IDataCopyTaskDo {
return d.withDO(d.DO.Offset(offset))
}
func (d dataCopyTaskDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IDataCopyTaskDo {
return d.withDO(d.DO.Scopes(funcs...))
}
func (d dataCopyTaskDo) Unscoped() IDataCopyTaskDo {
return d.withDO(d.DO.Unscoped())
}
func (d dataCopyTaskDo) Create(values ...*model.DataCopyTask) error {
if len(values) == 0 {
return nil
}
return d.DO.Create(values)
}
func (d dataCopyTaskDo) CreateInBatches(values []*model.DataCopyTask, batchSize int) error {
return d.DO.CreateInBatches(values, batchSize)
}
// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (d dataCopyTaskDo) Save(values ...*model.DataCopyTask) error {
if len(values) == 0 {
return nil
}
return d.DO.Save(values)
}
func (d dataCopyTaskDo) First() (*model.DataCopyTask, error) {
if result, err := d.DO.First(); err != nil {
return nil, err
} else {
return result.(*model.DataCopyTask), nil
}
}
func (d dataCopyTaskDo) Take() (*model.DataCopyTask, error) {
if result, err := d.DO.Take(); err != nil {
return nil, err
} else {
return result.(*model.DataCopyTask), nil
}
}
func (d dataCopyTaskDo) Last() (*model.DataCopyTask, error) {
if result, err := d.DO.Last(); err != nil {
return nil, err
} else {
return result.(*model.DataCopyTask), nil
}
}
func (d dataCopyTaskDo) Find() ([]*model.DataCopyTask, error) {
result, err := d.DO.Find()
return result.([]*model.DataCopyTask), err
}
func (d dataCopyTaskDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.DataCopyTask, err error) {
buf := make([]*model.DataCopyTask, 0, batchSize)
err = d.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
defer func() { results = append(results, buf...) }()
return fc(tx, batch)
})
return results, err
}
func (d dataCopyTaskDo) FindInBatches(result *[]*model.DataCopyTask, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return d.DO.FindInBatches(result, batchSize, fc)
}
func (d dataCopyTaskDo) Attrs(attrs ...field.AssignExpr) IDataCopyTaskDo {
return d.withDO(d.DO.Attrs(attrs...))
}
func (d dataCopyTaskDo) Assign(attrs ...field.AssignExpr) IDataCopyTaskDo {
return d.withDO(d.DO.Assign(attrs...))
}
func (d dataCopyTaskDo) Joins(fields ...field.RelationField) IDataCopyTaskDo {
for _, _f := range fields {
d = *d.withDO(d.DO.Joins(_f))
}
return &d
}
func (d dataCopyTaskDo) Preload(fields ...field.RelationField) IDataCopyTaskDo {
for _, _f := range fields {
d = *d.withDO(d.DO.Preload(_f))
}
return &d
}
func (d dataCopyTaskDo) FirstOrInit() (*model.DataCopyTask, error) {
if result, err := d.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*model.DataCopyTask), nil
}
}
func (d dataCopyTaskDo) FirstOrCreate() (*model.DataCopyTask, error) {
if result, err := d.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*model.DataCopyTask), nil
}
}
func (d dataCopyTaskDo) FindByPage(offset int, limit int) (result []*model.DataCopyTask, count int64, err error) {
result, err = d.Offset(offset).Limit(limit).Find()
if err != nil {
return
}
if size := len(result); 0 < limit && 0 < size && size < limit {
count = int64(size + offset)
return
}
count, err = d.Offset(-1).Limit(-1).Count()
return
}
func (d dataCopyTaskDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = d.Count()
if err != nil {
return
}
err = d.Offset(offset).Limit(limit).Scan(result)
return
}
func (d dataCopyTaskDo) Scan(result interface{}) (err error) {
return d.DO.Scan(result)
}
func (d dataCopyTaskDo) Delete(models ...*model.DataCopyTask) (result gen.ResultInfo, err error) {
return d.DO.Delete(models)
}
func (d *dataCopyTaskDo) withDO(do gen.Dao) *dataCopyTaskDo {
d.DO = *do.(*gen.DO)
return d
}

View File

@@ -0,0 +1,103 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"database/sql"
"gorm.io/gorm"
"gorm.io/gen"
"gorm.io/plugin/dbresolver"
)
var (
Q = new(Query)
DataCopyTask *dataCopyTask
)
func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
*Q = *Use(db, opts...)
DataCopyTask = &Q.DataCopyTask
}
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
return &Query{
db: db,
DataCopyTask: newDataCopyTask(db, opts...),
}
}
type Query struct {
db *gorm.DB
DataCopyTask dataCopyTask
}
func (q *Query) Available() bool { return q.db != nil }
func (q *Query) clone(db *gorm.DB) *Query {
return &Query{
db: db,
DataCopyTask: q.DataCopyTask.clone(db),
}
}
func (q *Query) ReadDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}
func (q *Query) WriteDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
return &Query{
db: db,
DataCopyTask: q.DataCopyTask.replaceDB(db),
}
}
type queryCtx struct {
DataCopyTask IDataCopyTaskDo
}
func (q *Query) WithContext(ctx context.Context) *queryCtx {
return &queryCtx{
DataCopyTask: q.DataCopyTask.WithContext(ctx),
}
}
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
tx := q.db.Begin(opts...)
return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}
type QueryTx struct {
*Query
Error error
}
func (q *QueryTx) Commit() error {
return q.db.Commit().Error
}
func (q *QueryTx) Rollback() error {
return q.db.Rollback().Error
}
func (q *QueryTx) SavePoint(name string) error {
return q.db.SavePoint(name).Error
}
func (q *QueryTx) RollbackTo(name string) error {
return q.db.RollbackTo(name).Error
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package service
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/coze-dev/coze-studio/backend/domain/datacopy"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/entity"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/internal/convert"
"github.com/coze-dev/coze-studio/backend/domain/datacopy/internal/dal/dao"
"github.com/coze-dev/coze-studio/backend/infra/impl/idgen"
)
type DataCopySVCConfig struct {
DB *gorm.DB // required
IDGen idgen.IDGenerator // required
}
func NewDataCopySVC(config *DataCopySVCConfig) datacopy.DataCopy {
svc := &dataCopySVC{
dataCopyTaskRepo: dao.NewDataCopyTaskDAO(config.DB),
idgen: config.IDGen,
}
return svc
}
type dataCopySVC struct {
dataCopyTaskRepo dao.DataCopyTaskRepo
idgen idgen.IDGenerator
}
func (svc *dataCopySVC) CheckAndGenCopyTask(ctx context.Context, req *datacopy.CheckAndGenCopyTaskReq) (*datacopy.CheckAndGenCopyTaskResp, error) {
if req == nil || req.Task == nil {
return nil, errors.New("invalid request")
}
if req.Task.OriginDataID == 0 {
return nil, errors.New("invalid origin data id")
}
if len(req.Task.TaskUniqKey) == 0 {
return nil, errors.New("invalid task uniq key")
}
var err error
resp := datacopy.CheckAndGenCopyTaskResp{}
// 检查是否已经存在任务
task, err := svc.dataCopyTaskRepo.GetCopyTask(ctx, req.Task.TaskUniqKey, req.Task.OriginDataID, int32(req.Task.DataType))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
if task != nil {
taskStatus := entity.DataCopyTaskStatus(task.Status)
resp.CopyTaskStatus = taskStatus
resp.TargetID = task.TargetDataID
return &resp, nil
}
task = convert.ConvertToDataCopyTaskModel(req.Task)
task.Status = int32(entity.DataCopyTaskStatusCreate)
err = svc.dataCopyTaskRepo.UpsertCopyTask(ctx, task)
if err != nil {
return nil, err
}
resp.CopyTaskStatus = entity.DataCopyTaskStatusCreate
resp.TargetID = task.TargetDataID
return &resp, nil
}
func (svc *dataCopySVC) UpdateCopyTask(ctx context.Context, req *datacopy.UpdateCopyTaskReq) error {
task := convert.ConvertToDataCopyTaskModel(req.Task)
return svc.dataCopyTaskRepo.UpsertCopyTask(ctx, task)
}
func (svc *dataCopySVC) UpdateCopyTaskWithTX(ctx context.Context, req *datacopy.UpdateCopyTaskReq, tx *gorm.DB) error {
task := convert.ConvertToDataCopyTaskModel(req.Task)
return svc.dataCopyTaskRepo.UpsertCopyTaskWithTX(ctx, task, tx)
}