|
@@ -15,10 +15,7 @@ import (
|
|
func Order(r *GinRouter) {
|
|
func Order(r *GinRouter) {
|
|
r.POSTJWT("/order/createBatch", OrderAddBatch)
|
|
r.POSTJWT("/order/createBatch", OrderAddBatch)
|
|
r.GETJWT("/order/list", OrderList)
|
|
r.GETJWT("/order/list", OrderList)
|
|
- // ??? 更新订单的操作 订单
|
|
|
|
- r.POSTJWT("/order/complete", OrderComplete)
|
|
|
|
- r.POSTJWT("/order/cancel", OrderCancel)
|
|
|
|
- r.POSTJWT("/order/update", OrderUpdate)
|
|
|
|
|
|
+ r.POSTJWT("/order/cancelOrComplete/:id", OrderCancelOrComplete)
|
|
r.GETJWT("/order/detail/:id", OrderDetail)
|
|
r.GETJWT("/order/detail/:id", OrderDetail)
|
|
r.GETJWT("/order/count", OrderCount)
|
|
r.GETJWT("/order/count", OrderCount)
|
|
}
|
|
}
|
|
@@ -127,9 +124,9 @@ func OrderCount(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
return resMap, nil
|
|
return resMap, nil
|
|
}
|
|
}
|
|
|
|
|
|
-// 订单完成
|
|
|
|
-func OrderComplete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- // 订单状态为已发货
|
|
|
|
|
|
+// 订单取消和完成操作
|
|
|
|
+func OrderCancelOrComplete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
+ // 订单中每个商品都处于未发货状态
|
|
_id := c.Param("id")
|
|
_id := c.Param("id")
|
|
if len(_id) < 1 {
|
|
if len(_id) < 1 {
|
|
return nil, errors.New("id不能为空")
|
|
return nil, errors.New("id不能为空")
|
|
@@ -149,99 +146,33 @@ func OrderComplete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
if len(order.Products) < 1 {
|
|
if len(order.Products) < 1 {
|
|
return nil, errors.New("该订单为空")
|
|
return nil, errors.New("该订单为空")
|
|
}
|
|
}
|
|
|
|
+ // 操作人非订单所属人
|
|
|
|
+ if order.UserId.Hex() != apictx.User.ID {
|
|
|
|
+ return nil, errors.New("非法操作")
|
|
|
|
+ }
|
|
|
|
+
|
|
statusMap := map[int]struct{}{}
|
|
statusMap := map[int]struct{}{}
|
|
for _, v := range order.Products {
|
|
for _, v := range order.Products {
|
|
statusMap[v.Status] = struct{}{}
|
|
statusMap[v.Status] = struct{}{}
|
|
}
|
|
}
|
|
- // 状态为一种,且都为已发货时 可以完成订单
|
|
|
|
|
|
+
|
|
|
|
+ // 状态为一种
|
|
if len(statusMap) == 1 {
|
|
if len(statusMap) == 1 {
|
|
- if _, ok := statusMap[1]; ok {
|
|
|
|
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, _id, &model.Order{Status: 2, IsCancel: -1})
|
|
|
|
|
|
+ // 且都为未发货状态时 可以取消订单
|
|
|
|
+ // 取消流程
|
|
|
|
+ if _, ok := statusMap[-1]; ok && order.Status == -1 {
|
|
|
|
+ return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, _id, &model.Order{Status: 3, IsCancel: -1})
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
- return nil, errors.New("该订单商品未全部发货")
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 订单取消
|
|
|
|
-func OrderCancel(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- // 订单状态为未发货
|
|
|
|
-
|
|
|
|
- return nil, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 更新订单 发货
|
|
|
|
-func OrderUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- var form model.Order
|
|
|
|
- err := c.ShouldBindJSON(&form)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, errors.New("参数错误")
|
|
|
|
- }
|
|
|
|
- if form.Id.Hex() == "" {
|
|
|
|
- return nil, errors.New("更新的产品id不能为空")
|
|
|
|
- }
|
|
|
|
- form.UpdateTime = time.Now()
|
|
|
|
-
|
|
|
|
- result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &form)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
|
|
|
|
- // 更新订单状态
|
|
|
|
- if result.ModifiedCount > 0 {
|
|
|
|
- // 查询订单
|
|
|
|
- curOder := model.Order{}
|
|
|
|
- found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
|
- CollectName: repo.CollectionOrder,
|
|
|
|
- Query: repo.Map{"_id": form.Id},
|
|
|
|
- Project: []string{"products"},
|
|
|
|
- }, &curOder)
|
|
|
|
|
|
+ // 且都为已发货状态时 可以取消订单
|
|
|
|
+ // 完成流程
|
|
|
|
+ if _, ok := statusMap[1]; ok && order.Status == 1 {
|
|
|
|
|
|
- if found {
|
|
|
|
- if len(curOder.Products) > 0 {
|
|
|
|
- statusMap := make(map[int]struct{})
|
|
|
|
- for _, v := range curOder.Products {
|
|
|
|
- statusMap[v.Status] = struct{}{}
|
|
|
|
- }
|
|
|
|
- // {"-1",1,2}
|
|
|
|
- // {-1,1}
|
|
|
|
- // {-1,2}
|
|
|
|
- // {1,2}
|
|
|
|
- // {-1}
|
|
|
|
- // {1}
|
|
|
|
- // {3}
|
|
|
|
- status := curOder.Status
|
|
|
|
- if len(statusMap) == 1 {
|
|
|
|
- if _, ok := statusMap[-1]; ok {
|
|
|
|
- status = -1
|
|
|
|
- }
|
|
|
|
- if _, ok := statusMap[1]; ok {
|
|
|
|
- status = 1
|
|
|
|
- }
|
|
|
|
- if _, ok := statusMap[2]; ok {
|
|
|
|
- status = 2
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if len(statusMap) == 2 {
|
|
|
|
- if _, ok := statusMap[-1]; ok {
|
|
|
|
- status = -1
|
|
|
|
- } else if _, ok := statusMap[1]; ok {
|
|
|
|
- status = 1
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if len(statusMap) == 3 {
|
|
|
|
- status = -1
|
|
|
|
- }
|
|
|
|
- var statusOrder model.Order
|
|
|
|
- statusOrder.Status = status
|
|
|
|
- repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &statusOrder)
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+ return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, _id, &model.Order{Status: 2, IsCancel: -1})
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
- return result, err
|
|
|
|
|
|
|
|
|
|
+ return nil, errors.New("该订单不满足取消或完成操作")
|
|
}
|
|
}
|
|
|
|
|
|
// 订单信息
|
|
// 订单信息
|