提交 679c8712 authored 作者: 梁业锦's avatar 梁业锦 💬

- 商品导入相关逻辑

上级 affc4772
...@@ -118,6 +118,16 @@ public class ImpartCode { ...@@ -118,6 +118,16 @@ public class ImpartCode {
} }
} }
/**
* 商品导入处理
*
* @param link 请求链接
*/
private void updateCategory(String link) {
}
/** /**
* 商品导入处理 * 商品导入处理
* *
...@@ -288,32 +298,46 @@ public class ImpartCode { ...@@ -288,32 +298,46 @@ public class ImpartCode {
} }
// 解析二三级分类名 // 解析二三级分类名
String tags = ""; String two = "";
String three = ""; String three = "";
String[] s = productsObj.getString("tags").split(","); String[] s = productsObj.getString("tags").split(",");
for (String str : s) { // 切割的字符串数组有 2 个
if (str.indexOf("#") == 0) { if (s.length > 1) {
tags = str.replaceAll("#", ""); // 可能出现 #分类名,#分类名:第一个为二级
if (s[0].contains("#") && s[1].contains("#")) {
two = s[0].replaceAll("#", "");
three = s[1].replaceAll("#", "");
} else { } else {
three = str; // 包含着 #分类名 的是三级
if (s[0].contains("#")) {
three = s[0].replaceAll("#", "");
two = s[1];
} else {
two = s[0].replaceAll("#", "");
three = s[1];
}
} }
} else {
// 如果只有一个,那么为二级
two = s[0].replaceAll("#", "");
} }
// 保存二级分类名 // 保存二级分类名
String goodsTwoId = IdUtil.simpleUUID(); String goodsTwoId = IdUtil.simpleUUID();
if (tbCfGoodstwotypeDao.queryTitle(tags).size() == 0) { if (tbCfGoodstwotypeDao.queryTitle(two).size() == 0) {
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = new TbCfGoodstwotypeEntity(); TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = new TbCfGoodstwotypeEntity();
tbCfGoodstwotypeEntity.setGoodstwotypeId(goodsTwoId); tbCfGoodstwotypeEntity.setGoodstwotypeId(goodsTwoId);
tbCfGoodstwotypeEntity.setGoodstwotypeTitle(tags); tbCfGoodstwotypeEntity.setGoodstwotypeTitle(two);
tbCfGoodstwotypeEntity.setGoodstypeId(goods_type_id); tbCfGoodstwotypeEntity.setGoodstypeId(goods_type_id);
if (tbCfGoodstwotypeDao.queryTitle(tags).size() == 0) { if (tbCfGoodstwotypeDao.queryTitle(two).size() == 0) {
tbCfGoodstwotypeDao.save(tbCfGoodstwotypeEntity); tbCfGoodstwotypeDao.save(tbCfGoodstwotypeEntity);
} else { } else {
List<TbCfGoodstwotypeEntity> tbCfGoodstwotypeEntity1 = tbCfGoodstwotypeDao.queryTitle(tags); List<TbCfGoodstwotypeEntity> tbCfGoodstwotypeEntity1 = tbCfGoodstwotypeDao.queryTitle(two);
goodsTwoId = tbCfGoodstwotypeEntity1.get(0).getGoodstwotypeId(); goodsTwoId = tbCfGoodstwotypeEntity1.get(0).getGoodstwotypeId();
} }
} else { } else {
List<TbCfGoodstwotypeEntity> tbCfGoodstwotypeEntity = tbCfGoodstwotypeDao.queryTitle(tags); List<TbCfGoodstwotypeEntity> tbCfGoodstwotypeEntity = tbCfGoodstwotypeDao.queryTitle(two);
goodsTwoId = tbCfGoodstwotypeEntity.get(0).getGoodstwotypeId(); goodsTwoId = tbCfGoodstwotypeEntity.get(0).getGoodstwotypeId();
} }
...@@ -348,14 +372,14 @@ public class ImpartCode { ...@@ -348,14 +372,14 @@ public class ImpartCode {
tbCfStationItem.setItemCategory(goods_type_id); tbCfStationItem.setItemCategory(goods_type_id);
tbCfStationItem.setItemCategorytwo(goodsTwoId); tbCfStationItem.setItemCategorytwo(goodsTwoId);
tbCfStationItem.setItemDescritionId(goodsThreeId); tbCfStationItem.setItemDescritionId(goodsThreeId);
tbCfStationItem.setItemTags(product_type + "," + tags + "," + three); tbCfStationItem.setItemTags(product_type + "," + two + "," + three);
tbCfStationItem.setItemTop("N"); tbCfStationItem.setItemTop("N");
// 上传图片获取主图 // 上传图片获取主图
String src = productsObj.getJSONObject("image").getString("src"); String src = productsObj.getJSONObject("image").getString("src");
InputStream inputStream = getImageStream(src); InputStream inputStream = getImageStream(src);
byte[] bytes = toByteArray(inputStream); byte[] bytes = toByteArray(inputStream);
String url = OssUtil.upload(bytes, id + "_station.jpg", "image"); String url = OssUtil.upload(bytes, id + "_station.jpg", "zion");
System.err.println("主图的链接为: " + url); System.err.println("主图的链接为: " + url);
tbCfStationItem.setItemImg(url); tbCfStationItem.setItemImg(url);
...@@ -368,14 +392,24 @@ public class ImpartCode { ...@@ -368,14 +392,24 @@ public class ImpartCode {
JSONObject imageObj = imagesArr.getJSONObject(j); JSONObject imageObj = imagesArr.getJSONObject(j);
// 上传图片 // 上传图片
String skuSrc = imageObj.getString("src"); String skuSrc = imageObj.getString("src");
try {
InputStream skuInputStream = getImageStream(skuSrc); InputStream skuInputStream = getImageStream(skuSrc);
byte[] skuBytes = toByteArray(skuInputStream); byte[] skuBytes = toByteArray(skuInputStream);
String skuUrl = OssUtil.upload(skuBytes, id + "_sku_" + j + ".jpg", "image"); String skuUrl = OssUtil.upload(skuBytes, id + "_sku_" + j + ".jpg", "zion");
// 拼接富文本的 HTML // 拼接富文本的 HTML
imageBuilder.append("<p><img src=\"") imageBuilder.append("<p><img src=\"")
.append(skuUrl).append("\" title=\"") .append(skuUrl).append("\" title=\"")
.append(IdUtil.randomUUID()).append("_350x350") .append(IdUtil.randomUUID()).append("_350x350")
.append(IdUtil.simpleUUID()).append(".jpg\"/></p>"); .append(IdUtil.simpleUUID()).append(".jpg\"/></p>");
} catch (Exception e) {
// 没有详情图片,则用主图凑活一下
imageBuilder = new StringBuilder();
imageBuilder.append("<p><img src=\"")
.append(url).append("\" title=\"")
.append(IdUtil.randomUUID()).append("_350x350")
.append(IdUtil.simpleUUID()).append(".jpg\"/></p>");
break;
}
System.err.println("富文本图片内容:" + imageBuilder.toString()); System.err.println("富文本图片内容:" + imageBuilder.toString());
} }
TbCfItemDescEntity itemDescEntity = tbCfItemDescDao.queryObject(itemId); TbCfItemDescEntity itemDescEntity = tbCfItemDescDao.queryObject(itemId);
......
...@@ -3,21 +3,17 @@ package test; ...@@ -3,21 +3,17 @@ package test;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import com.google.gson.annotations.JsonAdapter;
import com.platform.controller.api.ProductExitException; import com.platform.controller.api.ProductExitException;
import com.platform.dao.*; import com.platform.dao.*;
import com.platform.entity.*; import com.platform.entity.*;
import com.platform.utils.OssUtil; import com.platform.utils.OssUtil;
import com.platform.utils.R;
import net.sf.json.JSONArray; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apiguardian.api.API;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.PostMapping;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
...@@ -124,6 +120,20 @@ public class TestApi { ...@@ -124,6 +120,20 @@ public class TestApi {
for (int j = 0; j < productArr.size(); j++) { for (int j = 0; j < productArr.size(); j++) {
JSONObject product = productArr.getJSONObject(j); JSONObject product = productArr.getJSONObject(j);
String tags = product.getString("tags");
String[] s = tags.split(",");
if (s.length > 1) {
for (String i : s) {
if (i.contains("#")) {
System.out.println(i);
String three = i.replaceAll("#", "");
System.out.println(three);
}
}
} else {
}
JSONArray variantsArr = product.getJSONArray("variants"); JSONArray variantsArr = product.getJSONArray("variants");
for (int i = 0; i < variantsArr.size(); i++) { for (int i = 0; i < variantsArr.size(); i++) {
JSONObject variantsObj = variantsArr.getJSONObject(i); JSONObject variantsObj = variantsArr.getJSONObject(i);
...@@ -136,14 +146,6 @@ public class TestApi { ...@@ -136,14 +146,6 @@ public class TestApi {
} }
/*String tags = product.getString("tags");
String[] s = tags.split(",");
Console.error(Arrays.toString(s));
for (String i : s) {
if (i.indexOf("#") == 0) {
System.out.println(i);
}
}*/
} }
/** /**
...@@ -157,528 +159,12 @@ public class TestApi { ...@@ -157,528 +159,12 @@ public class TestApi {
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=") .header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().body(); .execute().body();
JSONObject jsonObject = JSONObject.fromObject(result); JSONObject jsonObject = JSONObject.fromObject(result);
// 第一次导入
// once(result);
// 解析请求头,获取到下一页的 url
String headerLink = HttpRequest.get(link) String headerLink = HttpRequest.get(link)
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=") .header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().header("link"); .execute().header("link");
headerLink = headerLink.split(";")[0].replaceAll("<", "").replaceAll(">", ""); headerLink = headerLink.split(";")[0].replaceAll("<", "").replaceAll(">", "");
// 无限导入
// unlimited(headerLink);
}
/**
* 第一遍循环导入
*/
/* private void once(String data) {
// 记录页数的
int productCount = 0;
// json 化返回数据
JSONObject jsonObject = JSONObject.fromObject(data);
// 取每个数组中的 products 数组节点
JSONArray productsArr = jsonObject.getJSONArray("products");
for (int i = 0; i < productsArr.size(); i++) {
JSONObject productsObj = productsArr.getJSONObject(i);
Console.log("当前正在导入的商品个数为:" + productCount);
productCount++;
// 商品主体信息的商品 id
String itemId = IdUtil.simpleUUID();
// 复用的日期
Date date = new Date();
// 商品的平台 id
String id = productsObj.getString("id");
try {
// 查询该商品的 id 是否已保存
if (tbCfStationItemDao.queryByCode(id) != null) {
// 继续循环
continue;
}
// 用于记录 option 的属性名
int optionNum = 0;
// 下方商品主体信息需要的商品属性,以"/"隔开
StringBuilder optionBuilder = new StringBuilder();
// 商品的属性
JSONArray optionsArr = productsObj.getJSONArray("options");
for (int j = 0; j < optionsArr.size(); j++) {
JSONObject optionObj = optionsArr.getJSONObject(j);
TbCfCategoryEntity categoryEntity = new TbCfCategoryEntity();
String optionId = optionObj.getString("id");
categoryEntity.setCategoryId(optionId);
categoryEntity.setCreateTime(date);
categoryEntity.setUpdateTime(date);
categoryEntity.setDelFlag(1);
categoryEntity.setItemId(itemId);
String optionName = optionObj.getString("name");
categoryEntity.setCategoryName(optionName);
optionBuilder.append(optionName);
if (j != optionsArr.size() - 1) {
optionBuilder.append("/");
}
optionNum++;
categoryEntity.setOrderNum(optionNum);
categoryEntity.setOption("option" + optionNum);
// 保存属性名
String categoryId = IdUtil.simpleUUID();
if (tbCategoryTemplateDao.queryByDesc(optionName) == null) {
TbCategoryTemplateEntity tbCategoryTemplateEntity = new TbCategoryTemplateEntity();
tbCategoryTemplateEntity.setId(categoryId);
tbCategoryTemplateEntity.setCategoryDesc(optionName);
tbCategoryTemplateEntity.setDelFlag(1);
tbCategoryTemplateEntity.setCategoryName(optionName);
tbCategoryTemplateEntity.setUpdateTime(date);
tbCategoryTemplateEntity.setUpdateTime(date);
tbCategoryTemplateDao.save(tbCategoryTemplateEntity);
}
StringBuilder categoryBuilder = new StringBuilder();
JSONArray valuesArr = optionObj.getJSONArray("values");
for (int k = 0; k < valuesArr.size(); k++) {
// 商品属性
TbCfOptionEntity optionEntity = new TbCfOptionEntity();
optionEntity.setOptionId(IdUtil.simpleUUID());
optionEntity.setCid(optionId);
optionEntity.setItemId(itemId);
String optionSpecies = valuesArr.getString(k);
optionEntity.setOptiionSpecies(optionSpecies);
categoryBuilder.append(optionSpecies);
if (k != valuesArr.size() - 1) {
categoryBuilder.append(",");
}
optionEntity.setCreateTime(date);
optionEntity.setUpdateTime(date);
optionEntity.setDelFlag(1);
optionEntity.setOptionName(optionName);
tbCfOptionDao.save(optionEntity);
}
categoryEntity.setCategoryDesc(categoryBuilder.toString());
tbCfCategoryDao.save(categoryEntity);
}
TbCfStationItemEntity tbCfStationItem = new TbCfStationItemEntity(); // 商品主体信息实体类
// 保存商品的变体数据
JSONArray variantsArr = productsObj.getJSONArray("variants");
for (int j = 0; j < variantsArr.size(); j++) {
JSONObject variantsObj = variantsArr.getJSONObject(j);
Integer quantity = variantsObj.getInt("inventory_quantity"); // 库存信息
BigDecimal price = new BigDecimal(variantsObj.getString("price")); // 价格
TbCfItemSkusEntity skusEntity = new TbCfItemSkusEntity();
skusEntity.setId(variantsObj.getString("id"));
skusEntity.setItemId(itemId);
skusEntity.setDelFlag(1);
skusEntity.setCreateTime(date);
skusEntity.setUpdateTime(date);
skusEntity.setSkuDesc(variantsObj.getString("title"));
skusEntity.setSkuName(optionBuilder.toString());
skusEntity.setSkuPrice(price);
skusEntity.setSkuCount(quantity);
skusEntity.setOrderNum(variantsObj.getInt("position"));
tbCfItemSkusDao.save(skusEntity);
tbCfStationItem.setItemCount(quantity.longValue());
tbCfStationItem.setItemPrice(null);
// 商品的折扣价
tbCfStationItem.setDiscountPrice(price);
}
String title = productsObj.getString("title"); // 商品的名称
String product_type = productsObj.getString("product_type"); // 商品的分类
// 保存一级分类名
String goods_type_id = IdUtil.simpleUUID(); // 商品分类id
if (tbCfGoodstypeDao.queryByTitle(product_type) == null) { // 如果分类名已存在则不保存
TbCfGoodstypeEntity tbCfGoodstypeEntity = new TbCfGoodstypeEntity();
tbCfGoodstypeEntity.setGoodstypeId(goods_type_id);
tbCfGoodstypeEntity.setGoodstypeTitle(product_type);
tbCfGoodstypeEntity.setGoodstypeSort(tbCfGoodstypeDao.queryTotal() + 1);
product_type = product_type.replaceAll(" ", "-");
tbCfGoodstypeEntity.setGoodstypeUrl("https://www.afrieshop.com/collections/" + product_type);
tbCfGoodstypeDao.save(tbCfGoodstypeEntity);
} else {
// 如果已存在一级分类名,则使用已存在的 id
*//*TbCfGoodstypeEntity tbCfGoodstypeEntity = tbCfGoodstypeDao.queryByTitle(product_type);*//*
*//*goods_type_id = tbCfGoodstypeEntity.getGoodstypeId();*//*
}
// 二级分类名
String tags = "";
// 三级分类名
String three = "";
String[] s = productsObj.getString("tags").split(",");
Console.error(Arrays.toString(s));
for (String str : s) {
if (str.indexOf("#") == 0) {
tags = str.replaceAll("#", "");
} else {
three = str;
}
}
// 保存二级分类名
String goodsTwoId = IdUtil.simpleUUID(); // 商品二级分类id
if (tbCfGoodstwotypeDao.queryTitle(tags) == null) {
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = new TbCfGoodstwotypeEntity();
tbCfGoodstwotypeEntity.setGoodstwotypeId(goodsTwoId);
tbCfGoodstwotypeEntity.setGoodstwotypeTitle(tags);
tbCfGoodstwotypeEntity.setGoodstypeId(goods_type_id);
tbCfGoodstwotypeDao.save(tbCfGoodstwotypeEntity);
} else {
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = tbCfGoodstwotypeDao.queryTitle(tags);
goodsTwoId = tbCfGoodstwotypeEntity.getGoodstwotypeId();
}
// 保存三级分类名(品名)
String goodsThreeId = IdUtil.simpleUUID(); // 商品三级分类id
if (tbCfDescripitonDao.queryByDescriptionName(three) == null) {
TbCfDescripitonEntity tbCfDescripitonEntity = new TbCfDescripitonEntity();
tbCfDescripitonEntity.setDescripitionId(goodsThreeId);
tbCfDescripitonEntity.setDescripitionName(three);
tbCfDescripitonEntity.setGoodstypeId(goods_type_id);
tbCfDescripitonEntity.setGoodstwotypeId(goodsTwoId);
tbCfDescripitonDao.save(tbCfDescripitonEntity);
} else {
TbCfDescripitonEntity tbCfDescripitonEntity = tbCfDescripitonDao.queryByDescriptionName(three);
goodsThreeId = tbCfDescripitonEntity.getDescripitionId();
}
// 保存商品的主体信息
tbCfStationItem.setItemId(itemId);
tbCfStationItem.setCreateTime(date);
tbCfStationItem.setItemNum(0L);
tbCfStationItem.setItemUrl("https://www.afrieshop.com/products/" + productsObj.getString("handle"));
tbCfStationItem.setSupplier(productsObj.getString("vendor"));
tbCfStationItem.setItemBrief(title);
tbCfStationItem.setItemName(title);
tbCfStationItem.setItemCode(id);
tbCfStationItem.setItemCategory(goods_type_id);
tbCfStationItem.setItemCategorytwo(goodsTwoId);
tbCfStationItem.setItemDescritionId(goodsThreeId);
tbCfStationItem.setItemTags(product_type + "," + tags + "," + three);
tbCfStationItem.setItemTop("N");
// 上传图片
String stationSrc = productsObj.getJSONObject("image").getString("src");
InputStream inputStream = getImageStream(stationSrc);
byte[] bytes = toByteArray(inputStream);
String url = OssUtil.upload(bytes, id + "_station.jpg", "zion");
tbCfStationItem.setItemImg(url);
tbCfStationItem.setEnableFlag(2);
tbCfStationItemDao.save(tbCfStationItem);
// 商品的富文本描述
StringBuilder imageBuilder = new StringBuilder();
JSONArray imagesArr = productsObj.getJSONArray("images");
for (int j = 0; j < imagesArr.size(); j++) {
JSONObject imageObj = imagesArr.getJSONObject(j);
// 上传图片
String skuSrc = imageObj.getString("src");
inputStream = getImageStream(skuSrc);
bytes = toByteArray(inputStream);
url = OssUtil.upload(bytes, id + "_sku_" + j + ".jpg", "zion");
// 拼接富文本的 HTML
imageBuilder.append("<p><img src=\"")
.append(url).append("\" title=\"")
.append(IdUtil.randomUUID()).append("_350x350")
.append(IdUtil.simpleUUID()).append(".jpg\"/></p>");
}
// 商品详情
TbCfItemDescEntity itemDescEntity = tbCfItemDescDao.queryObject(itemId);
if (itemDescEntity == null) {
itemDescEntity = new TbCfItemDescEntity();
itemDescEntity.setItemId(itemId);
itemDescEntity.setItemDesc(imageBuilder.toString());
itemDescEntity.setCreateTime(date);
tbCfItemDescDao.save(itemDescEntity);
}
} catch (ProductExitException e) {
Console.error("该商品已存在");
// 记录已存在的商品
TbProductMissEntity tbProductMissEntity = new TbProductMissEntity();
tbProductMissEntity.setId(IdUtil.simpleUUID());
tbProductMissEntity.setProductId(id);
tbProductMissEntity.setMissType(1);
tbProductMissEntity.setPageSize(1);
tbProductMissEntity.setCreateTime(new Date());
tbProductMissDao.save(tbProductMissEntity);
} catch (Exception e) {
e.printStackTrace();
// 记录失败的产品记录
TbProductMissEntity tbProductMissEntity = new TbProductMissEntity();
tbProductMissEntity.setId(IdUtil.simpleUUID());
tbProductMissEntity.setProductId(id);
tbProductMissEntity.setMissType(2);
tbProductMissEntity.setPageSize(1);
tbProductMissEntity.setCreateTime(new Date());
tbProductMissDao.save(tbProductMissEntity);
}
}
}*/
/**
* 无限循环导入
*/
/*private void unlimited(String link) {
// 记录页数的
int pageSize = 0;
int productCount = 1;
// 循环翻页
while (true) {
Console.error("当前的页数为:" + pageSize);
// 这个请求需要密钥,可以先拿着账号密码去浏览器请求一遍,F12 获取 token 复制粘贴即可
String result = HttpRequest.get(link)
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().body();
JSONObject jsonObject = JSONObject.fromObject(result);
// 取每个数组中的 products 数组节点
JSONArray productsArr = jsonObject.getJSONArray("products");
for (int i = 0; i < productsArr.size(); i++) {
JSONObject productsObj = productsArr.getJSONObject(i);
Console.log("当前正在导入的商品个数为:" + productCount);
productCount++;
// 商品主体信息的商品 id
String itemId = IdUtil.simpleUUID();
// 复用的日期
Date date = new Date();
// 商品的平台 id
String id = productsObj.getString("id");
try {
// 查询该商品的 id 是否已保存
if (tbCfStationItemDao.queryByCode(id) != null) {
// 继续循环
continue;
} }
// 用于记录 option 的属性名
int optionNum = 0;
// 下方商品主体信息需要的商品属性,以"/"隔开
StringBuilder optionBuilder = new StringBuilder();
// 商品的属性
JSONArray optionsArr = productsObj.getJSONArray("options");
for (int j = 0; j < optionsArr.size(); j++) {
JSONObject optionObj = optionsArr.getJSONObject(j);
TbCfCategoryEntity categoryEntity = new TbCfCategoryEntity();
String optionId = optionObj.getString("id");
categoryEntity.setCategoryId(optionId);
categoryEntity.setCreateTime(date);
categoryEntity.setUpdateTime(date);
categoryEntity.setDelFlag(1);
categoryEntity.setItemId(itemId);
String optionName = optionObj.getString("name");
categoryEntity.setCategoryName(optionName);
optionBuilder.append(optionName);
if (j != optionsArr.size() - 1) {
optionBuilder.append("/");
}
optionNum++;
categoryEntity.setOrderNum(optionNum);
categoryEntity.setOption("option" + optionNum);
// 保存属性名
String categoryId = IdUtil.simpleUUID();
if (tbCategoryTemplateDao.queryByDesc(optionName) == null) {
TbCategoryTemplateEntity tbCategoryTemplateEntity = new TbCategoryTemplateEntity();
tbCategoryTemplateEntity.setId(categoryId);
tbCategoryTemplateEntity.setCategoryDesc(optionName);
tbCategoryTemplateEntity.setDelFlag(1);
tbCategoryTemplateEntity.setCategoryName(optionName);
tbCategoryTemplateEntity.setUpdateTime(date);
tbCategoryTemplateEntity.setUpdateTime(date);
tbCategoryTemplateDao.save(tbCategoryTemplateEntity);
}
StringBuilder categoryBuilder = new StringBuilder();
JSONArray valuesArr = optionObj.getJSONArray("values");
for (int k = 0; k < valuesArr.size(); k++) {
// 商品属性
TbCfOptionEntity optionEntity = new TbCfOptionEntity();
optionEntity.setOptionId(IdUtil.simpleUUID());
optionEntity.setCid(optionId);
optionEntity.setItemId(itemId);
String optionSpecies = valuesArr.getString(k);
optionEntity.setOptiionSpecies(optionSpecies);
categoryBuilder.append(optionSpecies);
if (k != valuesArr.size() - 1) {
categoryBuilder.append(",");
}
optionEntity.setCreateTime(date);
optionEntity.setUpdateTime(date);
optionEntity.setDelFlag(1);
optionEntity.setOptionName(optionName);
tbCfOptionDao.save(optionEntity);
}
categoryEntity.setCategoryDesc(categoryBuilder.toString());
tbCfCategoryDao.save(categoryEntity);
}
TbCfStationItemEntity tbCfStationItem = new TbCfStationItemEntity(); // 商品主体信息实体类
// 保存商品的变体数据
JSONArray variantsArr = productsObj.getJSONArray("variants");
for (int j = 0; j < variantsArr.size(); j++) {
JSONObject variantsObj = variantsArr.getJSONObject(j);
Integer quantity = variantsObj.getInt("inventory_quantity"); // 库存信息
BigDecimal price = new BigDecimal(variantsObj.getString("price")); // 价格
TbCfItemSkusEntity skusEntity = new TbCfItemSkusEntity();
skusEntity.setId(variantsObj.getString("id"));
skusEntity.setItemId(itemId);
skusEntity.setDelFlag(1);
skusEntity.setCreateTime(date);
skusEntity.setUpdateTime(date);
skusEntity.setSkuDesc(variantsObj.getString("title"));
skusEntity.setSkuName(optionBuilder.toString());
skusEntity.setSkuPrice(price);
skusEntity.setSkuCount(quantity);
skusEntity.setOrderNum(variantsObj.getInt("position"));
tbCfItemSkusDao.save(skusEntity);
tbCfStationItem.setItemCount(quantity.longValue());
tbCfStationItem.setItemPrice(null);
tbCfStationItem.setDiscountPrice(price);
}
String title = productsObj.getString("title"); // 商品的名称
String product_type = productsObj.getString("product_type"); // 商品的分类
// 保存一级分类名
String goods_type_id = IdUtil.simpleUUID(); // 商品分类id
if (tbCfGoodstypeDao.queryByTitle(product_type) == null) { // 如果分类名已存在则不保存
TbCfGoodstypeEntity tbCfGoodstypeEntity = new TbCfGoodstypeEntity();
tbCfGoodstypeEntity.setGoodstypeId(goods_type_id);
tbCfGoodstypeEntity.setGoodstypeTitle(product_type);
tbCfGoodstypeEntity.setGoodstypeSort(tbCfGoodstypeDao.queryTotal() + 1);
product_type = product_type.replaceAll(" ", "-");
tbCfGoodstypeEntity.setGoodstypeUrl("https://www.afrieshop.com/collections/" + product_type);
tbCfGoodstypeDao.save(tbCfGoodstypeEntity);
} else {
// 如果已存在一级分类名,则使用已存在的 id
*//* TbCfGoodstypeEntity tbCfGoodstypeEntity = tbCfGoodstypeDao.queryByTitle(product_type);
goods_type_id = tbCfGoodstypeEntity.getGoodstypeId();*//*
}
// 二级分类名
String tags = "";
// 三级分类名
String three = "";
String[] s = productsObj.getString("tags").split(",");
Console.error(Arrays.toString(s));
for (String str : s) {
if (str.indexOf("#") == 0) {
tags = str.replaceAll("#", "");
} else {
three = str;
}
}
// 保存二级分类名
String goodsTwoId = IdUtil.simpleUUID(); // 商品二级分类id
if (tbCfGoodstwotypeDao.queryTitle(tags) == null) {
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = new TbCfGoodstwotypeEntity();
tbCfGoodstwotypeEntity.setGoodstwotypeId(goodsTwoId);
tbCfGoodstwotypeEntity.setGoodstwotypeTitle(tags);
tbCfGoodstwotypeEntity.setGoodstypeId(goods_type_id);
tbCfGoodstwotypeDao.save(tbCfGoodstwotypeEntity);
} else {
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = tbCfGoodstwotypeDao.queryTitle(tags);
goodsTwoId = tbCfGoodstwotypeEntity.getGoodstwotypeId();
}
// 保存三级分类名(品名)
String goodsThreeId = IdUtil.simpleUUID(); // 商品三级分类id
if (tbCfDescripitonDao.queryByDescriptionName(three) == null) {
TbCfDescripitonEntity tbCfDescripitonEntity = new TbCfDescripitonEntity();
tbCfDescripitonEntity.setDescripitionId(goodsThreeId);
tbCfDescripitonEntity.setDescripitionName(three);
tbCfDescripitonEntity.setGoodstypeId(goods_type_id);
tbCfDescripitonEntity.setGoodstwotypeId(goodsTwoId);
tbCfDescripitonDao.save(tbCfDescripitonEntity);
} else {
TbCfDescripitonEntity tbCfDescripitonEntity = tbCfDescripitonDao.queryByDescriptionName(three);
goodsThreeId = tbCfDescripitonEntity.getDescripitionId();
}
// 保存商品的主体信息
tbCfStationItem.setItemId(itemId);
tbCfStationItem.setCreateTime(date);
tbCfStationItem.setItemNum(0L);
tbCfStationItem.setItemUrl("https://www.afrieshop.com/products/" + productsObj.getString("handle"));
tbCfStationItem.setSupplier(productsObj.getString("vendor"));
tbCfStationItem.setItemBrief(title);
tbCfStationItem.setItemName(title);
tbCfStationItem.setItemCode(id);
tbCfStationItem.setItemCategory(goods_type_id);
tbCfStationItem.setItemCategorytwo(goodsTwoId);
tbCfStationItem.setItemDescritionId(goodsThreeId);
tbCfStationItem.setItemTags(product_type + "," + tags + "," + three);
tbCfStationItem.setItemTop("N");
tbCfStationItem.setItemImg(productsObj.getJSONObject("image").getString("src"));
tbCfStationItem.setEnableFlag(2);
tbCfStationItemDao.save(tbCfStationItem);
// 商品的富文本描述
StringBuilder imageBuilder = new StringBuilder();
JSONArray imagesArr = productsObj.getJSONArray("images");
for (int j = 0; j < imagesArr.size(); j++) {
JSONObject imageObj = imagesArr.getJSONObject(j);
// 拼接富文本的 HTML
imageBuilder.append("<p><img src=\"")
.append(imageObj.getString("src")).append("\" title=\"")
.append(IdUtil.randomUUID()).append("_350x350")
.append(IdUtil.simpleUUID()).append(".jpg\"/></p>");
}
// 商品详情
TbCfItemDescEntity itemDescEntity = tbCfItemDescDao.queryObject(itemId);
if (itemDescEntity == null) {
itemDescEntity = new TbCfItemDescEntity();
itemDescEntity.setItemId(itemId);
itemDescEntity.setItemDesc(imageBuilder.toString());
itemDescEntity.setCreateTime(date);
tbCfItemDescDao.save(itemDescEntity);
}
} catch (Exception e) {
e.printStackTrace();
// 记录失败的产品记录
TbProductMissEntity tbProductMissEntity = new TbProductMissEntity();
tbProductMissEntity.setId(IdUtil.simpleUUID());
tbProductMissEntity.setProductId(id);
tbProductMissEntity.setMissType(2);
tbProductMissEntity.setPageSize(pageSize);
tbProductMissEntity.setCreateTime(new Date());
tbProductMissDao.save(tbProductMissEntity);
}
}
// 循环翻页
link = HttpRequest.get(link)
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().header("link")
.split(",")[1];
Console.error(link);
link = link.substring(2, link.indexOf(">"));
// 记录页数
pageSize++;
}
}
*/
/** /**
* 通过URL获取网络图片 * 通过URL获取网络图片
* 获取网络图片流 * 获取网络图片流
...@@ -720,5 +206,4 @@ public class TestApi { ...@@ -720,5 +206,4 @@ public class TestApi {
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论