完善套餐与账单接口 & 完善支付数据保存,记录实付价格并关联优惠券
This commit is contained in:
@@ -70,7 +70,7 @@ func PageResourceShort(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
resource, err := q.Resource.Where(do).
|
||||
Joins(q.Resource.Short, q.ResourceShort.Sku).
|
||||
Joins(q.Resource.Short).
|
||||
Order(q.Resource.CreatedAt.Desc()).
|
||||
Offset(req.GetOffset()).
|
||||
Limit(req.GetLimit()).
|
||||
@@ -240,9 +240,28 @@ func PageResourceShortByAdmin(c *fiber.Ctx) error {
|
||||
time := u.DateTail(*req.CreatedAtEnd)
|
||||
do = do.Where(q.Resource.CreatedAt.Lte(time))
|
||||
}
|
||||
if req.Expired != nil {
|
||||
if *req.Expired {
|
||||
do = do.Where(q.Resource.Where(
|
||||
q.ResourceShort.As("Short").Type.Eq(int(m.ResourceModeTime)),
|
||||
q.ResourceShort.As("Short").ExpireAt.Lte(time.Now()),
|
||||
).Or(
|
||||
q.ResourceShort.As("Short").Type.Eq(int(m.ResourceModeQuota)),
|
||||
q.ResourceShort.As("Short").Quota.LteCol(q.ResourceShort.As("Short").Used),
|
||||
))
|
||||
} else {
|
||||
do = do.Where(q.Resource.Where(
|
||||
q.ResourceShort.As("Short").Type.Eq(int(m.ResourceModeTime)),
|
||||
q.ResourceShort.As("Short").ExpireAt.Gt(time.Now()),
|
||||
).Or(
|
||||
q.ResourceShort.As("Short").Type.Eq(int(m.ResourceModeQuota)),
|
||||
q.ResourceShort.As("Short").Quota.GtCol(q.ResourceShort.As("Short").Used),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
list, total, err := q.Resource.Debug().
|
||||
Joins(q.Resource.User, q.Resource.Short).
|
||||
Joins(q.Resource.User, q.Resource.Short, q.Resource.Short.Sku).
|
||||
Select(
|
||||
q.Resource.ALL,
|
||||
q.User.As("User").Phone.As("User__phone"),
|
||||
@@ -254,9 +273,14 @@ func PageResourceShortByAdmin(c *fiber.Ctx) error {
|
||||
q.ResourceShort.As("Short").Daily.As("Short__daily"),
|
||||
q.ResourceShort.As("Short").LastAt.As("Short__last_at"),
|
||||
q.ResourceShort.As("Short").ExpireAt.As("Short__expire_at"),
|
||||
q.ProductSku.As("Short__Sku").Name.As("Short__Sku__name"),
|
||||
).
|
||||
Where(q.Resource.Type.Eq(int(m.ResourceTypeShort)), do).
|
||||
Order(q.Resource.CreatedAt.Desc()).
|
||||
FindByPage(req.GetOffset(), req.GetLimit())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(core.PageResp{
|
||||
List: list,
|
||||
@@ -274,9 +298,10 @@ type PageResourceShortByAdminReq struct {
|
||||
Mode *int `json:"mode" form:"mode"`
|
||||
CreatedAtStart *time.Time `json:"created_at_start" form:"created_at_start"`
|
||||
CreatedAtEnd *time.Time `json:"created_at_end" form:"created_at_end"`
|
||||
Expired *bool `json:"expired" form:"expired"`
|
||||
}
|
||||
|
||||
// PageResourceLongByAdmin 分页查询全部短效套餐
|
||||
// PageResourceLongByAdmin 分页查询全部长效套餐
|
||||
func PageResourceLongByAdmin(c *fiber.Ctx) error {
|
||||
_, err := auth.GetAuthCtx(c).PermitAdmin()
|
||||
if err != nil {
|
||||
@@ -307,9 +332,28 @@ func PageResourceLongByAdmin(c *fiber.Ctx) error {
|
||||
if req.CreatedAtEnd != nil {
|
||||
do = do.Where(q.Resource.CreatedAt.Lte(*req.CreatedAtEnd))
|
||||
}
|
||||
if req.Expired != nil {
|
||||
if *req.Expired {
|
||||
do = do.Where(q.Resource.Where(
|
||||
q.ResourceLong.As("Long").Type.Eq(int(m.ResourceModeTime)),
|
||||
q.ResourceLong.As("Long").ExpireAt.Lte(time.Now()),
|
||||
).Or(
|
||||
q.ResourceLong.As("Long").Type.Eq(int(m.ResourceModeQuota)),
|
||||
q.ResourceLong.As("Long").Quota.LteCol(q.ResourceLong.As("Long").Used),
|
||||
))
|
||||
} else {
|
||||
do = do.Where(q.Resource.Where(
|
||||
q.ResourceLong.As("Long").Type.Eq(int(m.ResourceModeTime)),
|
||||
q.ResourceLong.As("Long").ExpireAt.Gt(time.Now()),
|
||||
).Or(
|
||||
q.ResourceLong.As("Long").Type.Eq(int(m.ResourceModeQuota)),
|
||||
q.ResourceLong.As("Long").Quota.GtCol(q.ResourceLong.As("Long").Used),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
list, total, err := q.Resource.
|
||||
Joins(q.Resource.User, q.Resource.Long).
|
||||
list, total, err := q.Resource.Debug().
|
||||
Joins(q.Resource.User, q.Resource.Long, q.Resource.Long.Sku).
|
||||
Select(
|
||||
q.Resource.ALL,
|
||||
q.User.As("User").Phone.As("User__phone"),
|
||||
@@ -321,9 +365,14 @@ func PageResourceLongByAdmin(c *fiber.Ctx) error {
|
||||
q.ResourceLong.As("Long").Daily.As("Long__daily"),
|
||||
q.ResourceLong.As("Long").LastAt.As("Long__last_at"),
|
||||
q.ResourceLong.As("Long").ExpireAt.As("Long__expire_at"),
|
||||
q.ProductSku.As("Long__Sku").Name.As("Long__Sku__name"),
|
||||
).
|
||||
Where(q.Resource.Type.Eq(int(m.ResourceTypeLong)), do).
|
||||
Order(q.Resource.CreatedAt.Desc()).
|
||||
FindByPage(req.GetOffset(), req.GetLimit())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(core.PageResp{
|
||||
List: list,
|
||||
@@ -341,6 +390,7 @@ type PageResourceLongByAdminReq struct {
|
||||
Mode *int `json:"mode" form:"mode"`
|
||||
CreatedAtStart *time.Time `json:"created_at_start" form:"created_at_start"`
|
||||
CreatedAtEnd *time.Time `json:"created_at_end" form:"created_at_end"`
|
||||
Expired *bool `json:"expired" form:"expired"`
|
||||
}
|
||||
|
||||
// AllActiveResource 所有可用套餐
|
||||
@@ -402,6 +452,24 @@ func AllActiveResource(c *fiber.Ctx) error {
|
||||
type AllResourceReq struct {
|
||||
}
|
||||
|
||||
func UpdateResourceByAdmin(c *fiber.Ctx) error {
|
||||
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeResourceWrite)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var req s.UpdateResourceData
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.Resource.Update(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(nil)
|
||||
}
|
||||
|
||||
// StatisticResourceFree 统计每日可用
|
||||
func StatisticResourceFree(c *fiber.Ctx) error {
|
||||
// 检查权限
|
||||
@@ -602,26 +670,28 @@ func ResourcePrice(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
// 获取套餐价格
|
||||
sku, err := s.Resource.GetSku(req.CreateResourceData)
|
||||
sku, err := s.Resource.GetSku(req.CreateResourceData.Code())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
before, after, err := s.Resource.GetPrice(sku, req.Count(), nil)
|
||||
_, amount, discounted, couponApplied, err := s.Resource.GetPrice(sku, req.Count(), nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算折扣
|
||||
return c.JSON(ResourcePriceResp{
|
||||
Price: before.StringFixed(2),
|
||||
Discounted: float32(sku.Discount.Discount) / 100,
|
||||
DiscountedPrice: after.StringFixed(2),
|
||||
Discount: float32(sku.Discount.Discount) / 100,
|
||||
Price: amount.StringFixed(2),
|
||||
Discounted: discounted.StringFixed(2),
|
||||
CouponApplied: couponApplied.StringFixed(2),
|
||||
})
|
||||
}
|
||||
|
||||
type ResourcePriceResp struct {
|
||||
Price string `json:"price"`
|
||||
Discounted float32 `json:"discounted"`
|
||||
DiscountedPrice string `json:"discounted_price"`
|
||||
Price string `json:"price"`
|
||||
Discount float32 `json:"discounted"`
|
||||
Discounted string `json:"discounted_price"`
|
||||
CouponApplied string `json:"coupon_applied"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user