jiangshuai
2023-09-22 f89bbcf77dae0465e829ca6f7548cd36ef57aaa6
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package controllers
 
import (
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/request"
    "wms/utils"
)
 
type ProductController struct {
}
 
// AddProduct
// @Tags      产品
// @Summary   添加产品
// @Produce   application/json
// @Param     object  body  models.Material true  "产品信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/product/addProduct [post]
func (slf ProductController) AddProduct(c *gin.Context) {
    var params models.Material
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.Name == "" {
        util.ResponseFormat(c, code.RequestParamError, "产品名称不能为空")
        return
    }
    if params.SalePrice.IntPart() <= 0 {
        util.ResponseFormat(c, code.RequestParamError, "产品售价不能小于等于零")
        return
    }
    if params.Model == "" {
        util.ResponseFormat(c, code.RequestParamError, "物料类型不能为空")
        return
    }
    if params.Unit == "" {
        util.ResponseFormat(c, code.RequestParamError, "单位不能为空")
        return
    }
    params.ID = utils.GetUUID()
    err := models.NewMaterialSearch().Create(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "产品信息保存失败")
        return
    }
    util.ResponseFormat(c, code.Success, "保存成功")
}
 
// GetProductList
// @Tags      产品
// @Summary   获取产品列表
// @Produce   application/json
// @Param     object  body  request.GetProductList true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]models.Material}    "成功"
// @Router    /api-wms/v1/product/getProductList [post]
func (slf ProductController) GetProductList(c *gin.Context) {
    var params request.GetProductList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    search := models.NewMaterialSearch()
    if params.PageInfo.Check() {
        search.SetPage(params.Page, params.PageSize)
    }
    products, 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, products, int(total))
}
 
// GetProductDetails
// @Tags      产品
// @Summary   获取产品详情
// @Produce   application/json
// @Param        id    path        string            true    "id"  "查询参数"
// @Success   200 {object} util.Response{data=models.Material}    "成功"
// @Router    /api-wms/v1/product/getProductDetails/{id} [get]
func (slf ProductController) GetProductDetails(c *gin.Context) {
    id := c.Param("id")
    if id == "" {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    material, err := models.NewMaterialSearch().SetID(id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    util.ResponseFormat(c, code.Success, material)
}
 
// UpdateProduct
// @Tags      产品
// @Summary   修改产品
// @Produce   application/json
// @Param     object  body  models.Material true  "产品信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/product/updateProduct [post]
func (slf ProductController) UpdateProduct(c *gin.Context) {
    var params models.Material
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.Name == "" {
        util.ResponseFormat(c, code.RequestParamError, "产品名称不能为空")
        return
    }
    if params.SalePrice.IntPart() <= 0 {
        util.ResponseFormat(c, code.RequestParamError, "产品售价不能小于等于零")
        return
    }
    if params.Model == "" {
        util.ResponseFormat(c, code.RequestParamError, "物料类型不能为空")
        return
    }
    if params.Unit == "" {
        util.ResponseFormat(c, code.RequestParamError, "单位不能为空")
        return
    }
    err := models.NewMaterialSearch().SetID(params.ID).Save(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "产品信息更新失败")
        return
    }
    util.ResponseFormat(c, code.Success, "更新成功")
}
 
// DeleteProduct
// @Tags      产品
// @Summary   删除产品
// @Produce   application/json
// @Param        id    path        string            true    "id"  "查询参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/product/deleteProduct/{id} [delete]
func (slf ProductController) DeleteProduct(c *gin.Context) {
    id := c.Param("id")
    if id == "" {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    err := models.NewMaterialSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.Success, "删除成功")
}
 
// AddProductCategory
// @Tags      产品类型
// @Summary   添加产品类型
// @Produce   application/json
// @Param     object  body  models.ProductCategory true  "产品类型信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/product/addProductCategory [post]
func (slf ProductController) AddProductCategory(c *gin.Context) {
    var params models.ProductCategory
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.Name == "" {
        util.ResponseFormat(c, code.RequestParamError, "产品类型名称不能为空")
        return
    }
    err := models.NewProductCategorySearch().Create(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "产品类型信息保存失败")
        return
    }
    util.ResponseFormat(c, code.Success, "保存成功")
}
 
// GetProductCategoryList
// @Tags      产品类型
// @Summary   获取产品类型列表
// @Produce   application/json
// @Param     object  body  request.GetProductList true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]models.ProductCategory}    "成功"
// @Router    /api-wms/v1/product/getProductCategoryList [post]
func (slf ProductController) GetProductCategoryList(c *gin.Context) {
    var params request.GetProductList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    search := models.NewProductCategorySearch()
    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))
}
 
// GetProductCategoryDetails
// @Tags      产品类型
// @Summary   获取产品类型详情
// @Produce   application/json
// @Param        id    path        string            true    "id"  "查询参数"
// @Success   200 {object} util.Response{data=models.Material}    "成功"
// @Router    /api-wms/v1/product/getProductCategoryDetails/{id} [get]
func (slf ProductController) GetProductCategoryDetails(c *gin.Context) {
    id := c.Param("id")
    if id == "" {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
 
    first, err := models.NewProductCategorySearch().SetID(cast.ToUint(id)).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    util.ResponseFormat(c, code.Success, first)
}
 
// UpdateProductCategory
// @Tags      产品类型
// @Summary   修改产品类型
// @Produce   application/json
// @Param     object  body  models.ProductCategory true  "产品信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/product/updateProductCategory [post]
func (slf ProductController) UpdateProductCategory(c *gin.Context) {
    var params models.ProductCategory
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.Name == "" {
        util.ResponseFormat(c, code.RequestParamError, "产品类型名称不能为空")
        return
    }
    err := models.NewProductCategorySearch().SetID(params.ID).Save(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "产品类型信息更新失败")
        return
    }
    util.ResponseFormat(c, code.Success, "更新成功")
}
 
// DeleteProductCategory
// @Tags      产品类型
// @Summary   删除产品类型
// @Produce   application/json
// @Param        id    path        string            true    "id"  "查询参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/product/deleteProductCategory/{id} [delete]
func (slf ProductController) DeleteProductCategory(c *gin.Context) {
    id := c.Param("id")
    if id == "" {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    err := models.NewProductCategorySearch().SetID(cast.ToUint(id)).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.Success, "删除成功")
}
 
// ListOperation
// @Tags      产品
// @Summary   产品历史出入库信息
// @Produce   application/json
// @Param         object  body  request.QueryOperationList true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]models.Operation}    "成功"
// @Router    /api-wms/v1/product/listOperaton [post]
func (slf ProductController) ListOperation(c *gin.Context) {
    var params request.QueryOperationList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if !params.PageInfo.Check() {
        util.ResponseFormat(c, code.RequestParamError, "页码信息错误")
        return
    }
 
    search := models.NewOperationSearch().SetPage(params.Page, params.PageSize).SetPreload(true).SetOrder("created_at desc")
    search.SetOrm(search.Orm.InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id").Where("wms_operation_details.product_id=?", params.ProductId))
 
    list, total, err := search.Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
        return
    }
 
    util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}