liujiandao
2023-09-20 2baa7bad0482613829e217997c44a51d8ec0ec66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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(&params); 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
}