package db import ( "context" "database/sql" "errors" "strings" ) // ItemPublishBatches 管理商品批量发布批次及其明细行的持久化。 // // 一个批次对应一次 Excel/CSV 导入,包含若干明细行(每行一个待发布商品)。 // 发布 worker 按 row_no 顺序逐行发布,通过状态机(pending→running→success/failed) // 跟踪进度,支持失败重置(ResetFailed)和实时计数(Recount)。 // ItemPublishBatches 用于本次流程后续判断的商品发布批次列表 type ItemPublishBatches struct{ DB *sql.DB } // ErrPublishBatchChanged 表示批次在读取与状态切换之间被其他 worker 更新,调用方可安全重试。 var ErrPublishBatchChanged = errors.New("批量发布任务状态已变化") // ItemPublishBatch 是一个发布批次的元信息(不含明细行)。 type ItemPublishBatch struct { ID string // 批次 ID(上传时生成,UUID 形式) UserID int64 // 所属用户 DefaultCookieID string // 默认发布账号(明细行未指定账号时回退到此) Filename string // 原始上传文件名 UploadDir string // 图片资源目录(发布时读取商品图片的根目录) LocationJSON string // 批次统一使用的发货地 JSON // PublishIntervalSeconds 是相邻两次最终商品发布请求的最小间隔秒数。 PublishIntervalSeconds int // LastPublishStartedAtMillis 是最近一次最终商品发布请求开始的 Unix 毫秒时间戳。 LastPublishStartedAtMillis int64 Status string // 批次状态:pending/running/completed/partially_failed/failed TotalCount int // 明细行总数(Recount 维护) SuccessCount int // 成功数(Recount 维护) FailedCount int // 失败数(Recount 维护) WorkerToken string LeaseExpiresAt int64 CreatedAt string UpdatedAt string } // ItemPublishBatchRow 是一条待发布的商品明细。 type ItemPublishBatchRow struct { ID int64 // 自增主键,worker 按此标记状态 BatchID string // 所属批次 ID RowNo int // 批次内序号(1 起,按导入顺序) CookieID string // 发布到哪个账号 Title string Description string Price string OriginalPrice string Quantity int PostageMode string // 邮费模式:free/buyer/seller Postage string ImagesJSON string // 图片引用 JSON 数组(相对 UploadDir 的路径) CategoryJSON string // 用户指定的优先类目 JSON;为空时自动识别 AutomationJSON string // 发布后自动创建的自动化规则配置 JSON Status string // pending/running/success/failed ItemID string // 发布成功后回填的闲鱼商品 ID ItemURL string // 发布成功后回填的商品 URL ErrorMessage string // 失败原因 FailureKind string // validation/publish/interrupted WorkerToken string RawJSON string // 发布接口原始返回 JSON CreatedAt string UpdatedAt string } // Create 在单事务内创建批次及其全部明细行。 // 明细行的 quantity/postage_mode/status/images_json/raw_json/automation_json 缺省值在此补齐。 // total_count 取 len(rows),success/failed 初始为 0。 // Create 创建当前值。 func (b *ItemPublishBatches) Create(ctx context.Context, batch *ItemPublishBatch, rows []ItemPublishBatchRow) error { if strings.TrimSpace(batch.LocationJSON) == "" { batch.LocationJSON = "{}" } if batch.PublishIntervalSeconds <= 0 { batch.PublishIntervalSeconds = 5 } // tx、err 用于本次流程后续判断的tx、err tx, err := b.DB.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() if // err 用于本次流程后续判断的err _, err := tx.ExecContext(ctx, `INSERT INTO item_publish_batches (id,user_id,default_cookie_id,filename,upload_dir,location_json,publish_interval_seconds,status,total_count,success_count,failed_count) VALUES (?,?,?,?,?,?,?,?,?,?,?)`, batch.ID, batch.UserID, batch.DefaultCookieID, batch.Filename, batch.UploadDir, batch.LocationJSON, batch.PublishIntervalSeconds, batch.Status, len(rows), 0, 0); err != nil { return err } // row 表示当前遍历过程中的row for _, row := range rows { if row.Quantity <= 0 { row.Quantity = 1 } if row.PostageMode == "" { row.PostageMode = "free" } if row.Status == "" { row.Status = "pending" } if row.ImagesJSON == "" { row.ImagesJSON = "[]" } if row.RawJSON == "" { row.RawJSON = "{}" } if row.CategoryJSON == "" { row.CategoryJSON = "{}" } if row.AutomationJSON == "" { row.AutomationJSON = "{}" } if // err 用于本次流程后续判断的err _, err := tx.ExecContext(ctx, `INSERT INTO item_publish_batch_rows (batch_id,row_no,cookie_id,title,description,price,original_price,quantity,postage_mode,postage, images_json,category_json,automation_json,status,item_id,item_url,error_message,failure_kind,raw_json) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, batch.ID, row.RowNo, row.CookieID, row.Title, row.Description, row.Price, row.OriginalPrice, row.Quantity, row.PostageMode, row.Postage, row.ImagesJSON, row.CategoryJSON, row.AutomationJSON, row.Status, row.ItemID, row.ItemURL, row.ErrorMessage, row.FailureKind, row.RawJSON); err != nil { return err } } return tx.Commit() } // Get 按 ID 取批次(含 user_id 隔离校验)。未找到返回 ErrNotFound。 func (b *ItemPublishBatches) Get(ctx context.Context, userID int64, id string) (*ItemPublishBatch, error) { // out 用于本次流程后续判断的out var out ItemPublishBatch // err 用于本次流程后续判断的err err := b.DB.QueryRowContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,COALESCE(location_json,'{}'),COALESCE(publish_interval_seconds,5),COALESCE(last_publish_started_at_millis,0),status,total_count,success_count,failed_count, COALESCE(worker_token,''),COALESCE(lease_expires_at,0), created_at,updated_at FROM item_publish_batches WHERE id=? AND user_id=?`, id, userID).Scan( &out.ID, &out.UserID, &out.DefaultCookieID, &out.Filename, &out.UploadDir, &out.LocationJSON, &out.PublishIntervalSeconds, &out.LastPublishStartedAtMillis, &out.Status, &out.TotalCount, &out.SuccessCount, &out.FailedCount, &out.WorkerToken, &out.LeaseExpiresAt, &out.CreatedAt, &out.UpdatedAt) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } return nil, err } return &out, nil } // ListForUser 返回用户最近的批量任务,供页面重载后重新发现运行记录。 func (b *ItemPublishBatches) ListForUser(ctx context.Context, userID int64, limit int) ([]ItemPublishBatch, error) { if limit <= 0 || limit > 100 { limit = 20 } // rows、err 用于本次流程后续判断的rows、err rows, err := b.DB.QueryContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,COALESCE(location_json,'{}'),COALESCE(publish_interval_seconds,5),COALESCE(last_publish_started_at_millis,0),status, total_count,success_count,failed_count,COALESCE(worker_token,''),COALESCE(lease_expires_at,0),created_at,updated_at FROM item_publish_batches WHERE user_id=? ORDER BY created_at DESC,id DESC LIMIT ?`, userID, limit) if err != nil { return nil, err } defer rows.Close() // out 用于本次流程后续判断的out var out []ItemPublishBatch for rows.Next() { // batch 用于本次流程后续判断的批次 var batch ItemPublishBatch if // err 用于本次流程后续判断的err err := rows.Scan(&batch.ID, &batch.UserID, &batch.DefaultCookieID, &batch.Filename, &batch.UploadDir, &batch.LocationJSON, &batch.PublishIntervalSeconds, &batch.LastPublishStartedAtMillis, &batch.Status, &batch.TotalCount, &batch.SuccessCount, &batch.FailedCount, &batch.WorkerToken, &batch.LeaseExpiresAt, &batch.CreatedAt, &batch.UpdatedAt); err != nil { return nil, err } out = append(out, batch) } return out, rows.Err() } // Recoverable 返回租约过期或因进程退出中断、可安全自动续跑的任务。 func (b *ItemPublishBatches) Recoverable(ctx context.Context, now int64, limit int) ([]ItemPublishBatch, error) { if limit <= 0 { limit = 20 } // rows、err 用于本次流程后续判断的rows、err rows, err := b.DB.QueryContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,COALESCE(location_json,'{}'),COALESCE(publish_interval_seconds,5),COALESCE(last_publish_started_at_millis,0),status, total_count,success_count,failed_count,COALESCE(worker_token,''),COALESCE(lease_expires_at,0),created_at,updated_at FROM item_publish_batches b WHERE (b.status IN ('running','canceling') AND (b.lease_expires_at=0 OR b.lease_expires_at 0 { return "partially_failed" } return "failed" } // FinalizeExpiredCancellation 接管租约已过期的两阶段取消任务。远端调用已经开始但 // 结果尚未落库的行必须保持为 uncertain_remote,其余未完成行统一标记为已取消。 // FinalizeExpiredCancellation 封装FinalizeExpiredCancellation业务协调。 func (b *ItemPublishBatches) FinalizeExpiredCancellation(ctx context.Context, batchID string, now int64) (bool, error) { // tx、err 用于本次流程后续判断的tx、err tx, err := b.DB.BeginTx(ctx, nil) if err != nil { return false, err } defer tx.Rollback() // res、err 用于本次流程后续判断的res、err res, err := tx.ExecContext(ctx, `UPDATE item_publish_batches SET status='canceled',worker_token='',lease_expires_at=0,updated_at=CURRENT_TIMESTAMP WHERE id=? AND status='canceling' AND (lease_expires_at=0 OR lease_expires_at