us-coach-spider/product/controller.go

118 lines
3.1 KiB
Go
Raw Normal View History

2024-05-14 15:27:40 +08:00
package product
import (
"context"
"fmt"
2024-05-14 20:43:05 +08:00
"gitea.timerzz.com/kedaya_haitao/common/model/product"
2024-05-21 15:38:09 +08:00
coach_client "gitea.timerzz.com/kedaya_haitao/common/pkg/coach-client"
2024-05-15 20:41:42 +08:00
"github.com/golang/glog"
2024-05-14 15:27:40 +08:00
"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"log/slog"
"time"
)
type Controller struct {
ctx context.Context
client *coach_client.Client
updateTime time.Time // 上次抓取时间
db *gorm.DB
Option
}
2024-05-14 16:17:04 +08:00
func NewController(client *coach_client.Client, db *gorm.DB) *Controller {
ctl := &Controller{
2024-05-14 15:27:40 +08:00
client: client,
db: db,
}
2024-05-14 17:15:27 +08:00
ctl.AutoMigrate()
2024-05-14 16:17:04 +08:00
ctl.LoadOption()
return ctl
2024-05-14 15:27:40 +08:00
}
2024-05-14 17:04:27 +08:00
func (c *Controller) AutoMigrate() {
2024-05-14 20:43:05 +08:00
if err := c.db.AutoMigrate(&productv1.Product{}, &productv1.HistoryPrice{}, &Option{}); err != nil {
2024-05-14 17:04:27 +08:00
panic(err)
}
}
2024-05-14 15:27:40 +08:00
func (c *Controller) Run(ctx context.Context) {
c.ctx = ctx
ticker := time.NewTicker(c.Interval)
2024-05-14 17:22:10 +08:00
if err := c.Crawl(); err != nil {
slog.Error(err.Error())
} else {
slog.Info("抓取信息成功")
c.updateTime = time.Now()
}
2024-05-14 15:27:40 +08:00
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := c.Crawl(); err != nil {
slog.Error(err.Error())
} else {
slog.Info("抓取信息成功")
c.updateTime = time.Now()
}
}
}
}
func (c *Controller) Crawl() error {
2024-05-14 17:22:10 +08:00
slog.Info("开始抓取信息")
2024-05-14 15:27:40 +08:00
for page, totalPage := 1, -1; page <= totalPage || totalPage == -1; page++ {
resp, err := c.client.ViewAllBags(c.ctx, page)
if err != nil {
return fmt.Errorf("访问coach第%d页失败: %w", page, err)
}
2024-05-14 17:43:08 +08:00
totalPage = resp.PageData.TotalPages
2024-05-14 15:27:40 +08:00
2024-05-14 17:43:08 +08:00
if err = c.saveRespData(resp.PageData.Products); err != nil {
2024-05-14 15:27:40 +08:00
return fmt.Errorf("保存第%d页数据失败: %w", page, err)
}
2024-05-15 20:41:42 +08:00
glog.Infof("第%d页数据保存成功", page)
2024-05-14 15:27:40 +08:00
}
return nil
}
func (c *Controller) saveRespData(list []coach_client.Product) error {
2024-05-14 20:43:05 +08:00
var products = make([]productv1.Product, 0, len(list))
2024-05-14 15:27:40 +08:00
for _, resp := range list {
for _, color := range resp.Colors {
price, _ := lo.Find(resp.VariantsOnSale, func(item coach_client.Variant) bool {
return item.Id == color.VgId
})
2024-05-15 13:59:07 +08:00
// 获取已经存的运费
var saveFreight float64
c.db.Model(&productv1.Product{}).Where("pid = ?", color.VgId).Select("freight").Scan(&saveFreight)
if saveFreight > 0 {
c.Freight = saveFreight
}
2024-05-14 20:43:05 +08:00
products = append(products, productv1.Product{
2024-05-15 20:41:42 +08:00
UpdatedAt: time.Now(),
2024-05-14 15:27:40 +08:00
Name: resp.Name,
Pid: color.VgId,
Color: color.Text,
Link: fmt.Sprintf("%s/%s", "https://www.coachoutlet.com", color.Url),
Image: color.Media.Thumbnail.Src,
2024-05-15 19:48:39 +08:00
Orderable: color.Orderable,
2024-05-15 20:41:42 +08:00
DiscPercent: price.Price.MarkdownDiscPercent,
2024-05-14 15:27:40 +08:00
USPrice: price.Price.Sales.Value,
Freight: c.Freight,
ExchangeRate: c.ExchangeRate,
})
}
}
// 去重
2024-05-14 20:43:05 +08:00
products = lo.UniqBy(products, func(p productv1.Product) string {
return p.Pid
})
2024-05-14 15:27:40 +08:00
return c.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "pid"}},
2024-05-15 20:41:42 +08:00
DoUpdates: clause.AssignmentColumns([]string{"name", "color", "link", "orderable", "us_price", "cny_price", "cal_mark", "rate", "price_status", "disc_percent", "updated_at"}),
2024-05-14 15:27:40 +08:00
}).Create(products).Error
}