package mongo import ( "context" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/repository" mgo "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo" "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo/bson" ) var _ repository.IBundleRepo = new(BundleByMongoRepo) type BundleByMongoRepo struct { } type BundleMongo struct { BundleID string `json:"bundle_id" bson:"bundle_id"` Remark string `json:"remark" bson:"remark"` SDKKey string `json:"sdk_key" bson:"sdk_key"` SDKID string `json:"sdk_id" bson:"sdk_id"` IsFirstCreate bool `json:"is_first_create" bson:"is_first_create"` //是否第一次创建 CreatedAt int64 `json:"created_at" bson:"created_at"` CreatedAccount string `json:"created_account" bson:"created_account"` CreatedBy string `json:"created_by" bson:"created_by"` IsForbidden int `json:"is_forbidden" bson:"is_forbidden"` //0:可用 1:禁用 } func (b BundleByMongoRepo) Insert(ctx context.Context, bundles []entity.Bundle) error { var err error for _, v := range bundles { err = bundleTable.Insert(ctx, convertBundleToBundleMongo(v)) } if err != nil { return err } return err } func (b BundleByMongoRepo) GetListByBundleId(ctx context.Context, ids []string, pageNo int, pageSize int) (int, []entity.Bundle, error) { bundleMongos := make([]BundleMongo, 0) filter := bson.M{"bundle_id": bson.M{"$in": ids}} total, err := bundleTable.GetSome(ctx, filter, []string{}, pageSize, pageNo, &bundleMongos) if b.NotFound(err) { return 0, []entity.Bundle{}, nil } bundles := make([]entity.Bundle, 0) for _, bundleMongo := range bundleMongos { bundles = append(bundles, convertBundleMongoToBundle(bundleMongo)) } return total, bundles, err } func (b BundleByMongoRepo) GetListByBundleIds(ctx context.Context, ids []string) ([]entity.Bundle, error) { bundles := make([]BundleMongo, 0) filter := bson.M{"bundle_id": bson.M{"$in": ids}} _, err := bundleTable.GetAllV2(ctx, filter, []string{}, &bundles) if b.NotFound(err) { return []entity.Bundle{}, nil } result := make([]entity.Bundle, 0) for _, bundleMongo := range bundles { result = append(result, convertBundleMongoToBundle(bundleMongo)) } return result, err } func (b BundleByMongoRepo) ExistBundleId(ctx context.Context, bundleId string) (bool, error) { total, err := bundleTable.Count(ctx, bson.M{"bundle_id": bundleId}) if err != nil { return false, err } return total > 0, nil } func (b BundleByMongoRepo) GetInfoByBundleId(ctx context.Context, bundleId string) (entity.Bundle, error) { mgoInfo := BundleMongo{} err := bundleTable.GetOne(ctx, bson.M{"bundle_id": bundleId}, &mgoInfo) if err != nil { return entity.Bundle{}, err } return convertBundleMongoToBundle(mgoInfo), err } func (b BundleByMongoRepo) ListAllBundleInfos(ctx context.Context, searchText string, selectType, pageNo int, pageSize int) (int, []entity.Bundle, error) { bundleInfos := make([]BundleMongo, 0, pageSize) filter := bson.M{} if searchText != "" { filter["bundle_id"] = bson.RegEx{Pattern: searchText, Options: "i"} } if selectType == 1 { filter["is_forbidden"] = 0 } else if selectType == 2 { filter["is_forbidden"] = 1 } pageNo += 1 total, err := bundleTable.GetSome(context.Background(), filter, []string{"-created_at"}, pageSize, pageNo, &bundleInfos) if err != nil { return 0, nil, err } result := make([]entity.Bundle, 0) for _, v := range bundleInfos { result = append(result, convertBundleMongoToBundle(v)) } return total, result, nil } func (b BundleByMongoRepo) AllCount(ctx context.Context) (int, error) { return bundleTable.Count(ctx, bson.M{"is_forbidden": 0}) } func (b BundleByMongoRepo) UpdateBundleForbidden(ctx context.Context, bundleId string, IsForbidden int) error { return bundleTable.UpdateOne(ctx, bson.M{"bundle_id": bundleId}, bson.M{"$set": bson.M{"is_forbidden": IsForbidden}}) } func (b BundleByMongoRepo) UpdateBundlePlatform(ctx context.Context, bundleId, remark string) error { return bundleTable.UpdateOne(ctx, bson.M{"bundle_id": bundleId}, bson.M{"$set": bson.M{"remark": remark}}) } func (b BundleByMongoRepo) NotFound(err error) bool { return err == mgo.ErrNotFound } func convertBundleToBundleMongo(bundleInfo entity.Bundle) BundleMongo { result := BundleMongo{} result.BundleID = bundleInfo.BundleID result.Remark = bundleInfo.Remark result.SDKKey = bundleInfo.SDKKey result.SDKID = bundleInfo.SDKID result.IsFirstCreate = bundleInfo.IsFirstCreate result.CreatedAt = bundleInfo.CreatedAt result.CreatedAccount = bundleInfo.CreatedAccount result.CreatedBy = bundleInfo.CreatedBy result.IsForbidden = bundleInfo.IsForbidden return result } func convertBundleMongoToBundle(bundleMongo BundleMongo) entity.Bundle { result := entity.Bundle{} result.BundleID = bundleMongo.BundleID result.Remark = bundleMongo.Remark result.SDKKey = bundleMongo.SDKKey result.SDKID = bundleMongo.SDKID result.IsFirstCreate = bundleMongo.IsFirstCreate result.CreatedAt = bundleMongo.CreatedAt result.CreatedAccount = bundleMongo.CreatedAccount result.CreatedBy = bundleMongo.CreatedBy result.IsForbidden = bundleMongo.IsForbidden return result }