303 lines
6.9 KiB
Go
303 lines
6.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"hospital-admin-api/api/dao"
|
|
"hospital-admin-api/api/dto"
|
|
"hospital-admin-api/api/requests"
|
|
"hospital-admin-api/api/responses"
|
|
"hospital-admin-api/api/service"
|
|
"hospital-admin-api/global"
|
|
"hospital-admin-api/utils"
|
|
"strconv"
|
|
)
|
|
|
|
type Product struct{}
|
|
|
|
// GetPlatformProductPage 获取平台商品列表-分页
|
|
func (r *Product) GetPlatformProductPage(c *gin.Context) {
|
|
productRequest := requests.ProductRequest{}
|
|
req := productRequest.GetPlatformProductPage
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
productPlatformDao := dao.ProductPlatformDao{}
|
|
productPlatform, total, err := productPlatformDao.GetPlatformProductPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
GetProductPlatformPageResponses := dto.GetProductPlatformListDto(productPlatform)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = GetProductPlatformPageResponses
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// GetProductPlatform 平台商品详情
|
|
func (r *Product) GetProductPlatform(c *gin.Context) {
|
|
id := c.Param("product_platform_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
productPlatformId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
productService := service.ProductService{}
|
|
getPlatformProductResponses, err := productService.GetProductPlatform(productPlatformId)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.OkWithData(getPlatformProductResponses, c)
|
|
}
|
|
|
|
// GetProductPage 获取商品列表-分页
|
|
func (r *Product) GetProductPage(c *gin.Context) {
|
|
productRequest := requests.ProductRequest{}
|
|
req := productRequest.GetProductPage
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
productDao := dao.ProductDao{}
|
|
product, total, err := productDao.GetProductPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
GetProductPageResponses := dto.GetProductListDto(product)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = GetProductPageResponses
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// GetProduct 商品详情
|
|
func (r *Product) GetProduct(c *gin.Context) {
|
|
id := c.Param("product_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
productId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
productService := service.ProductService{}
|
|
getPlatformProductResponses, err := productService.GetProduct(productId)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.OkWithData(getPlatformProductResponses, c)
|
|
}
|
|
|
|
// AddProduct 新增商品
|
|
func (r *Product) AddProduct(c *gin.Context) {
|
|
productRequest := requests.ProductRequest{}
|
|
req := productRequest.AddProduct
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
// 获取当前登陆用户id
|
|
userId := c.GetInt64("UserId")
|
|
if userId == 0 {
|
|
responses.FailWithMessage("角色错误", c)
|
|
return
|
|
}
|
|
|
|
userIdStr := strconv.FormatInt(userId, 10)
|
|
|
|
// 业务处理
|
|
productService := service.ProductService{}
|
|
_, err := productService.AddProduct(userIdStr, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutProduct 修改商品
|
|
func (r *Product) PutProduct(c *gin.Context) {
|
|
productRequest := requests.ProductRequest{}
|
|
req := productRequest.PutProduct
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("product_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
productId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
productService := service.ProductService{}
|
|
_, err = productService.PutProduct(productId, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutProductStatus 修改商品状态(上/下架)
|
|
func (r *Product) PutProductStatus(c *gin.Context) {
|
|
productRequest := requests.ProductRequest{}
|
|
req := productRequest.PutProductStatus
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("product_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
productId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
productService := service.ProductService{}
|
|
_, err = productService.PutProductStatus(productId, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// GetPlatformProductList 获取平台商品列表
|
|
func (r *Product) GetPlatformProductList(c *gin.Context) {
|
|
productRequest := requests.ProductRequest{}
|
|
req := productRequest.GetPlatformProductList
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
productPlatformDao := dao.ProductPlatformDao{}
|
|
productPlatform, err := productPlatformDao.GetPlatformProductListSearch(req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
productPlatforms := dto.GetProductPlatformListDto(productPlatform)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.OkWithData(productPlatforms, c)
|
|
}
|