123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package api
- import (
- "3dshow-supplier/db/model"
- "3dshow-supplier/db/repo"
- "3dshow-supplier/log"
- "errors"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- // 产品管理
- func Product(r *GinRouter) {
- // ??? supplier apis
- r.GETJWT("/product/list", ProductList)
- r.POSTJWT("/product/onOrOffShelves", OnOrOffShelves)
- r.GETJWT("/product/detail/:id", ProductDetail)
- r.POSTJWT("/product/delete/:id", ProductDelete)
- }
- // 上下架商品
- func OnOrOffShelves(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- // 根据id查询商品
- _id := c.Param("id")
- if len(_id) < 1 {
- return nil, errors.New("商品id不能为空")
- }
- productId, err := primitive.ObjectIDFromHex(_id)
- if err != nil {
- return nil, errors.New("非法id")
- }
- product := model.Product{}
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionProduct,
- Query: repo.Map{"_id": productId},
- }, &product)
- if !found || err != nil {
- return nil, errors.New("该商品不存在")
- }
- _userId := apictx.User.ID
- userId, _ := primitive.ObjectIDFromHex(_userId)
- // 不是自己的商品
- if product.SupplyId != userId {
- return nil, errors.New("非法操作")
- }
- // 更新状态
- if product.Status == -1 {
- product.Status = 1
- product.OnsaleTime = time.Now()
- product.UpdateTime = time.Now()
- } else if product.Status == 1 {
- product.Status = -1
- product.UpdateTime = time.Now()
- }
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, _id, &product)
- }
- // 产品列表
- func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- // asc:升序1 desc:降序-1
- // 默认排序
- onsaleTimeSort := repo.Map{"onsaleTime": -1}
- if _, ok := query["sort"]; ok {
- if query["sort"].(string) == "asc" {
- onsaleTimeSort = repo.Map{"onsaleTime": 1}
- }
- delete(query, "sort")
- }
- // 供应商 查询自己的商品
- _userId := apictx.User.ID
- userId, _ := primitive.ObjectIDFromHex(_userId)
- query["supplyId"] = userId
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionProduct,
- Page: page,
- Size: size,
- Query: query,
- // Project: []string{},
- Sort: onsaleTimeSort,
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- // 删除产品
- func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- _id := c.Param("id")
- if len(_id) < 1 {
- return nil, errors.New("id不能为空")
- }
- productId, err := primitive.ObjectIDFromHex(_id)
- if err != nil {
- return nil, errors.New("非法id")
- }
- product := model.Product{}
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionProduct,
- Query: repo.Map{"_id": productId},
- }, &product)
- if !found || err != nil {
- return nil, errors.New("该商品不存在")
- }
- _userId := apictx.User.ID
- userId, _ := primitive.ObjectIDFromHex(_userId)
- // 不是自己的商品
- if product.SupplyId != userId {
- return nil, errors.New("非法操作")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, _id)
- }
- // 产品信息
- func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- _id := c.Param("id")
- if len(_id) < 1 {
- return nil, errors.New("id不能为空")
- }
- productId, err := primitive.ObjectIDFromHex(_id)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var product model.Product
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionProduct,
- Query: repo.Map{"_id": productId},
- }
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &product)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- return product, nil
- }
|