123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package api
- import (
- "assetcenter/db/model"
- "assetcenter/db/repo"
- "errors"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- // 注册设计相关接口
- func CreateDatabaseDesignProductRouter(router *GinRouter) {
- //获取项目单品详情
- router.GETJWT("/design/product/detail", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- id := c.Query("id")
- ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDesignProducts, Query: repo.Map{"_id": id}})
- if !ok {
- return nil, NewError("获取单品失败!")
- }
- return ret, nil
- })
- router.POSTJWT("/design/product/add/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- projId := c.Param("id")
- body := &model.ProductHeader{}
- c.ShouldBindJSON(body)
- if len(projId) < 1 || len(body.FromDbConfId) < 1 || len(body.FromAssetConfId) < 1 || len(body.FromId) < 1 {
- return nil, NewError("参数不合法!")
- }
- db, assetConf := repo.GetDatabaseCollection(apictx.CreateRepoCtx(), body.FromDbConfId, body.FromAssetConfId)
- if len(db.Name) < 1 || assetConf == nil {
- return nil, NewError("没有对应的资产定义!")
- }
- body.CreateTime = time.Now()
- if assetConf.Type == model.AssetTypeMesh {
- asset := &model.AssetStaticMesh{}
- ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{Db: db.Name, CollectName: assetConf.Collection, Query: repo.Map{"_id": body.FromId}}, asset)
- if err != nil {
- return nil, err
- }
- if !ok {
- return nil, errors.New("对应的模型不存在!")
- }
- product := &model.DesignProduct{
- FromDbConfId: body.FromDbConfId,
- FromAssetConfId: body.FromAssetConfId,
- FromId: body.FromId,
- Thumbnail: asset.Thumbnail,
- Name: asset.Name,
- StaticMesh: asset.Source,
- }
- product.ProjectId, _ = primitive.ObjectIDFromHex(projId)
- id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDesignProducts, product)
- if err != nil {
- return nil, err
- }
- body.Id = id
- body.Name = asset.Name
- body.Thumbnail = asset.Thumbnail
- } else {
- return nil, NewError("该资产类型不支持创建单品!")
- }
- ret, err := repo.RepoDocArrayAppend(apictx.CreateRepoCtx(), repo.CollectionDesigns, projId, "products", body)
- if err != nil {
- return nil, err
- }
- if ret.ModifiedCount < 1 {
- return nil, errors.New("添加失败!")
- }
- return body, nil
- })
- router.POSTJWT("/design/product/update", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- // if len(c.Param("id")) < 1 {
- // return nil, NewError("项目Id不能为空!")
- // }
- body := &model.DesignProduct{}
- c.ShouldBindJSON(body)
- if body.Id == primitive.NilObjectID {
- return nil, NewError("单品Id不能为空!")
- }
- id := body.Id.Hex()
- body.Id = primitive.NilObjectID
- body.ProjectId = primitive.NilObjectID
- body.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionDesignProducts, id, body)
- })
- router.POSTJWT("/design/product/delete/:did/:pid", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- desiginId := c.Param("did")
- prouductId := c.Param("pid")
- if len(desiginId) < 1 || len(prouductId) < 1 {
- return nil, NewError("参数不合法!")
- }
- _, err := repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionDesignProducts, prouductId)
- if err != nil {
- return nil, err
- }
- //删除数组内容
- return repo.RepoDocArrayOneRemove(apictx.CreateRepoCtx(), &repo.ArrayOneRemoveOption{CollectName: repo.CollectionDesigns, Id: desiginId, ArrayQuery: repo.Map{"products": bson.M{"id": prouductId}}})
- })
- }
|