2022-03-25 07:03:17 +00:00
|
|
|
package appier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/redisdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SyncToService ...
|
|
|
|
type SyncToService struct{}
|
|
|
|
|
|
|
|
const limitGetKeyRedis int64 = 20
|
|
|
|
|
|
|
|
var listTypeSync = []string{
|
|
|
|
RedisSyncProduct,
|
|
|
|
RedisSyncSupplier,
|
|
|
|
RedisSyncInventory,
|
|
|
|
RedisSyncBrand,
|
|
|
|
RedisSyncCategory,
|
|
|
|
RedisSyncSubCategory,
|
|
|
|
RedisSyncProperty,
|
|
|
|
RedisSyncPropertyValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncData ...
|
|
|
|
func (s SyncToService) syncData() {
|
|
|
|
for _, item := range listTypeSync {
|
|
|
|
keyProductPattern := getRedisPrefixPattern(item)
|
|
|
|
handleSyncDataToServiceAppier(keyProductPattern, item)
|
|
|
|
}
|
|
|
|
fmt.Println("Sync data to service done!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleSyncDataToServiceAppier ...
|
|
|
|
func handleSyncDataToServiceAppier(pattern string, typeData string) {
|
|
|
|
for {
|
2022-03-25 08:15:51 +00:00
|
|
|
keys, values := redisdb.GetWithPrefixPattern(pattern)
|
2022-03-25 07:03:17 +00:00
|
|
|
if len(keys) == 0 {
|
2022-03-25 08:27:24 +00:00
|
|
|
fmt.Println("No keys data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) == 0 {
|
|
|
|
fmt.Println("No values DATA")
|
2022-03-25 07:03:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-25 08:15:51 +00:00
|
|
|
processSyncToServiceAppier(typeData, values)
|
2022-03-25 07:03:17 +00:00
|
|
|
|
2022-03-25 08:27:24 +00:00
|
|
|
//// Del keys
|
|
|
|
//for _, key := range keys {
|
|
|
|
// redisdb.DelKey(context.Background(), key)
|
|
|
|
//}
|
2022-03-25 07:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 08:15:51 +00:00
|
|
|
|
|
|
|
// processSyncToServiceAppier ...
|
|
|
|
func processSyncToServiceAppier(typeData string, values []string) {
|
2022-03-25 08:27:24 +00:00
|
|
|
fmt.Println("Sends")
|
2022-03-25 08:15:51 +00:00
|
|
|
for {
|
|
|
|
if len(values) <= 20 {
|
|
|
|
syncDataByList(typeData, values)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var list = values[0:19]
|
|
|
|
syncDataByList(typeData, list)
|
|
|
|
values = values[20:]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncDataByList ...
|
|
|
|
func syncDataByList(typeData string, data []string) {
|
|
|
|
payload := toBytes(data)
|
|
|
|
// Convert data
|
|
|
|
switch typeData {
|
|
|
|
case RedisSyncProduct:
|
|
|
|
Pull{}.ProductUpsert(payload)
|
|
|
|
case RedisSyncSKU:
|
|
|
|
Pull{}.SKUUpsert(payload)
|
|
|
|
case RedisSyncCategory:
|
|
|
|
Pull{}.CategoryUpsert(payload)
|
|
|
|
case RedisSyncSubCategory:
|
|
|
|
Pull{}.SubCategoryUpsert(payload)
|
|
|
|
case RedisSyncInventory:
|
|
|
|
Pull{}.InventoryUpsert(payload)
|
|
|
|
case RedisSyncBrand:
|
|
|
|
Pull{}.BrandUpsert(payload)
|
|
|
|
case RedisSyncSupplier:
|
|
|
|
Pull{}.SupplierUpsert(payload)
|
|
|
|
case RedisSyncProperty:
|
|
|
|
Pull{}.PropertyUpsert(payload)
|
|
|
|
case RedisSyncPropertyValue:
|
|
|
|
Pull{}.PropertyValueUpsert(payload)
|
|
|
|
}
|
|
|
|
}
|