package api import ( "3dshow/db/model" "3dshow/db/repo" "errors" "fmt" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) // 产品管理 func Order(r *GinRouter) { r.POSTJWT("/order/createBatch", OrderAddBatch) r.GETJWT("/order/list", OrderList) r.POSTJWT("/order/update", OrderUpdate) 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) { // customer // supplier page, size, query := UtilQueryPageSize(c) _userId := apictx.User.ID userId, _ := primitive.ObjectIDFromHex(_userId) // if apictx.User.Role == "customer"{ query["userId"] = userId // } result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: repo.CollectionOrder, Page: page, Size: size, Query: query, // Project: []string{}, Sort: repo.Map{"createTime": -1}, }) if len(result.List) > 0 { for _, v := range result.List { supplyId := v["supplyId"].(primitive.ObjectID) supply := model.Supply{} repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: repo.CollectionSupply, Query: repo.Map{"_id": supplyId}, }, &supply) v["supplyName"] = supply.Name } } 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 } // 更新订单 发货 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 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 result, err } // 订单信息 func OrderDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) { orderId := c.Param("id") id, err := primitive.ObjectIDFromHex(orderId) if err != nil { 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 { 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) }