package api import ( "3dshow/db/model" "3dshow/db/repo" "errors" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) // 产品管理 func Order(r *GinRouter) { r.POSTJWT("/order/create", OrderAdd) 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 OrderAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) { var form model.Order err := c.ShouldBindJSON(&form) if err != nil { return nil, errors.New("参数错误!") } ctx := apictx.CreateRepoCtx() _userId := apictx.User.ID userId, err := primitive.ObjectIDFromHex(_userId) if err != nil { return nil, errors.New("非法用户") } for _, v := range form.Products { v.Status = -1 } form.UserId = userId form.Status = -1 form.CreateTime = time.Now() form.UpdateTime = time.Now() return repo.RepoAddDoc(ctx, repo.CollectionOrder, &form) } // 订单列表 func OrderList(c *gin.Context, apictx *ApiSession) (interface{}, error) { // customer // supplier page, size, query := UtilQueryPageSize(c) _userId := apictx.User.ID userId, err := primitive.ObjectIDFromHex(_userId) if err != nil { return nil, errors.New("非法用户") } // if apictx.User.Role == "customer"{ query["userId"] = userId // } return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: repo.CollectionOrder, Page: page, Size: size, Query: query, // Project: []string{}, // Sort: repo.Map{"createTime": -1}, }) } // 个人页面数量展示 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) }