package creatGoodsUtil import ( "fmt" "log" "time" _type "xianyv/type" "xianyv/utils/requestUtil" ) func XianYvCreat(body _type.Body, requestPath string, appId int, appSecret, domain string, flag bool) ([]byte, error) { funcStart := time.Now() type CreateProductRequest struct { ProductData []_type.BashCreat `json:"product_data"` } var allItems []_type.BashCreat for i := 0; i < len(body.BookData); i++ { var item _type.BashCreat var catId string if body.BookData[i].CatIds != "" { catId = body.BookData[i].CatIds } else { catId = body.CatIds } // 赋值item item.ItemKey = body.ItemKey item.ChannelCatId = catId item.Price = body.BookData[i].Prices[0] item.OriginalPrice = body.BookData[i].Prices[1] item.Stock = body.BookData[i].Stock item.ItemBizType = body.BookData[i].ItemBizType item.SpBizType = body.BookData[i].SpBizType item.OuterId = body.OuterId fmt.Printf("body: %v \n", body) fmt.Printf("body.OuterId: %s \n", body.OuterId) fmt.Printf("item.OuterId: %+v \n", item.OuterId) if flag { item.ItemBizType = 2 item.SpBizType = 99 } var publishShops []_type.PublishShop // 循环所有的Shop来赋值PublishShop for j := 0; j < len(body.Shop); j++ { var province, city, district int32 if body.Shop[j].Province == 0 { province = body.Province } else { province = body.Shop[j].Province } if body.Shop[j].City == 0 { city = body.City } else { city = body.Shop[j].City } if body.Shop[j].District == 0 { district = body.District } else { district = body.Shop[j].District } publishShop := _type.PublishShop{ Images: append(body.Shop[j].MainImgs, body.Shop[j].ContentImgs...), UserName: body.Shop[j].UserName, Province: province, City: city, District: district, Title: body.Shop[j].Title, Content: body.Shop[j].Content, } publishShops = append(publishShops, publishShop) } item.PublishShop = publishShops item.BookData = _type.BookData{ ISBN: body.BookData[i].ISBN, Title: body.BookData[i].Title, Author: body.BookData[i].Author, Publisher: body.BookData[i].Publisher, } if err := item.Validate(); err != nil { log.Println("商品信息有误: ", err) return nil, err } allItems = append(allItems, item) } log.Printf("build allItems elapsed=%v", time.Since(funcStart)) if len(allItems) == 0 { return nil, fmt.Errorf("没有有效的商品可以创建") } request := CreateProductRequest{ ProductData: allItems, } fmt.Printf("正在创建 %d 个商品...\n", len(allItems)) fmt.Printf("请求数据: %v\n", request) createPath := requestPath reqStart := time.Now() createResponse, err := requestUtil.MakeAPIRequest(appId, appSecret, domain, createPath, request) log.Printf("MakeAPIRequest elapsed=%v", time.Since(reqStart)) if err != nil { log.Println("创建商品失败: ", err) return nil, err } // 打印结果 fmt.Println("创建商品响应:") fmt.Println(string(createResponse)) log.Printf("XianYvCreat total elapsed=%v", time.Since(funcStart)) return createResponse, nil } // 创建:有Isbn增加bookdata参数 func XianYvCreatNew(body _type.BodyNew, requestPath string, appId int, appSecret, domain string, flag bool) ([]byte, error) { funcStart := time.Now() type CreateProductRequest struct { ProductData []_type.BashCreat `json:"product_data"` } var allItems []_type.BashCreat var item _type.BashCreat var createResponse []byte // 赋值itemNoIsbn item.ItemKey = body.ItemKey item.ChannelCatId = body.CatIds item.Price = body.Price item.OriginalPrice = body.OriginalPrice item.Stock = body.Stock item.ItemBizType = body.ItemBizType item.SpBizType = body.SpBizType item.OuterId = body.OuterId fmt.Printf("body: %v \n", body) fmt.Printf("body.OuterId: %s \n", body.OuterId) fmt.Printf("item.OuterId: %+v \n", item.OuterId) if flag { item.ItemBizType = 2 item.SpBizType = 99 } var publishShops []_type.PublishShop // 循环所有的Shop来赋值PublishShop for j := 0; j < len(body.Shop); j++ { var province, city, district int32 if body.Shop[j].Province == 0 { province = body.Province } else { province = body.Shop[j].Province } if body.Shop[j].City == 0 { city = body.City } else { city = body.Shop[j].City } if body.Shop[j].District == 0 { district = body.District } else { district = body.Shop[j].District } publishShop := _type.PublishShop{ Images: append(body.Shop[j].MainImgs, body.Shop[j].ContentImgs...), UserName: body.Shop[j].UserName, Province: province, City: city, District: district, Title: body.Shop[j].Title, Content: body.Shop[j].Content, } publishShops = append(publishShops, publishShop) } item.PublishShop = publishShops item.BookData = _type.BookData{ ISBN: body.BookData[0].ISBN, Title: body.BookData[0].Title, Author: body.BookData[0].Author, Publisher: body.BookData[0].Publisher, } if err := item.Validate(); err != nil { log.Println("商品信息有误: ", err) return nil, err } allItems = append(allItems, item) log.Printf("build allItems elapsed=%v", time.Since(funcStart)) if len(allItems) == 0 { return nil, fmt.Errorf("没有有效的商品可以创建") } request := CreateProductRequest{ ProductData: allItems, } fmt.Printf("正在创建 %d 个商品...\n", len(allItems)) fmt.Printf("请求数据: %v\n", request) createPath := requestPath reqStart := time.Now() createResponse, err := requestUtil.MakeAPIRequest(appId, appSecret, domain, createPath, request) log.Printf("MakeAPIRequest elapsed=%v", time.Since(reqStart)) if err != nil { log.Println("创建商品失败: ", err) return nil, err } // 打印结果 fmt.Println("创建商品响应:") fmt.Println(string(createResponse)) log.Printf("XianYvCreat total elapsed=%v", time.Since(funcStart)) return createResponse, nil } // 创建:无Isbn不加bookdata参数 func XianYvCreatNoIsbn(body _type.BodyNew, requestPath string, appId int, appSecret, domain string, flag bool) ([]byte, error) { funcStart := time.Now() type CreateProductRequestNoIsbn struct { ProductData []_type.BashCreatNew `json:"product_data"` } var allItemsNoIsbn []_type.BashCreatNew var itemNoIsbn _type.BashCreatNew var createResponse []byte // 赋值itemNoIsbn itemNoIsbn.ItemKey = body.ItemKey itemNoIsbn.ChannelCatId = body.CatIds itemNoIsbn.Price = body.Price itemNoIsbn.OriginalPrice = body.OriginalPrice itemNoIsbn.Stock = body.Stock itemNoIsbn.ItemBizType = body.ItemBizType itemNoIsbn.SpBizType = body.SpBizType itemNoIsbn.OuterId = body.OuterId fmt.Printf("body: %v \n", body) fmt.Printf("body.OuterId: %s \n", body.OuterId) fmt.Printf("itemNoIsbn.OuterId: %+v \n", itemNoIsbn.OuterId) if flag { itemNoIsbn.ItemBizType = 2 itemNoIsbn.SpBizType = 99 } var publishShops []_type.PublishShop // 循环所有的Shop来赋值PublishShop for j := 0; j < len(body.Shop); j++ { var province, city, district int32 if body.Shop[j].Province == 0 { province = body.Province } else { province = body.Shop[j].Province } if body.Shop[j].City == 0 { city = body.City } else { city = body.Shop[j].City } if body.Shop[j].District == 0 { district = body.District } else { district = body.Shop[j].District } publishShop := _type.PublishShop{ Images: append(body.Shop[j].MainImgs, body.Shop[j].ContentImgs...), UserName: body.Shop[j].UserName, Province: province, City: city, District: district, Title: body.Shop[j].Title, Content: body.Shop[j].Content, } publishShops = append(publishShops, publishShop) } itemNoIsbn.PublishShop = publishShops if err := itemNoIsbn.ValidateNew(); err != nil { log.Println("商品信息有误: ", err) return nil, err } allItemsNoIsbn = append(allItemsNoIsbn, itemNoIsbn) log.Printf("build allItems elapsed=%v", time.Since(funcStart)) if len(allItemsNoIsbn) == 0 { return nil, fmt.Errorf("没有有效的商品可以创建") } request := CreateProductRequestNoIsbn{ ProductData: allItemsNoIsbn, } fmt.Printf("正在创建 %d 个商品...\n", len(allItemsNoIsbn)) fmt.Printf("请求数据: %v\n", request) createPath := requestPath reqStart := time.Now() createResponse, err := requestUtil.MakeAPIRequest(appId, appSecret, domain, createPath, request) log.Printf("MakeAPIRequest elapsed=%v", time.Since(reqStart)) if err != nil { log.Println("创建商品失败: ", err) return nil, err } // 打印结果 fmt.Println("创建商品响应:") fmt.Println(string(createResponse)) log.Printf("XianYvCreat total elapsed=%v", time.Since(funcStart)) //*******************************End return createResponse, nil }