feat: 增加真实采购任务安全模式 (#98)
This commit is contained in:
+45
-10
@@ -30,10 +30,17 @@ import (
|
||||
//
|
||||
// InnoDB 事务用 FOR UPDATE SKIP LOCKED 锁住一条候选任务。领取状态和
|
||||
// task_claims 历史在同一个事务提交,避免只改了状态却没留下领取凭据。
|
||||
func ClaimNextTask(db *sql.DB, clientID string, supportedTypes []string) (*model.Task, error) {
|
||||
func ClaimNextTask(db *sql.DB, clientID string, supportedTypes []string, purchaseModes ...string) (*model.Task, error) {
|
||||
if clientID == "" {
|
||||
return nil, fmt.Errorf("client_id 不能为空")
|
||||
}
|
||||
purchaseMode := string(model.TaskExecutionDryRun)
|
||||
if len(purchaseModes) > 0 {
|
||||
purchaseMode = purchaseModes[0]
|
||||
}
|
||||
if purchaseMode != string(model.TaskExecutionDryRun) && purchaseMode != string(model.TaskExecutionLive) {
|
||||
return nil, fmt.Errorf("purchase_mode 必须是 dry_run 或 live")
|
||||
}
|
||||
ctx := context.Background()
|
||||
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
|
||||
if err != nil {
|
||||
@@ -45,6 +52,11 @@ func ClaimNextTask(db *sql.DB, clientID string, supportedTypes []string) (*model
|
||||
WHERE ( (assigned_client = ? AND status = 'assigned')
|
||||
OR (assigned_client IS NULL AND status = 'pending') )`
|
||||
args := []any{clientID}
|
||||
// dry_run 客户端永远看不到真实采购任务。声明 live 的客户端仍可执行
|
||||
// 演练任务,避免它在没有真实任务时闲置。
|
||||
if purchaseMode != string(model.TaskExecutionLive) {
|
||||
query += ` AND execution_mode = 'dry_run'`
|
||||
}
|
||||
|
||||
// 客户端只声明支持某些类型时,不要给它别的类型
|
||||
if len(supportedTypes) > 0 {
|
||||
@@ -101,23 +113,26 @@ func GetTask(db *sql.DB, taskID string) (*model.Task, error) {
|
||||
var t model.Task
|
||||
var assigned, claimedAt, sybID, orderNo, goodsID, skuID sql.NullString
|
||||
var pddGoodsID, pddOptions, resultData, errCode, errMsg, finishedAt sql.NullString
|
||||
var liveConfirmedBy, liveConfirmedAt sql.NullString
|
||||
var quantity, maxPrice sql.NullInt64
|
||||
|
||||
err := db.QueryRow(`
|
||||
SELECT task_id, task_type, status, version, priority,
|
||||
SELECT task_id, task_type, status, execution_mode, version, priority,
|
||||
assigned_client, claimed_at,
|
||||
syb_id, order_no, goods_id, shopee_sku_id,
|
||||
pdd_goods_url, pdd_goods_id, pdd_options,
|
||||
quantity, max_price_cent,
|
||||
result_data, error_code, error_message, finished_at,
|
||||
live_confirmed_by, live_confirmed_at,
|
||||
created_at, updated_at
|
||||
FROM tasks WHERE task_id = ?`, taskID).Scan(
|
||||
&t.TaskID, &t.TaskType, &t.Status, &t.Version, &t.Priority,
|
||||
&t.TaskID, &t.TaskType, &t.Status, &t.ExecutionMode, &t.Version, &t.Priority,
|
||||
&assigned, &claimedAt,
|
||||
&sybID, &orderNo, &goodsID, &skuID,
|
||||
&t.PddGoodsURL, &pddGoodsID, &pddOptions,
|
||||
&quantity, &maxPrice,
|
||||
&resultData, &errCode, &errMsg, &finishedAt,
|
||||
&liveConfirmedBy, &liveConfirmedAt,
|
||||
&t.CreatedAt, &t.UpdatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
@@ -140,6 +155,8 @@ func GetTask(db *sql.DB, taskID string) (*model.Task, error) {
|
||||
t.ErrorCode = errCode.String
|
||||
t.ErrorMessage = errMsg.String
|
||||
t.FinishedAt = finishedAt.String
|
||||
t.LiveConfirmedBy = liveConfirmedBy.String
|
||||
t.LiveConfirmedAt = liveConfirmedAt.String
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
@@ -269,6 +286,7 @@ type TaskListRow struct {
|
||||
TaskID string
|
||||
TaskType model.TaskType
|
||||
Status model.TaskStatus
|
||||
ExecutionMode model.TaskExecutionMode
|
||||
AssignedClient string // 空表示无主任务
|
||||
|
||||
OrderNo string
|
||||
@@ -323,7 +341,7 @@ func taskFilterClause(filter TaskFilter) (string, []any) {
|
||||
func ListTasks(q Execer, filter TaskFilter, limit, offset int) ([]TaskListRow, error) {
|
||||
where, args := taskFilterClause(filter)
|
||||
sqlText := `
|
||||
SELECT t.task_id, t.task_type, t.status, t.assigned_client,
|
||||
SELECT t.task_id, t.task_type, t.status, t.execution_mode, t.assigned_client,
|
||||
t.order_no, t.pdd_goods_id, t.pdd_options, t.quantity, t.max_price_cent,
|
||||
t.updated_at, p.title
|
||||
FROM tasks t
|
||||
@@ -345,7 +363,7 @@ func ListTasks(q Execer, filter TaskFilter, limit, offset int) ([]TaskListRow, e
|
||||
var quantity, maxPrice sql.NullInt64
|
||||
|
||||
if err := rows.Scan(
|
||||
&r.TaskID, &r.TaskType, &r.Status, &assigned,
|
||||
&r.TaskID, &r.TaskType, &r.Status, &r.ExecutionMode, &assigned,
|
||||
&orderNo, &pddGoodsID, &pddOptions, &quantity, &maxPrice,
|
||||
&r.UpdatedAt, &title,
|
||||
); err != nil {
|
||||
@@ -465,17 +483,34 @@ func InsertPurchaseTask(q Execer, task model.Task) error {
|
||||
task.Quantity <= 0 || task.MaxPriceCent <= 0 {
|
||||
return fmt.Errorf("采购任务缺少客户端、商品、规格、数量或人民币价格上限")
|
||||
}
|
||||
if task.ExecutionMode == "" {
|
||||
task.ExecutionMode = model.TaskExecutionDryRun
|
||||
}
|
||||
if task.ExecutionMode != model.TaskExecutionDryRun && task.ExecutionMode != model.TaskExecutionLive {
|
||||
return fmt.Errorf("采购任务执行模式无效")
|
||||
}
|
||||
if task.ExecutionMode == model.TaskExecutionLive &&
|
||||
(strings.TrimSpace(task.LiveConfirmedBy) == "" || strings.TrimSpace(task.LiveConfirmedAt) == "") {
|
||||
return fmt.Errorf("真实采购任务缺少创建确认审计信息")
|
||||
}
|
||||
if task.ExecutionMode == model.TaskExecutionDryRun {
|
||||
task.LiveConfirmedBy = ""
|
||||
task.LiveConfirmedAt = ""
|
||||
}
|
||||
now := model.NowISO()
|
||||
liveConfirmedBy := sql.NullString{String: task.LiveConfirmedBy, Valid: task.LiveConfirmedBy != ""}
|
||||
liveConfirmedAt := sql.NullString{String: task.LiveConfirmedAt, Valid: task.LiveConfirmedAt != ""}
|
||||
_, err := q.Exec(`
|
||||
INSERT INTO tasks
|
||||
(task_id, task_type, status, assigned_client,
|
||||
(task_id, task_type, status, execution_mode, assigned_client,
|
||||
syb_id, order_no, goods_id, shopee_sku_id,
|
||||
pdd_goods_url, pdd_goods_id, pdd_options,
|
||||
quantity, max_price_cent, created_at, updated_at)
|
||||
VALUES (?, 'purchase', 'assigned', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
task.TaskID, task.AssignedClient, task.SybID, task.OrderNo,
|
||||
quantity, max_price_cent, live_confirmed_by, live_confirmed_at,
|
||||
created_at, updated_at)
|
||||
VALUES (?, 'purchase', 'assigned', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
task.TaskID, task.ExecutionMode, task.AssignedClient, task.SybID, task.OrderNo,
|
||||
task.GoodsID, task.ShopeeSKUID, task.PddGoodsURL, task.PddGoodsID,
|
||||
task.PddOptions, task.Quantity, task.MaxPriceCent, now, now)
|
||||
task.PddOptions, task.Quantity, task.MaxPriceCent, liveConfirmedBy, liveConfirmedAt, now, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建顺运宝明细 %s 的采购任务失败: %w", task.SybID, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user