feat: 按允许店铺筛选顺运宝同步 (#196)
This commit is contained in:
@@ -97,6 +97,11 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
|
||||
fail(c, http.StatusInternalServerError, "读取可分配客户端失败,数据没有被改动。")
|
||||
return
|
||||
}
|
||||
enabledShopCount, err := service.CountEnabledSybAllowedShops(h.db)
|
||||
if err != nil {
|
||||
fail(c, http.StatusInternalServerError, "读取顺运宝同步店铺数量失败,数据没有被改动。刷新后重试。")
|
||||
return
|
||||
}
|
||||
|
||||
syncStatus := service.GetSybSyncStatus()
|
||||
status := msg
|
||||
@@ -217,10 +222,11 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
|
||||
keyword, shop, stage, dateFrom, dateTo),
|
||||
"HistoryFetchPagination": sybHistoryPartialPagination(history.Page, history.TotalPages,
|
||||
keyword, shop, stage, dateFrom, dateTo),
|
||||
"Pagination": service.NewPaginationView(result.Page, result.TotalPages, values.Encode()),
|
||||
"DetailURL": "/syb/detail?" + detailValues.Encode(),
|
||||
"AssignableClients": purchaseClients.Rows,
|
||||
"PurchaseClientCount": purchaseClients.SelectableCount,
|
||||
"Pagination": service.NewPaginationView(result.Page, result.TotalPages, values.Encode()),
|
||||
"DetailURL": "/syb/detail?" + detailValues.Encode(),
|
||||
"AssignableClients": purchaseClients.Rows,
|
||||
"PurchaseClientCount": purchaseClients.SelectableCount,
|
||||
"EnabledSyncShopCount": enabledShopCount,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -363,6 +369,10 @@ func (h *Handler) SybSync(c *gin.Context) {
|
||||
h.sybRedirectRange(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := service.EnsureEnabledSybAllowedShops(h.db); err != nil {
|
||||
h.sybRedirect(c, "同步没有启动:"+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
@@ -481,6 +491,10 @@ func (h *Handler) SybLoginAndSync(c *gin.Context) {
|
||||
h.sybRedirectRange(c, optionErr.Error())
|
||||
return
|
||||
}
|
||||
if err := service.EnsureEnabledSybAllowedShops(h.db); err != nil {
|
||||
h.sybRedirect(c, "同步没有启动:"+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"cmautobuy/admin/repository"
|
||||
"cmautobuy/admin/service"
|
||||
)
|
||||
|
||||
func (h *Handler) SybAllowedShopList(c *gin.Context) {
|
||||
rows, err := service.ListSybAllowedShops(h.db, currentUser(c))
|
||||
if err != nil {
|
||||
fail(c, http.StatusInternalServerError, "读取顺运宝同步店铺失败,数据没有被改动。刷新后重试。")
|
||||
return
|
||||
}
|
||||
enabled := 0
|
||||
for _, row := range rows {
|
||||
if row.Enabled {
|
||||
enabled++
|
||||
}
|
||||
}
|
||||
c.HTML(http.StatusOK, "syb/shops", page(c, "syb", "顺运宝同步店铺", gin.H{
|
||||
"Rows": rows, "EnabledCount": enabled, "Message": c.Query("msg"), "Error": c.Query("error"),
|
||||
}))
|
||||
}
|
||||
|
||||
func (h *Handler) SybAllowedShopCreate(c *gin.Context) {
|
||||
err := service.CreateSybAllowedShop(h.db, currentUser(c), c.PostForm("shop_name"), time.Now())
|
||||
if err != nil {
|
||||
if service.IsValidationError(err) || errors.Is(err, repository.ErrSybAllowedShopExists) {
|
||||
redirectSybShops(c, "", err.Error())
|
||||
return
|
||||
}
|
||||
fail(c, http.StatusInternalServerError, "新增同步店铺失败,已有店铺没有被改动。")
|
||||
return
|
||||
}
|
||||
redirectSybShops(c, "店铺已加入允许列表并启用", "")
|
||||
}
|
||||
|
||||
func (h *Handler) SybAllowedShopStatus(c *gin.Context) {
|
||||
enabled := c.PostForm("enabled") == "1"
|
||||
err := service.SetSybAllowedShopEnabled(h.db, currentUser(c), c.PostForm("shop_id"), enabled, time.Now())
|
||||
if err != nil {
|
||||
if service.IsValidationError(err) {
|
||||
redirectSybShops(c, "", err.Error())
|
||||
return
|
||||
}
|
||||
fail(c, http.StatusInternalServerError, "更新同步店铺失败,原状态保持不变。")
|
||||
return
|
||||
}
|
||||
message := "店铺已停用,后续同步会跳过该店铺"
|
||||
if enabled {
|
||||
message = "店铺已重新启用"
|
||||
}
|
||||
redirectSybShops(c, message, "")
|
||||
}
|
||||
|
||||
func redirectSybShops(c *gin.Context, message, errorMessage string) {
|
||||
values := url.Values{}
|
||||
if message != "" {
|
||||
values.Set("msg", message)
|
||||
}
|
||||
if errorMessage != "" {
|
||||
values.Set("error", errorMessage)
|
||||
}
|
||||
target := "/syb/shops"
|
||||
if encoded := values.Encode(); encoded != "" {
|
||||
target += "?" + encoded
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, target)
|
||||
}
|
||||
@@ -84,6 +84,10 @@ func Register(r *gin.Engine, db *sql.DB, onlineThreshold time.Duration) {
|
||||
pages.POST("/syb/match", h.SybMatch)
|
||||
pages.POST("/syb/create-task", h.SybCreateTask)
|
||||
pages.POST("/syb/delete", h.SybDelete)
|
||||
sybShops := pages.Group("/syb/shops", AdminRequired())
|
||||
sybShops.GET("", h.SybAllowedShopList)
|
||||
sybShops.POST("/create", h.SybAllowedShopCreate)
|
||||
sybShops.POST("/status", h.SybAllowedShopStatus)
|
||||
|
||||
// 4. 采集采购
|
||||
pages.GET("/tasks", h.TaskList)
|
||||
|
||||
Reference in New Issue
Block a user