package controllers
|
|
import (
|
"errors"
|
"github.com/gin-gonic/gin"
|
"wms/extend/code"
|
"wms/extend/util"
|
"wms/models"
|
"wms/request"
|
)
|
|
type LocationController struct {
|
}
|
|
func (slf LocationController) ListLocationByType(c *gin.Context) {
|
var params request.LocationByType
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数绑定失败")
|
return
|
}
|
if err := slf.CheckLocationByTypeParams(params); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
}
|
search := models.NewLocationSearch()
|
search.SetType(params.Type)
|
if params.Keyword != "" {
|
search.SetKeyword(params.Keyword)
|
}
|
if params.ParentId != 0 {
|
search.SetParentId(params.ParentId)
|
}
|
if params.CompanyId != 0 {
|
search.SetCompanyId(params.CompanyId)
|
}
|
res, err := search.SetOrder("created_at desc").FindAll()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestError, err.Error())
|
}
|
util.ResponseFormat(c, code.Success, res)
|
}
|
|
func (slf LocationController) CheckLocationByTypeParams(params request.LocationByType) error {
|
if params.Type == 0 {
|
return errors.New("请选择正确的位置类型")
|
}
|
if params.ParentId < 0 {
|
return errors.New("错误参数ParentId")
|
}
|
if params.CompanyId < 0 {
|
return errors.New("错误参数CompanyId")
|
}
|
return nil
|
}
|