This commit is contained in:
Leo
2024-11-18 00:03:09 +08:00
parent d316243d24
commit 7a6cbc2f7d
2 changed files with 169 additions and 128 deletions

3
.idea/misc.xml generated
View File

@@ -11,4 +11,7 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8(202)" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8(202)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project> </project>

View File

@@ -17,12 +17,14 @@ import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionCommonGetRequest; import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionCommonGetRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse; import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionCommonGetResponse; import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionCommonGetResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.SetOperations; import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -59,13 +61,14 @@ public class JDUtils {
private static final String ACCESS_TOKEN = ""; private static final String ACCESS_TOKEN = "";
//标记唯一订单行:订单+sku维度的唯一标识 //标记唯一订单行:订单+sku维度的唯一标识
private static final String ORDER_ROW_KEY = "jd:order:row:"; private static final String ORDER_ROW_KEY = "jd:order:row:";
private static final Logger logger = LoggerFactory.getLogger(JDUtils.class);
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Autowired @Resource
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;
@Autowired @Resource
private OrderRowRepository orderRowRepository; private OrderRowRepository orderRowRepository;
@Autowired @Resource
private WXUtil wxUtil; private WXUtil wxUtil;
/** /**
@@ -215,6 +218,126 @@ public class JDUtils {
} }
/**
* 每小时拉取过去两个月的订单
* 因为有的延迟发货,而接口只能拉取两个月前的数据
*/
@Scheduled(cron = "0 0 * * * ?")
public void fetchHistoricalOrders() throws Exception {
// 从设定的开始日期到昨天的同一时间
System.out.println("开始拉取历史订单");
// 拉最近两个月的订单
// 获取当前时间,并调整为整点开始
LocalDateTime startDate = LocalDateTime.now().minusMonths(2).truncatedTo(ChronoUnit.HOURS);
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastHour = now.truncatedTo(ChronoUnit.HOURS);
while (!startDate.isEqual(lastHour)) {
startDate = startDate.plusHours(1);
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startDate, false); // 假的代表历史订单
if (response != null) {
int code = response.getQueryResult().getCode();
if (code == 200) {
if (response.getQueryResult().getCode() == 200) {
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
if (orderRowResps == null) {
continue;
}
for (OrderRowResp orderRowResp : orderRowResps) {
// 固化到数据库
OrderRow orderRow = createOrderRow(orderRowResp);
// 订单号不存在就保存,存在就更新订单状态
orderRowRepository.save(orderRow);
}
}
}
}
}
}
/**
* 手动调用 将订单发送到微信
*/
private void orderToWx(OrderRow orderRow, Boolean isAutoFlush) {
// 查询订单状态
Integer newValidCode = orderRow.getValidCode();
String oldValidCode = redisTemplate.opsForValue().get(ORDER_ROW_KEY + orderRow.getId());
Integer lastValidCode = 0;
// 更新 Redis 状态
redisTemplate.opsForValue().set(ORDER_ROW_KEY + orderRow.getId(), String.valueOf(orderRow.getValidCode()));
if (Util.isNotEmpty(oldValidCode)) {
lastValidCode = Integer.valueOf(oldValidCode);
}
// 如果订单状态没变化,就不发送
if (isAutoFlush && lastValidCode.equals(newValidCode)) {
} else {
String content;
content = getFormattedOrderInfo(orderRow, Util.isEmpty(oldValidCode) ? -100 : Integer.parseInt(oldValidCode));
// 推送
wxUtil.sendTextMessage(WXUtil.super_admin_wxid, content, 1, WXUtil.super_admin_wxid);
}
}
/**
* 将数据库订单转化成微信所需要文本
*/
public String getFormattedOrderInfo(OrderRow orderRow, Integer oldValidCode) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ValidCodeConverter converter = new ValidCodeConverter();
String orderInfo =
//+ "订单+sku" + orderRow.getId() + "\r"
"订单号:" + orderRow.getOrderId() + "(" + (orderRow.getPlus() == 1 ? "plus" : "非plus") + ")\r" +
"最新订单状态:" + (converter.getCodeDescription(orderRow.getValidCode())) + "\r" +
"商品名称:" + orderRow.getSkuName() + "\r"
+ "商品单价:" + orderRow.getPrice() + "\r"
+ "商品数量:" + orderRow.getSkuNum() + "\r"
+ "商品总价:" + (orderRow.getPrice() * orderRow.getSkuNum()) + "\r"
+ "预估计佣金额:" + orderRow.getEstimateCosPrice() + "\n"
+ "佣金比例:" + orderRow.getCommissionRate() + "%\r\r"
+ "推客的预估佣金:" + orderRow.getEstimateFee() + "\r"
+ "实际计算佣金的金额:" + orderRow.getActualCosPrice() + "\r"
+ "下单时间:" + formatter.format(orderRow.getOrderTime()) + "\r"
+ "完成时间:" + (orderRow.getFinishTime() != null ? formatter.format(orderRow.getFinishTime()) : "未完成") + "\r\n";
if (oldValidCode != -100) {
orderInfo = "订单状态从 " + (converter.getCodeDescription(oldValidCode)) + " --变成-- " +
(converter.getCodeDescription(orderRow.getValidCode())) + "\r" + orderInfo;
}
return orderInfo;
}
/**
* 根据指定的日期时间拉取订单
*/
public UnionOpenOrderRowQueryResponse fetchOrdersForDateTime(LocalDateTime startTime, boolean isRealTime) throws Exception {
LocalDateTime endTime = isRealTime ? startTime.plusMinutes(10) : startTime.plusHours(1);
String key = startTime.format(DATE_TIME_FORMATTER);
String hourRange = isRealTime ? "minute" : "hour";
SetOperations<String, String> setOps = redisTemplate.opsForSet();
// 调用 API 以拉取订单
UnionOpenOrderRowQueryResponse unionOpenOrderRowQueryResponse = getUnionOpenOrderRowQueryResponse(startTime, endTime);
// 标记已拉取
setOps.add(key, hourRange);
// 打印方法调用和开始结束时间
logger.info("拉取订单:开始时间:{}结束时间:{}", startTime.format(DATE_TIME_FORMATTER), endTime.format(DATE_TIME_FORMATTER));
return unionOpenOrderRowQueryResponse;
}
/** /**
* 接收京粉指令指令 * 接收京粉指令指令
*/ */
@@ -240,6 +363,8 @@ public class JDUtils {
content.append("一个月统计\r"); content.append("一个月统计\r");
content.append("两个月统计\r"); content.append("两个月统计\r");
content.append("三个月统计\r"); content.append("三个月统计\r");
content.append("这个月统计\r");
content.append("上个月统计\r");
content.append("今日订单\r"); content.append("今日订单\r");
content.append("昨日订单\r"); content.append("昨日订单\r");
content.append("刷新三天\r"); content.append("刷新三天\r");
@@ -343,6 +468,42 @@ public class JDUtils {
content.append("\r" + "违规佣金:").append(getStreamForWeiGui(last90DaysOrders).mapToDouble(orderRow -> orderRow.getEstimateCosPrice() * orderRow.getCommissionRate() * 0.01).sum()); content.append("\r" + "违规佣金:").append(getStreamForWeiGui(last90DaysOrders).mapToDouble(orderRow -> orderRow.getEstimateCosPrice() * orderRow.getCommissionRate() * 0.01).sum());
break; break;
} }
case "这个月统计": {
// 计算出距离1号有几天
int days = LocalDate.now().getDayOfMonth();
List<OrderRow> thisMonthOrders = filterOrdersByDate(orderRows, days);
content.append("本月统计:\n");
content.append("订单总数:").append(thisMonthOrders.size()).append("\r");
content.append("已付款:").append(thisMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 16).count()).append("\r");
content.append("已取消:").append(thisMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() != 16 && orderRow.getValidCode() != 17).count()).append("\r");
content.append("已完成:").append(thisMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 17).count()).append("\r");
content.append("违规:").append(getStreamForWeiGui(thisMonthOrders).count()).append("\r");
content.append("已付款佣金:").append(thisMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 16).mapToDouble(OrderRow::getEstimateFee).sum()).append("\r");
content.append("已完成佣金:").append(thisMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 17).mapToDouble(OrderRow::getEstimateFee).sum());
content.append("\r" + "违规佣金:").append(getStreamForWeiGui(thisMonthOrders).mapToDouble(orderRow -> orderRow.getEstimateCosPrice() * orderRow.getCommissionRate() * 0.01).sum());
break;
}
case "上个月统计": {
LocalDate lastMonth = LocalDate.now().minusMonths(1);
int days = LocalDate.now().getDayOfMonth();
List<OrderRow> lastMonthOrders = filterOrdersByDate(orderRows, lastMonth.lengthOfMonth() + days);
List<OrderRow> thisMonthOrders = filterOrdersByDate(orderRows, days);
lastMonthOrders = lastMonthOrders.stream().filter(orderRow -> !thisMonthOrders.contains(orderRow)).collect(Collectors.toList());
content.append("上个月统计:\n");
content.append("订单总数:").append(lastMonthOrders.size()).append("\r");
content.append("已付款:").append(lastMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 16).count()).append("\r");
content.append("已取消:").append(lastMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() != 16 && orderRow.getValidCode() != 17).count()).append("\r");
content.append("已完成:").append(lastMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 17).count()).append("\r");
content.append("违规:").append(getStreamForWeiGui(lastMonthOrders).count()).append("\r");
content.append("已付款佣金:").append(lastMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 16).mapToDouble(OrderRow::getEstimateFee).sum()).append("\r");
content.append("已完成佣金:").append(lastMonthOrders.stream().filter(orderRow -> orderRow.getValidCode() == 17).mapToDouble(OrderRow::getEstimateFee).sum());
content.append("\r" + "违规佣金:").append(getStreamForWeiGui(lastMonthOrders).mapToDouble(orderRow -> orderRow.getEstimateCosPrice() * orderRow.getCommissionRate() * 0.01).sum());
break;
}
case "今日订单": { case "今日订单": {
List<OrderRow> todayOrders = filterOrdersByDate(orderRows, 0); List<OrderRow> todayOrders = filterOrdersByDate(orderRows, 0);
// 订单总数,已付款,已取消,佣金总计 // 订单总数,已付款,已取消,佣金总计
@@ -432,7 +593,7 @@ public class JDUtils {
* 接收京粉指令指令 * 接收京粉指令指令
* 高级菜单 * 高级菜单
*/ */
public void sendOrderToWxByOrderJDAdvanced(String order) throws Exception { public void sendOrderToWxByOrderJDAdvanced(String order) {
int[] parm = {-1}; int[] parm = {-1};
List<OrderRow> orderRows = orderRowRepository.findByValidCodeNotInOrderByOrderTimeDesc(parm); List<OrderRow> orderRows = orderRowRepository.findByValidCodeNotInOrderByOrderTimeDesc(parm);
@@ -476,90 +637,6 @@ public class JDUtils {
} }
} }
/**
* 手动调用 将订单发送到微信
*/
private void orderToWx(OrderRow orderRow, Boolean isAutoFlush) {
// 查询订单状态
Integer newValidCode = orderRow.getValidCode();
String oldValidCode = redisTemplate.opsForValue().get(ORDER_ROW_KEY + orderRow.getId());
Integer lastValidCode = 0;
// 更新 Redis 状态
redisTemplate.opsForValue().set(ORDER_ROW_KEY + orderRow.getId(), String.valueOf(orderRow.getValidCode()));
if (Util.isNotEmpty(oldValidCode)) {
lastValidCode = Integer.valueOf(oldValidCode);
}
// 如果订单状态没变化,就不发送
if (isAutoFlush && lastValidCode.equals(newValidCode)) {
} else {
String content;
content = getFormattedOrderInfo(orderRow, Util.isEmpty(oldValidCode) ? -100 : Integer.parseInt(oldValidCode));
// 推送
wxUtil.sendTextMessage(WXUtil.super_admin_wxid, content, 1, WXUtil.super_admin_wxid);
}
}
/**
* 将数据库订单转化成微信所需要文本
*/
public String getFormattedOrderInfo(OrderRow orderRow, Integer oldValidCode) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ValidCodeConverter converter = new ValidCodeConverter();
String orderInfo =
//+ "订单+sku" + orderRow.getId() + "\r"
"订单号:" + orderRow.getOrderId() + "(" + (orderRow.getPlus() == 1 ? "plus" : "非plus") + ")\r" +
"最新订单状态:" + (converter.getCodeDescription(orderRow.getValidCode())) + "\r" +
"商品名称:" + orderRow.getSkuName() + "\r"
+ "商品单价:" + orderRow.getPrice() + "\r"
+ "商品数量:" + orderRow.getSkuNum() + "\r"
+ "商品总价:" + (orderRow.getPrice() * orderRow.getSkuNum()) + "\r"
+ "预估计佣金额:" + orderRow.getEstimateCosPrice() + "\n"
+ "佣金比例:" + orderRow.getCommissionRate() + "%\r\r"
+ "推客的预估佣金:" + orderRow.getEstimateFee() + "\r"
+ "实际计算佣金的金额:" + orderRow.getActualCosPrice() + "\r"
+ "下单时间:" + formatter.format(orderRow.getOrderTime()) + "\r"
+ "完成时间:" + (orderRow.getFinishTime() != null ? formatter.format(orderRow.getFinishTime()) : "未完成") + "\r\n";
if (oldValidCode != -100) {
orderInfo = "订单状态从 " + (converter.getCodeDescription(oldValidCode)) + " --变成-- " +
(converter.getCodeDescription(orderRow.getValidCode())) + "\r" + orderInfo;
}
return orderInfo;
}
/**
* 根据指定的日期时间拉取订单
*/
public UnionOpenOrderRowQueryResponse fetchOrdersForDateTime(LocalDateTime startTime, boolean isRealTime) throws Exception {
LocalDateTime endTime = isRealTime ? startTime.plusMinutes(10) : startTime.plusHours(1);
String key = startTime.format(DATE_TIME_FORMATTER);
String hourRange = isRealTime ? "minute" : "hour";
SetOperations<String, String> setOps = redisTemplate.opsForSet();
// 检查是否标记为已拉取
//if (Boolean.TRUE.equals(setOps.isMember(key, hourRange))) {
// System.out.println(dateTime.format(DATE_TIME_FORMATTER) + " 已经拉取,跳过");
// return null;
//}
// 调用 API 以拉取订单
UnionOpenOrderRowQueryResponse unionOpenOrderRowQueryResponse = getUnionOpenOrderRowQueryResponse(startTime, endTime);
// 标记已拉取
setOps.add(key, hourRange);
// 打印方法调用和开始结束时间
System.out.println("拉取订单:" + "开始时间:" + startTime.format(DATE_TIME_FORMATTER) + "结束时间:" + endTime.format(DATE_TIME_FORMATTER));
return unionOpenOrderRowQueryResponse;
}
/** /**
* 获取订单列表 * 获取订单列表
@@ -634,44 +711,5 @@ public class JDUtils {
return response.getGetResult().getData().getClickURL(); return response.getGetResult().getData().getClickURL();
} }
/**
* 每小时拉取过去两个月的订单
* 因为有的延迟发货,而接口只能拉取两个月前的数据
*/
@Scheduled(cron = "0 0 * * * ?")
public void fetchHistoricalOrders() throws Exception {
// 从设定的开始日期到昨天的同一时间
System.out.println("开始拉取历史订单");
// 拉最近两个月的订单
// 获取当前时间,并调整为整点开始
LocalDateTime startDate = LocalDateTime.now().minusMonths(2).truncatedTo(ChronoUnit.HOURS);
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastHour = now.truncatedTo(ChronoUnit.HOURS);
while (!startDate.isEqual(lastHour)) {
startDate = startDate.plusHours(1);
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(startDate, false); // 假的代表历史订单
if (response != null) {
int code = response.getQueryResult().getCode();
if (code == 200) {
if (response.getQueryResult().getCode() == 200) {
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
if (orderRowResps == null) {
continue;
}
for (OrderRowResp orderRowResp : orderRowResps) {
// 固化到数据库
OrderRow orderRow = createOrderRow(orderRowResp);
// 订单号不存在就保存,存在就更新订单状态
orderRowRepository.save(orderRow);
}
}
}
}
}
}
} }