This commit is contained in:
parent
ad86e8671c
commit
837ec8223d
@ -67,16 +67,6 @@ func (c *CoachOutlet) getDetail() {
|
|||||||
if err := c.requestProductDetail(subCtx); err != nil {
|
if err := c.requestProductDetail(subCtx); err != nil {
|
||||||
slog.Error(fmt.Sprintf("获取coach %s 详情失败:%v", c.cfg.Pid, err))
|
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()
|
c.getDetail()
|
||||||
|
if c.getInventory() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(c.cfg.Interval)
|
ticker := time.NewTicker(c.cfg.Interval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
@ -106,44 +100,7 @@ func (c *CoachOutlet) Watch() {
|
|||||||
// 加一些干扰
|
// 加一些干扰
|
||||||
salt := rand.IntN(70)
|
salt := rand.IntN(70)
|
||||||
time.Sleep(time.Second * time.Duration(salt))
|
time.Sleep(time.Second * time.Duration(salt))
|
||||||
// 请求
|
if c.getInventory() {
|
||||||
subCtx, cancel := context.WithTimeout(c.ctx, time.Minute*5)
|
|
||||||
inventory, err := c.requestProductInventory(subCtx)
|
|
||||||
cancel()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn(fmt.Sprintf("获取coach %s 库存失败:%v", c.cfg.Pid, err))
|
|
||||||
}
|
|
||||||
c.detail.UpdateErr = err != nil
|
|
||||||
//如果获取库存没出错,那么更新一下状态
|
|
||||||
if err == nil {
|
|
||||||
c.detail.Orderable = inventory.Orderable
|
|
||||||
if !inventory.AllocationResetDate.IsZero() {
|
|
||||||
c.detail.AllocationResetDate = inventory.AllocationResetDate
|
|
||||||
}
|
|
||||||
c.detail.Watch = true
|
|
||||||
if c.detail.Orderable {
|
|
||||||
c.detail.Watch = false
|
|
||||||
c.detail.AllocationResetDate = time.Now()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = c.db.Where("uid = ?", c.Uid()).Updates(
|
|
||||||
map[string]interface{}{
|
|
||||||
"orderable": c.detail.Orderable,
|
|
||||||
"allocation_reset_date": c.detail.AllocationResetDate,
|
|
||||||
"watch": c.detail.Watch,
|
|
||||||
}).Error; err != nil {
|
|
||||||
slog.Error(fmt.Sprintf("更新数据库失败:%v", err))
|
|
||||||
}
|
|
||||||
// 如果可以预定了,那么退出
|
|
||||||
if 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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-c.ctx.Done():
|
case <-c.ctx.Done():
|
||||||
@ -152,6 +109,50 @@ func (c *CoachOutlet) Watch() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CoachOutlet) getInventory() bool {
|
||||||
|
// 请求
|
||||||
|
subCtx, cancel := context.WithTimeout(c.ctx, time.Minute*5)
|
||||||
|
inventory, err := c.requestProductInventory(subCtx)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn(fmt.Sprintf("获取coach %s 库存失败:%v", c.cfg.Pid, err))
|
||||||
|
}
|
||||||
|
c.detail.UpdateErr = err != nil
|
||||||
|
//如果获取库存没出错,那么更新一下状态
|
||||||
|
if err == nil {
|
||||||
|
c.detail.Orderable = inventory.Orderable
|
||||||
|
if !inventory.AllocationResetDate.IsZero() {
|
||||||
|
c.detail.AllocationResetDate = inventory.AllocationResetDate
|
||||||
|
}
|
||||||
|
c.detail.Watch = true
|
||||||
|
if c.detail.Orderable {
|
||||||
|
c.detail.Watch = false
|
||||||
|
c.detail.AllocationResetDate = time.Now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
"watch": c.detail.Watch,
|
||||||
|
}).Error; err != nil {
|
||||||
|
slog.Error(fmt.Sprintf("更新数据库失败:%v", err))
|
||||||
|
}
|
||||||
|
// 如果可以预定了,那么发通知
|
||||||
|
if 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type ProductData struct {
|
type ProductData struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Loading…
Reference in New Issue
Block a user