This commit is contained in:
雷欧(林平凡)
2024-11-29 11:51:08 +08:00
parent f3a9536685
commit 2a77c74ba8
7 changed files with 138 additions and 79 deletions

View File

@@ -2,7 +2,6 @@ package cn.van;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
@@ -13,7 +12,6 @@ import org.springframework.scheduling.annotation.EnableScheduling;
*/
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {
public static void main(String[] args) {

View File

@@ -0,0 +1,30 @@
package cn.van.business.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* @author Leo
* @version 1.0
* @create 2024/11/29 11:41
* @description
*/
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(128);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("Async-");
executor.initialize();
return executor;
}
}

View File

@@ -2,8 +2,6 @@ package cn.van.business.model.jd;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**

View File

@@ -6,7 +6,6 @@ package cn.van.business.model.jd;
* @create 2024/11/6 10:52
* @description
*/
import com.jd.open.api.sdk.domain.kplunion.OrderService.response.query.GoodsInfo;
import javax.persistence.*;
import java.util.Date;

View File

@@ -1,7 +1,6 @@
package cn.van.business.util;
import cn.van.business.enums.ValidCodeConverter;
import cn.van.business.model.jd.GoodsInfoVO;
import cn.van.business.model.jd.OrderRow;
import cn.van.business.repository.OrderRowRepository;
@@ -22,12 +21,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -38,8 +34,6 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -64,17 +58,26 @@ public class JDUtils {
// "a4fb15d7bedd4316b97b4e96e4effc1c";
//accessToken
private static final String ACCESS_TOKEN = "";
//标记唯一订单行:订单+sku维度的唯一标识
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");
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private OrderRowRepository orderRowRepository;
@Resource
private WXUtil wxUtil;
private final StringRedisTemplate redisTemplate;
private final OrderRowRepository orderRowRepository;
private final WXUtil wxUtil;
private final OrderUtil orderUtil;
// 通过构造函数注入所有依赖项Spring将自动注入这些依赖
@Autowired // @Autowired 在构造函数上可以省略,如果类只有一个构造函数
public JDUtils(StringRedisTemplate redisTemplate,
OrderRowRepository orderRowRepository,
WXUtil wxUtil,
OrderUtil orderUtil) {
this.redisTemplate = redisTemplate;
this.orderRowRepository = orderRowRepository;
this.wxUtil = wxUtil;
this.orderUtil = orderUtil;
}
/**
* 将 响应参数转化为 OrderRow并返回
@@ -219,7 +222,7 @@ public class JDUtils {
for (OrderRow orderRow : orderRows) {
orderToWx(orderRow, true);
orderUtil.orderToWx(orderRow, true);
}
@@ -276,61 +279,6 @@ public class JDUtils {
}
}
/**
* 手动调用 将订单发送到微信
*/
@Async
public void orderToWx(OrderRow orderRow, Boolean isAutoFlush) {
// 获取订单当前状态
Integer newValidCode = orderRow.getValidCode();
String oldValidCode = redisTemplate.opsForValue().get(ORDER_ROW_KEY + orderRow.getId());
// 检查Redis中是否有旧的状态码没有的话赋予默认值
Integer lastValidCode = oldValidCode != null ? Integer.parseInt(oldValidCode) : -100;
// 这里使用了逻辑非(!)操作符来简化条件判断
if (!isAutoFlush || !lastValidCode.equals(newValidCode)) {
// 当 isAutoFlush 为 false 或状态确实有变化时,进行消息发送
String content = getFormattedOrderInfo(orderRow, lastValidCode);
wxUtil.sendTextMessage(WXUtil.super_admin_wxid, content, 1, WXUtil.super_admin_wxid);
}
// 更新 Redis 状态值
redisTemplate.opsForValue().set(ORDER_ROW_KEY + orderRow.getId(), String.valueOf(orderRow.getValidCode()));
}
/**
* 将数据库订单转化成微信所需要文本
*/
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;
}
/**
* 根据指定的日期时间拉取订单
@@ -552,7 +500,7 @@ public class JDUtils {
content.append("\r" + "违规佣金:").append(getStreamForWeiGui(todayOrders).mapToDouble(orderRow -> orderRow.getEstimateCosPrice() * orderRow.getCommissionRate() * 0.01).sum());
for (OrderRow orderRow : todayOrders) {
orderToWx(orderRow, false);
orderUtil.orderToWx(orderRow, false);
}
break;
@@ -571,7 +519,7 @@ public class JDUtils {
content.append("已完成佣金:").append(yesterdayOrders.stream().filter(orderRow -> orderRow.getValidCode() == 17).mapToDouble(OrderRow::getEstimateFee).sum());
content.append("\r" + "违规佣金:").append(getStreamForWeiGui(yesterdayOrders).mapToDouble(orderRow -> orderRow.getEstimateCosPrice() * orderRow.getCommissionRate() * 0.01).sum());
for (OrderRow orderRow : yesterdayOrders) {
orderToWx(orderRow, false);
orderUtil.orderToWx(orderRow, false);
}
break;
}
@@ -666,7 +614,7 @@ public class JDUtils {
List<OrderRow> orderRowList = orderRowRepository.findByOrderId(Long.parseLong(order));
if (!orderRowList.isEmpty()) {
OrderRow orderRow = orderRowList.get(0);
content.append(getFormattedOrderInfo(orderRow, orderRow.getValidCode()));
content.append(orderUtil.getFormattedOrderInfo(orderRow, orderRow.getValidCode()));
} else {
content.append("订单不存在");
}

View File

@@ -0,0 +1,87 @@
package cn.van.business.util;
import cn.van.business.enums.ValidCodeConverter;
import cn.van.business.model.jd.OrderRow;
import cn.van.business.repository.OrderRowRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
/**
* @author Leo
* @version 1.0
* @create 2024/11/29 11:43
* @description
*/
@Service
public class OrderUtil {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private OrderRowRepository orderRowRepository;
@Resource
private WXUtil wxUtil;
//标记唯一订单行:订单+sku维度的唯一标识
private static final String ORDER_ROW_KEY = "jd:order:row:";
/**
* 手动调用 将订单发送到微信
*/
@Async
public void orderToWx(OrderRow orderRow, Boolean isAutoFlush) {
// 获取订单当前状态
Integer newValidCode = orderRow.getValidCode();
String oldValidCode = redisTemplate.opsForValue().get(ORDER_ROW_KEY + orderRow.getId());
// 检查Redis中是否有旧的状态码没有的话赋予默认值
Integer lastValidCode = oldValidCode != null ? Integer.parseInt(oldValidCode) : -100;
// 这里使用了逻辑非(!)操作符来简化条件判断
if (!isAutoFlush || !lastValidCode.equals(newValidCode)) {
// 当 isAutoFlush 为 false 或状态确实有变化时,进行消息发送
String content = getFormattedOrderInfo(orderRow, lastValidCode);
wxUtil.sendTextMessage(WXUtil.super_admin_wxid, content, 1, WXUtil.super_admin_wxid);
}
// 更新 Redis 状态值
redisTemplate.opsForValue().set(ORDER_ROW_KEY + orderRow.getId(), String.valueOf(orderRow.getValidCode()));
}
/**
* 将数据库订单转化成微信所需要文本
*/
@Async
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;
}
}

View File

@@ -1,7 +1,6 @@
package cn.van.business.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.van.business.enums.WXReqType;
import com.alibaba.fastjson2.JSON;