Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
C
chinafrica
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
zhengfg
chinafrica
Commits
af83089e
提交
af83089e
authored
3月 17, 2020
作者:
zgy
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'origin/master'
上级
98973db5
9481b5d9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
742 行增加
和
260 行删除
+742
-260
ImpartCode.java
platform-admin/src/main/test/java/test/ImpartCode.java
+409
-0
TestApi.java
platform-admin/src/main/test/java/test/TestApi.java
+333
-260
没有找到文件。
platform-admin/src/main/test/java/test/ImpartCode.java
0 → 100644
浏览文件 @
af83089e
package
test
;
import
cn.hutool.core.lang.Console
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.http.HttpRequest
;
import
com.platform.dao.*
;
import
com.platform.entity.*
;
import
com.platform.utils.OssUtil
;
import
net.sf.json.JSONArray
;
import
net.sf.json.JSONObject
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.List
;
/**
* 商品导入功能
*
* @author 爱酱油不爱醋
* @version 1.0
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
locations
=
{
"classpath*:/applicationContext-test.xml"
})
public
class
ImpartCode
{
@Autowired
private
TbCfStationItemDao
tbCfStationItemDao
;
@Autowired
private
TbCfItemDescDao
tbCfItemDescDao
;
@Autowired
private
TbCfItemSkusDao
tbCfItemSkusDao
;
@Autowired
private
TbCfCategoryDao
tbCfCategoryDao
;
@Autowired
private
TbCfOptionDao
tbCfOptionDao
;
@Autowired
private
TbCfGoodstypeDao
tbCfGoodstypeDao
;
@Autowired
private
TbProductMissDao
tbProductMissDao
;
@Autowired
private
TbCategoryTemplateDao
tbCategoryTemplateDao
;
@Autowired
private
TbCfGoodstwotypeDao
tbCfGoodstwotypeDao
;
@Autowired
private
TbCfDescripitonDao
tbCfDescripitonDao
;
/**
* 接口数据版本号
*/
private
final
String
VERSION
=
"2020-01"
;
/**
* 接口路径
*/
private
final
String
API_URL
=
"https://mollykitty.myshopify.com/admin/api/"
+
VERSION
+
"/products.json"
;
/**
* 根据分类进行导入
*/
@Test
public
void
goImport
()
{
List
<
String
>
list
=
Arrays
.
asList
(
"Men"
,
"Women"
,
"Children"
,
"Afri Home"
,
"Cosmetics"
,
"Hair"
,
"Sportswear"
,
"Electronics"
);
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
data
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
body
();
System
.
err
.
println
(
product_type
+
":"
+
data
);
// 导入请求
link
=
API_URL
+
"?&product_type="
+
product_type
;
// 开始第一次导入商品
importMethod
(
link
,
pageSize
,
productCount
);
// 获取第一次循环的请求头
String
headerLink
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
header
(
"link"
);
// 获取第一次循环后的请求
headerLink
=
headerLink
.
split
(
";"
)[
0
].
replaceAll
(
"<"
,
""
).
replaceAll
(
">"
,
""
);
// 无限导入
while
(
true
)
{
// 导入商品
productCount
++;
importMethod
(
headerLink
,
pageSize
,
productCount
);
// 获取下一页的链接
try
{
headerLink
=
HttpRequest
.
get
(
headerLink
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
header
(
"link"
)
.
split
(
","
)[
1
];
Console
.
error
(
headerLink
);
headerLink
=
headerLink
.
substring
(
2
,
headerLink
.
indexOf
(
">"
));
}
catch
(
Exception
e
)
{
// 死循环的终点为获取下一页链接抛出数组越界异常
break
;
}
}
}
}
/**
* 商品导入处理
*
* @param data 接口返回数据
* @param pageSize 正在导入的页数
* @param productCount 正在导入的商品数量
*/
private
void
importMethod
(
String
data
,
int
pageSize
,
int
productCount
)
{
// 格式化
JSONObject
jsonObject
=
JSONObject
.
fromObject
(
data
);
// 取每个数组中的 products 数组节点
JSONArray
productsArr
=
jsonObject
.
getJSONArray
(
"products"
);
for
(
int
i
=
0
;
i
<
productsArr
.
size
();
i
++)
{
JSONObject
productsObj
=
productsArr
.
getJSONObject
(
i
);
productCount
++;
Console
.
log
(
"当前正在导入的商品个数为:"
+
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
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
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
(
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
);
}
}
}
/**
* 通过URL获取网络图片
* 获取网络图片流
*
* @param url 传入的 URL 必须是以 http:// 开头的,因为我们使用了 HttpURLConnection
* @return 输入流
*/
public
static
InputStream
getImageStream
(
String
url
)
{
try
{
HttpURLConnection
connection
=
(
HttpURLConnection
)
new
URL
(
url
).
openConnection
();
connection
.
setReadTimeout
(
5000
);
connection
.
setConnectTimeout
(
5000
);
connection
.
setRequestMethod
(
"GET"
);
if
(
connection
.
getResponseCode
()
==
HttpURLConnection
.
HTTP_OK
)
{
return
connection
.
getInputStream
();
}
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"获取网络图片出现异常,图片路径为:"
+
url
);
e
.
printStackTrace
();
}
return
null
;
}
/**
* 将 InputStream 流转换为 byte[]
*
* @param input InputStream
* @return byte[]
* @throws IOException IO流异常
*/
public
static
byte
[]
toByteArray
(
InputStream
input
)
throws
IOException
{
ByteArrayOutputStream
output
=
new
ByteArrayOutputStream
();
byte
[]
buffer
=
new
byte
[
1024
*
4
];
int
n
=
0
;
while
(-
1
!=
(
n
=
input
.
read
(
buffer
)))
{
output
.
write
(
buffer
,
0
,
n
);
}
return
output
.
toByteArray
();
}
}
platform-admin/src/main/test/java/test/TestApi.java
浏览文件 @
af83089e
...
...
@@ -10,6 +10,7 @@ import com.platform.utils.OssUtil;
import
com.platform.utils.R
;
import
net.sf.json.JSONArray
;
import
net.sf.json.JSONObject
;
import
org.apiguardian.api.API
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -25,6 +26,7 @@ import java.net.HttpURLConnection;
import
java.net.URL
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.List
;
/**
* @author 爱酱油不爱醋
...
...
@@ -54,10 +56,20 @@ public class TestApi {
private
TbCfGoodstwotypeDao
tbCfGoodstwotypeDao
;
@Autowired
private
TbCfDescripitonDao
tbCfDescripitonDao
;
/**
* 接口数据版本号
*/
private
final
String
VERSION
=
"2020-01"
;
/**
* 接口路径
*/
private
final
String
API_URL
=
"https://mollykitty.myshopify.com/admin/api/"
+
VERSION
+
"/products.json"
;
@Test
public
void
host
()
{
String
link
=
"https://mollykitty.myshopify.com/admin/api/2019-10/products.json
?limit=250&page_info=eyJwcm9kdWN0X3R5cGUiOiJXb21lbiIsImxhc3RfaWQiOjQ0OTc2Nzg5NTg2ODksImxhc3RfdmFsdWUiOiJDYXN1YWwgU2xpbSBTd2VhdGVycyIsImRpcmVjdGlvbiI6Im5leHQifQ"
;
String
link
=
API_URL
+
"
?limit=250&page_info=eyJwcm9kdWN0X3R5cGUiOiJXb21lbiIsImxhc3RfaWQiOjQ0OTc2Nzg5NTg2ODksImxhc3RfdmFsdWUiOiJDYXN1YWwgU2xpbSBTd2VhdGVycyIsImRpcmVjdGlvbiI6Im5leHQifQ"
;
Console
.
error
(
link
);
link
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
...
...
@@ -67,13 +79,46 @@ public class TestApi {
link
=
link
.
substring
(
2
,
link
.
indexOf
(
">"
));
Console
.
log
(
"链接:"
+
link
);
}
/**
* 测试分类的商品数量
*/
@Test
public
void
testCount
()
{
List
<
String
>
list
=
Arrays
.
asList
(
"Men"
,
"Women"
,
"Children"
,
"Afri Home"
,
"Cosmetics"
,
"Hair"
,
"Sportswear"
,
"Electronics"
);
for
(
String
product_type
:
list
)
{
String
link
=
"https://mollykitty.myshopify.com/admin/api/"
+
VERSION
+
"/products/count.json"
+
"?&product_type="
+
product_type
;
System
.
out
.
println
(
link
);
link
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
body
();
System
.
err
.
println
(
product_type
+
":"
+
link
);
}
}
/**
* 测试第一次连接时的返回
*/
@Test
public
void
getLink
()
{
String
link
=
API_URL
+
"?&product_type="
+
"Men"
+
"&limit=250"
;
String
headerLink
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
header
(
"link"
);
headerLink
=
headerLink
.
split
(
";"
)[
0
].
replaceAll
(
"<"
,
""
).
replaceAll
(
">"
,
""
);
System
.
err
.
println
(
headerLink
);
}
/**
* 连接测试
*/
@Test
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=戴菊"
;
link
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
body
();
JSONObject
json
=
JSONObject
.
fromObject
(
link
);
JSONObject
product
=
json
.
getJSONArray
(
"products"
).
getJSONObject
(
0
);
String
tags
=
product
.
getString
(
"tags"
);
...
...
@@ -85,14 +130,293 @@ public class TestApi {
}
}
}
/**
* 导入商品
*/
@Test
public
void
save
()
{
public
void
test
()
{
String
link
=
""
;
// 这个请求需要密钥,可以先拿着账号密码去浏览器请求一遍,F12 获取 token 复制粘贴即可
String
result
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
body
();
JSONObject
jsonObject
=
JSONObject
.
fromObject
(
result
);
// 第一次导入
once
(
result
);
// 解析请求头,获取到下一页的 url
String
headerLink
=
HttpRequest
.
get
(
link
)
.
header
(
"Authorization"
,
"Basic N2YwZTA0OGFjNDAxNmI5MzU3YmIxZWIyMjE3ZTQyMDE6MWM5N2ExMjIyYzViNDBlMDU5N2M4MTZjNmRmZWNhYzA="
)
.
execute
().
header
(
"link"
);
headerLink
=
headerLink
.
split
(
";"
)[
0
].
replaceAll
(
"<"
,
""
).
replaceAll
(
">"
,
""
);
// 无限导入
unlimited
(
headerLink
);
}
/**
* 第一遍循环导入
*/
private
void
once
(
String
data
)
{
// 记录页数的
int
pageSize
=
0
;
int
productCount
=
0
;
// 初始页
String
link
=
"https://mollykitty.myshopify.com/admin/api/2020-01/products.json?limit=250&page_info=eyJsYXN0X2lkIjo0NDQzNjE1MDAyNzIxLCJsYXN0X3ZhbHVlIjoiQW5kcm9pZCBwaG9uZSBkYXRhIGNhYmxlIiwiZGlyZWN0aW9uIjoibmV4dCJ9"
;
// 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
);
...
...
@@ -110,7 +434,7 @@ public class TestApi {
Console
.
log
(
"当前正在导入的商品个数为:"
+
productCount
);
productCount
++;
// 商品主体信息的商品 id
String
itemId
=
IdUtil
.
simpleUUID
();
// 复用的日期
...
...
@@ -340,257 +664,6 @@ public class TestApi {
}
}
@Test
public
void
onLoop
()
{
// 记录页数的
int
pageSize
=
0
;
int
productCount
=
0
;
// 初始页
String
link
=
"https://mollykitty.myshopify.com/admin/api/2019-10/products.json?limit=150&vendor=戴菊"
;
// 这个请求需要密钥,可以先拿着账号密码去浏览器请求一遍,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
)
{
// 记录失败的产品记录
throw
new
ProductExitException
();
}
// 用于记录 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
(
ProductExitException
e
)
{
Console
.
error
(
"该商品已存在"
);
// 记录已存在的商品
TbProductMissEntity
tbProductMissEntity
=
new
TbProductMissEntity
();
tbProductMissEntity
.
setId
(
IdUtil
.
simpleUUID
());
tbProductMissEntity
.
setProductId
(
id
);
tbProductMissEntity
.
setMissType
(
1
);
tbProductMissEntity
.
setPageSize
(
pageSize
);
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
(
pageSize
);
tbProductMissEntity
.
setCreateTime
(
new
Date
());
tbProductMissDao
.
save
(
tbProductMissEntity
);
}
}
}
/**
* 通过URL获取网络图片
* 获取网络图片流
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论