Commit 53636eac authored by zhengyaoqiu's avatar zhengyaoqiu

feat(script): 分类同步脚本

parent f24a7fa1
......@@ -4,8 +4,9 @@ go 1.24.3
require (
git.chillcy.com/golang/chillcy/pkg v0.0.0-20250627035541-7956996f27ce
git.chillcy.com/golang/chillcy/project/pc4 v0.0.0-20250630014959-9d2b4facb28b
git.chillcy.com/golang/chillcy/project/pc4 v0.0.0-20250630145511-385ed232051f
github.com/zeromicro/go-zero v1.8.4
go.uber.org/multierr v1.11.0
google.golang.org/grpc v1.73.0
google.golang.org/protobuf v1.36.6
gorm.io/driver/mysql v1.6.0
......@@ -76,7 +77,6 @@ require (
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/automaxprocs v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.38.0 // indirect
......
......@@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.chillcy.com/golang/chillcy/pkg v0.0.0-20250627035541-7956996f27ce h1:JBhBnnNR7DCqi67fxfLC7ZIJJXJP4jMTtP8DeW9XcJQ=
git.chillcy.com/golang/chillcy/pkg v0.0.0-20250627035541-7956996f27ce/go.mod h1:1N8ux+rPaW+/OT74EGSz2oX51NvsEK3H4nlBZ+5Xx0M=
git.chillcy.com/golang/chillcy/project/pc4 v0.0.0-20250630014959-9d2b4facb28b h1:Lr5xxH47rdzrsMiClgZ5UH51RLMp/It08GSA+gfIsxg=
git.chillcy.com/golang/chillcy/project/pc4 v0.0.0-20250630014959-9d2b4facb28b/go.mod h1:+oGrruo/MyXkrh5VPjDu/jvHN7tZGNkdZiKMJ/gIi4c=
git.chillcy.com/golang/chillcy/project/pc4 v0.0.0-20250630145511-385ed232051f h1:08l02ihb9QCINdg2o202kclqEQK4gaqxYo0H7kz6/p8=
git.chillcy.com/golang/chillcy/project/pc4 v0.0.0-20250630145511-385ed232051f/go.mod h1:+oGrruo/MyXkrh5VPjDu/jvHN7tZGNkdZiKMJ/gIi4c=
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
github.com/alicebob/miniredis/v2 v2.35.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
......
......@@ -6,7 +6,7 @@ type Category struct {
gorm.Model
Code string `gorm:"uniqueIndex; size:50"`
Name string `gorm:"size:50"`
Level int `gorm:"not null"`
ParentCode string `gorm:"size:50"`
}
type Categories []Category
......@@ -3,10 +3,12 @@ package repository
import (
"git.chillcy.com/golang/colorway/internal/pkg/database/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type CategoryInterface interface {
BaseInterface
UpsertManyByCode(categories model.Categories) error
}
type Category struct {
......@@ -18,3 +20,15 @@ func NewCategory(db *gorm.DB) *Category {
Base: NewBase[model.Category](db),
}
}
func (receiver *Category) UpsertManyByCode(categories model.Categories) error {
result := receiver.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "code"}},
DoUpdates: clause.Assignments(map[string]interface{}{
"name": clause.Column{Name: "name"},
"parent_code": clause.Column{Name: "parent_code"},
"updated_at": clause.Expr{SQL: "NOW()"}, // 手动更新时间戳
}),
}).Create(&categories)
return result.Error
}
......@@ -32,7 +32,11 @@ func (l *UploadLogic) Upload(in *colorway.ProductUploadRequest) (*colorway.Produ
if err != nil {
return nil, err
}
sourceItems := l.svcCtx.Cache.Source.GetSourceItems()
for _, designerProduct := range productList.GetDesignerProducts() {
designerProduct.GetDesignerId()
}
log.Println(productList)
log.Println(productList, sourceItems)
return &colorway.ProductUploadResponse{}, nil
}
package cache
import (
"context"
"fmt"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/client/source"
"github.com/zeromicro/go-zero/core/logc"
"go.uber.org/multierr"
"log"
"time"
)
var _cache *Cache
type Cache struct {
refreshMap map[RefreshKey]Refresh
Source *Source
}
func InitCache(config Config) {
s := NewSource(config.Source)
_cache = &Cache{
refreshMap: map[RefreshKey]Refresh{
RefreshKeySource: s,
},
Source: s,
}
err := _cache.Notify(RefreshKeySource)
if err != nil {
panic(err)
}
_cache.startRefresh()
}
func (receiver *Cache) Notify(keys ...RefreshKey) error {
var err error
for _, key := range keys {
refresh, ok := receiver.refreshMap[key]
if !ok {
err = multierr.Append(err, fmt.Errorf("缺失 %s 对应的更新对象", key))
}
e := refresh.Refresh(context.TODO())
if e != nil {
err = multierr.Append(err, e)
}
logc.Infof(context.TODO(), "缓存更新 %s", key)
}
return err
}
func (receiver *Cache) startRefresh() {
for refreshKey, refresh := range receiver.refreshMap {
go func(k RefreshKey, r Refresh) {
for {
interval := r.Interval()
time.Sleep(interval)
err := receiver.Notify(k)
if err != nil {
log.Println(err)
continue
}
}
}(refreshKey, refresh)
}
}
type RefreshKey string
const (
RefreshKeySource = "source"
)
type Config struct {
Source source.Source
}
func GetCache() *Cache {
return _cache
}
package cache
import (
"context"
"time"
)
type Refresh interface {
Refresh(ctx context.Context) error
Interval() time.Duration
}
package cache
import (
"context"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/client/source"
"sync"
"time"
)
type Source struct {
items SourceItems
rwMutes *sync.RWMutex
source source.Source
}
func NewSource(source source.Source) *Source {
return &Source{source: source, rwMutes: &sync.RWMutex{}}
}
func (receiver *Source) Refresh(ctx context.Context) error {
receiver.rwMutes.Lock()
defer receiver.rwMutes.Unlock()
sources, err := receiver.source.List(ctx, &source.SourceListRequest{})
if err != nil {
return err
}
var sourceItems SourceItems
for _, s := range sources.GetSources() {
sourceItems = append(sourceItems, SourceItem{
Id: s.GetId(),
CombineWeight: CombineWeight{
Image: int(s.GetCombineWeight().GetImage()),
Title: int(s.GetCombineWeight().GetTitle()),
Category: int(s.GetCombineWeight().GetCategory()),
Compositions: int(s.GetCombineWeight().GetComposition()),
Care: int(s.GetCombineWeight().GetCare()),
Description: int(s.GetCombineWeight().GetDescription()),
Fits: int(s.GetCombineWeight().GetFits()),
Models: int(s.GetCombineWeight().GetModels()),
Season: int(s.GetCombineWeight().GetSeason()),
},
})
}
receiver.items = sourceItems
return nil
}
func (receiver *Source) Interval() time.Duration {
return time.Hour
}
func (receiver *Source) GetSourceItems() SourceItems {
receiver.rwMutes.RLock()
defer receiver.rwMutes.RUnlock()
return receiver.items
}
type SourceItem struct {
Id string
CombineWeight CombineWeight
}
type SourceItems []SourceItem
type CombineWeight struct { // https://trello.com/c/vrEKHL8e
Image int `json:"image" bson:"image" form:"image"`
Title int `json:"title" bson:"title" form:"title"`
Category int `json:"category" bson:"category" form:"category"`
Compositions int `json:"compositions" bson:"compositions" form:"compositions"`
Care int `json:"care" bson:"care" form:"care"`
Description int `json:"description" bson:"description" form:"weight_desc"`
Fits int `json:"fits" bson:"fits" form:"fits"`
Models int `json:"models" bson:"models" form:"models"`
Season int `json:"season" bson:"season" form:"season"`
}
package convert
import (
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/pb/pc4"
"git.chillcy.com/golang/colorway/internal/pkg/database/model"
)
type Result struct {
CompleteSpus model.CompleteSpus
ColorSpus model.ColorSpus
}
type Product struct{}
func (receiver Product) Convert(designerProducts []*pc4.DesignerProduct) {
for _, designerProduct := range designerProducts {
designerProduct.GetDesignerId()
designerProduct.GetColorProducts()
}
}
......@@ -3,8 +3,10 @@ package svc
import (
"git.chillcy.com/golang/chillcy/pkg/etcd"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/client/product"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/client/source"
"git.chillcy.com/golang/colorway/internal/pkg/database"
"git.chillcy.com/golang/colorway/internal/rpc/internal/config"
"git.chillcy.com/golang/colorway/internal/rpc/internal/svc/cache"
"github.com/zeromicro/go-zero/zrpc"
)
......@@ -12,6 +14,7 @@ type ServiceContext struct {
Config config.Config
DatabaseManager *database.Manager
Pc4RpcService Pc4RpcService
Cache *cache.Cache
}
func NewServiceContext(c config.Config) *ServiceContext {
......@@ -22,17 +25,26 @@ func NewServiceContext(c config.Config) *ServiceContext {
if err != nil {
panic(err)
}
// todo etcd config
pc4RpcProductServiceTarget := "127.0.0.1:5100"
return &ServiceContext{
pc4RpcProductServiceTarget := etcd.Get(etcd.KeyRpcPc4)
if etcd.GetEnv() == etcd.EnvDev {
pc4RpcProductServiceTarget = "127.0.0.1:5100"
}
svc := &ServiceContext{
Config: c,
DatabaseManager: manager,
Pc4RpcService: Pc4RpcService{
Product: product.NewProduct(zrpc.MustNewClient(zrpc.RpcClientConf{Target: pc4RpcProductServiceTarget})),
Source: source.NewSource(zrpc.MustNewClient(zrpc.RpcClientConf{Target: pc4RpcProductServiceTarget})),
},
}
cache.InitCache(cache.Config{
Source: svc.Pc4RpcService.Source,
})
svc.Cache = cache.GetCache()
return svc
}
type Pc4RpcService struct {
Product product.Product
Source source.Source
}
package main
import (
"context"
"flag"
"git.chillcy.com/golang/chillcy/pkg/etcd"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/client/category"
"git.chillcy.com/golang/colorway/internal/pkg/database"
"git.chillcy.com/golang/colorway/internal/pkg/database/model"
"github.com/zeromicro/go-zero/zrpc"
"log"
)
var _env = flag.String("env", "dev", "环境配置")
var _categoryService category.Category
var _databaseManager *database.Manager
func init() {
flag.Parse()
etcd.Init(etcd.Env(*_env))
pc4RpcProductServiceTarget := etcd.Get(etcd.KeyRpcPc4)
if etcd.GetEnv() == etcd.EnvDev {
pc4RpcProductServiceTarget = "127.0.0.1:5100"
}
_categoryService = category.NewCategory(zrpc.MustNewClient(zrpc.RpcClientConf{Target: pc4RpcProductServiceTarget}))
// todo dsn etcd
dsn := "root:root@tcp(127.0.0.1:3306)/colorway?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=true&loc=Local"
var err error
_databaseManager, err = database.NewManager(dsn)
if err != nil {
panic(err)
}
}
func main() {
log.Println("脚本开始")
defer log.Println("脚本结束")
categoryList, err := _categoryService.List(context.TODO(), &category.CategoryListRequest{})
if err != nil {
panic(err)
}
var categories model.Categories
for _, item := range categoryList.GetItems() {
categories = append(categories, model.Category{
Code: item.GetId(),
Name: item.GetLabel(),
ParentCode: item.GetParentId(),
})
}
err = _databaseManager.CategoryRepository.UpsertManyByCode(categories)
if err != nil {
panic(err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment