update 修改计算利润方式

This commit is contained in:
timerzz 2025-03-29 21:00:13 +08:00
parent 845cabf65e
commit b4a6ac0c9f
3 changed files with 6 additions and 37 deletions

2
go.mod

@ -5,7 +5,7 @@ go 1.23.0
toolchain go1.24.1
require (
gitea.timerzz.com/kedaya_haitao/common v0.0.0-20250329125059-285bb98158a8
gitea.timerzz.com/kedaya_haitao/common v0.0.0-20250329125718-37b1ee0b6a4b
github.com/gofiber/fiber/v3 v3.0.0-beta.4
github.com/golang/glog v1.2.2
github.com/redis/go-redis/v9 v9.6.1

4
go.sum

@ -1,5 +1,5 @@
gitea.timerzz.com/kedaya_haitao/common v0.0.0-20250329125059-285bb98158a8 h1:c6T9T3VOkJEWzXr2VyvOr/QpuTS3BH0S0k0wqfNBUgc=
gitea.timerzz.com/kedaya_haitao/common v0.0.0-20250329125059-285bb98158a8/go.mod h1:cfkwyDHbOjucM8xLLg8yIkZKz33kdVqvBZYrfNjM8oc=
gitea.timerzz.com/kedaya_haitao/common v0.0.0-20250329125718-37b1ee0b6a4b h1:JPa3QxIOsgiKgT66ujH4EiAoIAHxp++Gacf/NpV1xds=
gitea.timerzz.com/kedaya_haitao/common v0.0.0-20250329125718-37b1ee0b6a4b/go.mod h1:cfkwyDHbOjucM8xLLg8yIkZKz33kdVqvBZYrfNjM8oc=
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=

@ -62,43 +62,12 @@ func (c *Controller) rate(ctx context.Context, idString string) error {
if err != nil {
return fmt.Errorf("获取商品 id: %d 失败:%v", id, err)
}
// 拿到最低成本价和最低售价
var cost, sell float64
//检查能不能购买,只要有一个供应商能买,就认为能购买
var available bool
for _, provider := range article.Providers {
final := provider.Cost.FinalPrice
if final > 0 && (final < cost || cost == 0) && !provider.Exclude {
if available && !provider.Available {
// 上个最低价供应商能买,这个供应商不能买,那么即使现在的供应商价格更低,也不使用
continue
}
cost = final
}
available = available || provider.Available
}
for _, seller := range article.Sellers {
final := seller.Sell.FinalPrice
if final > 0 && (final < sell || sell == 0) && !seller.Exclude {
sell = final
}
}
// 成本和售价没有改动,不保存,直接返回
if cost == article.CostPrice && sell == article.SellPrice {
after, changed := utils.ProfitRate(&article)
if !changed {
return nil
}
// 到这里说明成本和售价有变动
article.CostPrice = Decimal(cost)
article.SellPrice = Decimal(sell)
article.Available = available
if cost > 0 {
article.Rate = Decimal((article.SellPrice - article.CostPrice) * 100 / article.CostPrice)
}
article.GrossProfit = Decimal(article.SellPrice - article.CostPrice)
// 保存更新
return c.storage.Article().Update(article, "cost_price", "sell_price", "available", "updated_at", "rate")
return c.storage.Article().Update(*after, "cost_price", "sell_price", "available", "updated_at", "rate", "GrossProfit")
}
func Decimal(num float64) float64 {