automation 重构期间的配套修改: - browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go - db:cookies/orders_ext/extras/sessions/users/password/repository 等调整 - xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua - server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers - frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage - auth、notify、webui、go.mod 等 本次会话补充: - cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档 - .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库) - internal/webui/static/index.html 重新构建 全量 go build/vet/test + race + 前端 test/build 通过。 Co-Authored-By: Claude Opus 4.8 <[email protected]>
229 lines
7.7 KiB
Go
229 lines
7.7 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
)
|
|
|
|
type ItemPublishBatches struct{ DB *sql.DB }
|
|
|
|
type ItemPublishBatch struct {
|
|
ID string
|
|
UserID int64
|
|
DefaultCookieID string
|
|
Filename string
|
|
UploadDir string
|
|
Status string
|
|
TotalCount int
|
|
SuccessCount int
|
|
FailedCount int
|
|
CreatedAt string
|
|
UpdatedAt string
|
|
}
|
|
|
|
type ItemPublishBatchRow struct {
|
|
ID int64
|
|
BatchID string
|
|
RowNo int
|
|
CookieID string
|
|
Title string
|
|
Description string
|
|
Price string
|
|
OriginalPrice string
|
|
Quantity int
|
|
PostageMode string
|
|
Postage string
|
|
ImagesJSON string
|
|
AutomationJSON string
|
|
Status string
|
|
ItemID string
|
|
ItemURL string
|
|
ErrorMessage string
|
|
RawJSON string
|
|
CreatedAt string
|
|
UpdatedAt string
|
|
}
|
|
|
|
func (b *ItemPublishBatches) Create(ctx context.Context, batch *ItemPublishBatch, rows []ItemPublishBatchRow) error {
|
|
tx, err := b.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err := tx.ExecContext(ctx,
|
|
`INSERT INTO item_publish_batches
|
|
(id,user_id,default_cookie_id,filename,upload_dir,status,total_count,success_count,failed_count)
|
|
VALUES (?,?,?,?,?,?,?,?,?)`,
|
|
batch.ID, batch.UserID, batch.DefaultCookieID, batch.Filename, batch.UploadDir,
|
|
batch.Status, len(rows), 0, 0); err != nil {
|
|
return err
|
|
}
|
|
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.AutomationJSON == "" {
|
|
row.AutomationJSON = "{}"
|
|
}
|
|
if _, 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,automation_json,status,error_message,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.AutomationJSON,
|
|
row.Status, row.ErrorMessage, row.RawJSON); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (b *ItemPublishBatches) Get(ctx context.Context, userID int64, id string) (*ItemPublishBatch, error) {
|
|
var out ItemPublishBatch
|
|
err := b.DB.QueryRowContext(ctx,
|
|
`SELECT id,user_id,default_cookie_id,filename,upload_dir,status,total_count,success_count,failed_count,
|
|
COALESCE(created_at,''),COALESCE(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.Status,
|
|
&out.TotalCount, &out.SuccessCount, &out.FailedCount, &out.CreatedAt, &out.UpdatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
func (b *ItemPublishBatches) Rows(ctx context.Context, batchID string) ([]ItemPublishBatchRow, error) {
|
|
rows, err := b.DB.QueryContext(ctx,
|
|
`SELECT id,batch_id,row_no,cookie_id,title,description,price,original_price,quantity,postage_mode,postage,
|
|
images_json,COALESCE(automation_json,'{}'),status,item_id,item_url,error_message,
|
|
raw_json,COALESCE(created_at,''),COALESCE(updated_at,'')
|
|
FROM item_publish_batch_rows WHERE batch_id=? ORDER BY row_no`, batchID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []ItemPublishBatchRow{}
|
|
for rows.Next() {
|
|
var r ItemPublishBatchRow
|
|
if err := rows.Scan(&r.ID, &r.BatchID, &r.RowNo, &r.CookieID, &r.Title, &r.Description, &r.Price,
|
|
&r.OriginalPrice, &r.Quantity, &r.PostageMode, &r.Postage, &r.ImagesJSON, &r.AutomationJSON,
|
|
&r.Status, &r.ItemID, &r.ItemURL, &r.ErrorMessage, &r.RawJSON, &r.CreatedAt,
|
|
&r.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (b *ItemPublishBatches) PendingRows(ctx context.Context, batchID string, failedOnly bool) ([]ItemPublishBatchRow, error) {
|
|
statuses := "('pending')"
|
|
if failedOnly {
|
|
statuses = "('failed')"
|
|
}
|
|
rows, err := b.DB.QueryContext(ctx,
|
|
`SELECT id,batch_id,row_no,cookie_id,title,description,price,original_price,quantity,postage_mode,postage,
|
|
images_json,COALESCE(automation_json,'{}'),status,item_id,item_url,error_message,
|
|
raw_json,COALESCE(created_at,''),COALESCE(updated_at,'')
|
|
FROM item_publish_batch_rows WHERE batch_id=? AND status IN `+statuses+` ORDER BY row_no`, batchID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []ItemPublishBatchRow{}
|
|
for rows.Next() {
|
|
var r ItemPublishBatchRow
|
|
if err := rows.Scan(&r.ID, &r.BatchID, &r.RowNo, &r.CookieID, &r.Title, &r.Description, &r.Price,
|
|
&r.OriginalPrice, &r.Quantity, &r.PostageMode, &r.Postage, &r.ImagesJSON, &r.AutomationJSON,
|
|
&r.Status, &r.ItemID, &r.ItemURL, &r.ErrorMessage, &r.RawJSON, &r.CreatedAt,
|
|
&r.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (b *ItemPublishBatches) SetBatchStatus(ctx context.Context, batchID, status string) error {
|
|
_, err := b.DB.ExecContext(ctx,
|
|
`UPDATE item_publish_batches SET status=?,updated_at=CURRENT_TIMESTAMP WHERE id=?`, status, batchID)
|
|
return err
|
|
}
|
|
|
|
func (b *ItemPublishBatches) BatchStatus(ctx context.Context, batchID string) (string, error) {
|
|
var status string
|
|
err := b.DB.QueryRowContext(ctx, `SELECT status FROM item_publish_batches WHERE id=?`, batchID).Scan(&status)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", ErrNotFound
|
|
}
|
|
return "", err
|
|
}
|
|
return status, nil
|
|
}
|
|
|
|
func (b *ItemPublishBatches) MarkRowRunning(ctx context.Context, rowID int64) error {
|
|
_, err := b.DB.ExecContext(ctx,
|
|
`UPDATE item_publish_batch_rows SET status='running',error_message='',updated_at=CURRENT_TIMESTAMP WHERE id=?`, rowID)
|
|
return err
|
|
}
|
|
|
|
func (b *ItemPublishBatches) MarkRowSuccess(ctx context.Context, rowID int64, itemID, itemURL, rawJSON string) error {
|
|
if rawJSON == "" {
|
|
rawJSON = "{}"
|
|
}
|
|
_, err := b.DB.ExecContext(ctx,
|
|
`UPDATE item_publish_batch_rows
|
|
SET status='success',item_id=?,item_url=?,error_message='',raw_json=?,updated_at=CURRENT_TIMESTAMP
|
|
WHERE id=?`, itemID, itemURL, rawJSON, rowID)
|
|
return err
|
|
}
|
|
|
|
func (b *ItemPublishBatches) MarkRowFailed(ctx context.Context, rowID int64, message string) error {
|
|
_, err := b.DB.ExecContext(ctx,
|
|
`UPDATE item_publish_batch_rows SET status='failed',error_message=?,updated_at=CURRENT_TIMESTAMP WHERE id=?`,
|
|
message, rowID)
|
|
return err
|
|
}
|
|
|
|
func (b *ItemPublishBatches) ResetFailed(ctx context.Context, batchID string) error {
|
|
_, err := b.DB.ExecContext(ctx,
|
|
`UPDATE item_publish_batch_rows
|
|
SET status='pending',error_message='',updated_at=CURRENT_TIMESTAMP
|
|
WHERE batch_id=? AND status='failed'`, batchID)
|
|
return err
|
|
}
|
|
|
|
func (b *ItemPublishBatches) Recount(ctx context.Context, batchID string) error {
|
|
var total, success, failed int
|
|
if err := b.DB.QueryRowContext(ctx,
|
|
`SELECT COUNT(*),
|
|
COALESCE(SUM(CASE WHEN status='success' THEN 1 ELSE 0 END),0),
|
|
COALESCE(SUM(CASE WHEN status='failed' THEN 1 ELSE 0 END),0)
|
|
FROM item_publish_batch_rows WHERE batch_id=?`, batchID).Scan(&total, &success, &failed); err != nil {
|
|
return err
|
|
}
|
|
_, err := b.DB.ExecContext(ctx,
|
|
`UPDATE item_publish_batches
|
|
SET total_count=?,success_count=?,failed_count=?,updated_at=CURRENT_TIMESTAMP
|
|
WHERE id=?`, total, success, failed, batchID)
|
|
return err
|
|
}
|