diff --git a/pkg/model/watcher.go b/pkg/model/watcher.go index 431315d..e3f7686 100644 --- a/pkg/model/watcher.go +++ b/pkg/model/watcher.go @@ -14,7 +14,7 @@ type WatchInfo struct { DeletedAt gorm.DeletedAt `gorm:"index"` UpdateErr bool `json:"updateErr"` //更新出错了 - Uid string `json:"uid" gorm:"index:,unique"` + Uid string `json:"uid" gorm:"index:,unique,not null"` Pid string `json:"pid" gorm:"index:,not null"` //产品编号 Name string `json:"name"` Brand string `json:"brand"` diff --git a/pkg/watcher/coach.go b/pkg/watcher/coach.go index 6dc762c..b4422ed 100644 --- a/pkg/watcher/coach.go +++ b/pkg/watcher/coach.go @@ -8,7 +8,6 @@ import ( "gitea.timerzz.com/kedaya_haitao/pusher/rpc/pusher" "github.com/golang/glog" "gorm.io/gorm" - "gorm.io/gorm/clause" "haitao_watcher/pkg/model" "math/rand/v2" "time" @@ -42,6 +41,7 @@ func (c *CoachOutlet) SetOption(cfg model.WatchInfo) { func (c *CoachOutlet) Cancel() { c.cancel() + c.db.Where("uid = ?", c.cfg.Uid).Model(c.cfg).Update("watch", false) } func (c *CoachOutlet) Restart() { @@ -52,7 +52,7 @@ func (c *CoachOutlet) Restart() { func (c *CoachOutlet) Watch() { c.ctx, c.cancel = context.WithCancel(c.fCtx) c.cfg.Watch, c.cfg.Orderable = true, false - if err := c.db.Model(c.cfg).Where("uid", c.cfg.Uid).Updates(map[string]interface{}{"watch": true, "orderable": false}).Error; err != nil { + if err := c.db.Model(c.cfg).Where("uid = ?", c.cfg.Uid).Updates(map[string]interface{}{"watch": true, "orderable": false}).Error; err != nil { glog.Errorf("watch update db err: %v", err) err = nil } @@ -102,10 +102,8 @@ func (c *CoachOutlet) doWatch() bool { if c.cfg.Orderable { c.cfg.Watch = false } - err = c.db.Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "uid"}}, - UpdateAll: true, - }).Create(c.cfg).Error + c.cfg.UpdatedAt = time.Now() + err = c.db.Where("uid = ?", c.cfg.Uid).Select("name", "brand", "watch", "orderable", "link", "remark", "has_detail", "allocation_reset_date", "updated_at").Updates(c.cfg).Error if err != nil { glog.Errorf("保存 %s detail失败:%v", c.cfg.Uid, err) c.cfg.HasDetail = false