You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.8 KiB
Go

1 month ago
package price
import (
"cls/internal/domain/price"
"cls/pkg/logger"
"cls/pkg/util/page"
"cls/pkg/web"
1 month ago
"errors"
)
var (
ErrInvalidAmount = errors.New("价格不能小于0")
ErrInvalidType = errors.New("无效的价格类型")
ErrInvalidDuration = errors.New("无效的订阅时长")
1 month ago
)
// PriceService 价格管理服务
type PriceService struct {
repo price.PriceRepository
log logger.Logger
}
// NewService 创建价格管理服务
func NewPriceService(repo price.PriceRepository, log logger.New) *PriceService {
1 month ago
return &PriceService{
repo: repo,
log: log("cls:service:price"),
}
}
// SetPrice 设置价格
func (s *PriceService) SetPrice(dto *PriceDto) error {
if dto.Amount < 0 {
1 month ago
return ErrInvalidAmount
}
// 检查是否已存在价格记录
existingPrice, err := s.repo.FindByTargetID(dto.TargetID, dto.Type)
1 month ago
if err != nil {
// 如果记录不存在,创建新记录
newPrice := dto.ToPrice()
1 month ago
return s.repo.Save(newPrice)
}
// 如果记录存在,更新价格
existingPrice.Amount = dto.Amount
existingPrice.OneMonthPrice = dto.OneMonthPrice
existingPrice.ThreeMonthsPrice = dto.ThreeMonthsPrice
existingPrice.SixMonthsPrice = dto.SixMonthsPrice
existingPrice.OneYearPrice = dto.OneYearPrice
existingPrice.AdminID = dto.AdminID
1 month ago
return s.repo.Update(existingPrice)
}
func (s *PriceService) GetArticlePrice(dto *PriceDto) (*PriceDto, error) {
dto.Type = price.TypeArticle
return s.GetPrice(dto)
}
func (s *PriceService) GetColumnPrice(dto *PriceDto) (*PriceDto, error) {
dto.Type = price.TypeColumn
return s.GetPrice(dto)
}
1 month ago
// GetPrice 获取价格(如果不存在则使用默认价格)
func (s *PriceService) GetPrice(dto *PriceDto) (*PriceDto, error) {
1 month ago
// 检查是否已存在价格记录
existingPrice, err := s.repo.FindByTargetID(dto.TargetID, dto.Type)
1 month ago
if err != nil {
// 如果记录不存在,使用默认价格
var defaultAmount int64
switch dto.Type {
1 month ago
case price.TypeArticle:
defaultAmount = price.DefaultArticlePrice
case price.TypeColumn:
defaultAmount = price.DefaultColumnPrice
default:
return nil, ErrInvalidType
1 month ago
}
// 创建默认价格记录
newPrice := price.NewPrice(dto.TargetID, dto.Type, defaultAmount, 0)
1 month ago
if err := s.repo.Save(newPrice); err != nil {
return nil, err
}
if dto.Type == price.TypeArticle {
return &PriceDto{
Amount: newPrice.Amount,
Discount: newPrice.Discount,
}, nil
1 month ago
}
return &PriceDto{
OneMonthPrice: newPrice.OneMonthPrice,
ThreeMonthsPrice: newPrice.ThreeMonthsPrice,
SixMonthsPrice: newPrice.SixMonthsPrice,
OneYearPrice: newPrice.OneYearPrice,
Discount: 0.3,
}, nil
}
priceDto := &PriceDto{
Discount: existingPrice.Discount,
}
if dto.Type == price.TypeArticle {
priceDto.Amount = existingPrice.Amount
} else {
priceDto.OneMonthPrice = existingPrice.OneMonthPrice
priceDto.ThreeMonthsPrice = existingPrice.ThreeMonthsPrice
priceDto.SixMonthsPrice = existingPrice.SixMonthsPrice
priceDto.OneYearPrice = existingPrice.OneYearPrice
1 month ago
}
return priceDto, nil
1 month ago
}
// GetPriceList 获取价格列表
func (s *PriceService) GetPriceList(page *page.Page, params map[string]string) error {
conds := web.ParseFilters(params)
1 month ago
return s.repo.FindAll(page, conds)
}
// DeletePrice 删除价格记录
func (s *PriceService) DeletePrice(id uint64) error {
return s.repo.Delete(id)
}
func (s *PriceService) UpdatePrice(dto *PriceDto) error {
if err := dto.Validate(); err != nil {
return err
}
existingPrice, err := s.repo.FindByTargetID(dto.TargetID, dto.Type)
if err != nil {
return err
}
existingPrice.Amount = dto.Amount
existingPrice.OneMonthPrice = dto.OneMonthPrice
existingPrice.ThreeMonthsPrice = dto.ThreeMonthsPrice
existingPrice.SixMonthsPrice = dto.SixMonthsPrice
existingPrice.OneYearPrice = dto.OneYearPrice
existingPrice.AdminID = dto.AdminID
return s.repo.Update(existingPrice)
}