|
|
package controller |
|
|
|
|
|
import ( |
|
|
"strconv" |
|
|
|
|
|
"github.com/QuantumNous/new-api/common" |
|
|
"github.com/QuantumNous/new-api/model" |
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
) |
|
|
|
|
|
|
|
|
func GetAllVendors(c *gin.Context) { |
|
|
pageInfo := common.GetPageQuery(c) |
|
|
vendors, err := model.GetAllVendors(pageInfo.GetStartIdx(), pageInfo.GetPageSize()) |
|
|
if err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
var total int64 |
|
|
model.DB.Model(&model.Vendor{}).Count(&total) |
|
|
pageInfo.SetTotal(int(total)) |
|
|
pageInfo.SetItems(vendors) |
|
|
common.ApiSuccess(c, pageInfo) |
|
|
} |
|
|
|
|
|
|
|
|
func SearchVendors(c *gin.Context) { |
|
|
keyword := c.Query("keyword") |
|
|
pageInfo := common.GetPageQuery(c) |
|
|
vendors, total, err := model.SearchVendors(keyword, pageInfo.GetStartIdx(), pageInfo.GetPageSize()) |
|
|
if err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
pageInfo.SetTotal(int(total)) |
|
|
pageInfo.SetItems(vendors) |
|
|
common.ApiSuccess(c, pageInfo) |
|
|
} |
|
|
|
|
|
|
|
|
func GetVendorMeta(c *gin.Context) { |
|
|
idStr := c.Param("id") |
|
|
id, err := strconv.Atoi(idStr) |
|
|
if err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
v, err := model.GetVendorByID(id) |
|
|
if err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
common.ApiSuccess(c, v) |
|
|
} |
|
|
|
|
|
|
|
|
func CreateVendorMeta(c *gin.Context) { |
|
|
var v model.Vendor |
|
|
if err := c.ShouldBindJSON(&v); err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
if v.Name == "" { |
|
|
common.ApiErrorMsg(c, "供应商名称不能为空") |
|
|
return |
|
|
} |
|
|
|
|
|
if dup, err := model.IsVendorNameDuplicated(0, v.Name); err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} else if dup { |
|
|
common.ApiErrorMsg(c, "供应商名称已存在") |
|
|
return |
|
|
} |
|
|
|
|
|
if err := v.Insert(); err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
common.ApiSuccess(c, &v) |
|
|
} |
|
|
|
|
|
|
|
|
func UpdateVendorMeta(c *gin.Context) { |
|
|
var v model.Vendor |
|
|
if err := c.ShouldBindJSON(&v); err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
if v.Id == 0 { |
|
|
common.ApiErrorMsg(c, "缺少供应商 ID") |
|
|
return |
|
|
} |
|
|
|
|
|
if dup, err := model.IsVendorNameDuplicated(v.Id, v.Name); err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} else if dup { |
|
|
common.ApiErrorMsg(c, "供应商名称已存在") |
|
|
return |
|
|
} |
|
|
|
|
|
if err := v.Update(); err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
common.ApiSuccess(c, &v) |
|
|
} |
|
|
|
|
|
|
|
|
func DeleteVendorMeta(c *gin.Context) { |
|
|
idStr := c.Param("id") |
|
|
id, err := strconv.Atoi(idStr) |
|
|
if err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
if err := model.DB.Delete(&model.Vendor{}, id).Error; err != nil { |
|
|
common.ApiError(c, err) |
|
|
return |
|
|
} |
|
|
common.ApiSuccess(c, nil) |
|
|
} |
|
|
|