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.
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
1 month ago
|
package price
|
||
|
|
||
|
import (
|
||
|
"cls/internal/domain/price"
|
||
|
"cls/pkg/logger"
|
||
|
"cls/pkg/util/page"
|
||
|
"errors"
|
||
|
"xorm.io/builder"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrInvalidAmount = errors.New("价格不能小于0")
|
||
|
ErrInvalidType = errors.New("无效的价格类型")
|
||
|
)
|
||
|
|
||
|
// PriceService 价格管理服务
|
||
|
type PriceService struct {
|
||
|
repo price.PriceRepository
|
||
|
log logger.Logger
|
||
|
}
|
||
|
|
||
|
// NewService 创建价格管理服务
|
||
|
func NewService(repo price.PriceRepository, log logger.New) *PriceService {
|
||
|
return &PriceService{
|
||
|
repo: repo,
|
||
|
log: log("cls:service:price"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// SetPrice 设置价格
|
||
|
func (s *PriceService) SetPrice(targetID uint64, priceType price.PriceType, amount int64, adminID uint64) error {
|
||
|
if amount < 0 {
|
||
|
return ErrInvalidAmount
|
||
|
}
|
||
|
|
||
|
// 检查是否已存在价格记录
|
||
|
existingPrice, err := s.repo.FindByTargetID(targetID, priceType)
|
||
|
if err != nil {
|
||
|
// 如果记录不存在,创建新记录
|
||
|
newPrice := &price.Price{
|
||
|
TargetID: targetID,
|
||
|
Type: priceType,
|
||
|
Amount: amount,
|
||
|
AdminID: adminID,
|
||
|
}
|
||
|
return s.repo.Save(newPrice)
|
||
|
}
|
||
|
|
||
|
// 如果记录存在,更新价格
|
||
|
existingPrice.Amount = amount
|
||
|
existingPrice.AdminID = adminID
|
||
|
return s.repo.Update(existingPrice)
|
||
|
}
|
||
|
|
||
|
// GetPrice 获取价格(如果不存在则使用默认价格)
|
||
|
func (s *PriceService) GetPrice(targetID uint64, priceType price.PriceType) (int64, error) {
|
||
|
// 检查是否已存在价格记录
|
||
|
existingPrice, err := s.repo.FindByTargetID(targetID, priceType)
|
||
|
if err != nil {
|
||
|
// 如果记录不存在,使用默认价格
|
||
|
var defaultAmount int64
|
||
|
switch priceType {
|
||
|
case price.TypeArticle:
|
||
|
defaultAmount = price.DefaultArticlePrice
|
||
|
case price.TypeColumn:
|
||
|
defaultAmount = price.DefaultColumnPrice
|
||
|
default:
|
||
|
return 0, ErrInvalidType
|
||
|
}
|
||
|
|
||
|
// 创建默认价格记录
|
||
|
newPrice := &price.Price{
|
||
|
TargetID: targetID,
|
||
|
Type: priceType,
|
||
|
Amount: defaultAmount,
|
||
|
AdminID: 0, // 系统默认价格
|
||
|
}
|
||
|
if err := s.repo.Save(newPrice); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return defaultAmount, nil
|
||
|
}
|
||
|
|
||
|
return existingPrice.Amount, nil
|
||
|
}
|
||
|
|
||
|
// GetPriceList 获取价格列表
|
||
|
func (s *PriceService) GetPriceList(page *page.Page, conds []builder.Cond) error {
|
||
|
return s.repo.FindAll(page, conds)
|
||
|
}
|
||
|
|
||
|
// DeletePrice 删除价格记录
|
||
|
func (s *PriceService) DeletePrice(id uint64) error {
|
||
|
return s.repo.Delete(id)
|
||
|
}
|