2024-05-14 20:36:33 +08:00
|
|
|
package productv1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultExchangeRate float64 = 7.3
|
|
|
|
defaultFreight float64 = 100
|
|
|
|
)
|
|
|
|
|
2024-06-14 15:26:27 +08:00
|
|
|
type Website int
|
|
|
|
|
|
|
|
const (
|
|
|
|
WebSite_US_Coach_Outlet Website = iota + 1
|
|
|
|
WebSite_CN_Coach
|
|
|
|
)
|
|
|
|
|
2024-05-15 13:54:53 +08:00
|
|
|
type PriceStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PriceStatus_NoChange PriceStatus = iota
|
|
|
|
PriceStatus_UP
|
|
|
|
PriceStatus_DOWN //降价
|
|
|
|
)
|
|
|
|
|
2024-05-14 20:36:33 +08:00
|
|
|
type Product struct {
|
|
|
|
ID uint `gorm:"primary_key" json:"id"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
|
|
|
|
|
|
|
Name string `gorm:"index" json:"name"`
|
|
|
|
Pid string `gorm:"unique;not null" json:"pid"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
Orderable bool `json:"orderable"`
|
|
|
|
|
2024-05-16 12:52:06 +08:00
|
|
|
USPrice float64 `json:"usPrice"`
|
2024-06-14 15:26:27 +08:00
|
|
|
Website Website `json:"website" gorm:"index"`
|
2024-05-16 12:52:06 +08:00
|
|
|
DiscPercent int `json:"discPercent" gorm:"index"` //折扣力度
|
|
|
|
CNYPrice float64 `json:"cnyPrice"`
|
|
|
|
CalMark string `json:"calMark"` //计算价格的过程
|
|
|
|
DWPrice float64 `json:"dwPrice"`
|
|
|
|
Freight float64 `json:"freight"` //运费
|
|
|
|
ExchangeRate float64 `json:"exchangeRate" gorm:"-"` //汇率
|
2024-05-23 20:42:37 +08:00
|
|
|
Discount int `json:"discount" gorm:"-"` //折扣
|
2024-05-16 12:52:06 +08:00
|
|
|
Rate float64 `json:"rate" gorm:"index"` //利润率
|
|
|
|
PriceStatus PriceStatus `json:"priceStatus"` //价格状态
|
|
|
|
Remark string `json:"remark"` //备注
|
|
|
|
|
|
|
|
HistoryPrices []HistoryPrice `gorm:"foreignKey:Pid;references:Pid" json:"historyPrices"`
|
|
|
|
DWHistoryPrices []DWHistoryPrice `gorm:"foreignKey:Pid;references:Pid" json:"dwHistoryPrices"`
|
2024-05-14 20:36:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) TableName() string {
|
|
|
|
return "products"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) BeforeSave(tx *gorm.DB) (err error) {
|
|
|
|
p.CalRate()
|
2024-06-14 16:27:31 +08:00
|
|
|
p.fillPriceStatus(tx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) fillPriceStatus(tx *gorm.DB) {
|
|
|
|
switch p.Website {
|
|
|
|
case WebSite_US_Coach_Outlet:
|
|
|
|
p.fillUSCoachPriceStatus(tx)
|
|
|
|
case WebSite_CN_Coach:
|
|
|
|
p.fillCNCoachPriceStatus(tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) fillUSCoachPriceStatus(tx *gorm.DB) {
|
2024-05-14 20:36:33 +08:00
|
|
|
var lastPrice float64
|
|
|
|
tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("us_price", &lastPrice)
|
|
|
|
if p.USPrice != lastPrice {
|
|
|
|
p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{
|
|
|
|
USPrice: p.USPrice,
|
|
|
|
})
|
2024-05-15 13:54:53 +08:00
|
|
|
if p.USPrice > lastPrice {
|
|
|
|
p.PriceStatus = PriceStatus_UP
|
|
|
|
} else {
|
|
|
|
p.PriceStatus = PriceStatus_DOWN
|
2024-06-14 16:27:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 13:54:53 +08:00
|
|
|
|
2024-06-14 16:27:31 +08:00
|
|
|
func (p *Product) fillCNCoachPriceStatus(tx *gorm.DB) {
|
|
|
|
var lastPrice float64
|
|
|
|
tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("cny_price", &lastPrice)
|
|
|
|
if p.CNYPrice != lastPrice {
|
|
|
|
p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{
|
|
|
|
USPrice: p.CNYPrice,
|
|
|
|
})
|
|
|
|
if p.CNYPrice > lastPrice {
|
|
|
|
p.PriceStatus = PriceStatus_UP
|
|
|
|
} else {
|
|
|
|
p.PriceStatus = PriceStatus_DOWN
|
2024-05-15 13:54:53 +08:00
|
|
|
}
|
2024-05-14 20:36:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) CalRate() {
|
2024-06-14 15:26:27 +08:00
|
|
|
switch p.Website {
|
|
|
|
case WebSite_US_Coach_Outlet:
|
|
|
|
p.CalRateUSCoach()
|
|
|
|
case WebSite_CN_Coach:
|
|
|
|
p.CalRateCNCoach()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) CalRateUSCoach() {
|
2024-05-14 20:36:33 +08:00
|
|
|
calculationProcess := make([]string, 0, 2) //存放计算过程
|
|
|
|
if p.ExchangeRate == 0 {
|
|
|
|
p.ExchangeRate = defaultExchangeRate
|
|
|
|
}
|
|
|
|
if p.Freight == 0 {
|
|
|
|
p.Freight = defaultFreight
|
|
|
|
}
|
2024-05-23 20:42:37 +08:00
|
|
|
if p.Discount == 0 {
|
|
|
|
p.Discount = 100
|
|
|
|
}
|
2024-05-14 20:36:33 +08:00
|
|
|
|
|
|
|
//先计算打九折的价格
|
|
|
|
tmpPrice := p.USPrice * 0.9 // 这是打九折的价格
|
|
|
|
p.CNYPrice = tmpPrice*p.ExchangeRate + p.Freight
|
|
|
|
calculationProcess = append(calculationProcess, fmt.Sprintf("【九折】%.2f * 0.9 * %.2f + %.2f = %.2f", p.USPrice, p.ExchangeRate, p.Freight, p.CNYPrice))
|
2024-05-23 20:42:37 +08:00
|
|
|
var discountPrice = p.USPrice
|
|
|
|
if 0 < p.Discount && p.Discount < 100 {
|
|
|
|
discountPrice = p.USPrice * float64(p.Discount) / 100
|
|
|
|
}
|
|
|
|
if discountPrice >= 150 {
|
|
|
|
calculationProcess = append(calculationProcess, fmt.Sprintf("【150 -20】(%.2f * %d%% - 20) * %.2f + %.2f = %.2f", p.USPrice, p.Discount, p.ExchangeRate, p.Freight, (discountPrice-20)*p.ExchangeRate+p.Freight))
|
|
|
|
if discountPrice-20 < tmpPrice {
|
2024-05-14 20:36:33 +08:00
|
|
|
// 符合满150-20,而且比九折便宜
|
2024-05-23 20:42:37 +08:00
|
|
|
tmpPrice = discountPrice - 20
|
2024-05-14 20:36:33 +08:00
|
|
|
p.CNYPrice = tmpPrice*p.ExchangeRate + p.Freight
|
|
|
|
}
|
2024-05-23 20:42:37 +08:00
|
|
|
} else if discountPrice >= 100 {
|
|
|
|
calculationProcess = append(calculationProcess, fmt.Sprintf("【100 -10】(%.2f * %d%% - 10) * %.2f + %.2f = %.2f", p.USPrice, p.Discount, p.ExchangeRate, p.Freight, (discountPrice-10)*p.ExchangeRate+p.Freight))
|
|
|
|
if discountPrice-10 < tmpPrice {
|
2024-05-14 20:36:33 +08:00
|
|
|
// 符合满100 -10,而且比九折便宜
|
2024-05-23 20:42:37 +08:00
|
|
|
tmpPrice = discountPrice - 10
|
2024-05-14 20:36:33 +08:00
|
|
|
p.CNYPrice = tmpPrice*p.ExchangeRate + p.Freight
|
|
|
|
}
|
2024-05-28 21:56:55 +08:00
|
|
|
} else if p.Discount < 90 {
|
|
|
|
calculationProcess = append(calculationProcess, fmt.Sprintf("【打折扣】%.2f * %d%% * %.2f + %.2f = %.2f", p.USPrice, p.Discount, p.ExchangeRate, p.Freight, p.USPrice*float64(p.Discount)/100*p.ExchangeRate+p.Freight))
|
|
|
|
p.CNYPrice = p.USPrice*float64(p.Discount)/100*p.ExchangeRate + p.Freight
|
2024-05-14 20:36:33 +08:00
|
|
|
}
|
|
|
|
p.CalMark = strings.Join(calculationProcess, "\n")
|
|
|
|
if p.DWPrice > 0 {
|
|
|
|
// 如果有得物价格,就计算收益率
|
|
|
|
p.Rate = Decimal((p.DWPrice - p.CNYPrice) / p.CNYPrice * 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.CNYPrice = Decimal(p.CNYPrice)
|
|
|
|
}
|
|
|
|
|
2024-06-14 15:26:27 +08:00
|
|
|
func (p *Product) CalRateCNCoach() {
|
|
|
|
if p.DWPrice > 0 {
|
|
|
|
// 如果有得物价格,就计算收益率
|
|
|
|
p.Rate = Decimal((p.DWPrice - p.CNYPrice) / p.CNYPrice * 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.CNYPrice = Decimal(p.CNYPrice)
|
|
|
|
}
|
|
|
|
|
2024-05-14 20:36:33 +08:00
|
|
|
// Decimal 保留两位小数
|
|
|
|
func Decimal(value float64) float64 {
|
|
|
|
return math.Trunc(value*1e2+0.5) * 1e-2
|
|
|
|
}
|
|
|
|
|
|
|
|
type HistoryPrice struct {
|
|
|
|
ID uint `gorm:"primary_key" json:"id"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
|
|
|
|
Pid string `gorm:"index" json:"pid"`
|
|
|
|
USPrice float64 `json:"usPrice"`
|
|
|
|
}
|
2024-05-16 12:52:06 +08:00
|
|
|
|
|
|
|
type DWHistoryPrice struct {
|
|
|
|
ID uint `gorm:"primary_key" json:"id"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
|
|
|
|
Pid string `gorm:"index" json:"pid"`
|
|
|
|
DWPrice float64 `json:"dwPrice"`
|
|
|
|
}
|