|
@@ -4,8 +4,6 @@ import (
|
|
"3dshow-supplier/db/model"
|
|
"3dshow-supplier/db/model"
|
|
"3dshow-supplier/db/repo"
|
|
"3dshow-supplier/db/repo"
|
|
"errors"
|
|
"errors"
|
|
- "fmt"
|
|
|
|
- "time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
@@ -13,224 +11,134 @@ import (
|
|
|
|
|
|
// 产品管理
|
|
// 产品管理
|
|
func Order(r *GinRouter) {
|
|
func Order(r *GinRouter) {
|
|
- r.POSTJWT("/order/createBatch", OrderAddBatch)
|
|
|
|
r.GETJWT("/order/list", OrderList)
|
|
r.GETJWT("/order/list", OrderList)
|
|
- r.POSTJWT("/order/update", OrderUpdate)
|
|
|
|
|
|
+ r.POST("/order/delivery", OrderDelivery)
|
|
r.GETJWT("/order/detail/:id", OrderDetail)
|
|
r.GETJWT("/order/detail/:id", OrderDetail)
|
|
- r.POSTJWT("/order/delete/:id", OrderDelete)
|
|
|
|
- r.GETJWT("/order/count", OrderCount)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 批量新增订单
|
|
|
|
-func OrderAddBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- var orders []*model.OrderAddReq
|
|
|
|
- err := c.ShouldBindJSON(&orders)
|
|
|
|
- if err != nil {
|
|
|
|
- fmt.Println(err)
|
|
|
|
- return nil, errors.New("参数错误!")
|
|
|
|
- }
|
|
|
|
- ctx := apictx.CreateRepoCtx()
|
|
|
|
- _userId := apictx.User.ID
|
|
|
|
- userId, _ := primitive.ObjectIDFromHex(_userId)
|
|
|
|
-
|
|
|
|
- if len(orders) < 1 {
|
|
|
|
- return nil, errors.New("购物车为空")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for _, v := range orders {
|
|
|
|
- order := &model.Order{}
|
|
|
|
- order.UserId = userId
|
|
|
|
- order.SupplyId = v.Supply.Id
|
|
|
|
- order.Address = v.Address
|
|
|
|
- // order.Products
|
|
|
|
- // 组装商品和设置默认状态
|
|
|
|
- if len(v.Products) > 0 {
|
|
|
|
- for _, p := range v.Products {
|
|
|
|
- order.Products = append(order.Products, &model.OrderProduct{
|
|
|
|
- Id: p.ProductId,
|
|
|
|
- SupplyId: p.SupplyId,
|
|
|
|
- Name: p.Name,
|
|
|
|
- Size: p.Size,
|
|
|
|
- Color: p.Color,
|
|
|
|
- Unit: p.Unit,
|
|
|
|
- Cover: p.Cover,
|
|
|
|
- ExpressNo: "",
|
|
|
|
- Status: -1,
|
|
|
|
- })
|
|
|
|
- // 加入购物车后 删除购物车对应商品
|
|
|
|
- repo.RepoDeleteDoc(ctx, repo.CollectionShopCart, p.Id.Hex())
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- order.DeliveryMethod = v.DeliveryMethod
|
|
|
|
- order.Remark = v.Remark
|
|
|
|
- order.Status = -1
|
|
|
|
- order.CreateTime = time.Now()
|
|
|
|
- repo.RepoAddDoc(ctx, repo.CollectionOrder, order)
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return true, nil
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// 订单列表
|
|
// 订单列表
|
|
func OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- // customer
|
|
|
|
- // supplier
|
|
|
|
page, size, query := UtilQueryPageSize(c)
|
|
page, size, query := UtilQueryPageSize(c)
|
|
_userId := apictx.User.ID
|
|
_userId := apictx.User.ID
|
|
userId, _ := primitive.ObjectIDFromHex(_userId)
|
|
userId, _ := primitive.ObjectIDFromHex(_userId)
|
|
-
|
|
|
|
- // if apictx.User.Role == "customer"{
|
|
|
|
- query["userId"] = userId
|
|
|
|
- // }
|
|
|
|
|
|
+ // 供应商的订单列表
|
|
|
|
+ query["supplyId"] = userId
|
|
result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
CollectName: repo.CollectionOrder,
|
|
CollectName: repo.CollectionOrder,
|
|
Page: page,
|
|
Page: page,
|
|
Size: size,
|
|
Size: size,
|
|
Query: query,
|
|
Query: query,
|
|
- // Project: []string{},
|
|
|
|
- Sort: repo.Map{"createTime": -1},
|
|
|
|
|
|
+ Project: []string{"createTime", "userId", "supplyId", "products"},
|
|
|
|
+ Sort: repo.Map{"createTime": -1},
|
|
})
|
|
})
|
|
|
|
|
|
if len(result.List) > 0 {
|
|
if len(result.List) > 0 {
|
|
for _, v := range result.List {
|
|
for _, v := range result.List {
|
|
- supplyId := v["supplyId"].(primitive.ObjectID)
|
|
|
|
- supply := model.Supply{}
|
|
|
|
|
|
+ orderCount := 0
|
|
|
|
+ notShipCount := 0
|
|
|
|
+
|
|
|
|
+ // 客户信息
|
|
|
|
+ customUserId := v["userId"].(primitive.ObjectID)
|
|
|
|
+ customerUser := model.UserSmaple{}
|
|
repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
- CollectName: repo.CollectionSupply,
|
|
|
|
- Query: repo.Map{"_id": supplyId},
|
|
|
|
- }, &supply)
|
|
|
|
- v["supplyName"] = supply.Name
|
|
|
|
|
|
+ Db: "customer-user",
|
|
|
|
+ CollectName: "users",
|
|
|
|
+ Query: repo.Map{"_id": customUserId},
|
|
|
|
+ }, &customerUser)
|
|
|
|
+ v["customer"] = customerUser
|
|
|
|
+
|
|
|
|
+ // 统计下单数和未发货数
|
|
|
|
+ orderCount = len(v["products"].([]*model.OrderProduct))
|
|
|
|
+ if orderCount > 0 {
|
|
|
|
+ for _, p := range v["products"].([]*model.OrderProduct) {
|
|
|
|
+ if p.Status == -1 {
|
|
|
|
+ notShipCount++
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ v["orderCount"] = orderCount
|
|
|
|
+ v["notShipCount"] = notShipCount
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return result, err
|
|
return result, err
|
|
}
|
|
}
|
|
|
|
|
|
-// 个人页面数量展示
|
|
|
|
-func OrderCount(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- _userId := apictx.User.ID
|
|
|
|
- userId, err := primitive.ObjectIDFromHex(_userId)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, errors.New("非法用户")
|
|
|
|
- }
|
|
|
|
- statusArray := []int{-1, 1, 2, 3}
|
|
|
|
- resMap := make(map[int]int64)
|
|
|
|
- // var ret []int64
|
|
|
|
- ctx := apictx.CreateRepoCtx()
|
|
|
|
- for _, v := range statusArray {
|
|
|
|
- query := repo.Map{"userId": userId, "status": v}
|
|
|
|
- res, _ := repo.RepoCountDoc(ctx, repo.CollectionOrder, query)
|
|
|
|
- // ret = append(ret, res)
|
|
|
|
- resMap[v] = res
|
|
|
|
- }
|
|
|
|
- return resMap, nil
|
|
|
|
|
|
+// 发货
|
|
|
|
+type OrderDeliveryReq struct {
|
|
|
|
+ Id primitive.ObjectID `json:"id"`
|
|
|
|
+ ProductId primitive.ObjectID `json:"productId"`
|
|
|
|
+ ExpressNo string `json:"expressNo"`
|
|
}
|
|
}
|
|
|
|
|
|
-// 更新订单 发货
|
|
|
|
-func OrderUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- var form model.Order
|
|
|
|
- err := c.ShouldBindJSON(&form)
|
|
|
|
|
|
+func OrderDelivery(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
+ var form OrderDeliveryReq
|
|
|
|
+ err := c.ShouldBind(&form)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, errors.New("参数错误")
|
|
return nil, errors.New("参数错误")
|
|
}
|
|
}
|
|
- if form.Id.Hex() == "" {
|
|
|
|
- return nil, errors.New("更新的产品id不能为空")
|
|
|
|
|
|
+ // 验证订单是不是自己的
|
|
|
|
+ _userId := apictx.User.ID
|
|
|
|
+ userId, _ := primitive.ObjectIDFromHex(_userId)
|
|
|
|
+ order := model.Order{}
|
|
|
|
+ found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
|
+ CollectName: repo.CollectionOrder,
|
|
|
|
+ Query: repo.Map{"_id": form.Id},
|
|
|
|
+ }, &order)
|
|
|
|
+ if !found || err != nil {
|
|
|
|
+ return nil, errors.New("该订单未找到")
|
|
}
|
|
}
|
|
- form.UpdateTime = time.Now()
|
|
|
|
-
|
|
|
|
- result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &form)
|
|
|
|
|
|
+ if userId != order.SupplyId {
|
|
|
|
+ return nil, errors.New("非法操作")
|
|
|
|
+ }
|
|
|
|
+ if len(order.Products) < 1 {
|
|
|
|
+ return nil, errors.New("无效订单")
|
|
|
|
+ }
|
|
|
|
+ // 发货
|
|
|
|
+ result, err := repo.RepoDocArrayOneUpdate(apictx.CreateRepoCtx(), &repo.ArrayOneUpdateOption{
|
|
|
|
+ CollectName: repo.CollectionOrder,
|
|
|
|
+ Id: form.Id.Hex(),
|
|
|
|
+ Query: repo.Map{"products.id": form.ProductId},
|
|
|
|
+ Set: repo.Map{"status": 1, "expressNo": form.ExpressNo},
|
|
|
|
+ })
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, errors.New("发货商品异常")
|
|
}
|
|
}
|
|
|
|
|
|
- // 更新订单状态
|
|
|
|
|
|
+ // 更新订单状态 已发货
|
|
if result.ModifiedCount > 0 {
|
|
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 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)
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, form.Id.Hex(), &model.Order{Status: 1})
|
|
}
|
|
}
|
|
- return result, err
|
|
|
|
|
|
|
|
|
|
+ return result, nil
|
|
}
|
|
}
|
|
|
|
|
|
// 订单信息
|
|
// 订单信息
|
|
func OrderDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func OrderDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- orderId := c.Param("id")
|
|
|
|
- id, err := primitive.ObjectIDFromHex(orderId)
|
|
|
|
|
|
+ _id := c.Param("id")
|
|
|
|
+ if len(_id) < 1 {
|
|
|
|
+ return nil, errors.New("id不能为空")
|
|
|
|
+ }
|
|
|
|
+ orderId, err := primitive.ObjectIDFromHex(_id)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, errors.New("非法id")
|
|
return nil, errors.New("非法id")
|
|
}
|
|
}
|
|
- var order model.Order
|
|
|
|
- option := &repo.DocSearchOptions{
|
|
|
|
- CollectName: repo.CollectionOrder,
|
|
|
|
- Query: repo.Map{"_id": id},
|
|
|
|
- }
|
|
|
|
|
|
|
|
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &order)
|
|
|
|
- if !found || err != nil {
|
|
|
|
|
|
+ ok, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
|
+ CollectName: repo.CollectionOrder,
|
|
|
|
+ Query: repo.Map{"_id": orderId},
|
|
|
|
+ })
|
|
|
|
+ if !ok {
|
|
return nil, errors.New("数据未找到")
|
|
return nil, errors.New("数据未找到")
|
|
}
|
|
}
|
|
-
|
|
|
|
- return order, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 删除订单
|
|
|
|
-func OrderDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- orderId := c.Param("id")
|
|
|
|
- _, err := primitive.ObjectIDFromHex(orderId)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, errors.New("非法id")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionOrder, orderId)
|
|
|
|
|
|
+ // 客户信息
|
|
|
|
+ customer := model.UserSmaple{}
|
|
|
|
+ repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
|
+ Db: "customer-user",
|
|
|
|
+ CollectName: "users",
|
|
|
|
+ Query: repo.Map{"_id": result["userId"].(primitive.ObjectID)},
|
|
|
|
+ }, &customer)
|
|
|
|
+ result["customer"] = customer
|
|
|
|
+
|
|
|
|
+ return result, nil
|
|
}
|
|
}
|