package wallet import ( "context" "strings" "hyapp/pkg/appcode" "hyapp/pkg/xerr" resourcedomain "hyapp/services/wallet-service/internal/domain/resource" ) func (s *Service) ListResources(ctx context.Context, query resourcedomain.ListResourcesQuery) ([]resourcedomain.Resource, int64, error) { if s.repository == nil { return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) if strings.TrimSpace(query.ResourceType) != "" && !resourcedomain.ValidResourceType(query.ResourceType) { return nil, 0, xerr.New(xerr.InvalidArgument, "resource_type is invalid") } return s.repository.ListResources(ctx, query) } func (s *Service) GetResource(ctx context.Context, appCode string, resourceID int64) (resourcedomain.Resource, error) { if resourceID <= 0 { return resourcedomain.Resource{}, xerr.New(xerr.InvalidArgument, "resource_id is required") } if s.repository == nil { return resourcedomain.Resource{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } ctx = appcode.WithContext(ctx, appcode.Normalize(appCode)) return s.repository.GetResource(ctx, resourceID) } func (s *Service) CreateResource(ctx context.Context, command resourcedomain.ResourceCommand) (resourcedomain.Resource, error) { if s.repository == nil { return resourcedomain.Resource{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.CreateResource(ctx, command) } func (s *Service) UpdateResource(ctx context.Context, command resourcedomain.ResourceCommand) (resourcedomain.Resource, error) { if s.repository == nil { return resourcedomain.Resource{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.UpdateResource(ctx, command) } func (s *Service) SetResourceStatus(ctx context.Context, command resourcedomain.StatusCommand) (resourcedomain.Resource, error) { if s.repository == nil { return resourcedomain.Resource{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.SetResourceStatus(ctx, command) } func (s *Service) DeleteResource(ctx context.Context, command resourcedomain.StatusCommand) (resourcedomain.Resource, error) { if s.repository == nil { return resourcedomain.Resource{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.DeleteResource(ctx, command) } func (s *Service) BatchDeleteResources(ctx context.Context, command resourcedomain.BatchDeleteResourcesCommand) ([]resourcedomain.Resource, error) { if s.repository == nil { return nil, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.BatchDeleteResources(ctx, command) } func (s *Service) ListResourceGroups(ctx context.Context, query resourcedomain.ListResourceGroupsQuery) ([]resourcedomain.ResourceGroup, int64, error) { if s.repository == nil { return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) return s.repository.ListResourceGroups(ctx, query) } func (s *Service) GetResourceGroup(ctx context.Context, appCode string, groupID int64) (resourcedomain.ResourceGroup, error) { if groupID <= 0 { return resourcedomain.ResourceGroup{}, xerr.New(xerr.InvalidArgument, "group_id is required") } if s.repository == nil { return resourcedomain.ResourceGroup{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } ctx = appcode.WithContext(ctx, appcode.Normalize(appCode)) return s.repository.GetResourceGroup(ctx, groupID) } func (s *Service) CreateResourceGroup(ctx context.Context, command resourcedomain.ResourceGroupCommand) (resourcedomain.ResourceGroup, error) { if s.repository == nil { return resourcedomain.ResourceGroup{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.CreateResourceGroup(ctx, command) } func (s *Service) UpdateResourceGroup(ctx context.Context, command resourcedomain.ResourceGroupCommand) (resourcedomain.ResourceGroup, error) { if s.repository == nil { return resourcedomain.ResourceGroup{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.UpdateResourceGroup(ctx, command) } func (s *Service) SetResourceGroupStatus(ctx context.Context, command resourcedomain.StatusCommand) (resourcedomain.ResourceGroup, error) { if s.repository == nil { return resourcedomain.ResourceGroup{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.SetResourceGroupStatus(ctx, command) } func (s *Service) ListGiftConfigs(ctx context.Context, query resourcedomain.ListGiftConfigsQuery) ([]resourcedomain.GiftConfig, int64, error) { if s.repository == nil { return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } if query.FilterRegion && query.RegionID < 0 { return nil, 0, xerr.New(xerr.InvalidArgument, "region_id is invalid") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) return s.repository.ListGiftConfigs(ctx, query) } func (s *Service) ListGiftTypeConfigs(ctx context.Context, query resourcedomain.ListGiftTypeConfigsQuery) ([]resourcedomain.GiftTypeConfig, error) { if s.repository == nil { return nil, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) return s.repository.ListGiftTypeConfigs(ctx, query) } func (s *Service) GetGiftCatalogVersion(ctx context.Context, appCode string) (int64, error) { if s.repository == nil { return 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } appCode = appcode.Normalize(appCode) ctx = appcode.WithContext(ctx, appCode) return s.repository.GetGiftCatalogVersion(ctx, appCode) } func (s *Service) CreateGiftConfig(ctx context.Context, command resourcedomain.GiftConfigCommand) (resourcedomain.GiftConfig, error) { if s.repository == nil { return resourcedomain.GiftConfig{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.CreateGiftConfig(ctx, command) } func (s *Service) BatchCreateGiftConfigs(ctx context.Context, appCode string, commands []resourcedomain.GiftConfigCommand, operatorUserID int64) ([]resourcedomain.GiftConfig, error) { if s.repository == nil { return nil, xerr.New(xerr.Unavailable, "wallet repository is not configured") } appCode = appcode.Normalize(appCode) ctx = appcode.WithContext(ctx, appCode) if len(commands) == 0 { return nil, xerr.New(xerr.InvalidArgument, "gift configs are required") } batchCommands := make([]resourcedomain.GiftConfigCommand, 0, len(commands)) for _, command := range commands { command.AppCode = appCode if command.OperatorUserID == 0 { command.OperatorUserID = operatorUserID } batchCommands = append(batchCommands, command) } return s.repository.BatchCreateGiftConfigs(ctx, batchCommands) } func (s *Service) UpdateGiftConfig(ctx context.Context, command resourcedomain.GiftConfigCommand) (resourcedomain.GiftConfig, error) { if s.repository == nil { return resourcedomain.GiftConfig{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.UpdateGiftConfig(ctx, command) } func (s *Service) SetGiftConfigStatus(ctx context.Context, command resourcedomain.StatusCommand) (resourcedomain.GiftConfig, error) { if s.repository == nil { return resourcedomain.GiftConfig{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.SetGiftConfigStatus(ctx, command) } func (s *Service) DeleteGiftConfig(ctx context.Context, command resourcedomain.StatusCommand) (resourcedomain.GiftConfig, error) { if s.repository == nil { return resourcedomain.GiftConfig{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.DeleteGiftConfig(ctx, command) } func (s *Service) UpsertGiftTypeConfig(ctx context.Context, command resourcedomain.GiftTypeConfigCommand) (resourcedomain.GiftTypeConfig, error) { if s.repository == nil { return resourcedomain.GiftTypeConfig{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.UpsertGiftTypeConfig(ctx, command) } func (s *Service) GrantResource(ctx context.Context, command resourcedomain.GrantResourceCommand) (resourcedomain.ResourceGrant, error) { if s.repository == nil { return resourcedomain.ResourceGrant{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.GrantResource(ctx, command) } func (s *Service) GrantResourceGroup(ctx context.Context, command resourcedomain.GrantResourceGroupCommand) (resourcedomain.ResourceGrant, error) { if s.repository == nil { return resourcedomain.ResourceGrant{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.GrantResourceGroup(ctx, command) } func (s *Service) RevokeResourceGrant(ctx context.Context, command resourcedomain.RevokeResourceGrantCommand) (resourcedomain.ResourceGrant, error) { if s.repository == nil { return resourcedomain.ResourceGrant{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.RevokeResourceGrant(ctx, command) } func (s *Service) ListUserResources(ctx context.Context, query resourcedomain.ListUserResourcesQuery) ([]resourcedomain.UserResourceEntitlement, error) { if s.repository == nil { return nil, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) return s.repository.ListUserResources(ctx, query) } func (s *Service) EquipUserResource(ctx context.Context, command resourcedomain.EquipUserResourceCommand) (resourcedomain.UserResourceEntitlement, error) { if s.repository == nil { return resourcedomain.UserResourceEntitlement{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.EquipUserResource(ctx, command) } func (s *Service) UnequipUserResource(ctx context.Context, command resourcedomain.UnequipUserResourceCommand) (resourcedomain.UnequipUserResourceResult, error) { if s.repository == nil { return resourcedomain.UnequipUserResourceResult{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.UnequipUserResource(ctx, command) } func (s *Service) BatchGetUserEquippedResources(ctx context.Context, query resourcedomain.BatchGetUserEquippedResourcesQuery) ([]resourcedomain.UserEquippedResources, error) { if s.repository == nil { return nil, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) return s.repository.BatchGetUserEquippedResources(ctx, query) } func (s *Service) ListResourceGrants(ctx context.Context, query resourcedomain.ListResourceGrantsQuery) ([]resourcedomain.ResourceGrant, int64, error) { if s.repository == nil { return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) return s.repository.ListResourceGrants(ctx, query) } func (s *Service) ListResourceShopItems(ctx context.Context, query resourcedomain.ListResourceShopItemsQuery) ([]resourcedomain.ResourceShopItem, int64, error) { if s.repository == nil { return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) if strings.TrimSpace(query.ResourceType) != "" { resourceType := resourcedomain.NormalizeResourceType(query.ResourceType) if !resourcedomain.ResourceTypeSellableInShop(resourceType) { return nil, 0, xerr.New(xerr.InvalidArgument, "resource_type is invalid") } query.ResourceType = resourceType } return s.repository.ListResourceShopItems(ctx, query) } func (s *Service) UpsertResourceShopItems(ctx context.Context, command resourcedomain.ResourceShopItemsCommand) ([]resourcedomain.ResourceShopItem, error) { if s.repository == nil { return nil, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.UpsertResourceShopItems(ctx, command) } func (s *Service) SetResourceShopItemStatus(ctx context.Context, command resourcedomain.ResourceShopItemStatusCommand) (resourcedomain.ResourceShopItem, error) { if s.repository == nil { return resourcedomain.ResourceShopItem{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.SetResourceShopItemStatus(ctx, command) } func (s *Service) ListResourceShopPurchaseOrders(ctx context.Context, query resourcedomain.ListResourceShopPurchaseOrdersQuery) ([]resourcedomain.ResourceShopPurchaseOrder, int64, error) { if s.repository == nil { return nil, 0, xerr.New(xerr.Unavailable, "wallet repository is not configured") } query.AppCode = appcode.Normalize(query.AppCode) ctx = appcode.WithContext(ctx, query.AppCode) if strings.TrimSpace(query.ResourceType) != "" { resourceType := resourcedomain.NormalizeResourceType(query.ResourceType) if !resourcedomain.ResourceTypeSellableInShop(resourceType) { return nil, 0, xerr.New(xerr.InvalidArgument, "resource_type is invalid") } query.ResourceType = resourceType } return s.repository.ListResourceShopPurchaseOrders(ctx, query) } func (s *Service) PurchaseResourceShopItem(ctx context.Context, command resourcedomain.ResourceShopPurchaseCommand) (resourcedomain.ResourceShopPurchaseReceipt, error) { if strings.TrimSpace(command.CommandID) == "" || command.UserID <= 0 || command.ShopItemID <= 0 { return resourcedomain.ResourceShopPurchaseReceipt{}, xerr.New(xerr.InvalidArgument, "resource shop purchase command is incomplete") } if len(command.CommandID) > 128 { return resourcedomain.ResourceShopPurchaseReceipt{}, xerr.New(xerr.InvalidArgument, "command_id is too long") } if s.repository == nil { return resourcedomain.ResourceShopPurchaseReceipt{}, xerr.New(xerr.Unavailable, "wallet repository is not configured") } command.AppCode = appcode.Normalize(command.AppCode) command.CommandID = strings.TrimSpace(command.CommandID) ctx = appcode.WithContext(ctx, command.AppCode) return s.repository.PurchaseResourceShopItem(ctx, command) }