package task import ( "git.oa00.com/go/mysql" "github.com/gin-gonic/gin" "github.com/shopspring/decimal" "log" "recook/internal/v2/lib/supply" manage "recook/internal/v2/model/third" "strings" ) type Supply struct { } // SyncAll @Title 同步所有数据 func (s *Supply) SyncAll(c *gin.Context) { go s.Sync() c.String(200, "suc") } // Sync @Title 同步数据 func (s *Supply) Sync() { scrollId := "" for true { lists, err := supply.Api.Sku.Lists(supply.ArgsSkuLists{ ScrollId: scrollId, Status: 1, PageSize: 100, }) if err != nil { log.Println(err) return } if len(lists.List) == 0 { return } scrollId = lists.ScrollId var skuIds []uint for _, skuItem := range lists.List { skuIds = append(skuIds, skuItem.Id) } log.Println(lists, err) details, err := supply.Api.Sku.Details(skuIds) if err != nil { log.Println(err) return } var thirdPartySupplies, supplies []manage.RecookThirdPartySupply mysql.Db.Where("supply_sku_id in ?", skuIds).Find(&supplies) mSupply := map[uint]bool{} for _, partySupply := range supplies { mSupply[partySupply.SupplySkuId] = true } for _, skuInfo := range details { if mSupply[skuInfo.Id] { continue } // 分类处理 category := manage.RecookThirdPartySupplyCategory{ FirstCategoryName: skuInfo.FirstCategoryName, SecondCategoryName: skuInfo.SecondCategoryName, ThirdCategoryName: skuInfo.ThirdCategoryName, } mysql.Db.Where(&category).FirstOrCreate(&category) thirdPartySupply := manage.RecookThirdPartySupply{ SupplySkuId: skuInfo.Id, Name: skuInfo.Name, BrandId: skuInfo.BrandId, BrandName: skuInfo.BrandName, ThirdPartySupplyCategoryId: category.Id, FirstCategoryId: skuInfo.FirstCategoryId, FirstCategoryName: skuInfo.FirstCategoryName, SecondCategoryId: skuInfo.SecondCategoryId, SecondCategoryName: skuInfo.SecondCategoryName, ThirdCategoryId: skuInfo.ThirdCategoryId, ThirdCategoryName: skuInfo.ThirdCategoryName, Price: decimal.NewFromFloat(skuInfo.Price), GuidePrice: decimal.NewFromFloat(skuInfo.GuidePrice), ImgUrl: skuInfo.ImgUrl, Profit: decimal.NewFromFloat(skuInfo.Profit), Size: skuInfo.Size, Color: skuInfo.Color, Tax: skuInfo.Tax, Unit: skuInfo.Unit, UpcCode: skuInfo.UpcCode, TaxName: skuInfo.TaxName, TaxCode: skuInfo.TaxCode, Content: skuInfo.Content, Status: manage.RecookThirdPartySupplyStatusNone, SupplyStatus: skuInfo.Status, } for _, skuImg := range skuInfo.Imgs { thirdPartySupply.Imgs = append(thirdPartySupply.Imgs, manage.RecookThirdPartySupplyImg{ SupplySkuId: skuInfo.Id, Path: skuImg.Path, }) } for _, specification := range skuInfo.Specifications { for _, attribute := range specification.Attributes { thirdPartySupply.Specifications = append(thirdPartySupply.Specifications, manage.RecookThirdPartySupplySpecification{ SupplySkuId: skuInfo.Id, Name: attribute.Name, Value: strings.Join(attribute.Value, ";"), GroupName: specification.Name, }) } } thirdPartySupplies = append(thirdPartySupplies, thirdPartySupply) } if len(thirdPartySupplies) > 0 { mysql.Db.Create(&thirdPartySupplies) } } }