package repo import ( "assetcenter/conf" "assetcenter/db/model" "errors" "time" "go.mongodb.org/mongo-driver/bson/primitive" ) type MaterialRepo struct { Conf *conf.AppAsset Model *model.AssetMaterial } func (repo *MaterialRepo) GetEntity() interface{} { return repo.Model } func (repo *MaterialRepo) Upload(ctx *RepoSession, userId string) (interface{}, error) { asset := repo.Model asset.CreateTime = time.Now() asset.UserId, _ = primitive.ObjectIDFromHex(userId) asset.UpdateTime = time.Now() asset.State = model.AssetState_Created collectionName := "asset-" + repo.Conf.Type assetId, err := RepoAddDoc(ctx, collectionName, asset) if err != nil { return nil, err } return assetId, err } func (repo *MaterialRepo) Update(ctx *RepoSession) (interface{}, error) { asset := repo.Model if len(asset.Id) < 1 { return nil, errors.New("id不能为空!") } id := asset.Id.Hex() asset.Id = primitive.NilObjectID asset.UpdateTime = time.Now() collectionName := "asset-" + repo.Conf.Type return RepoUpdateSetDoc(ctx, collectionName, id, asset) } func (repo *MaterialRepo) Detail(ctx *RepoSession, id string) (interface{}, error) { collectionName := "asset-" + repo.Conf.Type ok, ret := RepoSeachDocMap(ctx, &DocSearchOptions{ CollectName: collectionName, Query: Map{"_id": id}, }) if !ok { return nil, errors.New("数据不存在!") } return ret, nil }