daShangDao_psiServer/controllers/shop.go
2026-06-15 13:47:39 +08:00

121 lines
3.3 KiB
Go

package controllers
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net/http"
"psi/constant"
"psi/database"
systemReq "psi/models/request"
systemRes "psi/models/response"
"psi/service"
"psi/utils"
)
type ShopApi struct{}
var shopService = service.ShopService{}
func (r *ShopApi) GetShopList(c *gin.Context) {
var req systemReq.QueryShopRequest
if err := c.ShouldBindQuery(&req); err != nil {
ValidAndFail(constant.LoggerChannelRequest, "查询店铺列表请求参数异常", "参数错误: "+err.Error(), c, err)
return
}
list, total, err := shopService.GetShopList(req, database.GetDB(c))
if err != nil {
utils.FailWithRequestLog(constant.LoggerChannelWork, "查询店铺列表异常", err, c, req)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"list": list,
"total": total,
"page": req.Page,
"page_size": req.PageSize,
},
"msg": "查询成功",
})
}
func (r *ShopApi) GetShopDetail(c *gin.Context) {
idStr := c.Param("id")
if idStr == "" {
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields{
"source": "获取店铺详情请求参数异常",
"err_msg": "ID参数不能为空",
})
systemRes.FailWithValidateMessage("参数错误: ID不能为空", c)
return
}
var id int64
if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil || id <= 0 {
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields{
"source": "获取店铺详情请求参数异常",
"err_msg": "ID格式错误: " + idStr,
})
systemRes.FailWithValidateMessage("参数错误: ID格式不正确", c)
return
}
shop, err := shopService.GetShopByID(id, database.GetDB(c))
if err != nil {
utils.FailWithRequestLog(constant.LoggerChannelWork, "查询店铺详情异常", err, c, gin.H{"id": id})
return
}
systemRes.OkWithDetailed(shop, "查询成功", c)
}
func (r *ShopApi) CreateShop(c *gin.Context) {
var req systemReq.CreateShopRequest
if err := c.ShouldBind(&req); err != nil {
ValidAndFail(constant.LoggerChannelRequest, "创建店铺请求参数异常", "参数错误: "+err.Error(), c, err)
return
}
id, err := shopService.CreateShop(req, database.GetDB(c))
if err != nil {
utils.FailWithRequestLog(constant.LoggerChannelWork, "创建店铺异常", err, c, req)
return
}
systemRes.OkWithDetailed(gin.H{"id": id}, "创建成功", c)
}
func (r *ShopApi) UpdateShop(c *gin.Context) {
var req systemReq.UpdateShopRequest
if err := c.ShouldBind(&req); err != nil {
ValidAndFail(constant.LoggerChannelRequest, "更新店铺请求参数异常", "参数错误: "+err.Error(), c, err)
return
}
if err := shopService.UpdateShop(req, database.GetDB(c)); err != nil {
utils.FailWithRequestLog(constant.LoggerChannelWork, "更新店铺异常", err, c, req)
return
}
systemRes.OkWithMessage("更新成功", c)
}
func (r *ShopApi) DeleteShop(c *gin.Context) {
var req systemReq.DeleteShopRequest
if err := c.ShouldBind(&req); err != nil {
ValidAndFail(constant.LoggerChannelRequest, "删除店铺请求参数异常", "参数错误: "+err.Error(), c, err)
return
}
if err := shopService.DeleteShop(req.ID, database.GetDB(c)); err != nil {
utils.FailWithRequestLog(constant.LoggerChannelWork, "删除店铺异常", err, c, req)
return
}
systemRes.OkWithMessage("删除成功", c)
}