123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package api
- import (
- "3dshow/db/model"
- "3dshow/db/repo"
- "3dshow/log"
- "errors"
- "strconv"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- // 产品管理
- func Product(r *GinRouter) {
- r.POSTJWT("/product/create", ProductAdd)
- r.GETJWT("/product/list", ProductList)
- r.POSTJWT("/product/update", ProductUpdate)
- r.GET("/product/detail/:id", ProductDetail)
- r.POSTJWT("/product/delete/:id", ProductDelete)
- }
- // 新增产品
- func ProductAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var form model.Product
- err := c.ShouldBindJSON(&form)
- if err != nil {
- return nil, errors.New("参数错误!")
- }
- ctx := apictx.CreateRepoCtx()
- if form.SupplyId.Hex() == "" {
- return nil, errors.New("供应链id不能为空")
- }
- if form.Name == "" {
- return nil, errors.New("产品名不能为空")
- }
- form.CreateTime = time.Now()
- result, err := repo.RepoAddDoc(ctx, repo.CollectionProduct, &form)
- return result, err
- }
- // 产品列表
- func ProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- _sort := c.Query("sort")
- // 1:升序 -1:降序
- sort, _ := strconv.Atoi(_sort)
- // 默认排序
- onsaleTimeSort := repo.Map{"onsaleTime": -1}
- if sort != 0 {
- onsaleTimeSort = repo.Map{"onsaleTime": sort}
- }
- // 查询数据
- if _, ok := query["supplyId"]; !ok {
- return nil, errors.New("供应链id不能为空")
- }
- supplyId, err := primitive.ObjectIDFromHex(query["supplyId"].(string))
- if err != nil {
- return nil, errors.New("供应链id错误")
- }
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionSupply,
- Page: page,
- Size: size,
- Query: query,
- Project: []string{},
- Sort: onsaleTimeSort,
- }
- pageResult, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- if err != nil {
- return nil, err
- }
- // 查询收藏状态
- var collects []*model.Collect
- _userId := apictx.User.ID
- userId, err := primitive.ObjectIDFromHex(_userId)
- if err != nil {
- return nil, errors.New("非法用户")
- }
- option1 := &repo.PageSearchOptions{
- CollectName: repo.CollectionCollect,
- Query: repo.Map{"userId": userId, "supplyId": supplyId},
- Project: []string{"productId"},
- }
- err1 := repo.RepoDocsSearch(apictx.CreateRepoCtx(), option1, &collects)
- if len(pageResult.List) > 0 {
- for _, v := range pageResult.List {
- v["isCollect"] = false
- if len(collects) > 0 && err1 != nil {
- for _, col := range collects {
- if v["_id"].(primitive.ObjectID) == col.ProductId { // productId唯一
- v["isCollect"] = true
- break
- }
- }
- }
- }
- }
- return pageResult, err
- }
- // 更新产品
- func ProductUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var form model.Product
- err := c.ShouldBindJSON(&form)
- if err != nil {
- return nil, errors.New("参数错误")
- }
- if form.Id.Hex() == "" {
- return nil, errors.New("更新的产品id不能为空")
- }
- form.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, form.Id.Hex(), &form)
- }
- // 产品信息
- func ProductDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- productId := c.Param("id")
- id, err := primitive.ObjectIDFromHex(productId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var product model.Product
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionProduct,
- Query: repo.Map{"_id": id},
- }
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &product)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- // ??? 是否收藏 前端调用接口判断
- return product, nil
- }
- // 删除产品
- func ProductDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- productId := c.Param("id")
- _, err := primitive.ObjectIDFromHex(productId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProduct, productId)
- }
|