提交 9b85fb90 authored 作者: zgy's avatar zgy

Merge remote-tracking branch 'origin/master'

...@@ -25,6 +25,9 @@ public class TbProductMissEntity implements Serializable { ...@@ -25,6 +25,9 @@ public class TbProductMissEntity implements Serializable {
* 失败类型【1:已存在】【2:出现了异常】 * 失败类型【1:已存在】【2:出现了异常】
*/ */
private Integer missType; private Integer missType;
private String missReason;
/** /**
* 页数(每250个) * 页数(每250个)
*/ */
...@@ -99,4 +102,12 @@ public class TbProductMissEntity implements Serializable { ...@@ -99,4 +102,12 @@ public class TbProductMissEntity implements Serializable {
public Date getCreateTime() { public Date getCreateTime() {
return createTime; return createTime;
} }
public String getMissReason() {
return missReason;
}
public void setMissReason(String missReason) {
this.missReason = missReason;
}
} }
...@@ -229,7 +229,7 @@ ...@@ -229,7 +229,7 @@
#{itemUrl}, #{itemUrl},
#{itemImg}, #{itemImg},
#{itemTags}, #{itemTags},
#{itemLabel} #{itemLabel},
#{itemNum}, #{itemNum},
#{itemCount}, #{itemCount},
#{itemSku}, #{itemSku},
......
...@@ -60,12 +60,14 @@ ...@@ -60,12 +60,14 @@
`id`, `id`,
`product_id`, `product_id`,
`miss_type`, `miss_type`,
`miss_reason`,
`page_size`, `page_size`,
`create_time`) `create_time`)
values( values(
#{id}, #{id},
#{productId}, #{productId},
#{missType}, #{missType},
#{missReason},
#{pageSize}, #{pageSize},
#{createTime}) #{createTime})
</insert> </insert>
...@@ -92,4 +94,4 @@ ...@@ -92,4 +94,4 @@
</foreach> </foreach>
</delete> </delete>
</mapper> </mapper>
\ No newline at end of file
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.concurrent.*;
/**
* 并发测试
*
* @author 爱酱油不爱醋
* @version 1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/applicationContext-test.xml"})
public class ConcurrencyApi {
private final int corePoolSize = 15;
private final int maximumPoolSize = 35;
@Test
public void test() {
ExecutorService executor = Executors.newCachedThreadPool();
int i = 0;
while (true) {
RunningTest test = new RunningTest(i);
executor.submit(test);
System.out.println("正在执行的线程编号:" + i);
i++;
}
}
}
package test; package test;
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.platform.dao.*; import com.platform.dao.*;
...@@ -23,6 +22,8 @@ import java.net.URL; ...@@ -23,6 +22,8 @@ import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* 商品导入功能 * 商品导入功能
...@@ -63,7 +64,12 @@ public class ImpartCode { ...@@ -63,7 +64,12 @@ public class ImpartCode {
* 接口路径 * 接口路径
*/ */
private final String API_URL = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products.json"; private final String API_URL = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products.json";
/**
* 任务用线程池
*/
ExecutorService executorService = Executors.newCachedThreadPool();
/** /**
* 根据分类进行导入 * 根据分类进行导入
*/ */
...@@ -71,31 +77,31 @@ public class ImpartCode { ...@@ -71,31 +77,31 @@ public class ImpartCode {
public void goImport() { public void goImport() {
List<String> list = Arrays.asList("Men", "Women", "Children", "Afri Home", "Cosmetics", "Hair", "Sportswear", "Electronics"); List<String> list = Arrays.asList("Men", "Women", "Children", "Afri Home", "Cosmetics", "Hair", "Sportswear", "Electronics");
for (String product_type : list) { for (String product_type : list) {
// 记录商品数量
int productCount = 0;
// 记录页数
int pageSize = 1;
// 测试数量 // 测试数量
String link = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products/count.json" + "?&product_type=" + product_type; String link = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products/count.json" + "?product_type=" + product_type;
String data = HttpRequest.get(link) String data = HttpRequest.get(link)
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=") .header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().body(); .execute().body();
System.err.println(product_type + ":" + data); System.err.println(product_type + ":" + data);
// 导入请求 // 导入请求
link = API_URL + "?&product_type=" + product_type; link = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products.json" + "?product_type=" + product_type + "&limit=250";
// 开始第一次导入商品 // 开始第一次导入商品
importMethod(link, pageSize, productCount); importMethod(link);
// 获取第一次循环的请求头 // 获取第一次循环的请求头
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(">", ""); try {
headerLink = headerLink.split(";")[0].replaceAll("<", "").replaceAll(">", "");
} catch (Exception e) {
continue;
}
// 无限导入 // 无限导入
while (true) { while (true) {
// 导入商品
productCount++; importMethod(headerLink);
importMethod(headerLink, pageSize, productCount);
// 获取下一页的链接 // 获取下一页的链接
try { try {
...@@ -103,13 +109,11 @@ public class ImpartCode { ...@@ -103,13 +109,11 @@ public class ImpartCode {
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=") .header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().header("link") .execute().header("link")
.split(",")[1]; .split(",")[1];
Console.error(headerLink);
headerLink = headerLink.substring(2, headerLink.indexOf(">")); headerLink = headerLink.substring(2, headerLink.indexOf(">"));
} catch (Exception e) { } catch (Exception e) {
// 死循环的终点为获取下一页链接抛出数组越界异常 // 死循环的终点为获取下一页链接抛出数组越界异常
break; break;
} }
} }
} }
...@@ -118,252 +122,271 @@ public class ImpartCode { ...@@ -118,252 +122,271 @@ public class ImpartCode {
/** /**
* 商品导入处理 * 商品导入处理
* *
* @param data 接口返回数据 * @param link 请求链接
* @param pageSize 正在导入的页数
* @param productCount 正在导入的商品数量
*/ */
private void importMethod(String data, int pageSize, int productCount) { private void importMethod(String link) {
// 请求
String data = HttpRequest.get(link)
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().body();
// 格式化 // 格式化
JSONObject jsonObject = JSONObject.fromObject(data); JSONObject jsonObject = JSONObject.fromObject(data);
// 取每个数组中的 products 数组节点 // 取每个数组中的 products 数组节点
JSONArray productsArr = jsonObject.getJSONArray("products"); JSONArray productsArr = jsonObject.getJSONArray("products");
// 记录线程数
int threadCount = 0;
for (int i = 0; i < productsArr.size(); i++) { for (int i = 0; i < productsArr.size(); i++) {
JSONObject productsObj = productsArr.getJSONObject(i); JSONObject productsObj = productsArr.getJSONObject(i);
productCount++;
Console.log("当前正在导入的商品个数为:" + productCount);
// 商品主体信息的商品 id // 商品主体信息的商品 id
String itemId = IdUtil.simpleUUID(); String itemId = IdUtil.simpleUUID();
// 复用的日期 // 复用的日期
Date date = new Date(); Date date = new Date();
// 商品的平台 id // 商品的平台 id
String id = productsObj.getString("id"); String id = productsObj.getString("id");
try {
// 查询该商品的 id 是否已保存 // 查询该商品的 id 是否已保存
if (tbCfStationItemDao.queryByCode(id) != null) { if (tbCfStationItemDao.queryByCode(id) != null) {
// 继续循环 System.err.println("已存在的商品,跳过导入的商品id为:" + id);
continue; // 跳过循环
} continue;
// 用于记录 option 的属性名 }
int optionNum = 0;
// 下方商品主体信息需要的商品属性,以"/"隔开 // 线程任务
StringBuilder optionBuilder = new StringBuilder(); int finalThreadCount = threadCount;
threadCount++;
// 商品的属性 Runnable runnable = () -> {
JSONArray optionsArr = productsObj.getJSONArray("options"); try {
for (int j = 0; j < optionsArr.size(); j++) { // 商品详情
JSONObject optionObj = optionsArr.getJSONObject(j); int optionNum = 0;
StringBuilder optionBuilder = new StringBuilder();
TbCfCategoryEntity categoryEntity = new TbCfCategoryEntity(); StringBuilder imageBuilder = new StringBuilder();
String optionId = optionObj.getString("id"); JSONArray imagesArr = productsObj.getJSONArray("images");
categoryEntity.setCategoryId(optionId); for (int j = 0; j < imagesArr.size(); j++) {
categoryEntity.setCreateTime(date); JSONObject imageObj = imagesArr.getJSONObject(j);
categoryEntity.setUpdateTime(date); // 上传图片
categoryEntity.setDelFlag(1); String skuSrc = imageObj.getString("src");
categoryEntity.setItemId(itemId); InputStream skuInputStream = getImageStream(skuSrc);
String optionName = optionObj.getString("name"); byte[] skuBytes = toByteArray(skuInputStream);
categoryEntity.setCategoryName(optionName); String url = OssUtil.upload(skuBytes, id + "_sku_" + j + ".jpg", "image");
optionBuilder.append(optionName); // 拼接富文本的 HTML
if (j != optionsArr.size() - 1) { imageBuilder.append("<p><img src=\"")
optionBuilder.append("/"); .append(url).append("\" title=\"")
.append(IdUtil.randomUUID()).append("_350x350")
.append(IdUtil.simpleUUID()).append(".jpg\"/></p>");
System.err.println("富文本图片内容:" + imageBuilder.toString());
} }
optionNum++; TbCfItemDescEntity itemDescEntity = tbCfItemDescDao.queryObject(itemId);
categoryEntity.setOrderNum(optionNum); if (itemDescEntity == null) {
categoryEntity.setOption("option" + optionNum); itemDescEntity = new TbCfItemDescEntity();
// 保存属性名 itemDescEntity.setItemId(itemId);
String categoryId = IdUtil.simpleUUID(); itemDescEntity.setItemDesc(imageBuilder.toString());
if (tbCategoryTemplateDao.queryByDesc(optionName) == null) { itemDescEntity.setCreateTime(date);
TbCategoryTemplateEntity tbCategoryTemplateEntity = new TbCategoryTemplateEntity(); tbCfItemDescDao.save(itemDescEntity);
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"); String src = productsObj.getJSONObject("image").getString("src");
for (int k = 0; k < valuesArr.size(); k++) { InputStream inputStream = getImageStream(src);
// 商品属性 byte[] bytes = toByteArray(inputStream);
TbCfOptionEntity optionEntity = new TbCfOptionEntity(); String url = OssUtil.upload(bytes, id + "_station.jpg", "image");
optionEntity.setOptionId(IdUtil.simpleUUID()); System.err.println("主图的链接为: " + url);
optionEntity.setCid(optionId);
optionEntity.setItemId(itemId); // 商品的属性
String optionSpecies = valuesArr.getString(k); JSONArray optionsArr = productsObj.getJSONArray("options");
optionEntity.setOptiionSpecies(optionSpecies); for (int j = 0; j < optionsArr.size(); j++) {
categoryBuilder.append(optionSpecies); JSONObject optionObj = optionsArr.getJSONObject(j);
if (k != valuesArr.size() - 1) {
categoryBuilder.append(","); 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());
try {
tbCfCategoryDao.save(categoryEntity);
} catch (Exception e) {
tbCfCategoryDao.update(categoryEntity);
} }
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")); TbCfStationItemEntity tbCfStationItem = new TbCfStationItemEntity();
skusEntity.setItemId(itemId);
skusEntity.setDelFlag(1); // 保存商品的变体数据
skusEntity.setCreateTime(date); JSONArray variantsArr = productsObj.getJSONArray("variants");
skusEntity.setUpdateTime(date); for (int j = 0; j < variantsArr.size(); j++) {
skusEntity.setSkuDesc(variantsObj.getString("title")); JSONObject variantsObj = variantsArr.getJSONObject(j);
skusEntity.setSkuName(optionBuilder.toString()); Integer quantity = variantsObj.getInt("inventory_quantity"); // 库存信息
skusEntity.setSkuPrice(price); BigDecimal price = new BigDecimal(variantsObj.getString("price")); // 价格
skusEntity.setSkuCount(quantity);
skusEntity.setOrderNum(variantsObj.getInt("position")); TbCfItemSkusEntity skusEntity = new TbCfItemSkusEntity();
tbCfItemSkusDao.save(skusEntity); skusEntity.setId(variantsObj.getString("id"));
skusEntity.setItemId(itemId);
skusEntity.setDelFlag(1);
skusEntity.setCreateTime(date);
skusEntity.setUpdateTime(date);
skusEntity.setSkuDesc(variantsObj.getString("title"));
System.out.println(optionBuilder.toString());
skusEntity.setSkuName(optionBuilder.toString());
skusEntity.setSkuPrice(price);
skusEntity.setSkuCount(quantity);
skusEntity.setOrderNum(variantsObj.getInt("position"));
tbCfItemSkusDao.save(skusEntity);
tbCfStationItem.setItemCount(quantity.longValue());
// 商品的原价
String compare_at_price = variantsObj.getString("compare_at_price");
if (!"null".equals(compare_at_price)) {
tbCfStationItem.setItemPrice(new BigDecimal(compare_at_price));
} else {
tbCfStationItem.setItemPrice(null);
}
// 商品的折扣价
tbCfStationItem.setDiscountPrice(price);
}
String title = productsObj.getString("title"); // 商品的名称
String product_type = productsObj.getString("product_type"); // 商品的分类
// 保存一级分类名
String goods_type_id = IdUtil.simpleUUID();
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();
}
tbCfStationItem.setItemCount(quantity.longValue()); // 解析二三级分类名
tbCfStationItem.setItemPrice(null); String tags = "";
// 商品的折扣价 String three = "";
tbCfStationItem.setDiscountPrice(price); String[] s = productsObj.getString("tags").split(",");
} for (String str : s) {
if (str.indexOf("#") == 0) {
String title = productsObj.getString("title"); // 商品的名称 tags = str.replaceAll("#", "");
String product_type = productsObj.getString("product_type"); // 商品的分类 } else {
three = str;
// 保存一级分类名 }
String goods_type_id = IdUtil.simpleUUID(); // 商品分类id }
if (tbCfGoodstypeDao.queryByTitle(product_type) == null) { // 如果分类名已存在则不保存
TbCfGoodstypeEntity tbCfGoodstypeEntity = new TbCfGoodstypeEntity(); // 保存二级分类名
tbCfGoodstypeEntity.setGoodstypeId(goods_type_id); String goodsTwoId = IdUtil.simpleUUID();
tbCfGoodstypeEntity.setGoodstypeTitle(product_type); if (tbCfGoodstwotypeDao.queryTitle(tags) == null) {
tbCfGoodstypeEntity.setGoodstypeSort(tbCfGoodstypeDao.queryTotal() + 1); TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = new TbCfGoodstwotypeEntity();
product_type = product_type.replaceAll(" ", "-"); tbCfGoodstwotypeEntity.setGoodstwotypeId(goodsTwoId);
tbCfGoodstypeEntity.setGoodstypeUrl("https://www.afrieshop.com/collections/" + product_type); tbCfGoodstwotypeEntity.setGoodstwotypeTitle(tags);
tbCfGoodstypeDao.save(tbCfGoodstypeEntity); tbCfGoodstwotypeEntity.setGoodstypeId(goods_type_id);
} else { tbCfGoodstwotypeDao.save(tbCfGoodstwotypeEntity);
// 如果已存在一级分类名,则使用已存在的 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 { } else {
three = str; TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = tbCfGoodstwotypeDao.queryTitle(tags);
goodsTwoId = tbCfGoodstwotypeEntity.getGoodstwotypeId();
} }
}
// 保存三级分类名(品名)
// 保存二级分类名 String goodsThreeId = IdUtil.simpleUUID();
String goodsTwoId = IdUtil.simpleUUID(); // 商品二级分类id if (tbCfDescripitonDao.queryByDescriptionName(three) == null) {
if (tbCfGoodstwotypeDao.queryTitle(tags) == null) { TbCfDescripitonEntity tbCfDescripitonEntity = new TbCfDescripitonEntity();
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = new TbCfGoodstwotypeEntity(); tbCfDescripitonEntity.setDescripitionId(goodsThreeId);
tbCfGoodstwotypeEntity.setGoodstwotypeId(goodsTwoId); tbCfDescripitonEntity.setDescripitionName(three);
tbCfGoodstwotypeEntity.setGoodstwotypeTitle(tags); tbCfDescripitonEntity.setGoodstypeId(goods_type_id);
tbCfGoodstwotypeEntity.setGoodstypeId(goods_type_id); tbCfDescripitonEntity.setGoodstwotypeId(goodsTwoId);
tbCfGoodstwotypeDao.save(tbCfGoodstwotypeEntity); tbCfDescripitonDao.save(tbCfDescripitonEntity);
} else { } else {
TbCfGoodstwotypeEntity tbCfGoodstwotypeEntity = tbCfGoodstwotypeDao.queryTitle(tags); TbCfDescripitonEntity tbCfDescripitonEntity = tbCfDescripitonDao.queryByDescriptionName(three);
goodsTwoId = tbCfGoodstwotypeEntity.getGoodstwotypeId(); goodsThreeId = tbCfDescripitonEntity.getDescripitionId();
} }
// 保存三级分类名(品名) // 保存商品的主体信息
String goodsThreeId = IdUtil.simpleUUID(); // 商品三级分类id tbCfStationItem.setItemId(itemId);
if (tbCfDescripitonDao.queryByDescriptionName(three) == null) { tbCfStationItem.setCreateTime(date);
TbCfDescripitonEntity tbCfDescripitonEntity = new TbCfDescripitonEntity(); tbCfStationItem.setItemNum(0L);
tbCfDescripitonEntity.setDescripitionId(goodsThreeId); tbCfStationItem.setItemUrl("https://www.afrieshop.com/products/" + productsObj.getString("handle"));
tbCfDescripitonEntity.setDescripitionName(three); tbCfStationItem.setSupplier(productsObj.getString("vendor"));
tbCfDescripitonEntity.setGoodstypeId(goods_type_id); tbCfStationItem.setItemBrief(title);
tbCfDescripitonEntity.setGoodstwotypeId(goodsTwoId); tbCfStationItem.setItemName(title);
tbCfDescripitonDao.save(tbCfDescripitonEntity); tbCfStationItem.setItemCode(id);
} else { tbCfStationItem.setItemCategory(goods_type_id);
TbCfDescripitonEntity tbCfDescripitonEntity = tbCfDescripitonDao.queryByDescriptionName(three); tbCfStationItem.setItemCategorytwo(goodsTwoId);
goodsThreeId = tbCfDescripitonEntity.getDescripitionId(); tbCfStationItem.setItemDescritionId(goodsThreeId);
} tbCfStationItem.setItemTags(product_type + "," + tags + "," + three);
tbCfStationItem.setItemTop("N");
// 保存商品的主体信息 tbCfStationItem.setItemImg(url);
tbCfStationItem.setItemId(itemId); tbCfStationItem.setEnableFlag(2);
tbCfStationItem.setCreateTime(date); tbCfStationItemDao.save(tbCfStationItem);
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 src = productsObj.getJSONObject("image").getString("src");
InputStream inputStream = getImageStream(src);
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);
// 上传图片
src = imageObj.getString("src");
inputStream = getImageStream(src);
bytes = toByteArray(inputStream);
url = OssUtil.upload(bytes, id + "_sku_" + j + ".jpg", "zion");
// 拼接富文本的 HTML } catch (Exception e) {
imageBuilder.append("<p><img src=\"") e.printStackTrace();
.append(url).append("\" title=\"") // 记录失败的产品记录
.append(IdUtil.randomUUID()).append("_350x350") TbProductMissEntity tbProductMissEntity = new TbProductMissEntity();
.append(IdUtil.simpleUUID()).append(".jpg\"/></p>"); tbProductMissEntity.setId(IdUtil.simpleUUID());
} tbProductMissEntity.setProductId(id);
// 商品详情 tbProductMissEntity.setMissType(2);
TbCfItemDescEntity itemDescEntity = tbCfItemDescDao.queryObject(itemId); tbProductMissEntity.setMissReason(e.getMessage());
if (itemDescEntity == null) { tbProductMissEntity.setPageSize(1);
itemDescEntity = new TbCfItemDescEntity(); tbProductMissEntity.setCreateTime(new Date());
itemDescEntity.setItemId(itemId); tbProductMissDao.save(tbProductMissEntity);
itemDescEntity.setItemDesc(imageBuilder.toString());
itemDescEntity.setCreateTime(date);
tbCfItemDescDao.save(itemDescEntity);
} }
} catch (Exception e) { System.err.println("线程号:" + finalThreadCount + ",已执行完成!");
e.printStackTrace(); };
// 记录失败的产品记录
TbProductMissEntity tbProductMissEntity = new TbProductMissEntity(); // 提交任务执行
tbProductMissEntity.setId(IdUtil.simpleUUID()); executorService.submit(runnable);
tbProductMissEntity.setProductId(id); System.err.println("线程号:" + finalThreadCount + ",开始提交任务!");
tbProductMissEntity.setMissType(2);
tbProductMissEntity.setPageSize(pageSize);
tbProductMissEntity.setCreateTime(new Date());
tbProductMissDao.save(tbProductMissEntity);
}
} }
} }
/** /**
...@@ -376,8 +399,8 @@ public class ImpartCode { ...@@ -376,8 +399,8 @@ public class ImpartCode {
public static InputStream getImageStream(String url) { public static InputStream getImageStream(String url) {
try { try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(5000); connection.setReadTimeout(10000);
connection.setConnectTimeout(5000); connection.setConnectTimeout(10000);
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return connection.getInputStream(); return connection.getInputStream();
...@@ -405,5 +428,5 @@ public class ImpartCode { ...@@ -405,5 +428,5 @@ public class ImpartCode {
} }
return output.toByteArray(); return output.toByteArray();
} }
} }
package test;
/**
* 商品导入的任务块
*
* @author 爱酱油不爱醋
* @version 1.0
*/
public class ImportTask implements Runnable {
@Override
public void run() {
}
}
package test;
/**
* @author 爱酱油不爱醋
* @version 1.0
*/
public class RunningTest implements Runnable {
private int taskNum;
public RunningTest(int num) {
this.taskNum = num;
}
@Override
public void run() {
System.out.println("正在执行任务:" + taskNum);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行完毕:" + taskNum);
}
}
...@@ -3,6 +3,7 @@ package test; ...@@ -3,6 +3,7 @@ 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.*;
...@@ -66,7 +67,6 @@ public class TestApi { ...@@ -66,7 +67,6 @@ public class TestApi {
*/ */
private final String API_URL = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products.json"; private final String API_URL = "https://mollykitty.myshopify.com/admin/api/" + VERSION + "/products.json";
@Test @Test
public void host() { public void host() {
String link = API_URL + "?limit=250&page_info=eyJwcm9kdWN0X3R5cGUiOiJXb21lbiIsImxhc3RfaWQiOjQ0OTc2Nzg5NTg2ODksImxhc3RfdmFsdWUiOiJDYXN1YWwgU2xpbSBTd2VhdGVycyIsImRpcmVjdGlvbiI6Im5leHQifQ"; String link = API_URL + "?limit=250&page_info=eyJwcm9kdWN0X3R5cGUiOiJXb21lbiIsImxhc3RfaWQiOjQ0OTc2Nzg5NTg2ODksImxhc3RfdmFsdWUiOiJDYXN1YWwgU2xpbSBTd2VhdGVycyIsImRpcmVjdGlvbiI6Im5leHQifQ";
...@@ -114,21 +114,36 @@ public class TestApi { ...@@ -114,21 +114,36 @@ public class TestApi {
*/ */
@Test @Test
public void linkTest() { public void linkTest() {
String link = "https://mollykitty.myshopify.com/admin/api/2020-01/products.json?vendor=%E6%B3%B0%E9%82%A6%E6%9C%8D%E9%A5%B0&limit=1&vendor=戴菊"; String link = "https://mollykitty.myshopify.com/admin/api/2020-01/products.json?vendor=%E6%B3%B0%E9%82%A6%E6%9C%8D%E9%A5%B0&limit=100&vendor=戴菊";
link = HttpRequest.get(link) link = HttpRequest.get(link)
.header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=") .header("Authorization", "Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA=")
.execute().body(); .execute().body();
JSONObject json = JSONObject.fromObject(link); JSONObject json = JSONObject.fromObject(link);
JSONObject product = json.getJSONArray("products").getJSONObject(0); JSONArray productArr = json.getJSONArray("products");
String tags = product.getString("tags"); for (int j = 0; j < productArr.size(); j++) {
JSONObject product = productArr.getJSONObject(j);
JSONArray variantsArr = product.getJSONArray("variants");
for (int i = 0; i < variantsArr.size(); i++) {
JSONObject variantsObj = variantsArr.getJSONObject(i);
String compare_at_price = variantsObj.getString("compare_at_price");
if (!"null".equals(compare_at_price)) {
System.out.println(compare_at_price);
}
}
}
/*String tags = product.getString("tags");
String[] s = tags.split(","); String[] s = tags.split(",");
Console.error(Arrays.toString(s)); Console.error(Arrays.toString(s));
for (String i : s) { for (String i : s) {
if (i.indexOf("#") == 0) { if (i.indexOf("#") == 0) {
System.out.println(i); System.out.println(i);
} }
} }*/
} }
/** /**
...@@ -704,12 +719,6 @@ public class TestApi { ...@@ -704,12 +719,6 @@ public class TestApi {
return output.toByteArray(); return output.toByteArray();
} }
public static void main(String[] args) throws Exception {
String link = "https://cdn.shopify.com/s/files/1/0079/8330/0705/products/0_-9_aee971dc-5661-46bb-9d7e-11f0a3ee5b35.jpg?v=1580133539";
InputStream inputStream = getImageStream(link);
byte[] bytes = toByteArray(inputStream);
String url = OssUtil.upload(bytes, IdUtil.simpleUUID() + ".jpg", "zion");
System.out.println(url);
}
} }
...@@ -59,8 +59,8 @@ log4j.appender.slowsql.layout=org.apache.log4j.PatternLayout ...@@ -59,8 +59,8 @@ log4j.appender.slowsql.layout=org.apache.log4j.PatternLayout
log4j.appender.slowsql.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS}|%5p|%F.%M:%L|%m%n log4j.appender.slowsql.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS}|%5p|%F.%M:%L|%m%n
#控制台输出所有SQL #控制台输出所有SQL
#便于调试 生产环境注释 #便于调试 生产环境注释
log4j.logger.com.platform.dao=DEBUG,sql #log4j.logger.com.platform.dao=DEBUG,sql
log4j.appender.sql=org.apache.log4j.ConsoleAppender #log4j.appender.sql=org.apache.log4j.ConsoleAppender
log4j.appender.sql.Target=System.out #log4j.appender.sql.Target=System.out
log4j.appender.sql.layout=org.apache.log4j.PatternLayout #log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=%m %n #log4j.appender.sql.layout.ConversionPattern=%m %n
SELECT count(*) FROM tb_cf_station_item WHERE item_category = '2fe6be991b4941608c7ebcea7091288f';
SELECT count(*) FROM tb_cf_station_item WHERE item_category = '7c0d04650ad649fd9bf6b9476947c118';
SELECT count(*) FROM tb_cf_station_item WHERE item_category = '03f2d9ab2ea44893a27d54c280233c9f';
SELECT count(*) FROM tb_cf_station_item WHERE item_category = '59a9e2c64d2a4a1696a06045f39c0166';
SELECT count(*) FROM tb_cf_station_item WHERE item_category = 'f0100ca392074027bb99da3a93625d63';
SELECT count(*) FROM tb_cf_station_item WHERE item_category = 'a777a18b4fc4423082fa843231cfc7bc';
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论