zhangqian
2024-03-22 2dc22e8068ac5616a3581d4a261725d4ac02cf2b
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
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/structx"
    "outsourcing/request"
)
 
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
    }
    list, total, err := models.NewOutsourcingOrderSearch().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().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
    }
    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
    }
    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
    }
    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
    }
    //限定状态参数
    if params.Status == constvar.OutsourcingOrderStatusCreate || //取消确认
        params.Status == constvar.OutsourcingOrderStatusWaitProduce || //确认接受
        params.Status == constvar.OutsourcingOrderStatusFinish || //生产完成
        params.Status == constvar.OutsourcingOrderStatusDeliveryFinish { //发货完成
        m := make(map[string]interface{})
        m["status"] = params.Status
        m["reason"] = params.Reason
        err := models.NewOutsourcingOrderSearch().SetNumber(params.OutsourcingOrderNumber).UpdateByMap(m)
        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
    }
 
    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 == "" || params.Carrier == "" || params.WaybillNumber == "" {
        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,
            })
        }
        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
    }
    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)
}