package api

import (
	"3dshow-supplier/db/model"
	"3dshow-supplier/db/repo"
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

func regAssetApi(r *GinRouter) {

	// 添加管理端接口
	CreateCRUD(r, "/assets", &CRUDOption{
		Collection: repo.CollectionAssets,
		NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			entity := &model.Asset360Fake3d{}
			c.ShouldBindJSON(entity)

			entity.SupplyId, _ = primitive.ObjectIDFromHex(apictx.User.Parent)
			entity.CreateTime = time.Now()

			return entity, nil
		},

		EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
			return &model.Asset360Fake3d{}
		},
		SearchSort: bson.M{"createTime": -1},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			if query["supplyId"] != nil {
				query["supplyId"], _ = primitive.ObjectIDFromHex(query["supplyId"].(string))
			}
			return query
		},
		JWT:           true,
		SearchProject: []string{"name", "createTime", "type", "unit", "price", "cover", "status"},
	})

	r.POSTJWT("/assets/conv/:id/:groupId", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
		return nil, fmt.Errorf("尚未实现")
	})
}
