初始
This commit is contained in:
46
api/project/pv_update.go
Normal file
46
api/project/pv_update.go
Normal file
@ -0,0 +1,46 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// 根据 ID 更新 pv_module 数据库的信息
|
||||
type PvModuleUpdateBatchReq struct {
|
||||
g.Meta `path:"updateBatch" method:"post" tags:"项目首页相关" summary:"批量更新数据"`
|
||||
Modules []PVModule
|
||||
}
|
||||
|
||||
type PvModuleUpdateRes struct {
|
||||
}
|
||||
|
||||
// PvModuleUpdateBatch 根据ID批量更新对应的pv_module数据
|
||||
func (p Project) PvModuleUpdateBatch(ctx context.Context, req *PvModuleUpdateBatchReq) (res *PvModuleUpdateRes, err error) {
|
||||
res = &PvModuleUpdateRes{}
|
||||
batchSize := 300 // 每批处理的数据量
|
||||
|
||||
for i := 0; i < len(req.Modules); i += batchSize {
|
||||
end := i + batchSize
|
||||
if end > len(req.Modules) {
|
||||
end = len(req.Modules)
|
||||
}
|
||||
batch := req.Modules[i:end]
|
||||
|
||||
for _, module := range batch {
|
||||
dataMap := g.Map{
|
||||
"detail": module.Detail,
|
||||
}
|
||||
// 更新操作
|
||||
if _, err := g.Model("pv_module").Data(dataMap).Where("id", module.ID).Update(); err != nil {
|
||||
fmt.Printf("Error updating module ID %d: %v\n", module.ID, err)
|
||||
return nil, err // 如果更新过程中出现错误,提前返回
|
||||
}
|
||||
}
|
||||
// 可以在这里加入日志,记录这一批次的处理情况
|
||||
fmt.Printf("Updated a batch of %d modules starting from ID %d\n", len(batch), batch[0].ID)
|
||||
}
|
||||
|
||||
// 返回更新结果
|
||||
return res, nil
|
||||
}
|
Reference in New Issue
Block a user