yinbentan
2024-07-16 93c71a94d77ffe1f36654decc0bada0a2ac27c88
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package controllers
 
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "strconv"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/structx"
    "wms/request"
)
 
type AttributeController struct{}
 
// Add
// @Tags      属性
// @Summary   添加属性
// @Produce   application/json
// @Param     object  body  request.AddAttribute true  "属性信息"
// @Param     Authorization    header string true "token"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/attribute [post]
func (slf AttributeController) Add(c *gin.Context) {
    var reqParams request.AddAttribute
    var params models.Attribute
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
        return
    }
    if err := slf.CheckAttribute(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err)
        return
    }
    err := models.WithTransaction(func(tx *gorm.DB) error {
        if err := models.NewAttributeSearch().BeforeCreate(tx); err != nil {
            return err
        }
        if err := models.NewAttributeSearch().Create(&params); err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "创建失败")
        return
    }
    util.ResponseFormat(c, code.Success, "创建成功")
}
 
func (slf AttributeController) CheckAttribute(params models.Attribute) error {
    if params.Name == "" {
        return errors.New("名称不能为空")
    }
    if params.DataType == 0 {
        return errors.New("产品不能为空")
    }
    if params.DataType == 3 && len(params.SelectValues) == 0 {
        return errors.New("下拉框属性值不能为空")
    }
    return nil
}
 
// Update
// @Tags      属性
// @Summary   编辑属性
// @Produce   application/json
// @Param     object  body request.UpdateAttribute true  "属性信息"
// @Param     id  path string true  "属性id"
// @Param     Authorization    header string true "token"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/updateAttribute/{id} [put]
func (slf AttributeController) Update(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    var (
        reqParams request.UpdateAttribute
        params    models.Attribute
    )
    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 := models.NewAttributeSearch().SetID(params.ID).First(); errors.Is(err, gorm.ErrRecordNotFound) {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("记录不存在: %v", err))
        return
    }
    err := models.WithTransaction(func(tx *gorm.DB) error {
        if err := models.NewAttributeSearch().BeforeUpdate(tx); err != nil {
            return err
        }
        if err := models.NewAttributeSearch().SetID(params.ID).Save(&params); err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
 
// Delete
// @Tags      属性
// @Summary   删除属性
// @Produce   application/json
// @Param     Authorization    header string true "token"
// @Param     id  path string true  "属性id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/delete/{id} [delete]
func (slf AttributeController) Delete(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    if _, err := models.NewAttributeSearch().SetID(id).First(); errors.Is(err, gorm.ErrRecordNotFound) {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("记录不存在: %v", err))
        return
    }
    err := models.NewAttributeSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}
 
// ListAttribute
// @Tags      属性值和对象
// @Summary   添加属性值和对象
// @Produce   application/json
// @Param     object  body  request.AttributeList true  "属性值和对象信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/attribute [get]
func (slf AttributeController) ListAttribute(c *gin.Context) {
    var params request.AttributeList
    if err := c.ShouldBind(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, count, err := models.NewAttributeSearch().SetEntityType(params.EntityType).SetPage(params.Page, params.PageSize).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询失败")
        return
    }
    util.ResponseFormatList(c, code.Success, list, cast.ToInt(count))
}
 
// GetAttributeDetail
// @Tags      属性
// @Summary   获取属性详情
// @Produce   application/json
// @Param     id  path string true  "属性id"
// @Success   200   {object}  util.ResponseList{data=models.Attribute}  "成功"
// @Router    /api-wms/v1/attribute/attribute/{id} [get]
//func (slf AttributeController) GetAttributeDetail(c *gin.Context) {
//    id := cast.ToUint(c.Param("id"))
//    if id == 0 {
//        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
//        return
//    }
//    first, err := models.NewAttributeSearch().SetID(id).First()
//    if err != nil {
//        util.ResponseFormat(c, code.RequestParamError, "获取属性信息失败")
//        return
//    }
//
//    util.ResponseFormat(c, code.UpdateSuccess, first)
//}
 
// PrimaryAttribute
// @Tags      属性值和对象
// @Summary   查询属性值和对象 通过主键ID查询
// object  body  request.OperationList true  "查询参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/primary/{id}  [get]
func (slf AttributeController) PrimaryAttribute(c *gin.Context) {
    id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    attribute, err := models.NewAttributeSearch().SetID(uint(id)).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询失败")
        return
    }
    util.ResponseFormat(c, code.Success, attribute)
}