123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- 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.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 {
- 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,
- })
- }
- }
- 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 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)
- }
|