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.
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
![]()
4 weeks ago
|
package coupon
|
||
|
|
||
|
import (
|
||
|
"cls/internal/domain/coupon"
|
||
|
"cls/internal/domain/user"
|
||
|
"cls/pkg/logger"
|
||
|
)
|
||
|
|
||
|
// CouponService 优惠券应用服务
|
||
|
type CouponService struct {
|
||
|
repo coupon.CouponRepository
|
||
|
userRepo user.UserRepository
|
||
|
log logger.Logger
|
||
|
}
|
||
|
|
||
|
// NewCouponService 创建优惠券应用服务
|
||
|
func NewCouponService(repo coupon.CouponRepository, userRepo user.UserRepository, log logger.New) *CouponService {
|
||
|
return &CouponService{
|
||
|
repo: repo,
|
||
|
userRepo: userRepo,
|
||
|
log: log("cls:service:coupon"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// CreateCoupon 创建优惠券
|
||
|
func (s *CouponService) CreateCoupon(req *CouponDto) error {
|
||
|
_, err := s.userRepo.FindByID(req.UserId)
|
||
|
if err != nil {
|
||
|
s.log.Error(err)
|
||
|
return err
|
||
|
}
|
||
|
coupon := &coupon.Coupon{
|
||
|
Name: req.Name,
|
||
|
Type: req.Type,
|
||
|
Value: req.Value,
|
||
|
MinAmount: req.MinAmount,
|
||
|
StartTime: req.StartTime,
|
||
|
EndTime: req.EndTime,
|
||
|
AdminID: req.AdminId,
|
||
|
UserID: req.UserId,
|
||
|
Status: coupon.CouponStatusNormal,
|
||
|
}
|
||
|
if err := s.repo.Create(coupon); err != nil {
|
||
|
s.log.Error(err)
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// IssueCouponRequest 发放优惠券请求
|
||
|
type IssueCouponRequest struct {
|
||
|
CouponID uint64 `json:"couponId"` // 优惠券ID
|
||
|
UserID uint64 `json:"userId"` // 用户ID
|
||
|
}
|
||
|
|
||
|
// GetUserCoupons 获取用户的优惠券列表
|
||
|
func (s *CouponService) GetUserCoupons(ePhone string) ([]*CouponDto, error) {
|
||
|
user, err := s.userRepo.FindByPhone(ePhone)
|
||
|
if err != nil {
|
||
|
s.log.Error(err)
|
||
|
return nil, err
|
||
|
}
|
||
|
coupons, err := s.repo.ListByUserID(user.Id)
|
||
|
if err != nil {
|
||
|
s.log.Error(err)
|
||
|
return nil, err
|
||
|
}
|
||
|
var resp []*CouponDto
|
||
|
for _, c := range coupons {
|
||
|
if c.Status == coupon.CouponStatusNormal {
|
||
|
resp = append(resp, &CouponDto{
|
||
|
Id: c.ID,
|
||
|
Name: c.Name,
|
||
|
Type: c.Type,
|
||
|
Value: c.Value,
|
||
|
MinAmount: c.MinAmount,
|
||
|
StartTime: c.StartTime,
|
||
|
EndTime: c.EndTime,
|
||
|
Status: c.Status,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|