jiangshuai
2023-12-12 ec58bf196fb360bfcec35ed784ae9da90ffa3d42
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package controllers
 
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/pkg/structx"
    "wms/request"
)
 
type CompanyController struct{}
 
// Add
// @Tags      公司
// @Summary   添加公司
// @Produce   application/json
// @Param     object  body  request.AddCompany true  "公司信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/company/company [post]
func (slf CompanyController) Add(c *gin.Context) {
    var reqParams request.AddCompany
    var params models.Company
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
        return
    }
 
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := models.NewCompanySearch().Create(&params); err != nil {
        logx.Errorf("Company create err: %v", err)
        util.ResponseFormat(c, code.SaveFail, "插入失败")
        return
    }
 
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
// Update
// @Tags      公司
// @Summary   编辑公司
// @Produce   application/json
// @Param     object  body request.UpdateCompany true  "公司信息"
// @Param     id  path string true  "公司id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/company/company/{id} [put]
func (slf CompanyController) Update(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    var (
        reqParams request.UpdateCompany
        params    models.Company
    )
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error()))
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("数据转换错误: %v", err.Error()))
        return
    }
    params.ID = id
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    err := models.NewCompanySearch().SetID(params.ID).Update(&params)
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
 
func (slf CompanyController) ParamsCheck(params models.Company) (err error) {
    var oldRecord *models.Company
    if params.ID != 0 {
        oldRecord, err = models.NewCompanySearch().SetID(params.ID).First()
        if err == gorm.ErrRecordNotFound {
            return errors.New("记录不存在")
        }
    }
    if oldRecord == nil || params.Name != oldRecord.Name {
        _, err = models.NewCompanySearch().SetName(params.Name).First()
        if err != gorm.ErrRecordNotFound {
            return errors.New("公司名称重复")
        }
    }
 
    return nil
}
 
// List
// @Tags      公司
// @Summary   查询公司列表
// @Produce   application/json
// @Param     object  query    request.GetCompanyList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Company}  "成功"
// @Router    /api-wms/v1/company/company [get]
func (slf CompanyController) List(c *gin.Context) {
    var params request.GetCompanyList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, total, err := models.NewCompanySearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, cast.ToInt(total))
}
 
// Delete
// @Tags      公司
// @Summary   删除公司
// @Produce   application/json
// @Param     id  path string true  "公司id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/company/company/{id} [delete]
func (slf CompanyController) Delete(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
 
    err := models.NewCompanySearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}