138 lines
3.8 KiB
Go
138 lines
3.8 KiB
Go
package listing
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"hfb_sys/backend/internal/modules/qualityowner"
|
|
)
|
|
|
|
func (s *Service) Create(ctx context.Context, ownerID uint64, req CreateRequest, siteCode ...string) (*ListingDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if !req.AgreedVirtualAssetSale || !req.AgreedSellerAgreement {
|
|
return nil, ErrAgreementRequired
|
|
}
|
|
effectiveSiteCode := ""
|
|
if len(siteCode) > 0 {
|
|
effectiveSiteCode = strings.TrimSpace(siteCode[0])
|
|
}
|
|
if err := s.applyPublishEntitlements(ctx, ownerID, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
delta, onDistribution := s.repo.resolveSiteReceiveCostDelta(ctx, 0, effectiveSiteCode)
|
|
if err := s.applyMainSiteReceivePricing(ctx, &req, 0, delta, onDistribution); err != nil {
|
|
return nil, err
|
|
}
|
|
rules, err := s.publishRules(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateRequest(req, rules); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateRequiredOnlineTime(req); err != nil {
|
|
return nil, err
|
|
}
|
|
cooldown, err := s.publishCooldown(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reviewRequired, err := s.reviewRequired(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.repo.Create(ctx, ownerID, req, reviewRequired, cooldown, effectiveSiteCode)
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, ownerID uint64, id uint64, req UpdateRequest, siteCode ...string) (*ListingDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
effectiveSiteCode := ""
|
|
if len(siteCode) > 0 {
|
|
effectiveSiteCode = strings.TrimSpace(siteCode[0])
|
|
}
|
|
if err := s.applyPublishEntitlements(ctx, ownerID, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
delta, onDistribution := s.repo.resolveSiteReceiveCostDelta(ctx, id, effectiveSiteCode)
|
|
if err := s.applyMainSiteReceivePricing(ctx, &req, 0, delta, onDistribution); err != nil {
|
|
return nil, err
|
|
}
|
|
rules, err := s.publishRules(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateRequest(req, rules); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateRequiredOnlineTime(req); err != nil {
|
|
return nil, err
|
|
}
|
|
reviewRequired, err := s.reviewRequired(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.repo.Update(ctx, ownerID, id, req, reviewRequired)
|
|
}
|
|
|
|
func (s *Service) applyPublishEntitlements(ctx context.Context, ownerID uint64, req *CreateRequest) error {
|
|
req.CompensationPlan = strings.TrimSpace(req.CompensationPlan)
|
|
if req.CompensationPlan == "" {
|
|
req.CompensationPlan = CompensationPlanNormal
|
|
}
|
|
if req.CompensationPlan != CompensationPlanNormal && req.CompensationPlan != CompensationPlanInsurance {
|
|
return ErrInvalidInput
|
|
}
|
|
|
|
isQualityOwner, err := s.repo.IsQualityOwner(ctx, ownerID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if req.IsPremium && !isQualityOwner {
|
|
return ErrPremiumOwnerRequired
|
|
}
|
|
req.OwnerIsQualityOwner = isQualityOwner
|
|
|
|
req.InsuranceFeeBps = 0
|
|
if req.CompensationPlan != CompensationPlanInsurance {
|
|
return nil
|
|
}
|
|
rules := qualityowner.DefaultRules()
|
|
if s.config != nil {
|
|
value, err := s.config.FindValue(ctx, qualityowner.ConfigKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rules, err = qualityowner.ParseConfigValue(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
req.InsuranceFeeBps = rules.InsuranceFeeBps
|
|
if req.IsPremium && isQualityOwner {
|
|
req.InsuranceFeeBps = rules.PremiumInsuranceFeeBps
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) SubmitReview(ctx context.Context, ownerID uint64, id uint64) (*ListingDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
reviewRequired, err := s.reviewRequired(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.repo.SubmitReview(ctx, ownerID, id, reviewRequired)
|
|
}
|
|
|
|
func (s *Service) Offline(ctx context.Context, ownerID uint64, id uint64) (*ListingDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.Offline(ctx, ownerID, id)
|
|
}
|