jiangshuai
2023-09-20 5adf6ad89a4df69aa169beab89ca5afc738adfa4
controllers/location.go
@@ -3,6 +3,7 @@
import (
   "errors"
   "github.com/gin-gonic/gin"
   "strconv"
   "wms/extend/code"
   "wms/extend/util"
   "wms/models"
@@ -12,42 +13,130 @@
type LocationController struct {
}
func (slf LocationController) ListLocationByType(c *gin.Context) {
   var params request.LocationByType
// AddLocation
// @Tags      位置
// @Summary   添加位置信息
// @Produce   application/json
// @Param     object  body  models.Location true  "位置信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/location/addLocation [post]
func (slf LocationController) AddLocation(c *gin.Context) {
   var params models.Location
   if err := c.BindJSON(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数绑定失败")
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   if err := slf.CheckLocationByTypeParams(params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
   if err := slf.CheckLocation(params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, err)
      return
   }
   search := models.NewLocationSearch()
   search.SetType(params.Type)
   if params.Keyword != "" {
      search.SetKeyword(params.Keyword)
   }
   if params.ParentId != "" {
      search.SetParentId(params.ParentId)
   }
   if params.CompanyId != 0 {
      search.SetCompanyId(params.CompanyId)
   }
   res, err := search.SetOrder("created_at desc").FindAll()
   err := models.NewLocationSearch().Create(&params)
   if err != nil {
      util.ResponseFormat(c, code.RequestError, err.Error())
      util.ResponseFormat(c, code.RequestParamError, "创建失败")
      return
   }
   util.ResponseFormat(c, code.Success, res)
   util.ResponseFormat(c, code.RequestParamError, "创建成功")
}
func (slf LocationController) CheckLocationByTypeParams(params request.LocationByType) error {
// GetLocationList
// @Tags      位置
// @Summary   获取位置列表
// @Produce   application/json
// @Param     object  body  request.GetProductList true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]models.Location}   "成功"
// @Router    /api-wms/v1/location/getLocationList [post]
func (slf LocationController) GetLocationList(c *gin.Context) {
   var params request.GetLocationList
   if err := c.BindJSON(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   search := models.NewLocationSearch()
   if params.PageInfo.Check() {
      search.SetPage(params.Page, params.PageSize)
   }
   list, total, err := search.SetKeyword(params.KeyWord).SetOrder("created_at desc").Find()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查找失败")
      return
   }
   util.ResponseFormatList(c, code.Success, list, int(total))
}
// GetLocationDetails
// @Tags      位置
// @Summary   获取位置详情
// @Produce   application/json
// @Param      id   path      string         true   "id"  "查询参数"
// @Success   200 {object} util.Response{data=models.Location}   "成功"
// @Router    /api-wms/v1/location/getLocationDetails/{id} [get]
func (slf LocationController) GetLocationDetails(c *gin.Context) {
   id, _ := strconv.Atoi(c.Param("id"))
   if id == 0 {
      util.ResponseFormat(c, code.RequestParamError, "无效id")
      return
   }
   location, err := models.NewLocationSearch().SetID(id).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查找失败")
      return
   }
   util.ResponseFormat(c, code.Success, location)
}
// UpdateLocation
// @Tags      位置
// @Summary   修改位置
// @Produce   application/json
// @Param     object  body  models.Location true  "产品信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/location/updateLocation [post]
func (slf LocationController) UpdateLocation(c *gin.Context) {
   var params models.Location
   if err := c.BindJSON(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   if err := slf.CheckLocation(params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, err)
      return
   }
   err := models.NewLocationSearch().Update(&params)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "位置信息更新失败")
      return
   }
   util.ResponseFormat(c, code.Success, "更新成功")
}
// DeleteLocation
// @Tags      位置
// @Summary   删除位置
// @Produce   application/json
// @Param      id   path      string         true   "id"  "查询参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/location/deleteLocation/{id} [delete]
func (slf LocationController) DeleteLocation(c *gin.Context) {
   id, _ := strconv.Atoi(c.Param("id"))
   if id == 0 {
      util.ResponseFormat(c, code.RequestParamError, "无效id")
      return
   }
   err := models.NewLocationSearch().SetID(id).Delete()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "删除失败")
      return
   }
   util.ResponseFormat(c, code.Success, "删除成功")
}
func (slf LocationController) CheckLocation(params models.Location) error {
   if params.Name == "" {
      return errors.New("名称不能为空")
   }
   if params.Type == 0 {
      return errors.New("请选择正确的位置类型")
   }
   if params.ParentId == "" {
      return errors.New("错误参数ParentId")
   }
   if params.CompanyId < 0 {
      return errors.New("错误参数CompanyId")
   }
   return nil
}