us-coach-spider/product/calculate.go

127 lines
4.1 KiB
Go
Raw Normal View History

2024-06-15 21:10:37 +08:00
package product
import (
"errors"
productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product"
"github.com/samber/lo"
"gorm.io/gorm"
)
const (
USCoachOutletPid = "us-coach-outlet"
)
var (
defaultCalculateProcesses = []productv1.CalculateProcess{
{
Pid: USCoachOutletPid,
Name: "九折",
Process: "originalPrice * 0.9 * exchangeRate + freight",
},
{
Pid: USCoachOutletPid,
Name: "150 - 20",
Condition: "originalPrice >= 150",
Process: "(originalPrice - 20) * exchangeRate + freight",
},
{
Pid: USCoachOutletPid,
Name: "100 - 10",
Condition: "originalPrice >= 100",
Process: "(originalPrice - 10) * exchangeRate + freight",
},
}
)
func (c *Controller) LoadCalculateProcess() {
c.db.Model(&productv1.CalculateProcess{}).Find(&c.calculates, "pid = ?", USCoachOutletPid)
if len(c.calculates) == 0 {
c.db.Create(&defaultCalculateProcesses)
c.db.Model(&productv1.CalculateProcess{}).Find(&c.calculates, "pid = ?", USCoachOutletPid)
}
}
func (c *Controller) GetGlobalCalculateProcess() (cs []productv1.CalculateProcess, err error) {
err = c.db.Model(&productv1.CalculateProcess{}).Find(&cs, "pid = ?", USCoachOutletPid).Error
return
}
// DeleteGlobalCalculateProcess 删除全局的计算规则
func (c *Controller) DeleteGlobalCalculateProcess(id uint) error {
if _, idx, exist := lo.FindIndexOf(c.calculates, func(item productv1.CalculateProcess) bool {
return item.ID == id
}); exist {
if err := c.db.Model(&productv1.CalculateProcess{}).Delete(&c.calculates[idx], "id = ?", id).Error; err != nil {
return err
}
c.calculates = append(c.calculates[:idx], c.calculates[idx+1:]...)
go c.updateRate(c.Freight)
}
return nil
}
// SaveGlobalCalculateProcess 更新全局的计算规则
func (c *Controller) SaveGlobalCalculateProcess(cs []productv1.CalculateProcess) error {
if len(cs) == 0 {
return nil
}
cs = lo.Filter(cs, func(item productv1.CalculateProcess, index int) bool {
return item.Pid == USCoachOutletPid
})
return c.db.Transaction(func(tx *gorm.DB) error {
if err := c.db.Save(&cs).Error; err != nil {
return err
}
if err := c.db.Model(&productv1.CalculateProcess{}).Find(&c.calculates, "pid = ?", USCoachOutletPid).Error; err != nil {
return err
}
go c.updateRate(c.Freight)
return nil
})
}
// DeleteCalculateProcess 删除计算规则
func (c *Controller) DeleteCalculateProcess(id uint) error {
return c.db.Transaction(func(tx *gorm.DB) error {
cal := productv1.CalculateProcess{}
if err := tx.Model(&productv1.CalculateProcess{}).Where("id = ?", id).First(&cal).Error; err != nil && !errors.As(err, &gorm.ErrRecordNotFound) {
return err
} else if errors.As(err, &gorm.ErrRecordNotFound) {
return nil
}
if err := tx.Model(&productv1.CalculateProcess{}).Where("id = ?", cal.ID).Delete(&cal).Error; err != nil {
return err
}
var product productv1.Product
if err := tx.Preload("CalculateProcess").First(&product, "pid = ? AND website = ?", cal.Pid, productv1.WebSite_US_Coach_Outlet).Error; err != nil {
return err
}
product.ExchangeRate = c.ExchangeRate
product.CalCNY(append(product.CalculateProcess, c.calculates...))
return tx.Model(&product).Where("pid = ? AND website = ?", product.Pid, productv1.WebSite_US_Coach_Outlet).Select("rate", "cal_mark", "cny_price").Updates(&product).Error
})
}
// SaveCalculateProcess 更新计算规则
func (c *Controller) SaveCalculateProcess(cs []productv1.CalculateProcess) error {
if len(cs) == 0 {
return nil
}
return c.db.Transaction(func(tx *gorm.DB) error {
if err := c.db.Save(&cs).Error; err != nil {
return err
}
var product productv1.Product
if err := tx.Preload("CalculateProcess").First(&product, "pid = ? AND website = ?", cs[0].Pid, productv1.WebSite_US_Coach_Outlet).Error; err != nil {
return err
}
product.ExchangeRate = c.ExchangeRate
product.CalCNY(append(product.CalculateProcess, c.calculates...))
return tx.Model(&product).Where("pid = ? AND website = ?", product.Pid, productv1.WebSite_US_Coach_Outlet).Select("rate", "cal_mark", "cny_price").Updates(&product).Error
})
}