You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

package shop
import (
"recook/internal/model/order"
"recook/internal/model/user"
"github.com/jinzhu/gorm"
"github.com/shopspring/decimal"
)
func OrderProfit(od order.Information, tx *gorm.DB) error {
var u1 user.Information
tx.First(&u1, "id = ?", od.UserID)
var s1 user.Information
tx.First(&s1, "id = ?", od.SharerID)
condition := od.SharerID == od.UserID
base := od.GoodsTotalAmount.Sub(od.Cost.Mul(decimal.NewFromFloat32(1.03))).Mul(decimal.NewFromFloat32(.93)) // base
switch u1.Level {
case 0:
// 会员买
if condition {
// 自购平台100%
return nil
} else if s1.Level != 0 {
// 40%导购
if err := tx.Create(order.CreateProfit(od.SharerID, order.Guide, base, od.ID)).Error; err != nil {
return err
}
// 10%品牌
if err := tx.Create(order.CreateProfit(od.SharerID, order.Brand, base, od.ID)).Error; err != nil {
return err
}
spID := od.SharerID
if s1.Level == 1 {
var sp1 user.Information
tx.First(&sp1, "user_id = ?", s1.ParentID)
spID = sp1.ID
}
// 20%平台
if err := tx.Create(order.CreateProfit(spID, order.Shop, base, od.ID)).Error; err != nil {
return err
}
}
case 1:
// 店主买
if err := tx.Create(order.CreateProfit(od.UserID, order.Self, base, od.ID)).Error; err != nil {
return err
}
if err := tx.Create(order.CreateProfit(od.UserID, order.Brand, base, od.ID)).Error; err != nil {
return err
}
if err := tx.Create(order.CreateProfit(u1.ParentID, order.Shop, base, od.ID)).Error; err != nil {
return err
}
case 2:
// 店铺买
r1 := order.CreateProfit(od.UserID, order.Self, base, od.ID)
r2 := order.CreateProfit(od.UserID, order.Brand, base, od.ID)
r3 := order.CreateProfit(od.UserID, order.Shop, base, od.ID)
if err := tx.Create(r1).Error; err != nil {
return err
}
if err := tx.Create(r2).Error; err != nil {
return err
}
if err := tx.Create(r3).Error; err != nil {
return err
}
}
return nil
}