cn-coach-spider/spider/calculate.go

116 lines
3.7 KiB
Go
Raw Normal View History

2024-06-16 11:08:28 +08:00
package spider
import (
"errors"
productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product"
"github.com/samber/lo"
"gorm.io/gorm"
)
const (
CNCoachPid = "cn-coach"
CNCoachOutletPid = "cn-coach-outlet"
)
func (c *Controller) LoadCalculateProcess() {
c.db.Model(&productv1.CalculateProcess{}).Find(&c.calculates, "pid = ?", c.pid)
}
func (c *Controller) GetGlobalCalculateProcess() (cs []productv1.CalculateProcess, err error) {
err = c.db.Model(&productv1.CalculateProcess{}).Find(&cs, "pid = ?", c.pid).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()
}
return nil
}
// SaveGlobalCalculateProcess 更新全局的计算规则
func (c *Controller) SaveGlobalCalculateProcess(cs []productv1.CalculateProcess) error {
if len(cs) == 0 {
return nil
}
cs = lo.Map(cs, func(item productv1.CalculateProcess, index int) productv1.CalculateProcess {
item.Pid = c.pid
return item
})
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 = ?", c.pid).Error; err != nil {
return err
}
go c.updateRate()
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, c.website).Error; err != nil {
return err
}
product.CalCNY(append(product.CalculateProcess, c.calculates...))
return tx.Model(&product).Where("pid = ? AND website = ?", product.Pid, c.website).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, c.website).Error; err != nil {
return err
}
product.CalCNY(append(product.CalculateProcess, c.calculates...))
return tx.Model(&product).Where("pid = ? AND website = ?", product.Pid, c.website).Select("rate", "cal_mark", "cny_price").Updates(&product).Error
})
}
func (c *Controller) updateRate() {
var results []*productv1.Product
c.db.Where("website = ?", c.website).FindInBatches(&results, 20, func(tx *gorm.DB, batch int) error {
for _, result := range results {
var calculate []productv1.CalculateProcess
c.db.Find(&calculate, "pid = ? AND website = ?", result.Pid, c.website)
result.CalCNY(append(calculate, c.calculates...))
}
// 保存对当前批记录的修改
tx.Save(&results)
return nil
})
}