feat: 添加用户优惠券列表接口及相关数据结构
This commit is contained in:
@@ -5,12 +5,61 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
coupon_dto "quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type coupon struct{}
|
||||
|
||||
func (s *coupon) ListUserCoupons(ctx context.Context, status string) ([]coupon_dto.UserCouponItem, error) {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
tbl, q := models.UserCouponQuery.QueryContext(ctx)
|
||||
q = q.Where(tbl.UserID.Eq(uid))
|
||||
if status != "" {
|
||||
q = q.Where(tbl.Status.Eq(status))
|
||||
}
|
||||
|
||||
list, err := q.Order(tbl.CreatedAt.Desc()).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
var res []coupon_dto.UserCouponItem
|
||||
for _, v := range list {
|
||||
c, _ := models.CouponQuery.WithContext(ctx).Where(models.CouponQuery.ID.Eq(v.CouponID)).First()
|
||||
|
||||
item := coupon_dto.UserCouponItem{
|
||||
ID: cast.ToString(v.ID),
|
||||
CouponID: cast.ToString(v.CouponID),
|
||||
Status: v.Status,
|
||||
}
|
||||
if c != nil {
|
||||
item.Title = c.Title
|
||||
item.Description = c.Description
|
||||
item.Type = c.Type
|
||||
item.Value = c.Value
|
||||
item.MinOrderAmount = c.MinOrderAmount
|
||||
if !c.StartAt.IsZero() {
|
||||
item.StartAt = c.StartAt.Format(time.RFC3339)
|
||||
}
|
||||
if !c.EndAt.IsZero() {
|
||||
item.EndAt = c.EndAt.Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
res = append(res, item)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Validate checks if a coupon can be used for an order and returns the discount amount
|
||||
func (s *coupon) Validate(ctx context.Context, userID, userCouponID, amount int64) (int64, error) {
|
||||
uc, err := models.UserCouponQuery.WithContext(ctx).Where(models.UserCouponQuery.ID.Eq(userCouponID)).First()
|
||||
|
||||
@@ -108,4 +108,4 @@ func (s *CouponTestSuite) Test_CouponFlow() {
|
||||
So(ucReload.OrderID, ShouldEqual, oid)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +157,6 @@ func (s *order) Create(ctx context.Context, form *transaction_dto.OrderCreateFor
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if _, ok := err.(*errorx.AppError); ok {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user