优化dou订单拉取

This commit is contained in:
启航 2026-01-25 17:04:42 +08:00
parent 2ef4b45ae2
commit b561226db7
5 changed files with 117 additions and 85 deletions

View File

@ -98,7 +98,7 @@
<artifactId>open-sdk</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/open-sdk-2.1.9.jar</systemPath>
<systemPath>${project.basedir}/libs/open-sdk-2.1.11.jar</systemPath>
</dependency>
<dependency>

View File

@ -23,6 +23,7 @@ import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import cn.qihangerp.model.request.OrderPullRequest;
import java.time.Duration;
@ -32,6 +33,8 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 订单更新
@ -47,6 +50,10 @@ public class DouOrderApiController {
private final MqUtils mqUtils;
private final OShopPullLogsService pullLogsService;
private final OShopPullLasttimeService pullLasttimeService;
private final String DATE_PATTERN =
"^(?:(?:(?:\\d{4}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1\\d|2[0-8]))|(?:(?:(?:\\d{2}(?:0[48]|[2468][048]|[13579][26])|(?:(?:0[48]|[2468][048]|[13579][26])00))-0?2-29))$)|(?:(?:(?:\\d{4}-(?:0?[13578]|1[02]))-(?:0?[1-9]|[12]\\d|30))$)|(?:(?:(?:\\d{4}-0?[13-9]|1[0-2])-(?:0?[1-9]|[1-2]\\d|30))$)|(?:(?:(?:\\d{2}(?:0[48]|[13579][26]|[2468][048])|(?:(?:0[48]|[13579][26]|[2468][048])00))-0?2-29))$)$";
private final Pattern DATE_FORMAT = Pattern.compile(DATE_PATTERN);
/**
* 增量更新订单
* @param req
@ -60,6 +67,16 @@ public class DouOrderApiController {
if (req.getShopId() == null || req.getShopId() <= 0) {
return AjaxResult.error(HttpStatus.PARAMS_ERROR, "参数错误没有店铺Id");
}
if(StringUtils.isEmpty(req.getStartTime())) {
return AjaxResult.error("缺少参数:下单日期不能为空");
}
// 判断时间格式
Matcher matcher = DATE_FORMAT.matcher(req.getStartTime());
boolean b = matcher.find();
if (!b) {
return AjaxResult.error("下单日期格式错误");
}
Date currDateTime = new Date();
Long currTimeMillis = System.currentTimeMillis();
@ -72,34 +89,38 @@ public class DouOrderApiController {
String appSecret = checkResult.getData().getAppSecret();
Long douShopId = checkResult.getData().getSellerId();
String accessToken = checkResult.getData().getAccessToken();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 获取最后更新时间
LocalDateTime startTime = null;
LocalDateTime endTime = null;
OShopPullLasttime lasttime = pullLasttimeService.getLasttimeByShop(req.getShopId(), "ORDER");
if(lasttime == null){
endTime = LocalDateTime.now();
startTime = endTime.minusDays(1);
}else {
startTime = lasttime.getLasttime().minusHours(1);//取上次结束一个小时前
Duration duration = Duration.between(startTime, LocalDateTime.now());
long hours = duration.toHours();
if (hours > 24) {
// 大于24小时只取24小时
endTime = startTime.plusHours(24);
} else {
endTime = LocalDateTime.now();
}
// endTime = startTime.plusDays(1);//取24小时
// if(endTime.isAfter(LocalDateTime.now())){
// OShopPullLasttime lasttime = pullLasttimeService.getLasttimeByShop(req.getShopId(), "ORDER");
// if(lasttime == null){
// endTime = LocalDateTime.now();
// startTime = endTime.minusDays(1);
// }else {
// startTime = lasttime.getLasttime().minusHours(1);//取上次结束一个小时前
// Duration duration = Duration.between(startTime, LocalDateTime.now());
// long hours = duration.toHours();
// if (hours > 24) {
// // 大于24小时只取24小时
// endTime = startTime.plusHours(24);
// } else {
// endTime = LocalDateTime.now();
// }
}
String startTimeStr = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endTimeStr = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//// endTime = startTime.plusDays(1);//取24小时
//// if(endTime.isAfter(LocalDateTime.now())){
//// endTime = LocalDateTime.now();
//// }
// }
startTime = LocalDateTime.parse(req.getStartTime() + " 00:00:01", formatter);
endTime = LocalDateTime.parse(req.getStartTime() + " 23:59:59", formatter);
// String startTimeStr = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// String endTimeStr = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Long startTimestamp = startTime.toEpochSecond(ZoneOffset.ofHours(8));
Long endTimestamp = endTime.toEpochSecond(ZoneOffset.ofHours(8));
String pullParams = "{startTime:"+startTime+",endTime:"+endTime+"}";
String pullParams = "{startTime:"+startTime.format(formatter)+",endTime:"+endTime.format(formatter)+"}";
// ApiResultVo<Token> token = DouTokenApiHelper.getToken(appKey, appSecret,checkResult.getData().getSellerId());
//
// if(token.getCode()==0) {
@ -108,7 +129,7 @@ public class DouOrderApiController {
// return AjaxResult.error(token.getMsg());
// }
//第一次获取
ApiResultVo<Order> resultVo = DouOrderApiHelper.pullOrderList(startTimestamp, endTimestamp, 0, 20, appKey, appSecret, accessToken);
ApiResultVo<Order> resultVo = DouOrderApiHelper.pullOrderList(startTimestamp, endTimestamp, 0, 100, appKey, appSecret, accessToken);
if(resultVo.getCode() !=0 ){
OShopPullLogs logs = new OShopPullLogs();
logs.setShopId(req.getShopId());
@ -182,26 +203,26 @@ public class DouOrderApiController {
}
}
if(totalError==0) {
if (lasttime == null) {
// 新增
OShopPullLasttime insertLasttime = new OShopPullLasttime();
insertLasttime.setShopId(req.getShopId());
insertLasttime.setCreateTime(new Date());
insertLasttime.setLasttime(endTime);
insertLasttime.setPullType("ORDER");
pullLasttimeService.save(insertLasttime);
// if(totalError==0) {
// if (lasttime == null) {
// // 新增
// OShopPullLasttime insertLasttime = new OShopPullLasttime();
// insertLasttime.setShopId(req.getShopId());
// insertLasttime.setCreateTime(new Date());
// insertLasttime.setLasttime(endTime);
// insertLasttime.setPullType("ORDER");
// pullLasttimeService.save(insertLasttime);
//
// } else {
// // 修改
// OShopPullLasttime updateLasttime = new OShopPullLasttime();
// updateLasttime.setId(lasttime.getId());
// updateLasttime.setUpdateTime(new Date());
// updateLasttime.setLasttime(endTime);
// pullLasttimeService.updateById(updateLasttime);
// }
// }
} else {
// 修改
OShopPullLasttime updateLasttime = new OShopPullLasttime();
updateLasttime.setId(lasttime.getId());
updateLasttime.setUpdateTime(new Date());
updateLasttime.setLasttime(endTime);
pullLasttimeService.updateById(updateLasttime);
}
}
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
OShopPullLogs logs = new OShopPullLogs();
logs.setShopType(EnumShopType.DOU.getIndex());
logs.setShopId(req.getShopId());
@ -213,7 +234,7 @@ public class DouOrderApiController {
logs.setDuration(System.currentTimeMillis() - currTimeMillis);
pullLogsService.save(logs);
String msg = "成功{startTime:"+startTime.format(df)+",endTime:"+endTime.format(df)+"}总共找到:" + resultVo.getTotalRecords() + "条订单,新增:" + insertSuccess + "条,添加错误:" + totalError + "条,更新:" + hasExistOrder + "";
String msg = "成功{startTime:"+startTime.format(formatter)+",endTime:"+endTime.format(formatter)+"}总共找到:" + resultVo.getTotalRecords() + "条订单,新增:" + insertSuccess + "条,添加错误:" + totalError + "条,更新:" + hasExistOrder + "";
log.info("/**************主动更新DOU订单END" + msg + "****************/");
return AjaxResult.success(msg);
}

View File

@ -58,23 +58,27 @@ public class DouOrderServiceImpl extends ServiceImpl<DouOrderMapper, DouOrder>
if(StringUtils.hasText(bo.getStartTime())){
Matcher matcher = DATE_FORMAT.matcher(bo.getStartTime());
boolean b = matcher.find();
if(b){
bo.setStartTime(bo.getStartTime()+" 00:00:00");
if(!b){
bo.setStartTime("");
// startTimeStamp = DateUtils.dateTimeStrToTimeStamp(null,bo.getStartTime());
}
}
if(StringUtils.hasText(bo.getEndTime())){
Matcher matcher = DATE_FORMAT.matcher(bo.getEndTime());
boolean b = matcher.find();
if(b){
bo.setEndTime(bo.getEndTime()+" 23:59:59");
if(!b){
bo.setEndTime("");
// endTimeStamp = DateUtils.dateTimeStrToTimeStamp(null,bo.getEndTime());
}
}else{
if(StringUtils.hasText(bo.getStartTime())) {
bo.setEndTime(bo.getStartTime());
}
}
if(StringUtils.hasText(bo.getStartTime())) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startTime = LocalDateTime.parse(bo.getStartTime(), formatter);
LocalDateTime endTime = LocalDateTime.parse(bo.getEndTime(), formatter);
LocalDateTime startTime = LocalDateTime.parse(bo.getStartTime()+" 00:00:00", formatter);
LocalDateTime endTime = LocalDateTime.parse(bo.getEndTime()+" 23:59:59", formatter);
startTimestamp = startTime.toEpochSecond(ZoneOffset.ofHours(8));
endTimestamp = endTime.toEpochSecond(ZoneOffset.ofHours(8));

View File

@ -19,13 +19,10 @@
</el-option>
</el-select>
</el-form-item>
<el-form-item label="下单时间" prop="orderTime">
<el-date-picker clearable
v-model="orderTime" value-format="yyyy-MM-dd"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期">
<el-form-item label="下单时间" prop="startTime">
<el-date-picker clearable @change="handleQuery"
v-model="queryParams.startTime" value-format="yyyy-MM-dd"
type="date" placeholder="下单时间">
</el-date-picker>
</el-form-item>
@ -122,7 +119,7 @@
<td width="250px" align="left">标题</td>
<td width="150" align="left">SKU名</td>
<td width="200" align="left">Sku编码</td>
<td width="150" align="left">平台SkuId</td>
<td width="180" align="left">平台SkuId</td>
<td width="50" align="left">数量</td>
</th>
</table>
@ -142,7 +139,7 @@
</template>
</el-table-column>
<el-table-column label="Sku编码" align="left" prop="outerSkuId" width="200"/>
<el-table-column label="平台SkuId" align="left" prop="skuId" width="150"/>
<el-table-column label="平台SkuId" align="left" prop="skuId" width="180"/>
<el-table-column label="商品数量" align="center" prop="itemNum" width="50px">
<template slot-scope="scope">
<el-tag size="small" type="danger">{{scope.row.itemNum}}</el-tag>
@ -410,9 +407,12 @@ export default {
this.queryParams.startTime = this.orderTime[0]
this.queryParams.endTime = this.orderTime[1]
}else {
if(!this.queryParams.startTime){
this.queryParams.startTime = null
this.queryParams.endTime = null
}
}
this.loading = true;
listOrder(this.queryParams).then(response => {
this.orderList = response.rows;
@ -465,13 +465,24 @@ export default {
this.multiple = !selection.length
},
handlePull() {
if(this.queryParams.shopId){
if (!this.queryParams.shopId) {
this.$modal.msgError("请选择店铺");
return
}
if (!this.queryParams.startTime) {
this.$modal.msgError("请选择下单时间");
return
}
this.pullLoading = true
pullOrder({shopId:this.queryParams.shopId,updType:0}).then(response => {
console.log('拉取dou订单接口返回=====',response)
if(response.code === 1401) {
MessageBox.confirm('Token已过期需要重新授权请前往店铺列表重新获取授权', '系统提示', { confirmButtonText: '前往授权', cancelButtonText: '取消', type: 'warning' }).then(() => {
this.$router.push({path:"/shop/shop_list",query:{type:3}})
pullOrder({shopId: this.queryParams.shopId,startTime:this.queryParams.startTime}).then(response => {
console.log('拉取dou订单接口返回=====', response)
if (response.code === 1401) {
MessageBox.confirm('Token已过期需要重新授权请前往店铺列表重新获取授权', '系统提示', {
confirmButtonText: '前往授权',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.$router.push({path: "/shop/shop_list", query: {type: 400}})
// isRelogin.show = false;
// store.dispatch('LogOut').then(() => {
// location.href = response.data.tokenRequestUrl+'?shopId='+this.queryParams.shopId
@ -481,18 +492,14 @@ export default {
});
// return Promise.reject('')
}else{
}else if(response.code === 200){
this.$modal.msgSuccess(JSON.stringify(response));
this.getList()
this.pullLoading = false
}else{
this.$modal.msgError(response.msg)
}
this.pullLoading = false
})
}else{
this.$modal.msgSuccess("请先选择店铺");
}
// this.$modal.msgSuccess("API");
},
handlePullUpdate(row) {
//