完善定价与套餐关联表结构

This commit is contained in:
2026-03-24 16:25:21 +08:00
parent c9995ef566
commit 5ffa151f58
28 changed files with 1550 additions and 112 deletions

View File

@@ -0,0 +1,59 @@
package services
import (
"platform/web/core"
m "platform/web/models"
q "platform/web/queries"
"time"
"gorm.io/gen/field"
)
var ProductDiscount = &productDiscountService{}
type productDiscountService struct{}
func (s *productDiscountService) All() (result []*m.ProductDiscount, err error) {
return q.ProductDiscount.Find()
}
func (s *productDiscountService) Page(req *core.PageReq) (result []*m.ProductDiscount, count int64, err error) {
return q.ProductDiscount.FindByPage(req.GetOffset(), req.GetLimit())
}
func (s *productDiscountService) Create(data CreateProductDiscountData) (err error) {
return q.ProductDiscount.Create(&m.ProductDiscount{
Name: data.Name,
Discount: data.Discount,
})
}
type CreateProductDiscountData struct {
Name string `json:"name"`
Discount int32 `json:"discount" validate:"min=0,max=100"`
}
func (s *productDiscountService) Update(data UpdateProductDiscountData) (err error) {
do := make([]field.AssignExpr, 0)
if data.Name != nil {
do = append(do, q.ProductDiscount.Name.Value(*data.Name))
}
if data.Discount != nil {
do = append(do, q.ProductDiscount.Discount.Value(*data.Discount))
}
_, err = q.ProductDiscount.Where(q.ProductDiscount.ID.Eq(data.ID)).UpdateSimple(do...)
return err
}
type UpdateProductDiscountData struct {
ID int32 `json:"id"`
Name *string `json:"name"`
Discount *int32 `json:"discount" validate:"omitempty,min=0,max=100"`
}
func (s *productDiscountService) Delete(id int32) (err error) {
_, err = q.ProductDiscount.Where(q.ProductDiscount.ID.Eq(id)).UpdateColumn(q.ProductDiscount.DeletedAt, time.Now())
return
}

View File

@@ -21,6 +21,7 @@ func (s *productSkuService) Page(req *core.PageReq, productId *int32) (result []
do = append(do, q.ProductSku.ProductID.Eq(*productId))
}
return q.ProductSku.
Joins(q.ProductSku.Discount).
Where(do...).
FindByPage(req.GetOffset(), req.GetLimit())
}
@@ -32,20 +33,20 @@ func (s *productSkuService) Create(create CreateProductSkuData) (err error) {
}
return q.ProductSku.Create(&m.ProductSku{
ProductID: create.ProductID,
Code: create.Code,
Name: create.Name,
Price: price,
Discount: create.Discount,
ProductID: create.ProductID,
DiscountId: create.DiscountID,
Code: create.Code,
Name: create.Name,
Price: price,
})
}
type CreateProductSkuData struct {
ProductID int32 `json:"product_id"`
Code string `json:"code"`
Name string `json:"name"`
Price string `json:"price"`
Discount float32 `json:"discount"`
ProductID int32 `json:"product_id"`
DiscountID int32 `json:"discount_id"`
Code string `json:"code"`
Name string `json:"name"`
Price string `json:"price"`
}
func (s *productSkuService) Update(update UpdateProductSkuData) (err error) {
@@ -58,8 +59,8 @@ func (s *productSkuService) Update(update UpdateProductSkuData) (err error) {
}
do = append(do, q.ProductSku.Price.Value(price))
}
if update.Discount != nil {
do = append(do, q.ProductSku.Discount.Value(*update.Discount))
if update.DiscountID != nil {
do = append(do, q.ProductSku.DiscountId.Value(*update.DiscountID))
}
if update.Code != nil {
do = append(do, q.ProductSku.Code.Value(*update.Code))
@@ -73,14 +74,26 @@ func (s *productSkuService) Update(update UpdateProductSkuData) (err error) {
}
type UpdateProductSkuData struct {
ID int32 `json:"id"`
Code *string `json:"code"`
Name *string `json:"name"`
Price *string `json:"price"`
Discount *float32 `json:"discount"`
ID int32 `json:"id"`
DiscountID *int32 `json:"discount_id"`
Code *string `json:"code"`
Name *string `json:"name"`
Price *string `json:"price"`
}
func (s *productSkuService) Delete(id int32) (err error) {
_, err = q.ProductSku.Where(q.ProductSku.ID.Eq(id)).UpdateColumn(q.ProductSku.DeletedAt, time.Now())
return
}
func (s *productSkuService) BatchUpdateDiscount(data BatchUpdateSkuDiscountData) (err error) {
_, err = q.ProductSku.Where(q.ProductSku.ProductID.Eq(data.ProductID)).UpdateSimple(
q.ProductSku.DiscountId.Value(data.DiscountID),
)
return
}
type BatchUpdateSkuDiscountData struct {
ProductID int32 `json:"product_id"`
DiscountID int32 `json:"discount_id"`
}

View File

@@ -107,6 +107,7 @@ func createResource(q *q.Query, uid int32, now time.Time, data *CreateResourceDa
ResourceNo: u.P(ID.GenReadable("res")),
Active: true,
Type: data.Type,
Code: data.Type.Code(),
}
switch data.Type {
@@ -120,6 +121,7 @@ func createResource(q *q.Query, uid int32, now time.Time, data *CreateResourceDa
Live: short.Live,
Type: short.Mode,
Quota: short.Quota,
Code: data.Code(),
}
if short.Mode == m.ResourceModeTime {
if short.Expire == nil {
@@ -139,6 +141,7 @@ func createResource(q *q.Query, uid int32, now time.Time, data *CreateResourceDa
Live: long.Live,
Type: long.Mode,
Quota: long.Quota,
Code: data.Code(),
}
if long.Mode == m.ResourceModeTime {
if long.Expire == nil {
@@ -160,11 +163,18 @@ func createResource(q *q.Query, uid int32, now time.Time, data *CreateResourceDa
}
func (s *resourceService) GetSku(data *CreateResourceData) (*m.ProductSku, error) {
sku, err := q.ProductSku.Where(q.ProductSku.Code.Eq(data.Code())).Take()
sku, err := q.ProductSku.
Joins(q.ProductSku.Discount).
Where(q.ProductSku.Code.Eq(data.Code())).
Take()
if err != nil {
return nil, core.NewServErr("产品不可用", err)
}
if sku.Discount == nil {
return nil, core.NewServErr("价格查询失败", err)
}
return sku, nil
}
@@ -174,28 +184,30 @@ func (s *resourceService) GetPrice(sku *m.ProductSku, count int32, uid *int32) (
var uSku *m.ProductSkuUser
if uid != nil {
var err error
uSku, err = q.ProductSkuUser.Where(
q.ProductSkuUser.UserID.Eq(*uid),
q.ProductSkuUser.ProductSkuID.Eq(sku.ID),
).Take()
uSku, err = q.ProductSkuUser.
Joins(q.ProductSkuUser.Discount).
Where(
q.ProductSkuUser.UserID.Eq(*uid),
q.ProductSkuUser.ProductSkuID.Eq(sku.ID)).
Take()
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return decimal.Zero, decimal.Zero, core.NewServErr("客户特殊价查询失败", err)
}
}
// 返回计算价格
price := sku.Price
if uSku != nil && uSku.Price != nil {
price = *uSku.Price
if uSku.Discount == nil {
return decimal.Decimal{}, decimal.Decimal{}, core.NewServErr("价格获取失败")
}
discount := sku.Discount
if uSku != nil && uSku.Discount != nil {
discount = *uSku.Discount
// 返回计算价格
price := sku.Price
discount := sku.Discount.Decimal()
if uSku != nil {
discount = uSku.Discount.Decimal()
}
before := price.Mul(decimal.NewFromInt32(count))
after := before.Mul(decimal.NewFromFloat32(discount))
after := before.Mul(discount)
return before, after, nil
}