zhangqian
2024-03-30 85a6e689524c5c49f83f11e2d60ca2a584c7d137
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package controllers
 
import (
    "github.com/gin-gonic/gin"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "outsourcing/constvar"
    "outsourcing/extend/code"
    "outsourcing/extend/util"
    "outsourcing/models"
    "outsourcing/pkg/logx"
    "outsourcing/pkg/structx"
    "outsourcing/request"
    "outsourcing/response"
    "outsourcing/service"
    "outsourcing/service/outsourcing"
    "outsourcing/utils/jwt"
)
 
type OrderController struct{}
 
// OutsourcingOrderList
// @Tags      订单管理
// @Summary   委外订单列表
// @Produce   application/json
// @Param     object  query    request.OutsourcingOrderList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.OutsourcingOrder}  "成功"
// @Router    /api-outsourcing/v1/order/list [get]
func (slf *OrderController) OutsourcingOrderList(c *gin.Context) {
    var params request.OutsourcingOrderList
    if err := c.BindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    companyId := jwt.GetCompanyID(c)
    if companyId == 0 {
        util.ResponseFormat(c, code.InternalError, "内部错误")
        return
    }
 
    list, total, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(companyId).SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.InternalError, "查询错误")
        return
    }
    util.ResponseFormatList(c, code.Success, list, int(total))
}
 
// OrderOverview
// @Tags      订单管理
// @Summary   订单统计
// @Produce   application/json
// @Success   200   {object}  util.ResponseList{data=request.OutsourcingOrderOverview}  "成功"
// @Router    /api-outsourcing/v1/order/overview [get]
func (slf *OrderController) OrderOverview(c *gin.Context) {
    result, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).CountGroupByStatus()
    if err != nil {
        util.ResponseFormat(c, code.InternalError, "查询错误")
        return
    }
    resp := &request.OutsourcingOrderOverview{}
    for _, v := range result {
        resp.Total += v.Total
        status := constvar.OutsourcingOrderStatus(v.Status)
        if status == constvar.OutsourcingOrderStatusCreate {
            resp.WaitAssigned += v.Total
        } else {
            resp.HasAssigned += v.Total
        }
    }
    util.ResponseFormat(c, code.Success, resp)
}
 
// OutsourcingOrderProductList
// @Tags      订单管理
// @Summary   委外订单产品列表
// @Produce   application/json
// @Param     object  query    request.OutsourcingOrderProductList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.OutsourcingOrderProduct}  "成功"
// @Router    /api-outsourcing/v1/order/productList [get]
func (slf *OrderController) OutsourcingOrderProductList(c *gin.Context) {
    var params request.OutsourcingOrderProductList
    if err := c.BindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.OutsourcingOrderId == 0 {
        util.ResponseFormat(c, code.RequestParamError, "参数缺失")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetID(params.OutsourcingOrderId).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    list, total, err := models.NewOutsourcingOrderProductSearch().SetPage(params.Page, params.PageSize).SetOutsourcingOrderID(params.OutsourcingOrderId).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.InternalError, "查询错误")
        return
    }
    util.ResponseFormatList(c, code.Success, list, int(total))
}
 
// SaveMaterialApply
// @Tags      订单管理
// @Summary   保存物料申请单
// @Produce   application/json
// @Param     object  body    request.SaveMaterialApply true  "参数"
// @Success   200   {object}  util.Response{}  "成功"
// @Router    /api-outsourcing/v1/order/saveMaterialApply [post]
func (slf *OrderController) SaveMaterialApply(c *gin.Context) {
    var params request.SaveMaterialApply
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if len(params.ApplyList) == 0 {
        util.ResponseFormat(c, code.RequestParamError, "物料申请不能为空")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetNumber(params.ApplyList[0].OutsourcingOrderNumber).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    var apply []*models.OutsourcingMaterialApply
    err = structx.AssignTo(params.ApplyList, &apply)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换失败")
        return
    }
    err = models.WithTransaction(func(db *gorm.DB) error {
        err = models.NewOutsourcingMaterialApplySearch().SetOrm(db).CreateBatch(apply)
        if err != nil {
            return err
        }
        err = models.NewOutsourcingOrderSearch().SetOrm(db).SetNumber(params.ApplyList[0].OutsourcingOrderNumber).
            UpdateByMap(map[string]interface{}{"status": constvar.OutsourcingOrderStatusMaterialApplying})
        return err
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "保存失败")
        return
    }
 
    util.ResponseFormat(c, code.Success, "保存成功")
}
 
// GetMaterialApplyList
// @Tags      订单管理
// @Summary   获取物料申请单
// @Produce   application/json
// @Param     object  body    request.GetMaterialApplyList true  "参数"
// @Success   200   {object}  util.ResponseList{[]models.OutsourcingMaterialApply}  "成功"
// @Router    /api-outsourcing/v1/order/getMaterialApplyList [post]
func (slf *OrderController) GetMaterialApplyList(c *gin.Context) {
    var params request.GetMaterialApplyList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetNumber(params.Number).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    find, err := models.NewOutsourcingMaterialApplySearch().SetOutsourcingOrderNumber(params.Number).FindNotTotal()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询失败")
        return
    }
    util.ResponseFormat(c, code.Success, find)
}
 
// ChangeStatus
// @Tags      订单管理
// @Summary   修改状态
// @Produce   application/json
// @Param     object  body    request.ChangeStatus true  "参数"
// @Success   200   {object}  util.Response  "成功"
// @Router    /api-outsourcing/v1/order/changeStatus [post]
func (slf *OrderController) ChangeStatus(c *gin.Context) {
    var params request.ChangeStatus
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetNumber(params.OutsourcingOrderNumber).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    //限定状态参数
    if params.Status == constvar.OutsourcingOrderStatusCreate || //取消确认
        params.Status == constvar.OutsourcingOrderStatusWaitProduce || //确认接受
        params.Status == constvar.OutsourcingOrderStatusFinish || //生产完成
        params.Status == constvar.OutsourcingOrderStatusDeliveryFinish { //发货完成
        err = models.WithTransaction(func(db *gorm.DB) error {
            m := make(map[string]interface{})
            m["status"] = params.Status
            m["reason"] = params.Reason
            err := models.NewOutsourcingOrderSearch().SetNumber(params.OutsourcingOrderNumber).UpdateByMap(m)
            if err != nil {
                return err
            }
            if params.Status == constvar.OutsourcingOrderStatusCreate {
                return models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetNumber(params.OutsourcingOrderNumber).UpdateByMap(map[string]interface{}{
                    "enterprise_id": 0,
                })
            }
            return nil
        })
 
        if err != nil {
            util.ResponseFormat(c, code.RequestParamError, "状态更新失败")
            return
        }
        util.ResponseFormat(c, code.Success, "状态更新成功")
        return
    }
    util.ResponseFormat(c, code.RequestParamError, "状态参数不正确")
}
 
// DeliveryPrepare
// @Tags      订单管理
// @Summary   发货准备
// @Produce   application/json
// @Param     object  query    request.DeliveryPrepare true  "参数"
// @Success   200   {object}  util.Response{}  "成功"
// @Router    /api-outsourcing/v1/order/deliveryPrepare [get]
func (slf *OrderController) DeliveryPrepare(c *gin.Context) {
    var params request.DeliveryPrepare
    if err := c.BindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.OutsourcingOrderID == 0 {
        util.ResponseFormat(c, code.RequestParamError, "参数缺失")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetID(params.OutsourcingOrderID).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    orderProducts, err := models.NewOutsourcingOrderProductSearch().SetOutsourcingOrderID(params.OutsourcingOrderID).FindNotTotal()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询订单产品列表失败")
        return
    }
    deliveryDetails, err := models.NewOutsourcingOrderDeliveryDetailsSearch().SetOutsourcingOrderID(params.OutsourcingOrderID).FindNotTotal()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询订单产品发货明细失败")
        return
    }
    deliveryMap := make(map[uint]decimal.Decimal)
    for _, item := range deliveryDetails {
        deliveryMap[item.OutsourcingOrderProductID] = deliveryMap[item.OutsourcingOrderProductID].Add(item.SendAmount)
    }
    for _, v := range orderProducts {
        v.SendAmount = deliveryMap[v.ID]
    }
    util.ResponseFormat(c, code.Success, orderProducts)
}
 
// SaveDelivery
// @Tags      订单管理
// @Summary   保存发货信息
// @Produce   application/json
// @Param     object  body    request.SaveDelivery true  "参数"
// @Success   200   {object}  util.Response{}  "成功"
// @Router    /api-outsourcing/v1/order/saveDelivery [post]
func (slf *OrderController) SaveDelivery(c *gin.Context) {
    var params request.SaveDelivery
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if params.OutsourcingOrderID == 0 || params.Number == "" {
        util.ResponseFormat(c, code.RequestParamError, "参数缺失")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetID(params.OutsourcingOrderID).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    if len(params.DeliveryList) == 0 {
        util.ResponseFormat(c, code.RequestParamError, "发货列表不能为空")
        return
    }
 
    delivery := &models.OutsourcingOrderDelivery{
        OutsourcingOrderID: params.OutsourcingOrderID,
        Number:             params.Number,
        Carrier:            params.Carrier,
        WaybillNumber:      params.WaybillNumber,
    }
 
    err = models.WithTransaction(func(db *gorm.DB) error {
        err = models.NewOutsourcingOrderDeliverySearch().SetOrm(db).Create(delivery)
        if err != nil {
            return err
        }
        items := make([]*models.OutsourcingOrderDeliveryDetails, 0, len(params.DeliveryList))
        for _, v := range params.DeliveryList {
            items = append(items, &models.OutsourcingOrderDeliveryDetails{
                OutsourcingOrderID:         params.OutsourcingOrderID,
                OutsourcingOrderDeliveryID: delivery.ID,
                OutsourcingOrderProductID:  v.OutsourcingOrderProductID,
                SendAmount:                 v.SendAmount,
                IsReceived:                 2,
            })
        }
        return models.NewOutsourcingOrderDeliveryDetailsSearch().SetOrm(db).CreateBatch(items)
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "保存失败")
        return
    }
 
    util.ResponseFormat(c, code.Success, "保存成功")
}
 
// GetDeliveryList
// @Tags      订单管理
// @Summary   获取发货列表
// @Produce   application/json
// @Param     object  body    request.GetDeliveryList true  "参数"
// @Success   200   {object}  util.ResponseList{[]models.OutsourcingOrderDeliveryDetails}  "成功"
// @Router    /api-outsourcing/v1/order/deliveryList [post]
func (slf *OrderController) GetDeliveryList(c *gin.Context) {
    var params request.GetDeliveryList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    _, err := models.NewOutsourcingOrderSearch().SetEnterpriseID(jwt.GetCompanyID(c)).SetID(params.OutsourcingOrderID).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "订单不存在")
        return
    }
 
    list, err := models.NewOutsourcingOrderDeliveryDetailsSearch().
        SetOutsourcingOrderID(params.OutsourcingOrderID).
        SetPreload(true).
        SetOrder("id desc").
        FindNotTotal()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询失败")
        return
    }
    util.ResponseFormat(c, code.Success, list)
}
 
// MaterialSearch
// @Tags      订单管理
// @Summary   物料搜索
// @Produce   application/json
// @Param     object  body    request.MaterialSearch true  "参数"
// @Success   200   {object}  util.ResponseList{[]response.Material}  "成功"
// @Router    /api-outsourcing/v1/order/materialSearch [post]
func (slf *OrderController) MaterialSearch(c *gin.Context) {
    var params request.MaterialSearch
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    client := outsourcing.NewOutsourcingServiceClient(service.ApsServiceConn)
    resp, err := client.GetMaterialList(c, &outsourcing.GetMaterialListRequest{
        Page:     int32(params.Page),
        PageSize: int32(params.PageSize),
        Keyword:  params.Keyword,
    })
 
    if err != nil {
        logx.Errorf("grpc outsourcing GetProductList err:%v", err)
        util.ResponseFormat(c, code.RequestParamError, "查询失败")
        return
    }
 
    data := make([]*response.Material, 0, len(resp.List))
    for _, item := range resp.List {
        data = append(data, &response.Material{
            Number: item.ID,
            Name:   item.Name,
            Unit:   item.Unit,
            Specs:  item.Specs,
            Type:   item.Type,
        })
    }
 
    util.ResponseFormat(c, code.Success, data)
}