Files
cmautobuy/admin/handler/web/shop.go
T

94 lines
2.7 KiB
Go

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("shop_name"), 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("shop_name"), 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) 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)
}