feat: 增加真实采购任务安全模式 (#98)

This commit is contained in:
chengma
2026-08-10 15:11:38 +08:00
parent ba6710a2a2
commit 3aaa40cfe0
23 changed files with 642 additions and 66 deletions
+24
View File
@@ -2,6 +2,7 @@ package repository
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
@@ -9,6 +10,29 @@ import (
"cmautobuy/admin/model"
)
// ClientPurchaseMode 返回客户端最后一次登记时声明的采购能力。
// 旧数据、空值或无法解析的 JSON 一律按 dry_run 处理,这是安全默认值。
func ClientPurchaseMode(q Execer, clientID string) (string, error) {
var raw sql.NullString
if err := q.QueryRow(`SELECT capabilities FROM clients WHERE client_id = ?`, clientID).Scan(&raw); errors.Is(err, sql.ErrNoRows) {
return "", ErrClientNotFound
} else if err != nil {
return "", fmt.Errorf("读取客户端 %s 的采购能力失败: %w", clientID, err)
}
return ParseClientPurchaseMode(raw.String), nil
}
// ParseClientPurchaseMode 把登记能力转成安全的固定值,供列表和创建校验共用。
func ParseClientPurchaseMode(raw string) string {
var capabilities struct {
PurchaseMode string `json:"purchase_mode"`
}
if json.Unmarshal([]byte(raw), &capabilities) == nil && capabilities.PurchaseMode == string(model.TaskExecutionLive) {
return capabilities.PurchaseMode
}
return string(model.TaskExecutionDryRun)
}
var (
ErrClientNotFound = errors.New("客户端不存在")
ErrPurchaserNotActive = errors.New("采购员不存在或已禁用")