zhangqian
2024-03-11 7d4f02f6a8066018911d09bad42c5c540abaa66b
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
package controllers
 
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "strconv"
    "wms/constvar"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/pkg/structx"
    "wms/request"
)
 
type OperationTypeController struct{}
 
// Add
// @Tags      业务类型
// @Summary   添加作业类型
// @Produce   application/json
// @Param     object  body  request.AddOperationType true  "作业类型信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operationType/operationType [post]
func (slf OperationTypeController) Add(c *gin.Context) {
    var reqParams request.AddOperationType
    var params models.OperationType
    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.NewOperationTypeSearch().Create(&params); err != nil {
        logx.Errorf("OperationType 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.UpdateOperationType true  "作业类型信息"
// @Param     id  path string true  "作业类型id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operationType/operationType/{id} [put]
func (slf OperationTypeController) Update(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    var (
        reqParams request.UpdateOperationType
        params    models.OperationType
    )
    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.NewOperationTypeSearch().SetID(params.ID).Update(&params)
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
 
func (slf OperationTypeController) ParamsCheck(params models.OperationType) (err error) {
    if params.ID != 0 {
        _, err = models.NewOperationTypeSearch().SetID(params.ID).First()
        if err == gorm.ErrRecordNotFound {
            return errors.New("记录不存在")
        }
    }
 
    return nil
}
 
// List
// @Tags      业务类型
// @Summary   查询作业类型列表
// @Produce   application/json
// @Param     object  query    request.GetOperationTypeList true  "查询参数"
// @Success   200   {object}  util.ResponseList  "成功"
// @Router    /api-wms/v1/operationType/operationType [get]
func (slf OperationTypeController) List(c *gin.Context) {
    var params request.GetOperationTypeList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, total, err := models.NewOperationTypeSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetOrder("id desc").SetPreload(true).Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "查找失败")
        return
    }
    var idList []int
    for _, v := range list {
        idList = append(idList, v.Id)
    }
    statistics, err := models.NewOperationTypeSearch().ListByStatusAndCount(idList)
    if err != nil {
        util.ResponseFormat(c, code.RequestError, err.Error())
        return
    }
    mapStatistics := make(map[string]*models.OperationTypeByStatus, 0)
    for _, v := range statistics {
        mapStatistics[strconv.Itoa(v.Id)+string(v.Status)] = v
    }
    for k, v := range list {
        if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Ready)]; ok {
            list[k].ReadyCount = value.Count
        }
        if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Finish)]; ok {
            list[k].FinishCount = value.Count
        }
        if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Cancel)]; ok {
            list[k].CancelCount = value.Count
        }
    }
    util.ResponseFormatListWithPage(c, code.Success, list, cast.ToInt(total), params.Page, params.PageSize)
}
 
// Delete
// @Tags      业务类型
// @Summary   删除作业类型
// @Produce   application/json
// @Param     id  path string true  "作业类型id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operationType/operationType/{id} [delete]
func (slf OperationTypeController) Delete(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
 
    err := models.NewOperationTypeSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}
 
// ListTransfer
// @Tags      业务类型
// @Summary   调拨类型列表
// @Produce   application/json
// @Param     object  query    request.ListTransfer true  "查询参数"
// @Success   200   {object}  util.ResponseList  "成功"
// @Router    /api-wms/v1/operationType/listTransfer [get]
func (slf OperationTypeController) ListTransfer(c *gin.Context) {
    var params request.GetOperationTypeList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, total, err := models.NewOperationTypeSearch().SetPage(params.Page, params.PageSize).SetBaseOperationType(constvar.BaseOperationTypeInternal).SetKeyword(params.Keyword).SetOrder("id desc").SetPreload(true).Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "查找失败")
        return
    }
    var idList []int
    for _, v := range list {
        idList = append(idList, v.Id)
    }
    statistics, err := models.NewOperationTypeSearch().ListByStatusAndCount(idList)
    if err != nil {
        util.ResponseFormat(c, code.RequestError, err.Error())
        return
    }
    mapStatistics := make(map[string]*models.OperationTypeByStatus, 0)
    for _, v := range statistics {
        mapStatistics[strconv.Itoa(v.Id)+string(v.Status)] = v
    }
    for k, v := range list {
        if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Ready)]; ok {
            list[k].ReadyCount = value.Count
        }
        if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Finish)]; ok {
            list[k].FinishCount = value.Count
        }
        if value, ok := mapStatistics[strconv.Itoa(v.Id)+string(constvar.OperationStatus_Cancel)]; ok {
            list[k].FinishCount = value.Count
        }
    }
    util.ResponseFormatListWithPage(c, code.Success, list, cast.ToInt(total), params.Page, params.PageSize)
}