Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dc-java
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huluobin
dc-java
Commits
c2ffb8fa
Commit
c2ffb8fa
authored
Jul 25, 2019
by
wutong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载报告失败重试3次
parent
3528fbed
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
75 deletions
+85
-75
AmazonAdProductJob.java
...d/src/main/java/com/bailuntec/job/AmazonAdProductJob.java
+85
-75
No files found.
data-base/base-sync-amazon-ad/src/main/java/com/bailuntec/job/AmazonAdProductJob.java
View file @
c2ffb8fa
...
...
@@ -165,99 +165,102 @@ public class AmazonAdProductJob extends PointJob {
private
void
handleReport
(
String
reportLocationUrl
,
JobAccountLog
jobAccountLog
,
AmazonAdAuth
amazonAdAuth
){
Response
response
=
null
;
GZIPInputStream
gzin
=
null
;
try
{
Request
request
=
new
Request
.
Builder
()
.
url
(
reportLocationUrl
)
.
get
()
.
addHeader
(
"Authorization"
,
amazonAdAuth
.
getAccessToken
())
.
addHeader
(
"Amazon-Advertising-API-ClientId"
,
amazonAdAuth
.
getClientId
())
.
addHeader
(
"Amazon-Advertising-API-Scope"
,
amazonAdAuth
.
getProfileId
())
.
addHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
response
=
client
.
newCall
(
request
).
execute
();
if
(
response
.
isSuccessful
())
{
gzin
=
new
GZIPInputStream
(
response
.
body
().
byteStream
());
List
<
AmazonAdProduct
>
amazonAdProductList
=
JSON
.
parseObject
(
gzin
,
new
TypeReference
<
List
<
AmazonAdProduct
>>(){}.
getType
());
gzin
.
close
();
response
.
close
();
if
(
amazonAdProductList
!=
null
&&
amazonAdProductList
.
size
()
>
0
)
{
DcBaseFinanceAmazonAdProduct
dcBaseFinanceAmazonAdProduct
=
new
DcBaseFinanceAmazonAdProduct
();
DcBaseFinanceAmazonAdProductMapper
mapper
=
SessionUtil
.
getSession
().
getMapper
(
DcBaseFinanceAmazonAdProductMapper
.
class
);
for
(
AmazonAdProduct
amazonAdvert
:
amazonAdProductList
)
{
if
(
BigDecimal
.
ZERO
.
compareTo
(
amazonAdvert
.
getCost
())
!=
0
)
{
BeanUtils
.
copyProperties
(
dcBaseFinanceAmazonAdProduct
,
amazonAdvert
);
dcBaseFinanceAmazonAdProduct
.
setAccountId
(
jobAccountLog
.
getAccountId
());
dcBaseFinanceAmazonAdProduct
.
setCompanyId
(
jobAccountLog
.
getCompanyId
());
dcBaseFinanceAmazonAdProduct
.
setReportDate
(
jobAccountLog
.
getStartTime
().
toLocalDate
());
dcBaseFinanceAmazonAdProduct
.
setExchangeRate
(
CallBailunSystem
.
getExchangeRate
(
dcBaseFinanceAmazonAdProduct
.
getCurrency
(),
CurrencyType
.
CNY
.
value
(),
jobAccountLog
.
getStartTime
()));
dcBaseFinanceAmazonAdProduct
.
setExchangeRateUsd
(
CallBailunSystem
.
getExchangeRate
(
dcBaseFinanceAmazonAdProduct
.
getCurrency
(),
CurrencyType
.
USD
.
value
(),
jobAccountLog
.
getStartTime
()));
dcBaseFinanceAmazonAdProduct
.
setBjModifyTime
(
LocalDateTime
.
now
());
DcBaseFinanceAmazonAdProductExample
example
=
DcBaseFinanceAmazonAdProductExample
.
newAndCreateCriteria
().
andCompanyIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getCompanyId
()).
andAccountIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getAccountId
()).
andCampaignIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getCampaignId
()).
andAdGroupIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getAdGroupId
()).
andSkuEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getSku
()).
andReportDateEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getReportDate
()).
example
();
int
i
=
mapper
.
updateByExampleSelective
(
dcBaseFinanceAmazonAdProduct
,
example
);
if
(
i
==
0
)
{
mapper
.
insertSelective
(
dcBaseFinanceAmazonAdProduct
);
Request
request
=
new
Request
.
Builder
()
.
url
(
reportLocationUrl
)
.
get
()
.
addHeader
(
"Authorization"
,
amazonAdAuth
.
getAccessToken
())
.
addHeader
(
"Amazon-Advertising-API-ClientId"
,
amazonAdAuth
.
getClientId
())
.
addHeader
(
"Amazon-Advertising-API-Scope"
,
amazonAdAuth
.
getProfileId
())
.
addHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
try
{
response
=
client
.
newCall
(
request
).
execute
();
if
(
response
.
isSuccessful
())
{
gzin
=
new
GZIPInputStream
(
response
.
body
().
byteStream
());
List
<
AmazonAdProduct
>
amazonAdProductList
=
JSON
.
parseObject
(
gzin
,
new
TypeReference
<
List
<
AmazonAdProduct
>>()
{
}.
getType
());
gzin
.
close
();
response
.
close
();
if
(
amazonAdProductList
!=
null
&&
amazonAdProductList
.
size
()
>
0
)
{
DcBaseFinanceAmazonAdProduct
dcBaseFinanceAmazonAdProduct
=
new
DcBaseFinanceAmazonAdProduct
();
DcBaseFinanceAmazonAdProductMapper
mapper
=
SessionUtil
.
getSession
().
getMapper
(
DcBaseFinanceAmazonAdProductMapper
.
class
);
for
(
AmazonAdProduct
amazonAdvert
:
amazonAdProductList
)
{
if
(
BigDecimal
.
ZERO
.
compareTo
(
amazonAdvert
.
getCost
())
!=
0
)
{
BeanUtils
.
copyProperties
(
dcBaseFinanceAmazonAdProduct
,
amazonAdvert
);
dcBaseFinanceAmazonAdProduct
.
setAccountId
(
jobAccountLog
.
getAccountId
());
dcBaseFinanceAmazonAdProduct
.
setCompanyId
(
jobAccountLog
.
getCompanyId
());
dcBaseFinanceAmazonAdProduct
.
setReportDate
(
jobAccountLog
.
getStartTime
().
toLocalDate
());
dcBaseFinanceAmazonAdProduct
.
setExchangeRate
(
CallBailunSystem
.
getExchangeRate
(
dcBaseFinanceAmazonAdProduct
.
getCurrency
(),
CurrencyType
.
CNY
.
value
(),
jobAccountLog
.
getStartTime
()));
dcBaseFinanceAmazonAdProduct
.
setExchangeRateUsd
(
CallBailunSystem
.
getExchangeRate
(
dcBaseFinanceAmazonAdProduct
.
getCurrency
(),
CurrencyType
.
USD
.
value
(),
jobAccountLog
.
getStartTime
()));
dcBaseFinanceAmazonAdProduct
.
setBjModifyTime
(
LocalDateTime
.
now
());
DcBaseFinanceAmazonAdProductExample
example
=
DcBaseFinanceAmazonAdProductExample
.
newAndCreateCriteria
().
andCompanyIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getCompanyId
()).
andAccountIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getAccountId
()).
andCampaignIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getCampaignId
()).
andAdGroupIdEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getAdGroupId
()).
andSkuEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getSku
()).
andReportDateEqualTo
(
dcBaseFinanceAmazonAdProduct
.
getReportDate
()).
example
();
int
update
=
mapper
.
updateByExampleSelective
(
dcBaseFinanceAmazonAdProduct
,
example
);
if
(
update
==
0
)
{
mapper
.
insertSelective
(
dcBaseFinanceAmazonAdProduct
);
}
}
}
amazonAdProductList
=
null
;
}
amazonAdProductList
=
null
;
/*
* 今天只能拿到昨天的广告费, 不然不完整
* 如果开始时间比今天0点晚, 就设为22点(或者你随便减几个小时都可以)
*/
LocalDateTime
zeroTime
=
LocalDateTime
.
of
(
LocalDate
.
now
(),
LocalTime
.
MIN
);
LocalDateTime
overStartTime
=
jobAccountLog
.
getEndTime
().
isAfter
(
zeroTime
)
?
zeroTime
.
minusHours
(
8
)
:
jobAccountLog
.
getEndTime
();
jobAccountLog
.
setStartTime
(
overStartTime
);
jobAccountLog
.
setEndTime
(
jobAccountLog
.
getStartTime
().
plusDays
(
jobAccountLog
.
getIntervalTime
()).
isAfter
(
LocalDateTime
.
now
())
?
LocalDateTime
.
now
()
:
jobAccountLog
.
getEndTime
().
plusDays
(
jobAccountLog
.
getIntervalTime
()));
jobAccountLog
.
setMessage
(
"执行成功"
);
break
;
}
else
{
String
s
=
response
.
body
().
string
();
log
.
warn
(
"下载报告不成功:"
+
s
);
jobAccountLog
.
setMessage
(
s
);
}
/*
* 今天只能拿到昨天的广告费, 不然不完整
* 如果开始时间比今天0点晚, 就设为22点(或者你随便减几个小时都可以)
*/
LocalDateTime
zeroTime
=
LocalDateTime
.
of
(
LocalDate
.
now
(),
LocalTime
.
MIN
);
LocalDateTime
overStartTime
=
jobAccountLog
.
getEndTime
().
isAfter
(
zeroTime
)
?
zeroTime
.
minusHours
(
8
)
:
jobAccountLog
.
getEndTime
();
jobAccountLog
.
setStartTime
(
overStartTime
);
jobAccountLog
.
setEndTime
(
jobAccountLog
.
getStartTime
().
plusDays
(
jobAccountLog
.
getIntervalTime
()).
isAfter
(
LocalDateTime
.
now
())
?
LocalDateTime
.
now
()
:
jobAccountLog
.
getEndTime
().
plusDays
(
jobAccountLog
.
getIntervalTime
()));
jobAccountLog
.
setMessage
(
"执行成功"
);
}
else
{
String
s
=
response
.
body
().
string
();
log
.
warn
(
"下载报告不成功:"
+
s
);
jobAccountLog
.
setMessage
(
s
);
}
}
catch
(
Exception
e
){
e
.
printStackTrace
();
log
.
error
(
"调用获取AmazonADReportLocation错误:"
,
e
);
jobAccountLog
.
setMessage
(
reportLocationUrl
+
"调用获取AmazonADReportLocation错误:"
+
ExceptionUtil
.
transform
(
e
));
}
finally
{
if
(
gzin
!=
null
)
{
try
{
gzin
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
log
.
error
(
"gzin关闭错误:"
,
e
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
log
.
error
(
"调用获取AmazonADReportLocation错误:"
,
e
);
jobAccountLog
.
setMessage
(
"第"
+
i
+
"次调用获取AmazonADReportLocation错误:"
+
ExceptionUtil
.
transform
(
e
));
}
finally
{
if
(
gzin
!=
null
)
{
try
{
gzin
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
log
.
error
(
"gzin关闭错误:"
,
e
);
}
}
if
(
response
!=
null
)
{
response
.
close
();
}
}
if
(
response
!=
null
)
{
response
.
close
();
try
{
Thread
.
sleep
(
SLEEP_WAIT
);
}
catch
(
InterruptedException
e
)
{
jobAccountLog
.
setMessage
(
"线程异常"
+
ExceptionUtil
.
transform
(
e
));
e
.
printStackTrace
();
Thread
.
currentThread
().
interrupt
();
}
}
}
private
String
getReportLocation
(
String
reportId
,
JobAccountLog
jobAccountLog
,
AmazonAdAuth
amazonAdAuth
,
String
siteEn
)
{
Response
response
=
null
;
Request
request
=
new
Request
.
Builder
()
.
url
(
switchSiteUrl
(
siteEn
,
reportId
)
+
reportId
)
.
addHeader
(
"Authorization"
,
amazonAdAuth
.
getAccessToken
())
.
addHeader
(
"Amazon-Advertising-API-ClientId"
,
amazonAdAuth
.
getClientId
())
.
addHeader
(
"Amazon-Advertising-API-Scope"
,
amazonAdAuth
.
getProfileId
())
.
addHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
try
{
Request
request
=
new
Request
.
Builder
()
.
url
(
switchSiteUrl
(
siteEn
,
reportId
)
+
reportId
)
.
addHeader
(
"Authorization"
,
amazonAdAuth
.
getAccessToken
())
.
addHeader
(
"Amazon-Advertising-API-ClientId"
,
amazonAdAuth
.
getClientId
())
.
addHeader
(
"Amazon-Advertising-API-Scope"
,
amazonAdAuth
.
getProfileId
())
.
addHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
response
=
client
.
newCall
(
request
).
execute
();
if
(
response
.
isSuccessful
())
{
AmazonAdReportLocationResult
reportLocationResult
=
JSON
.
parseObject
(
response
.
body
().
string
(),
AmazonAdReportLocationResult
.
class
);
if
(
reportLocationResult
.
getStatus
().
equals
(
"SUCCESS"
))
{
return
reportLocationResult
.
getLocation
();
}
try
{
Thread
.
sleep
(
60000
);
}
catch
(
InterruptedException
e
)
{
jobAccountLog
.
setMessage
(
"线程异常"
+
ExceptionUtil
.
transform
(
e
));
e
.
printStackTrace
();
Thread
.
currentThread
().
interrupt
();
}
return
getReportLocation
(
reportId
,
jobAccountLog
,
amazonAdAuth
,
siteEn
);
}
else
{
String
s
=
response
.
body
().
string
();
log
.
warn
(
"获取报告下载地址不成功:"
+
s
);
...
...
@@ -271,7 +274,14 @@ public class AmazonAdProductJob extends PointJob {
response
.
close
();
}
}
return
null
;
try
{
Thread
.
sleep
(
60000
);
}
catch
(
InterruptedException
e
)
{
jobAccountLog
.
setMessage
(
"线程异常"
+
ExceptionUtil
.
transform
(
e
));
e
.
printStackTrace
();
Thread
.
currentThread
().
interrupt
();
}
return
getReportLocation
(
reportId
,
jobAccountLog
,
amazonAdAuth
,
siteEn
);
}
private
String
postReportId
(
JobAccountLog
jobAccountLog
,
AmazonAdAuth
amazonAdAuth
,
String
siteEn
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment