feat: 增加顺运宝批量 AI 规格匹配 (#202)
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"cmautobuy/admin/service"
|
||||
)
|
||||
|
||||
func (h *Handler) SybAIMatchCreate(c *gin.Context) {
|
||||
actor := currentUser(c)
|
||||
snapshot, err := service.LoadActiveAIMatchSnapshot(c.Request.Context(), h.db, h.aiSecrets, h.aiPolicy)
|
||||
if err != nil {
|
||||
h.sybRedirect(c, "AI 规格匹配未开始:"+err.Error())
|
||||
return
|
||||
}
|
||||
batch, err := service.CreateAIMatchBatch(h.db, actor, c.PostFormArray("ids"), snapshot, time.Now())
|
||||
if err != nil {
|
||||
message := "AI 规格匹配批次未创建,数据没有被改动。"
|
||||
if service.IsValidationError(err) {
|
||||
message = "AI 规格匹配未开始:" + err.Error()
|
||||
}
|
||||
h.sybRedirect(c, message)
|
||||
return
|
||||
}
|
||||
actorCopy := *actor
|
||||
go func() {
|
||||
if runErr := service.RunAIMatchBatch(context.Background(), h.db, actorCopy, batch.BatchID, snapshot); runErr != nil {
|
||||
log.Printf("ai_match_batch_failed batch_id=%s error=%v", batch.BatchID, runErr)
|
||||
if interruptErr := service.MarkAIMatchBatchInterrupted(h.db, batch.BatchID); interruptErr != nil {
|
||||
log.Printf("ai_match_batch_interrupt_failed batch_id=%s error=%v", batch.BatchID, interruptErr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
h.sybRedirectAIMatch(c, batch.BatchID, "AI 规格匹配已开始,可在进度窗口查看逐条结果")
|
||||
}
|
||||
|
||||
func (h *Handler) SybAIMatchStatus(c *gin.Context) {
|
||||
view, err := service.GetAIMatchBatchView(h.db, currentUser(c), c.Param("batch_id"))
|
||||
if errors.Is(err, service.ErrForbidden) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "没有权限查看这个 AI 匹配批次"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "读取 AI 匹配进度失败,请稍后重试"})
|
||||
return
|
||||
}
|
||||
if view == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "AI 匹配批次不存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, view)
|
||||
}
|
||||
|
||||
func (h *Handler) sybRedirectAIMatch(c *gin.Context, batchID, message string) {
|
||||
params := url.Values{}
|
||||
appendSybFormState(params, c)
|
||||
params.Set("ai_batch_id", strings.TrimSpace(batchID))
|
||||
if message != "" {
|
||||
params.Set("msg", message)
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/syb?"+params.Encode())
|
||||
}
|
||||
@@ -179,6 +179,46 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
|
||||
rangeWarning = defaults.Warning
|
||||
}
|
||||
|
||||
actor := currentUser(c)
|
||||
aiBatchID := strings.TrimSpace(c.Query("ai_batch_id"))
|
||||
var aiBatch *service.AIMatchBatchView
|
||||
if aiBatchID != "" {
|
||||
aiBatch, err = service.GetAIMatchBatchView(h.db, actor, aiBatchID)
|
||||
if errors.Is(err, service.ErrForbidden) {
|
||||
fail(c, http.StatusForbidden, "没有权限查看这个 AI 匹配批次。")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fail(c, http.StatusInternalServerError, "读取 AI 匹配批次失败,请刷新页面后重试。")
|
||||
return
|
||||
}
|
||||
if aiBatch == nil {
|
||||
fail(c, http.StatusNotFound, "AI 匹配批次不存在,可能已被清理。")
|
||||
return
|
||||
}
|
||||
}
|
||||
latestAIBatchID, err := service.LatestAIMatchBatchID(h.db, actor)
|
||||
if err != nil {
|
||||
fail(c, http.StatusInternalServerError, "读取最近 AI 匹配批次失败,请刷新页面后重试。")
|
||||
return
|
||||
}
|
||||
aiBatchPageURL := func(id string) string {
|
||||
query := url.Values{"ai_batch_id": {id}}
|
||||
if keyword != "" {
|
||||
query.Set("order_no", keyword)
|
||||
}
|
||||
if shop != "" {
|
||||
query.Set("shop", shop)
|
||||
}
|
||||
if stage != "" {
|
||||
query.Set("stage", stage)
|
||||
}
|
||||
query.Set("page", strconv.Itoa(result.Page))
|
||||
query.Set("date_from", dateFrom)
|
||||
query.Set("date_to", dateTo)
|
||||
return "/syb?" + query.Encode()
|
||||
}
|
||||
|
||||
cfg, cfgErr := config.Load()
|
||||
switch {
|
||||
case cfgErr != nil:
|
||||
@@ -238,6 +278,19 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
|
||||
"AssignableClients": purchaseClients.Rows,
|
||||
"PurchaseClientCount": purchaseClients.SelectableCount,
|
||||
"EnabledSyncShopCount": enabledShopCount,
|
||||
"AIMatchBatch": aiBatch,
|
||||
"AIMatchBatchFetchURL": func() string {
|
||||
if aiBatch == nil {
|
||||
return ""
|
||||
}
|
||||
return "/syb/ai-match/" + url.PathEscape(aiBatch.BatchID)
|
||||
}(),
|
||||
"LatestAIMatchBatchURL": func() string {
|
||||
if latestAIBatchID == "" {
|
||||
return ""
|
||||
}
|
||||
return aiBatchPageURL(latestAIBatchID)
|
||||
}(),
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,8 @@ func Register(r *gin.Engine, db *sql.DB, onlineThreshold time.Duration, aiSecret
|
||||
pages.POST("/syb/collect-pdd", h.SybCollectPdd)
|
||||
pages.POST("/syb/collect-pdd-batch", h.SybCollectPddBatch)
|
||||
pages.POST("/syb/match", h.SybMatch)
|
||||
pages.POST("/syb/ai-match", h.SybAIMatchCreate)
|
||||
pages.GET("/syb/ai-match/:batch_id", h.SybAIMatchStatus)
|
||||
pages.POST("/syb/create-task", h.SybCreateTask)
|
||||
pages.POST("/syb/delete", h.SybDelete)
|
||||
pages.GET("/syb/shops", AdminRequired(), func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/shops") })
|
||||
|
||||
Reference in New Issue
Block a user