qihang-ecom-erp-open/jd-api/src/main/java/com/qihang/jd/controller/RefundApiController.java

308 lines
15 KiB
Java
Raw Normal View History

2024-03-05 15:40:39 +08:00
package com.qihang.jd.controller;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.request.refundapply.PopAfsRefundapplyQuerylistRequest;
2024-03-10 20:05:38 +08:00
import com.jd.open.api.sdk.request.shangjiashouhou.AscQueryListRequest;
2024-03-10 20:31:11 +08:00
import com.jd.open.api.sdk.request.shangjiashouhou.AscSyncListRequest;
2024-03-05 15:40:39 +08:00
import com.jd.open.api.sdk.response.refundapply.PopAfsRefundapplyQuerylistResponse;
2024-03-10 20:05:38 +08:00
import com.jd.open.api.sdk.response.shangjiashouhou.AscQueryListResponse;
2024-03-10 20:31:11 +08:00
import com.jd.open.api.sdk.response.shangjiashouhou.AscSyncListResponse;
2024-03-05 15:40:39 +08:00
import com.qihang.common.common.ApiResult;
2024-03-10 17:25:25 +08:00
import com.qihang.common.common.ResultVoEnum;
import com.qihang.common.enums.EnumShopType;
2024-03-05 15:40:39 +08:00
import com.qihang.common.enums.HttpStatus;
2024-03-10 17:25:25 +08:00
import com.qihang.common.mq.MqMessage;
import com.qihang.common.mq.MqType;
import com.qihang.common.mq.MqUtils;
2024-03-10 20:05:38 +08:00
import com.qihang.jd.domain.JdOrderAfter;
2024-03-10 17:25:25 +08:00
import com.qihang.jd.domain.JdRefund;
2024-03-10 20:05:38 +08:00
import com.qihang.jd.domain.SysShopPullLasttime;
2024-03-10 17:25:25 +08:00
import com.qihang.jd.domain.SysShopPullLogs;
2024-03-09 20:59:36 +08:00
import com.qihang.jd.openApi.ApiCommon;
import com.qihang.jd.openApi.PullRequest;
2024-03-10 20:05:38 +08:00
import com.qihang.jd.service.JdOrderAfterService;
2024-03-10 17:25:25 +08:00
import com.qihang.jd.service.JdRefundService;
2024-03-10 20:05:38 +08:00
import com.qihang.jd.service.SysShopPullLasttimeService;
2024-03-10 17:25:25 +08:00
import com.qihang.jd.service.SysShopPullLogsService;
2024-03-05 15:40:39 +08:00
import lombok.AllArgsConstructor;
2024-03-10 17:25:25 +08:00
import org.springframework.beans.BeanUtils;
2024-03-05 15:40:39 +08:00
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
2024-03-10 20:05:38 +08:00
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
2024-03-10 17:25:25 +08:00
import java.util.Date;
2024-03-05 15:40:39 +08:00
@RequestMapping("/refund")
@RestController
@AllArgsConstructor
public class RefundApiController {
private final ApiCommon apiCommon;
2024-03-10 17:25:25 +08:00
private final SysShopPullLogsService pullLogsService;
private final JdRefundService refundService;
private final MqUtils mqUtils;
2024-03-10 20:05:38 +08:00
private final SysShopPullLasttimeService pullLasttimeService;
private final JdOrderAfterService afterService;
2024-03-10 17:25:25 +08:00
2024-03-10 20:05:38 +08:00
@RequestMapping(value = "/pull_refund_list", method = RequestMethod.POST)
public Object pullRefundList(@RequestBody PullRequest params) throws Exception {
2024-03-05 15:40:39 +08:00
if (params.getShopId() == null || params.getShopId() <= 0) {
return ApiResult.build(HttpStatus.PARAMS_ERROR, "参数错误没有店铺Id");
}
2024-03-10 17:25:25 +08:00
Date currDateTime = new Date();
long beginTime = System.currentTimeMillis();
2024-03-05 15:40:39 +08:00
var checkResult = apiCommon.checkBefore(params.getShopId());
if (checkResult.getCode() != HttpStatus.SUCCESS) {
return ApiResult.build(checkResult.getCode(), checkResult.getMsg(), checkResult.getData());
}
String accessToken = checkResult.getData().getAccessToken();
String serverUrl = checkResult.getData().getServerUrl();
String appKey = checkResult.getData().getAppKey();
String appSecret = checkResult.getData().getAppSecret();
JdClient client = new DefaultJdClient(serverUrl, accessToken, appKey, appSecret);
//https://open.jd.com/home/home/#/doc/api?apiCateId=71&apiId=307&apiName=jingdong.pop.afs.refundapply.querylist
2024-03-12 20:17:03 +08:00
PopAfsRefundapplyQuerylistRequest request2 = new PopAfsRefundapplyQuerylistRequest();
2024-03-05 15:40:39 +08:00
request2.setPageIndex(1);
2024-03-09 22:16:20 +08:00
request2.setPageSize(100);
2024-03-12 20:17:03 +08:00
PopAfsRefundapplyQuerylistResponse response2 = client.execute(request2);
2024-03-10 17:25:25 +08:00
// System.out.println(request2);
int hasExist = 0;
int insertSuccess = 0;
int totalError = 0;
2024-03-12 20:17:03 +08:00
if (response2 != null && response2.getRefundApplyResponse() != null) {
for (var item : response2.getRefundApplyResponse().getResults()) {
2024-03-10 17:25:25 +08:00
JdRefund refund = new JdRefund();
2024-03-12 20:17:03 +08:00
BeanUtils.copyProperties(item, refund);
2024-03-10 17:25:25 +08:00
refund.setRefundId(item.getId());
refund.setId(null);
refund.setShopId(params.getShopId());
//插入订单数据
var result = refundService.saveRefund(params.getShopId(), refund);
if (result.getCode() == ResultVoEnum.DataExist.getIndex()) {
//已经存在
hasExist++;
2024-03-10 20:05:38 +08:00
// mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD, MqType.REFUND_MESSAGE,item.getId()));
2024-03-10 17:25:25 +08:00
} else if (result.getCode() == ResultVoEnum.SUCCESS.getIndex()) {
insertSuccess++;
2024-03-10 20:05:38 +08:00
// mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD,MqType.REFUND_MESSAGE,item.getId()));
2024-03-10 17:25:25 +08:00
} else {
totalError++;
}
}
}
SysShopPullLogs logs = new SysShopPullLogs();
logs.setShopId(params.getShopId());
logs.setPullType("REFUND");
logs.setPullWay("主动拉取");
logs.setPullParams("{PageIndex:1,PageSize:100}");
2024-03-12 20:17:03 +08:00
logs.setPullResult("{total:" + insertSuccess + ",hasExist:" + hasExist + ",totalError:" + totalError + "}");
2024-03-10 17:25:25 +08:00
logs.setPullTime(currDateTime);
logs.setDuration(System.currentTimeMillis() - beginTime);
pullLogsService.save(logs);
2024-03-05 15:40:39 +08:00
return response2;
}
2024-03-10 20:05:38 +08:00
/**
* 拉取售后数据
2024-03-12 20:17:03 +08:00
*
2024-03-10 20:05:38 +08:00
* @param params
* @return
* @throws Exception
*/
@RequestMapping(value = "/pull_list", method = RequestMethod.POST)
public Object pullList(@RequestBody PullRequest params) throws Exception {
if (params.getShopId() == null || params.getShopId() <= 0) {
return ApiResult.build(HttpStatus.PARAMS_ERROR, "参数错误没有店铺Id");
}
Date currDateTime = new Date();
long beginTime = System.currentTimeMillis();
var checkResult = apiCommon.checkBefore(params.getShopId());
if (checkResult.getCode() != HttpStatus.SUCCESS) {
return ApiResult.build(checkResult.getCode(), checkResult.getMsg(), checkResult.getData());
}
String accessToken = checkResult.getData().getAccessToken();
String serverUrl = checkResult.getData().getServerUrl();
String appKey = checkResult.getData().getAppKey();
String appSecret = checkResult.getData().getAppSecret();
String sellerId = checkResult.getData().getSellerId();
// 获取最后更新时间
LocalDateTime startTime = null;
2024-03-12 20:17:03 +08:00
LocalDateTime endTime = null;
2024-03-10 20:05:38 +08:00
SysShopPullLasttime lasttime = pullLasttimeService.getLasttimeByShop(params.getShopId(), "REFUND");
2024-03-12 20:17:03 +08:00
if (lasttime == null) {
2024-03-10 20:05:38 +08:00
endTime = LocalDateTime.now();
startTime = endTime.minusDays(1);
2024-03-12 20:17:03 +08:00
} else {
2024-03-10 20:05:38 +08:00
startTime = lasttime.getLasttime().minusHours(1);//取上次结束一个小时前
endTime = startTime.plusDays(1);//取24小时
2024-03-12 20:17:03 +08:00
if (endTime.isAfter(LocalDateTime.now())) {
2024-03-10 20:05:38 +08:00
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"));
JdClient client = new DefaultJdClient(serverUrl, accessToken, appKey, appSecret);
2024-03-10 20:31:11 +08:00
// 用于更新状态
// https://open.jd.com/home/home/#/doc/api?apiCateId=241&apiId=2171&apiName=jingdong.asc.sync.list
2024-03-12 20:17:03 +08:00
AscSyncListRequest request1 = new AscSyncListRequest();
2024-03-10 20:31:11 +08:00
request1.setBuId(sellerId);
request1.setOperatePin("testPin");
request1.setOperateNick("testPin");
2024-03-12 20:17:03 +08:00
request1.setUpdateTimeBegin(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()));
request1.setUpdateTimeEnd(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant()));
2024-03-10 20:31:11 +08:00
request1.setPageNumber(1);
request1.setPageSize(10);
2024-03-12 20:17:03 +08:00
AscSyncListResponse response1 = client.execute(request1);
2024-03-10 20:31:11 +08:00
2024-03-10 20:05:38 +08:00
// https://open.jd.com/home/home/#/doc/api?apiCateId=241&apiId=2136&apiName=jingdong.asc.query.list
2024-03-12 20:17:03 +08:00
AscQueryListRequest request = new AscQueryListRequest();
2024-03-10 20:05:38 +08:00
request.setBuId(sellerId);
request.setOperatePin("testPin");
request.setOperateNick("testPin");
// request.setServiceId(123456);
// request.setOrderId(123456);
2024-03-12 20:17:03 +08:00
request.setApplyTimeBegin(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()));
request.setApplyTimeEnd(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant()));
2024-03-10 20:05:38 +08:00
// request.setFinishTimeBegin(Date.from(startTime.atZone( ZoneId.systemDefault()).toInstant()));
// request.setFinishTimeEnd(Date.from(endTime.atZone( ZoneId.systemDefault()).toInstant()));
// request.setVerificationCode("abcd");
// request.setExpressCode("JDVA1234");
// request.setOrderType(0);
// request.setProcessResult(40);
// request.setCustomerPin("testPin");
// request.setCustomerName("testPin");
// request.setCustomerTel("123456");
// request.setApproveTimeBegin(Date.from(startTime.atZone( ZoneId.systemDefault()).toInstant()));
// request.setApproveTimeEnd(Date.from(endTime.atZone( ZoneId.systemDefault()).toInstant()));
request.setPageNumber(1);
request.setPageSize(100);
// request.setExtJsonStr("a");
2024-03-12 20:17:03 +08:00
AscQueryListResponse response = client.execute(request);
2024-03-10 20:05:38 +08:00
int insertSuccess = 0;
int totalError = 0;
int hasExist = 0;
2024-03-12 20:17:03 +08:00
if (response != null && response.getPageResult() != null) {
if (response.getPageResult().getData() != null) {
for (var item : response.getPageResult().getData()) {
2024-03-10 20:05:38 +08:00
JdOrderAfter after = new JdOrderAfter();
2024-03-12 20:17:03 +08:00
BeanUtils.copyProperties(item, after);
2024-03-10 20:05:38 +08:00
after.setShopId(params.getShopId());
var result = afterService.saveAfter(params.getShopId(), after);
if (result.getCode() == ResultVoEnum.DataExist.getIndex()) {
//已经存在
hasExist++;
2024-03-12 20:17:03 +08:00
mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD, MqType.REFUND_MESSAGE, item.getApplyId().toString()));
2024-03-10 20:05:38 +08:00
} else if (result.getCode() == ResultVoEnum.SUCCESS.getIndex()) {
insertSuccess++;
2024-03-12 20:17:03 +08:00
mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD, MqType.REFUND_MESSAGE, item.getApplyId().toString()));
2024-03-10 20:05:38 +08:00
} else {
totalError++;
}
}
}
}
2024-03-12 20:17:03 +08:00
if (lasttime == null) {
2024-03-10 20:05:38 +08:00
// 新增
SysShopPullLasttime insertLasttime = new SysShopPullLasttime();
insertLasttime.setShopId(params.getShopId());
insertLasttime.setCreateTime(new Date());
insertLasttime.setLasttime(endTime);
insertLasttime.setPullType("REFUND");
pullLasttimeService.save(insertLasttime);
2024-03-12 20:17:03 +08:00
} else {
2024-03-10 20:05:38 +08:00
// 修改
SysShopPullLasttime updateLasttime = new SysShopPullLasttime();
updateLasttime.setId(lasttime.getId());
updateLasttime.setUpdateTime(new Date());
updateLasttime.setLasttime(endTime);
pullLasttimeService.updateById(updateLasttime);
}
SysShopPullLogs logs = new SysShopPullLogs();
logs.setShopId(params.getShopId());
logs.setPullType("REFUND");
logs.setPullWay("主动拉取");
2024-03-12 20:17:03 +08:00
logs.setPullParams("{ApplyTimeBegin:" + startTimeStr + ",ApplyTimeEnd:" + endTimeStr + ",PageIndex:1,PageSize:100}");
logs.setPullResult("{total:" + insertSuccess + ",hasExist:" + hasExist + ",totalError:" + totalError + "}");
logs.setPullTime(currDateTime);
logs.setDuration(System.currentTimeMillis() - beginTime);
pullLogsService.save(logs);
// mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD, MqType.REFUND_MESSAGE,item.getId()));
return response;
}
@RequestMapping(value = "/pull_update_status", method = RequestMethod.POST)
public Object pullUpdateStatus(@RequestBody PullRequest params) throws Exception {
if (params.getShopId() == null || params.getShopId() <= 0) {
return ApiResult.build(HttpStatus.PARAMS_ERROR, "参数错误没有店铺Id");
}
Date currDateTime = new Date();
long beginTime = System.currentTimeMillis();
var checkResult = apiCommon.checkBefore(params.getShopId());
if (checkResult.getCode() != HttpStatus.SUCCESS) {
return ApiResult.build(checkResult.getCode(), checkResult.getMsg(), checkResult.getData());
}
String accessToken = checkResult.getData().getAccessToken();
String serverUrl = checkResult.getData().getServerUrl();
String appKey = checkResult.getData().getAppKey();
String appSecret = checkResult.getData().getAppSecret();
String sellerId = checkResult.getData().getSellerId();
// 取24小时内的数据
LocalDateTime endTime = LocalDateTime.now();
LocalDateTime startTime = endTime.minusDays(1);
String startTimeStr = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endTimeStr = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
JdClient client = new DefaultJdClient(serverUrl, accessToken, appKey, appSecret);
// 用于更新状态
// https://open.jd.com/home/home/#/doc/api?apiCateId=241&apiId=2171&apiName=jingdong.asc.sync.list
AscSyncListRequest request1 = new AscSyncListRequest();
request1.setBuId(sellerId);
request1.setOperatePin("testPin");
request1.setOperateNick("testPin");
request1.setUpdateTimeBegin(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()));
request1.setUpdateTimeEnd(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant()));
request1.setPageNumber(1);
request1.setPageSize(100);
AscSyncListResponse response = client.execute(request1);
if (response != null && response.getPageResult() != null) {
if (response.getPageResult().getData() != null) {
for (var item : response.getPageResult().getData()) {
JdOrderAfter after = new JdOrderAfter();
BeanUtils.copyProperties(item, after);
after.setShopId(params.getShopId());
var result = afterService.updateAfterStatusByServiceId(after);
if (result.getCode() == ResultVoEnum.SUCCESS.getIndex()) {
// 更新成功,发送通知
mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD, MqType.REFUND_MESSAGE,result.getData().toString()));
}
}
}
}
SysShopPullLogs logs = new SysShopPullLogs();
logs.setShopId(params.getShopId());
logs.setPullType("REFUND");
logs.setPullWay("主动更新状态");
logs.setPullParams("{ApplyTimeBegin:" + startTimeStr + ",ApplyTimeEnd:" + endTimeStr + ",PageIndex:1,PageSize:100}");
logs.setPullResult("{total:,hasExist: ,totalError: }");
2024-03-10 20:05:38 +08:00
logs.setPullTime(currDateTime);
logs.setDuration(System.currentTimeMillis() - beginTime);
pullLogsService.save(logs);
// mqUtils.sendApiMessage(MqMessage.build(EnumShopType.JD, MqType.REFUND_MESSAGE,item.getId()));
return response;
}
2024-03-05 15:40:39 +08:00
}