You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
739 lines
27 KiB
739 lines
27 KiB
package third
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.oa00.com/go/mysql"
|
|
"github.com/golangkit/formatime"
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
"path"
|
|
"recook/internal/libs/bean"
|
|
"recook/internal/v2/lib/supply"
|
|
"recook/internal/v2/model/recook/goods"
|
|
manage "recook/internal/v2/model/third"
|
|
"recook/tools"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var SupplyLogic = &supplyLogic{}
|
|
|
|
type supplyLogic struct {
|
|
}
|
|
|
|
type skuItem struct {
|
|
Id uint `json:"id"`
|
|
Name string `json:"name"`
|
|
ImgUrl string `json:"imgUrl"`
|
|
SupplyStatus uint `json:"supplyStatus"`
|
|
SupplySkuId uint `json:"supplySkuId"`
|
|
FirstCategoryName string `json:"firstCategoryName"`
|
|
SecondCategoryName string `json:"secondCategoryName"`
|
|
ThirdCategoryName string `json:"thirdCategoryName"`
|
|
BrandName string `json:"brandName"`
|
|
Price decimal.Decimal `json:"price"`
|
|
GuidePrice decimal.Decimal `json:"guidePrice"`
|
|
}
|
|
type SupplySearch struct {
|
|
Status uint
|
|
Name string
|
|
SupplySkuId uint
|
|
IsExport bool
|
|
}
|
|
|
|
// Lists @Title 商品列表
|
|
func (s *supplyLogic) Lists(search SupplySearch, page bean.Page) (lists []skuItem, total int64) {
|
|
var thirdPartySupplies []manage.RecookThirdPartySupply
|
|
where := mysql.Db
|
|
// 入库状态
|
|
switch search.Status {
|
|
case manage.RecookThirdPartySupplyStatusNone: // 未入库
|
|
where = where.Where("status = ?", manage.RecookThirdPartySupplyStatusNone)
|
|
case manage.RecookThirdPartySupplyStatusAdopt: // 已入库
|
|
where = where.Where("status = ?", manage.RecookThirdPartySupplyStatusAdopt)
|
|
}
|
|
// 商品名称
|
|
if search.Name != "" {
|
|
where = where.Where("name like ?", fmt.Sprintf("%%%s%%", search.Name))
|
|
}
|
|
// 供应商skuId
|
|
if search.SupplySkuId > 0 {
|
|
where = where.Where("supply_sku_id = ?", search.SupplySkuId)
|
|
}
|
|
if search.IsExport {
|
|
mysql.Db.Where(where).Order("id desc").Find(&thirdPartySupplies)
|
|
} else {
|
|
mysql.Db.Model(thirdPartySupplies).Where(where).Count(&total)
|
|
if page.HasPage(total) {
|
|
mysql.Db.Where(where).Offset(page.GetStart()).Limit(page.GetLimit()).Order("id desc").Find(&thirdPartySupplies)
|
|
}
|
|
}
|
|
for _, thirdPartySupply := range thirdPartySupplies {
|
|
lists = append(lists, skuItem{
|
|
Id: thirdPartySupply.Id,
|
|
Name: thirdPartySupply.Name,
|
|
ImgUrl: thirdPartySupply.ImgUrl,
|
|
SupplyStatus: thirdPartySupply.SupplyStatus,
|
|
SupplySkuId: thirdPartySupply.SupplySkuId,
|
|
FirstCategoryName: thirdPartySupply.FirstCategoryName,
|
|
SecondCategoryName: thirdPartySupply.SecondCategoryName,
|
|
ThirdCategoryName: thirdPartySupply.ThirdCategoryName,
|
|
BrandName: thirdPartySupply.BrandName,
|
|
Price: thirdPartySupply.Price,
|
|
GuidePrice: thirdPartySupply.GuidePrice,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
type supplySkuDetail struct {
|
|
Id uint `json:"id"`
|
|
Name string `json:"name"`
|
|
ImgUrl string `json:"imgUrl"`
|
|
Status uint `json:"status"`
|
|
SupplyStatus uint `json:"supplyStatus"`
|
|
SupplySkuId uint `json:"supplySkuId"`
|
|
FirstCategoryName string `json:"firstCategoryName"`
|
|
SecondCategoryName string `json:"secondCategoryName"`
|
|
ThirdCategoryName string `json:"thirdCategoryName"`
|
|
BrandName string `json:"brandName"`
|
|
Price decimal.Decimal `json:"price"`
|
|
GuidePrice decimal.Decimal `json:"guidePrice"`
|
|
CategoryId uint `json:"categoryId"`
|
|
CategoryName string `json:"categoryName"`
|
|
CategoryParentName string `json:"categoryParentName"`
|
|
Color string `json:"color"`
|
|
Size string `json:"size"`
|
|
UpcCode string `json:"upcCode"`
|
|
Imgs []string `json:"imgs"`
|
|
Content string `json:"content"`
|
|
Unit string `json:"unit"`
|
|
TaxCode string `json:"taxCode"`
|
|
TaxName string `json:"taxName"`
|
|
Tax string `json:"tax"`
|
|
}
|
|
|
|
// Detail @Title 商品详情
|
|
func (s *supplyLogic) Detail(supplySkuId uint) (result supplySkuDetail, err error) {
|
|
thirdPartySupply := manage.RecookThirdPartySupply{}
|
|
if mysql.Db.Preload("ThirdPartySupplyCategory.Category.Parent").
|
|
Preload("Imgs").
|
|
Where("supply_sku_id = ?", supplySkuId).First(&thirdPartySupply).Error != nil {
|
|
return result, errors.New("商品错误")
|
|
}
|
|
result = supplySkuDetail{
|
|
Id: thirdPartySupply.Id,
|
|
Name: thirdPartySupply.Name,
|
|
ImgUrl: thirdPartySupply.ImgUrl,
|
|
Status: thirdPartySupply.Status,
|
|
SupplyStatus: thirdPartySupply.SupplyStatus,
|
|
SupplySkuId: thirdPartySupply.SupplySkuId,
|
|
FirstCategoryName: thirdPartySupply.FirstCategoryName,
|
|
SecondCategoryName: thirdPartySupply.SecondCategoryName,
|
|
ThirdCategoryName: thirdPartySupply.ThirdCategoryName,
|
|
CategoryId: thirdPartySupply.ThirdPartySupplyCategory.CategoryId,
|
|
BrandName: thirdPartySupply.BrandName,
|
|
Price: thirdPartySupply.Price,
|
|
GuidePrice: thirdPartySupply.GuidePrice,
|
|
Color: thirdPartySupply.Color,
|
|
Size: thirdPartySupply.Size,
|
|
UpcCode: thirdPartySupply.UpcCode,
|
|
Content: thirdPartySupply.Content,
|
|
Unit: thirdPartySupply.Unit,
|
|
TaxCode: thirdPartySupply.TaxCode,
|
|
TaxName: thirdPartySupply.TaxName,
|
|
Tax: thirdPartySupply.Tax,
|
|
}
|
|
if thirdPartySupply.ThirdPartySupplyCategory.CategoryId > 0 {
|
|
result.CategoryName = thirdPartySupply.ThirdPartySupplyCategory.Category.Name
|
|
result.CategoryParentName = thirdPartySupply.ThirdPartySupplyCategory.Category.Parent.Name
|
|
}
|
|
for _, thirdPartySupplyImg := range thirdPartySupply.Imgs {
|
|
result.Imgs = append(result.Imgs, thirdPartySupplyImg.Path)
|
|
}
|
|
return
|
|
}
|
|
|
|
type AdoptSkuInfo struct {
|
|
SupplySkuId uint
|
|
DiscountPrice decimal.Decimal // 售价
|
|
Coupon decimal.Decimal // 商品券
|
|
}
|
|
|
|
// Adopt @Title 商品入库
|
|
func (s *supplyLogic) Adopt(data AdoptSkuInfo) error {
|
|
thirdPartySupply := manage.RecookThirdPartySupply{}
|
|
if mysql.Db.Preload("ThirdPartySupplyCategory.Category").
|
|
Preload("Imgs").
|
|
Where("supply_sku_id = ?", data.SupplySkuId).First(&thirdPartySupply).Error != nil {
|
|
return errors.New("商品错误")
|
|
}
|
|
if thirdPartySupply.GoodsId > 0 {
|
|
return errors.New("商品已入库")
|
|
}
|
|
groups, err := supply.Api.Sku.Groups([]uint{data.SupplySkuId})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var recookGoodsSkuModels []goods.RecookGoodsSkuModel
|
|
mysql.Db.Where("third_party_type = ? and third_party_sku_id in ?", goods.RecookGoodsInfoThirdPartyTypeSupply, groups[0].GroupSkuIds).Find(&recookGoodsSkuModels)
|
|
|
|
if len(recookGoodsSkuModels) > 0 {
|
|
// 已添加过商品
|
|
return mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
// 详情
|
|
if thirdPartySupply.Content != "" {
|
|
if tx.Model(&goods.RecookGoodsContentModel{}).Where("goods_id = ?", recookGoodsSkuModels[0].GoodsId).Update("content", thirdPartySupply.Content).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
}
|
|
// 商品图片
|
|
var mainPhotoModels []goods.RecookGoodsMainPhotoModel
|
|
for key, skuImg := range thirdPartySupply.Imgs {
|
|
isMaster := 0
|
|
if key == 0 {
|
|
isMaster = 1
|
|
}
|
|
mainPhotoModels = append(mainPhotoModels, goods.RecookGoodsMainPhotoModel{
|
|
GoodsId: recookGoodsSkuModels[0].GoodsId,
|
|
Url: skuImg.Path,
|
|
Name: path.Base(skuImg.Path),
|
|
IsMaster: isMaster,
|
|
OrderNo: key,
|
|
Width: 800,
|
|
Height: 800,
|
|
})
|
|
}
|
|
if len(mainPhotoModels) > 0 {
|
|
if tx.Where("goods_id = ?", recookGoodsSkuModels[0].GoodsId).Delete(&goods.RecookGoodsMainPhotoModel{}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
if tx.Create(&mainPhotoModels).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
|
|
// 多规格处理
|
|
var goodsAttributeModels, recookGoodsAttributeModel []goods.RecookGoodsAttributeModel
|
|
mysql.Db.Where("goods_id = ?", recookGoodsSkuModels[0].GoodsId).Find(&recookGoodsAttributeModel)
|
|
|
|
if thirdPartySupply.Color != "" {
|
|
attItem := goods.RecookGoodsAttributeModel{
|
|
GoodsId: recookGoodsSkuModels[0].GoodsId,
|
|
Name: "规格",
|
|
Value: thirdPartySupply.Color,
|
|
}
|
|
for _, item := range recookGoodsAttributeModel {
|
|
if item.Name == "规格" && item.Value == thirdPartySupply.Color {
|
|
attItem = item
|
|
break
|
|
}
|
|
}
|
|
if attItem.Id == 0 {
|
|
if tx.Create(&attItem).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
}
|
|
goodsAttributeModels = append(goodsAttributeModels, attItem)
|
|
}
|
|
if thirdPartySupply.Size != "" {
|
|
attItem := goods.RecookGoodsAttributeModel{
|
|
GoodsId: recookGoodsSkuModels[0].GoodsId,
|
|
Name: "尺寸",
|
|
Value: thirdPartySupply.Size,
|
|
}
|
|
for _, item := range recookGoodsAttributeModel {
|
|
if item.Name == "规格" && item.Value == thirdPartySupply.Size {
|
|
attItem = item
|
|
break
|
|
}
|
|
}
|
|
if attItem.Id == 0 {
|
|
if tx.Create(&attItem).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
}
|
|
goodsAttributeModels = append(goodsAttributeModels, attItem)
|
|
}
|
|
if len(goodsAttributeModels) == 0 {
|
|
attItem := goods.RecookGoodsAttributeModel{
|
|
GoodsId: recookGoodsSkuModels[0].GoodsId,
|
|
Name: "规格",
|
|
Value: "标准",
|
|
}
|
|
if attItem.Id == 0 {
|
|
if tx.Create(&attItem).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
}
|
|
goodsAttributeModels = append(goodsAttributeModels, attItem)
|
|
}
|
|
sort.Slice(goodsAttributeModels, func(i, j int) bool {
|
|
return goodsAttributeModels[i].Id < goodsAttributeModels[j].Id
|
|
})
|
|
var combineId []string
|
|
var skuName []string
|
|
for _, goodsAttributeModel := range goodsAttributeModels {
|
|
combineId = append(combineId, strconv.Itoa(int(goodsAttributeModel.Id)))
|
|
skuName = append(skuName, goodsAttributeModel.Value)
|
|
}
|
|
|
|
inventory := 0
|
|
if thirdPartySupply.Status == manage.RecookThirdPartySupplySupplyStatusUp {
|
|
inventory = 500
|
|
}
|
|
// sku信息处理
|
|
sku := goods.RecookGoodsSkuModel{
|
|
GoodsId: recookGoodsSkuModels[0].GoodsId,
|
|
Name: strings.Join(skuName, "+"),
|
|
CombineId: strings.Join(combineId, ","),
|
|
PicURL: thirdPartySupply.ImgUrl,
|
|
Code: thirdPartySupply.UpcCode,
|
|
PurchasePrice: thirdPartySupply.Price,
|
|
OriginalPrice: data.DiscountPrice.Add(data.Coupon),
|
|
DiscountPrice: data.DiscountPrice,
|
|
Coupon: data.Coupon,
|
|
ControlPrice: thirdPartySupply.GuidePrice,
|
|
Inventory: uint(inventory),
|
|
ThirdPartySkuId: fmt.Sprintf("%d", thirdPartySupply.SupplySkuId),
|
|
ThirdPartyType: goods.RecookGoodsInfoThirdPartyTypeSupply,
|
|
}
|
|
// 税率
|
|
tax, err := decimal.NewFromString(thirdPartySupply.Tax)
|
|
if err == nil {
|
|
// 发票税率
|
|
sku.Invoice = goods.RecookSkuInvoiceModel{
|
|
GoodsId: recookGoodsSkuModels[0].GoodsId,
|
|
GoodsName: thirdPartySupply.Name,
|
|
TaxSn: thirdPartySupply.TaxCode,
|
|
TaxName: thirdPartySupply.TaxName,
|
|
Unit: thirdPartySupply.Unit,
|
|
TaxRate: tax,
|
|
PlatformRate: tax,
|
|
DeductionRate: tax,
|
|
}
|
|
}
|
|
if tx.Create(&sku).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
// 原数据处理
|
|
if tx.Model(&manage.RecookThirdPartySupply{}).Where("id = ?", thirdPartySupply.Id).Updates(map[string]interface{}{
|
|
"status": manage.RecookThirdPartySupplyStatusAdopt,
|
|
"goods_id": recookGoodsSkuModels[0].GoodsId,
|
|
}).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
return nil
|
|
})
|
|
} else {
|
|
// 未添加过商品
|
|
if thirdPartySupply.ThirdPartySupplyCategory.CategoryId == 0 {
|
|
return errors.New("分类未匹配")
|
|
}
|
|
return mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
goodsBrandModel := goods.RecookGoodsBrandModel{
|
|
Name: thirdPartySupply.BrandName,
|
|
}
|
|
if tx.Where(&goodsBrandModel).FirstOrCreate(&goodsBrandModel).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
recookGoodsInfoModel := goods.RecookGoodsInfoModel{
|
|
BrandID: goodsBrandModel.Id,
|
|
VendorID: manage.RecookThirdPartySupplyVendorId,
|
|
GoodsName: thirdPartySupply.Name,
|
|
FirstCategoryID: thirdPartySupply.ThirdPartySupplyCategory.Category.ParentId,
|
|
SecondCategoryID: thirdPartySupply.ThirdPartySupplyCategory.CategoryId,
|
|
Hash: tools.GenerateGoodsHashSign(),
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
UpdatedAt: formatime.NewSecondNow(),
|
|
ThirdPartyType: goods.RecookGoodsInfoThirdPartyTypeSupply,
|
|
}
|
|
if tx.Create(&recookGoodsInfoModel).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
|
|
// 商品详情
|
|
recookGoodsContentModel := goods.RecookGoodsContentModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
Content: thirdPartySupply.Content,
|
|
}
|
|
if tx.Create(&recookGoodsContentModel).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
// 商品图片
|
|
var mainPhotoModels []goods.RecookGoodsMainPhotoModel
|
|
for key, skuImg := range thirdPartySupply.Imgs {
|
|
isMaster := 0
|
|
if key == 0 {
|
|
isMaster = 1
|
|
}
|
|
mainPhotoModels = append(mainPhotoModels, goods.RecookGoodsMainPhotoModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
Url: skuImg.Path,
|
|
Name: path.Base(skuImg.Path),
|
|
IsMaster: isMaster,
|
|
OrderNo: key,
|
|
Width: 800,
|
|
Height: 800,
|
|
})
|
|
}
|
|
if len(mainPhotoModels) > 0 {
|
|
if tx.Create(&mainPhotoModels).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
|
|
// 多规格处理
|
|
var goodsAttributeModels []goods.RecookGoodsAttributeModel
|
|
if thirdPartySupply.Color != "" {
|
|
goodsAttributeModels = append(goodsAttributeModels, goods.RecookGoodsAttributeModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
Name: "规格",
|
|
Value: thirdPartySupply.Color,
|
|
})
|
|
}
|
|
if thirdPartySupply.Size != "" {
|
|
goodsAttributeModels = append(goodsAttributeModels, goods.RecookGoodsAttributeModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
Name: "尺寸",
|
|
Value: thirdPartySupply.Size,
|
|
})
|
|
}
|
|
if len(goodsAttributeModels) == 0 {
|
|
goodsAttributeModels = append(goodsAttributeModels, goods.RecookGoodsAttributeModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
Name: "规格",
|
|
Value: "标准",
|
|
})
|
|
}
|
|
if tx.Create(&goodsAttributeModels).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
var combineId []string
|
|
var skuName []string
|
|
for _, goodsAttributeModel := range goodsAttributeModels {
|
|
combineId = append(combineId, strconv.Itoa(int(goodsAttributeModel.Id)))
|
|
skuName = append(skuName, goodsAttributeModel.Value)
|
|
}
|
|
|
|
inventory := 0
|
|
if thirdPartySupply.Status == manage.RecookThirdPartySupplySupplyStatusUp {
|
|
inventory = 500
|
|
}
|
|
// sku信息处理
|
|
sku := goods.RecookGoodsSkuModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
Name: strings.Join(skuName, "+"),
|
|
CombineId: strings.Join(combineId, ","),
|
|
PicURL: thirdPartySupply.ImgUrl,
|
|
Code: thirdPartySupply.UpcCode,
|
|
PurchasePrice: thirdPartySupply.Price,
|
|
ControlPrice: thirdPartySupply.GuidePrice,
|
|
OriginalPrice: data.DiscountPrice.Add(data.Coupon),
|
|
DiscountPrice: data.DiscountPrice,
|
|
Coupon: data.Coupon,
|
|
Inventory: uint(inventory),
|
|
ThirdPartySkuId: fmt.Sprintf("%d", thirdPartySupply.SupplySkuId),
|
|
ThirdPartyType: goods.RecookGoodsInfoThirdPartyTypeSupply,
|
|
}
|
|
// 税率
|
|
tax, err := decimal.NewFromString(thirdPartySupply.Tax)
|
|
if err == nil {
|
|
// 发票税率
|
|
sku.Invoice = goods.RecookSkuInvoiceModel{
|
|
GoodsId: recookGoodsInfoModel.Id,
|
|
GoodsName: thirdPartySupply.Name,
|
|
TaxSn: thirdPartySupply.TaxCode,
|
|
TaxName: thirdPartySupply.TaxName,
|
|
Unit: thirdPartySupply.Unit,
|
|
TaxRate: tax,
|
|
PlatformRate: tax,
|
|
DeductionRate: tax,
|
|
}
|
|
}
|
|
if tx.Create(&sku).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
// 原数据处理
|
|
if tx.Model(&manage.RecookThirdPartySupply{}).Where("id = ?", thirdPartySupply.Id).Updates(map[string]interface{}{
|
|
"status": manage.RecookThirdPartySupplyStatusAdopt,
|
|
"goods_id": recookGoodsInfoModel.Id,
|
|
}).Error != nil {
|
|
return errors.New("入库失败")
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
}
|
|
|
|
// SyncPrice @Title 同步价格
|
|
func (s *supplyLogic) SyncPrice(skuIds []uint) error {
|
|
prices, err := supply.Api.Sku.Prices(skuIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
mSkuPrice := map[uint]supply.SkuPrice{}
|
|
for _, price := range prices {
|
|
mSkuPrice[price.Id] = price
|
|
}
|
|
var thirdPartySupplies []manage.RecookThirdPartySupply
|
|
mysql.Db.Where("supply_sku_id in ?", skuIds).Find(&thirdPartySupplies)
|
|
for _, partySupply := range thirdPartySupplies {
|
|
mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if tx.Model(&partySupply).Updates(map[string]interface{}{
|
|
"price": partySupply.Price,
|
|
"guide_price": partySupply.GuidePrice,
|
|
"supply_status": partySupply.Status,
|
|
}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
// 入库商品同步价格
|
|
if partySupply.Status == manage.RecookThirdPartySupplyStatusAdopt {
|
|
// 上游上下架修改库存
|
|
inventory := 0
|
|
if partySupply.Status == manage.RecookThirdPartySupplySupplyStatusUp {
|
|
inventory = 500
|
|
}
|
|
if tx.Model(&goods.RecookGoodsSkuModel{}).Where("third_party_type = ? and third_party_sku_id = ?", goods.RecookGoodsInfoThirdPartyTypeSupply, partySupply.SupplySkuId).Updates(map[string]interface{}{
|
|
"purchase_price": partySupply.Price,
|
|
"inventory": inventory,
|
|
}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SyncData @Title 同步商品信息
|
|
func (s *supplyLogic) SyncData(skuIds []uint) error {
|
|
details, err := supply.Api.Sku.Details(skuIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var thirdPartySupplies []manage.RecookThirdPartySupply
|
|
|
|
mysql.Db.Where("supply_sku_id in ?", skuIds).Find(&thirdPartySupplies)
|
|
mThirdPartySupply := map[uint]manage.RecookThirdPartySupply{}
|
|
for _, partySupply := range thirdPartySupplies {
|
|
mThirdPartySupply[partySupply.SupplySkuId] = partySupply
|
|
}
|
|
for _, skuInfo := range details {
|
|
if partySupply, ok := mThirdPartySupply[skuInfo.Id]; ok {
|
|
// 更新商品
|
|
mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if tx.Model(&partySupply).Updates(map[string]interface{}{
|
|
"name": partySupply.Name,
|
|
"brand_id": partySupply.BrandId,
|
|
"brand_name": partySupply.BrandName,
|
|
"first_category_id": partySupply.FirstCategoryId,
|
|
"first_category_name": partySupply.FirstCategoryName,
|
|
"second_category_id": partySupply.SecondCategoryId,
|
|
"second_category_name": partySupply.SecondCategoryName,
|
|
"third_category_id": partySupply.ThirdCategoryId,
|
|
"third_category_name": partySupply.ThirdCategoryName,
|
|
"price": partySupply.Price,
|
|
"guide_price": partySupply.GuidePrice,
|
|
"img_url": partySupply.ImgUrl,
|
|
"profit": partySupply.Profit,
|
|
"size": partySupply.Size,
|
|
"color": partySupply.Color,
|
|
"tax": partySupply.Tax,
|
|
"tax_code": partySupply.TaxCode,
|
|
"tax_name": partySupply.TaxName,
|
|
"unit": partySupply.Unit,
|
|
"upc_code": partySupply.UpcCode,
|
|
"content": partySupply.Content,
|
|
"supply_status": partySupply.Status,
|
|
}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
|
|
var thirdPartySupplyImgs []manage.RecookThirdPartySupplyImg
|
|
for _, skuImg := range skuInfo.Imgs {
|
|
thirdPartySupplyImgs = append(thirdPartySupplyImgs, manage.RecookThirdPartySupplyImg{
|
|
SupplySkuId: skuInfo.Id,
|
|
Path: skuImg.Path,
|
|
})
|
|
}
|
|
if len(thirdPartySupplyImgs) > 0 {
|
|
if tx.Where("supply_sku_id = ?", partySupply.SupplySkuId).Delete(&manage.RecookThirdPartySupplyImg{}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
if tx.Create(&thirdPartySupplyImgs).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
var thirdPartySupplySpecifications []manage.RecookThirdPartySupplySpecification
|
|
for _, specification := range skuInfo.Specifications {
|
|
for _, attribute := range specification.Attributes {
|
|
thirdPartySupplySpecifications = append(thirdPartySupplySpecifications, manage.RecookThirdPartySupplySpecification{
|
|
SupplySkuId: skuInfo.Id,
|
|
Name: attribute.Name,
|
|
Value: strings.Join(attribute.Value, ";"),
|
|
GroupName: specification.Name,
|
|
})
|
|
}
|
|
}
|
|
if len(thirdPartySupplySpecifications) > 0 {
|
|
if tx.Where("supply_sku_id = ?", partySupply.SupplySkuId).Delete(&manage.RecookThirdPartySupplySpecification{}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
if tx.Create(&thirdPartySupplySpecifications).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
|
|
// 入库商品同步信息
|
|
if partySupply.Status == manage.RecookThirdPartySupplyStatusAdopt {
|
|
// 商品信息同步
|
|
// 详情
|
|
if skuInfo.Content != "" {
|
|
if tx.Model(&goods.RecookGoodsContentModel{}).Where("goods_id = ?", partySupply.GoodsId).Update("content", skuInfo.Content).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
// 商品图片
|
|
var mainPhotoModels []goods.RecookGoodsMainPhotoModel
|
|
for key, skuImg := range skuInfo.Imgs {
|
|
isMaster := 0
|
|
if key == 0 {
|
|
isMaster = 1
|
|
}
|
|
mainPhotoModels = append(mainPhotoModels, goods.RecookGoodsMainPhotoModel{
|
|
GoodsId: partySupply.GoodsId,
|
|
Url: skuImg.Path,
|
|
Name: path.Base(skuImg.Path),
|
|
IsMaster: isMaster,
|
|
OrderNo: key,
|
|
Width: 800,
|
|
Height: 800,
|
|
})
|
|
}
|
|
if len(mainPhotoModels) > 0 {
|
|
if tx.Where("goods_id = ?", partySupply.GoodsId).Delete(&goods.RecookGoodsMainPhotoModel{}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
if tx.Create(&mainPhotoModels).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
// 税率
|
|
tax, err := decimal.NewFromString(skuInfo.Tax)
|
|
if err == nil {
|
|
// 获取sku信息
|
|
recookGoodsSkuModel := goods.RecookGoodsSkuModel{}
|
|
mysql.Db.Where("third_party_type = ? and third_party_sku_id = ?", goods.RecookGoodsInfoThirdPartyTypeSupply, partySupply.SupplySkuId).First(&recookGoodsSkuModel)
|
|
if recookGoodsSkuModel.Id > 0 {
|
|
// 发票税率
|
|
skuInvoiceModel := goods.RecookSkuInvoiceModel{}
|
|
mysql.Db.Where("sku_id = ?", recookGoodsSkuModel.Id).First(&skuInvoiceModel)
|
|
if skuInvoiceModel.Id > 0 {
|
|
if tx.Model(&skuInvoiceModel).Updates(map[string]interface{}{
|
|
"goods_name": skuInfo.Name,
|
|
"tax_sn": skuInfo.TaxCode,
|
|
"tax_name": skuInfo.TaxName,
|
|
"unit": skuInfo.Unit,
|
|
"tax_rate": tax,
|
|
"platform_rate": tax,
|
|
"deduction_rate": tax,
|
|
}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
} else {
|
|
skuInvoiceModel = goods.RecookSkuInvoiceModel{
|
|
SkuId: recookGoodsSkuModel.Id,
|
|
GoodsId: recookGoodsSkuModel.GoodsId,
|
|
GoodsName: skuInfo.Name,
|
|
TaxSn: skuInfo.TaxCode,
|
|
TaxName: skuInfo.TaxName,
|
|
Unit: skuInfo.Unit,
|
|
TaxRate: tax,
|
|
PlatformRate: tax,
|
|
DeductionRate: tax,
|
|
}
|
|
if tx.Create(&skuInvoiceModel).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 上游上下架修改库存
|
|
inventory := 0
|
|
if partySupply.Status == manage.RecookThirdPartySupplySupplyStatusUp {
|
|
inventory = 500
|
|
}
|
|
if tx.Model(&goods.RecookGoodsSkuModel{}).Where("third_party_type = ? and third_party_sku_id = ?", goods.RecookGoodsInfoThirdPartyTypeSupply, partySupply.SupplySkuId).Updates(map[string]interface{}{
|
|
"purchase_price": partySupply.Price,
|
|
"inventory": inventory,
|
|
"pic_url": skuInfo.ImgUrl,
|
|
"code": skuInfo.UpcCode,
|
|
}).Error != nil {
|
|
return errors.New("更新失败")
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
} else {
|
|
// 分类处理
|
|
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,
|
|
})
|
|
}
|
|
}
|
|
mysql.Db.Create(&thirdPartySupply)
|
|
}
|
|
}
|
|
return nil
|
|
}
|