liujiandao
2023-11-03 4a9bc4b7c84985047c7ebe0b991e8c8364bb56a2
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
307
308
309
310
311
package controllers
 
import (
    "github.com/gin-gonic/gin"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "strconv"
    "strings"
    "time"
    "wms/constvar"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/timex"
    "wms/request"
)
 
type ReorderRuleController struct {
}
 
// AddReorderRule
// @Tags      重订货规则
// @Summary   添加重订货规则
// @Produce   application/json
// @Param     object  body  models.ReorderRule true  "重订货规则"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/reorderRule/addReorderRule [post]
func (slf ReorderRuleController) AddReorderRule(c *gin.Context) {
    var params models.ReorderRule
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    count, err := models.NewReorderRuleSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).Count()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "数据验证错误")
        return
    }
    if count > 0 {
        util.ResponseFormat(c, code.RequestError, "当前位置已存在相同产品的重订货规则")
        return
    }
    err = models.NewReorderRuleSearch().Create(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "重订货规则保存失败")
        return
    }
    util.ResponseFormat(c, code.Success, "保存成功")
}
 
// GetReorderRuleList
// @Tags      重订货规则
// @Summary   获取重订货规则列表
// @Produce   application/json
// @Param     object  body  request.GetReorderRuleList true  "参数"
// @Success   200 {object} util.ResponseList{data=[]models.ReorderRule} "成功"
// @Router    /api-wms/v1/reorderRule/getReorderRuleList [post]
func (slf ReorderRuleController) GetReorderRuleList(c *gin.Context) {
    var params request.GetReorderRuleList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    search := models.NewReorderRuleSearch()
    if params.PageInfo.Check() {
        search.SetPage(params.Page, params.PageSize)
    }
    rules, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).SetLocationId(params.LocationId).SetProductId(params.ProductId).Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
        return
    }
    productIds := make([]string, 0)
    locationIds := make([]int, 0)
    for _, rule := range rules {
        productIds = append(productIds, rule.ProductId)
        locationIds = append(locationIds, rule.LocationId)
    }
    if params.LocationId != 0 {
        locationIds = []int{params.LocationId}
    }
    if params.ProductId != "" {
        productIds = []string{params.ProductId}
    }
    //在库
    amounts, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询在库数量失败")
        return
    }
    for _, rule := range rules {
        for _, amount := range amounts {
            if rule.ProductId == amount.ProductId && rule.LocationId == amount.LocationId {
                rule.Amount = rule.Amount.Add(amount.Amount)
            }
        }
    }
 
    //预测
    //入库就绪
    var status = []constvar.OperationStatus{constvar.OperationStatus_Ready}
    var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal}
    amount, err := GetProductAmount(productIds, locationIds, nil, status, operationType)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
        return
    }
    for _, rule := range rules {
        for _, productAmount := range amount {
            if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId {
                rule.Prediction = rule.Prediction.Add(productAmount.Amount)
            }
        }
        rule.Prediction = rule.Amount.Add(rule.Prediction)
    }
    //出库就绪
    operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
    amount, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
        return
    }
    for _, rule := range rules {
        for _, productAmount := range amount {
            if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.FromLocationId {
                rule.Prediction = rule.Prediction.Sub(productAmount.Amount)
            }
        }
    }
    var result []*models.ReorderRule
    if params.Type == "" {
        result = rules
    } else {
        for _, rule := range rules {
            if rule.MinInventory.GreaterThan(rule.Prediction) {
                result = append(result, rule)
            }
        }
    }
 
    util.ResponseFormatList(c, code.Success, result, int(total))
}
 
// 计算在库与预测数量
func GetProductAmount(productIds []string, toLocationIds []int, fromLocationIds []int, status []constvar.OperationStatus,
    operationType []constvar.BaseOperationType) ([]request.ProductAmount, error) {
    var pa []request.ProductAmount
    search := models.NewOperationDetailsSearch()
    search.Orm = search.Orm.Model(&models.OperationDetails{}).
        Select("wms_operation_details.product_id, wms_operation_details.amount, wms_operation.to_location_id as to_location_id, " +
            "wms_operation.from_location_id as from_location_id, wms_operation.base_operation_type").
        Joins("left join wms_operation on wms_operation_details.operation_id = wms_operation.id")
    if len(productIds) > 0 {
        search.Orm.Where("wms_operation_details.product_id in (?)", productIds)
    }
    if len(toLocationIds) > 0 {
        search.Orm.Where("wms_operation.to_location_id in (?)", toLocationIds)
    }
    if len(fromLocationIds) > 0 {
        search.Orm.Where("wms_operation.from_location_id in (?)", fromLocationIds)
    }
    if len(status) > 0 {
        search.Orm.Where("wms_operation.status in (?)", status)
    }
    if len(operationType) > 0 {
        search.Orm.Where("wms_operation.base_operation_type in (?)", operationType)
    }
    err := search.Orm.Find(&pa).Error
    return pa, err
}
 
// GetAmountAndPrediction
// @Tags      重订货规则
// @Summary   获取在库与预测数量
// @Produce   application/json
// @Param     object  body  request.GetAmountAndPrediction true  "重订货规则"
// @Success   200 {object} util.ResponseList{data=[]map[string]interface{}} "成功"
// @Router    /api-wms/v1/reorderRule/getAmountAndPrediction [post]
func (slf ReorderRuleController) GetAmountAndPrediction(c *gin.Context) {
    var params request.GetAmountAndPrediction
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    productIds := make([]string, 0)
    locationIds := make([]int, 0)
    productIds = append(productIds, params.ProductId)
    locationIds = append(locationIds, params.LocationId)
    amount := decimal.NewFromInt(0)
    prediction := decimal.NewFromInt(0)
    //在库
    find, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询在库数量失败")
        return
    }
    for _, productAmount := range find {
        if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.LocationId {
            amount = amount.Add(productAmount.Amount)
        }
    }
 
    //预测
    //入库就绪
    var status = []constvar.OperationStatus{constvar.OperationStatus_Ready}
    var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal}
    list, err := GetProductAmount(productIds, locationIds, nil, status, operationType)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
        return
    }
    for _, productAmount := range list {
        if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.ToLocationId {
            prediction = prediction.Add(productAmount.Amount)
        }
    }
    prediction = amount.Add(prediction)
    //出库就绪
    operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
    list, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
        return
    }
    for _, productAmount := range list {
        if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.FromLocationId {
            prediction = prediction.Sub(productAmount.Amount)
        }
    }
    m := make(map[string]int64)
    m["amount"] = amount.IntPart()
    m["prediction"] = prediction.IntPart()
    util.ResponseFormat(c, code.Success, m)
}
 
// UpdateReorderRule
// @Tags      重订货规则
// @Summary   更新重订货规则
// @Produce   application/json
// @Param     object  body  models.ReorderRule true  "重订货规则"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/reorderRule/updateReorderRule [post]
func (slf ReorderRuleController) UpdateReorderRule(c *gin.Context) {
    var params models.ReorderRule
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    err := models.NewReorderRuleSearch().SetID(params.Id).Update(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "重订货规则更新失败")
        return
    }
    util.ResponseFormat(c, code.Success, "更新成功")
}
 
// OrderAgain
// @Tags      重订货规则
// @Summary   再订一次
// @Produce   application/json
// @Param     object  body  models.ReorderRule true  "重订货规则"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/reorderRule/orderAgain [post]
func (slf ReorderRuleController) OrderAgain(c *gin.Context) {
    var params models.ReorderRule
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    location, err := models.NewLocationSearch().SetID(params.LocationId).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询位置信息失败")
        return
    }
    houseCode := strings.Split(location.JointName, "/")[0]
    var operationType models.OperationType
    err = models.NewOperationTypeSearch().Orm.Model(&models.OperationType{}).Joins("left join wms_warehouse on wms_job_type.warehouse_id = wms_warehouse.id").
        Where("wms_job_type.base_operation_type = 1 and wms_warehouse.code = ?", houseCode).First(&operationType).Error
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询位置信息失败")
        return
    }
    var operation models.Operation
    var details models.OperationDetails
    details.ProductId = params.ProductId
    details.Amount = params.OrderNumber
    operation.Details = append(operation.Details, &details)
    operation.BaseOperationType = constvar.BaseOperationTypeIncoming
    operation.Status = constvar.OperationStatus_Ready
    operation.OperationTypeId = operationType.Id
    operation.OperationTypeName = operationType.Name
    operation.OperationDate = timex.TimeToString2(time.Now())
    //todo 供应商位置
    operation.FromLocationID = 1
    operation.Number = strconv.FormatInt(time.Now().Unix(), 10)
    operation.ToLocationID = params.LocationId
 
    err = models.WithTransaction(func(db *gorm.DB) error {
        if err = models.NewOperationSearch().SetOrm(db).Create(&operation); err != nil {
            return err
        }
        params.OrderNumber = decimal.NewFromInt(0)
        err = models.NewReorderRuleSearch().SetID(params.Id).Update(&params)
        return err
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "重订失败")
        return
    }
    util.ResponseFormat(c, code.Success, "重订成功")
}