feat providerArticle添加watch

This commit is contained in:
timerzz 2024-11-29 15:45:38 +08:00
parent 18ceb8a077
commit 3eb2ed0d82
5 changed files with 78 additions and 14 deletions

View File

@ -35,15 +35,15 @@ const (
)
func beforeRequest(cli *resty.Client, req *resty.Request) error {
cli.SetHeader("User-Agent", uarand.GetRandom())
cli.SetHeader("sec-fetch-site", "same-origin")
cli.SetHeader("upgrade-insecure-requests", "1")
cli.SetHeaders(map[string]string{
"sec-fetch-dest": "empty",
"sec-fetch-mode": "navigate",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
req.SetHeaders(map[string]string{
"sec-fetch-dest": "empty",
"sec-fetch-mode": "navigate",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"sec-fetch-site": "same-origin",
"upgrade-insecure-requests": "1",
"User-Agent": uarand.GetRandom(),
})
return nil
}

View File

@ -13,6 +13,7 @@ type ProviderArticleApi interface {
Update(providerArticle v2.ProviderArticle, selects ...string) error
BatchUpdate(articles []v2.ProviderArticle) error
AutoMigrate() error
List(query PageListQuery, scopes ...func(db *gorm.DB) *gorm.DB) (articles []v2.ProviderArticle, total int64, err error)
FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) error
ProviderPrice(providerArticleID uint) (history []v2.ProviderPrice, err error)
UpdateStatus(article v2.ProviderArticle) error
@ -29,11 +30,13 @@ func NewProviderArticleApi(db *gorm.DB) ProviderArticleApi {
// ******************GET
type GetProviderArticleQuery struct {
ID uint `query:"id"`
Brand v2.Brand `query:"brand"`
Pid string `query:"pid"`
ProviderId v2.ProviderId `query:"providerId"`
SkuId string `query:"skuId"`
ID uint `query:"id"`
Brand v2.Brand `query:"brand"`
Pid string `query:"pid"`
ProviderId v2.ProviderId `query:"providerId"`
SkuId string `query:"skuId"`
WatchNotNull bool `query:"watchNotNull"`
Watch *bool `query:"watch"`
}
func NewGetProviderArticleQuery() *GetProviderArticleQuery {
@ -63,6 +66,16 @@ func (g *GetProviderArticleQuery) SetSkuId(skuId string) *GetProviderArticleQuer
return g
}
func (g *GetProviderArticleQuery) SetWatch(watch bool) *GetProviderArticleQuery {
g.Watch = &watch
return g
}
func (g *GetProviderArticleQuery) SetWatchNotNull(watchNotNull bool) *GetProviderArticleQuery {
g.WatchNotNull = watchNotNull
return g
}
func (g *GetProviderArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if g.ID > 0 {
db = db.Where("id=?", g.ID)
@ -79,6 +92,12 @@ func (g *GetProviderArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if g.SkuId != "" {
db = db.Where("sku_id=?", g.SkuId)
}
if g.WatchNotNull {
db = db.Not("watch", nil)
}
if g.Watch != nil {
db = db.Where("watch=?", *g.Watch)
}
return db
}
@ -108,6 +127,15 @@ func (p *providerArticleApi) FindInBatches(query *GetProviderArticleQuery, resul
return err
}
func (p *providerArticleApi) List(query PageListQuery, scopes ...func(db *gorm.DB) *gorm.DB) (articles []v2.ProviderArticle, total int64, err error) {
err = p.db.Scopes(query.Scoper.Scope).Model(&v2.ProviderArticle{}).Count(&total).Error
if err != nil {
return
}
err = p.db.Scopes(query.Scope).Scopes(scopes...).Find(&articles).Error
return
}
// 批量更新,更新价格时用到
func (p *providerArticleApi) BatchUpdate(articles []v2.ProviderArticle) error {
return p.db.Select("id", "cost").Save(&articles).Error

11
structs/v2/mock.go Normal file
View File

@ -0,0 +1,11 @@
package v2
import "time"
// 用于测试
type Mock struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Test *bool `json:"test"`
}

21
structs/v2/mock_test.go Normal file
View File

@ -0,0 +1,21 @@
package v2
import (
"fmt"
"testing"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func GetDevDB() (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432")
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
func TestMock(t *testing.T) {
db, _ := GetDevDB()
m := []Mock{}
db.Model(&Mock{}).Where("test", nil).Find(&m)
fmt.Println(m)
}

View File

@ -60,6 +60,8 @@ type ProviderArticle struct {
Brand Brand `gorm:"index" json:"brand"`
// 供应商商品链接链接
Link string `json:"link"`
// 图片
Image string `json:"image"`
// 供应商id
ProviderId ProviderId `gorm:"index:providerId_sku,unique" json:"providerId"`
// 供应商的sku
@ -70,6 +72,8 @@ type ProviderArticle struct {
Ast int `json:"ast"`
// 能否购买
Available bool `json:"available"`
// 是否蹲货
Watch *bool `json:"watch"`
// 状态
Status ProviderStatus `json:"status"`
// 是否排除,如果排除,就不用拉取价格