feat 获取详情和库存分离
Some checks failed
Build image / build (push) Failing after 19s

This commit is contained in:
timerzz 2024-04-14 14:14:08 +08:00
parent ad86e8671c
commit 837ec8223d

View File

@ -67,16 +67,6 @@ func (c *CoachOutlet) getDetail() {
if err := c.requestProductDetail(subCtx); err != nil {
slog.Error(fmt.Sprintf("获取coach %s 详情失败:%v", c.cfg.Pid, err))
}
if c.detail != nil && c.detail.Orderable {
if c.onOrderable != nil {
c.onOrderable <- model.PushMsg{
Title: "coachoutlet 商品补货",
Content: fmt.Sprintf("商品 %s 可以购买,链接: %s", c.detail.Name, c.detail.Link),
ToPusher: c.detail.PusherIds,
}
}
c.cancel()
}
}
}
@ -93,6 +83,10 @@ func (c *CoachOutlet) Watch() {
}
c.getDetail()
if c.getInventory() {
return
}
ticker := time.NewTicker(c.cfg.Interval)
defer ticker.Stop()
@ -106,6 +100,16 @@ func (c *CoachOutlet) Watch() {
// 加一些干扰
salt := rand.IntN(70)
time.Sleep(time.Second * time.Duration(salt))
if c.getInventory() {
return
}
case <-c.ctx.Done():
return
}
}
}
func (c *CoachOutlet) getInventory() bool {
// 请求
subCtx, cancel := context.WithTimeout(c.ctx, time.Minute*5)
inventory, err := c.requestProductInventory(subCtx)
@ -127,7 +131,7 @@ func (c *CoachOutlet) Watch() {
}
}
if err = c.db.Where("uid = ?", c.Uid()).Updates(
if err = c.db.Model(&model.Product{}).Where("uid = ?", c.Uid()).Updates(
map[string]interface{}{
"orderable": c.detail.Orderable,
"allocation_reset_date": c.detail.AllocationResetDate,
@ -135,7 +139,7 @@ func (c *CoachOutlet) Watch() {
}).Error; err != nil {
slog.Error(fmt.Sprintf("更新数据库失败:%v", err))
}
// 如果可以预定了,那么退出
// 如果可以预定了,那么发通知
if c.detail.Orderable {
if c.onOrderable != nil {
c.onOrderable <- model.PushMsg{
@ -144,12 +148,9 @@ func (c *CoachOutlet) Watch() {
ToPusher: c.detail.PusherIds,
}
}
return
}
case <-c.ctx.Done():
return
}
return true
}
return false
}
type ProductData struct {