package web import ( "net/http" "net/url" "time" "github.com/gin-gonic/gin" "cmautobuy/admin/service" ) func (h *Handler) ShopList(c *gin.Context) { result, err := service.ListShops(h.db, currentUser(c)) if err != nil { fail(c, http.StatusInternalServerError, "读取店铺管理数据失败,数据没有被改动。") return } c.HTML(http.StatusOK, "shop/list", page(c, "shops", "店铺管理", gin.H{ "Rows": result.Items, "UnlinkedShopeeCount": result.UnlinkedShopeeCount, "Message": c.Query("msg"), "Error": c.Query("error"), })) } func (h *Handler) ShopCreate(c *gin.Context) { err := service.CreateShop(h.db, currentUser(c), c.PostForm("display_name"), c.PostForm("syb_alias"), c.PostForm("shopee_alias"), time.Now()) if err != nil { if service.IsValidationError(err) { redirectShops(c, "", err.Error()) return } fail(c, http.StatusInternalServerError, "新增店铺失败,已有数据没有被改动。") return } redirectShops(c, "店铺已新增;历史数据已按渠道原始名称精确关联", "") } func (h *Handler) ShopUpdate(c *gin.Context) { err := service.UpdateShop(h.db, currentUser(c), c.PostForm("shop_id"), c.PostForm("display_name"), c.PostForm("syb_alias"), c.PostForm("shopee_alias"), time.Now()) if err != nil { if service.IsValidationError(err) { redirectShops(c, "", err.Error()) return } fail(c, http.StatusInternalServerError, "保存店铺失败,原配置保持不变。") return } redirectShops(c, "店铺名称和渠道关联已保存", "") } func (h *Handler) ShopStatus(c *gin.Context) { enabled := c.PostForm("enabled") == "1" if err := service.SetShopEnabled(h.db, currentUser(c), c.PostForm("shop_id"), enabled, time.Now()); err != nil { if service.IsValidationError(err) { redirectShops(c, "", err.Error()) return } fail(c, http.StatusInternalServerError, "更新店铺状态失败,原状态保持不变。") return } message := "业务店铺已停用;SYB 不再同步该店铺" if enabled { message = "业务店铺已启用" } redirectShops(c, message, "") } func (h *Handler) ShopSybStatus(c *gin.Context) { enabled := c.PostForm("enabled") == "1" if err := service.SetShopSybEnabled(h.db, currentUser(c), c.PostForm("shop_id"), enabled, time.Now()); err != nil { if service.IsValidationError(err) { redirectShops(c, "", err.Error()) return } fail(c, http.StatusInternalServerError, "更新 SYB 同步状态失败,原状态保持不变。") return } message := "SYB 同步已停用" if enabled { message = "SYB 同步已启用" } redirectShops(c, message, "") } func (h *Handler) ShopDelete(c *gin.Context) { if err := service.DeleteShop(h.db, currentUser(c), c.PostForm("shop_id")); err != nil { if service.IsValidationError(err) { redirectShops(c, "", err.Error()) return } fail(c, http.StatusInternalServerError, "删除店铺失败,已有数据没有被改动。") return } redirectShops(c, "已删除没有关联数据的停用店铺", "") } func redirectShops(c *gin.Context, message, errorMessage string) { values := url.Values{} if message != "" { values.Set("msg", message) } if errorMessage != "" { values.Set("error", errorMessage) } target := "/shops" if encoded := values.Encode(); encoded != "" { target += "?" + encoded } c.Redirect(http.StatusSeeOther, target) }