202508091201

This commit is contained in:
张潘 2025-08-09 12:01:17 +08:00
parent 04bf2bae48
commit 75b6f9ff71
5 changed files with 549 additions and 2406 deletions

2
package-lock.json generated
View File

@ -1,5 +1,5 @@
{
"name": "RuoYi-Vue-master",
"name": "202508091038",
"lockfileVersion": 2,
"requires": true,
"packages": {

View File

@ -78,7 +78,7 @@ public class OrderUtil {
/**
* 生成订单编码的工具
*
*
* @return 格式为 C + yyyyMMdd + 随机数字 的订单编号
*/
public static String generateCode() {
@ -126,7 +126,7 @@ public class OrderUtil {
/**
* 执行状态验证
*
*
* @param validator 状态验证器
* @param newOrder 新订单
* @param newStatus 新状态
@ -161,7 +161,7 @@ public class OrderUtil {
/**
* 保存订单日志
* 委托给OrderLogHandler处理具体的业务逻辑
*
*
* @param order 订单对象包含状态和服务进度信息
* @return 影响的行数大于0表示保存成功
* @throws IllegalArgumentException 当订单参数无效时
@ -174,10 +174,10 @@ public class OrderUtil {
// 委托给OrderLogHandler处理
OrderLog orderLog = orderLogHandler.createBaseOrderLog(order);
orderLogHandler.processOrderStatus(order, orderLog);
// 保存订单日志
return orderLogService.insertOrderLog(orderLog);
} catch (Exception e) {
// 记录异常信息这里可以添加日志记录
throw new RuntimeException("保存订单日志失败: " + e.getMessage(), e);
@ -186,7 +186,7 @@ public class OrderUtil {
/**
* 验证订单日志参数
*
*
* @param order 订单对象
*/
private void validateOrderForLog(Order order) {
@ -200,14 +200,14 @@ public class OrderUtil {
/**
* 根据用户手机号判断用户是否存在如果不存在则新增用户数据
*
*
* @param phone 用户手机号
* @param addressName 地址名称
* @return 包含用户和地址信息的Map
*/
public Map<String, Object> isUser(String phone, String addressName) {
Users existingUser = usersService.selectUsersByPhone(phone);
if (existingUser == null) {
// 用户不存在创建新用户
Users newUser = createNewUser(phone);
@ -223,7 +223,7 @@ public class OrderUtil {
/**
* 创建新用户对象
*
*
* @param phone 用户手机号
* @return 新用户对象
*/
@ -238,7 +238,7 @@ public class OrderUtil {
/**
* 创建失败结果
*
*
* @return 失败结果Map
*/
private Map<String, Object> createFailureResult() {
@ -249,7 +249,7 @@ public class OrderUtil {
/**
* 根据用户地址获取经纬度然后存储地址数据
*
*
* @param user 用户对象
* @param addressName 地址名称
* @return 包含处理结果的Map
@ -257,7 +257,7 @@ public class OrderUtil {
public Map<String, Object> isAddAddressUser(Users user, String addressName) {
// 查询用户是否已有该地址
List<UserAddress> existingAddresses = findExistingAddress(user.getId(), addressName);
if (!existingAddresses.isEmpty()) {
// 地址已存在
return createSuccessResult(user, existingAddresses.get(0));
@ -269,7 +269,7 @@ public class OrderUtil {
/**
* 查找已存在的地址
*
*
* @param userId 用户ID
* @param addressName 地址名称
* @return 地址列表
@ -283,7 +283,7 @@ public class OrderUtil {
/**
* 创建成功结果
*
*
* @param user 用户对象
* @param address 地址对象
* @return 成功结果Map
@ -298,7 +298,7 @@ public class OrderUtil {
/**
* 创建新的用户地址
*
*
* @param user 用户对象
* @param addressName 地址名称
* @return 包含处理结果的Map
@ -306,17 +306,17 @@ public class OrderUtil {
private Map<String, Object> createNewUserAddress(Users user, String addressName) {
try {
UserAddress userAddress = buildUserAddress(user, addressName);
// 获取地址的经纬度信息
setAddressCoordinates(userAddress, addressName);
// 保存地址
if (SpringUtils.getBean(IUserAddressService.class).insertUserAddress(userAddress) > 0) {
return createSuccessResult(user, userAddress);
} else {
return createFailureResult();
}
} catch (Exception e) {
Map<String, Object> map = new HashMap<>();
map.put("code", "0");
@ -327,7 +327,7 @@ public class OrderUtil {
/**
* 构建用户地址对象
*
*
* @param user 用户对象
* @param addressName 地址名称
* @return 用户地址对象
@ -346,7 +346,7 @@ public class OrderUtil {
/**
* 设置地址的经纬度坐标
*
*
* @param userAddress 用户地址对象
* @param addressName 地址名称
*/
@ -354,7 +354,7 @@ public class OrderUtil {
try {
AmapUtils amapUtils = new AmapUtils();
JSONObject geoResult = amapUtils.geocode(addressName);
if ("OK".equals(geoResult.get("info"))) {
parseCoordinates(userAddress, geoResult);
}
@ -366,7 +366,7 @@ public class OrderUtil {
/**
* 解析坐标信息
*
*
* @param userAddress 用户地址对象
* @param geoResult 地理编码结果
*/
@ -375,7 +375,7 @@ public class OrderUtil {
if (geocodes != null && !geocodes.isEmpty()) {
JSONObject firstGeocode = geocodes.getJSONObject(0);
String location = firstGeocode.getString("location");
if (location != null && location.contains(",")) {
String[] coordinates = location.split(",");
userAddress.setLongitude(coordinates[0]); // 经度
@ -471,23 +471,23 @@ public class OrderUtil {
/**
* 订单价格确认工具方法
* 根据服务IDSKU是否一口价和订单类型计算最终价格
*
*
* @param serviceId 服务ID
* @param sku SKU信息格式{"精细擦玻璃":"20㎡全屋精细擦窗","pic":"","price":"236","groupprice":"100","seckillprice":"99","stock":"1000"}
* @param type 订单类别 1=拼团 2=次卡 3=秒杀
* @return 计算后的价格如果计算失败返回null
*
*
* 使用示例
* // 一口价订单单规格
* BigDecimal price1 = OrderUtil.confirmOrderPrice(1L, null, 1, 1);
*
*
* // 一口价订单多规格
* String sku = "{\"精细擦玻璃\":\"20㎡全屋精细擦窗\",\"pic\":\"\",\"price\":\"236\",\"groupprice\":\"100\",\"seckillprice\":\"99\",\"stock\":\"1000\"}";
* BigDecimal price2 = OrderUtil.confirmOrderPrice(1L, sku, 1, 1);
*
*
* // 拼团订单单规格
* BigDecimal price3 = OrderUtil.confirmOrderPrice(1L, null, 2, 1);
*
*
* // 秒杀订单多规格
* BigDecimal price4 = OrderUtil.confirmOrderPrice(1L, sku, 2, 3);
*/
@ -501,7 +501,7 @@ public class OrderUtil {
// 获取服务商品信息
IServiceGoodsService serviceGoodsService = SpringUtils.getBean(IServiceGoodsService.class);
ServiceGoods serviceGoods = serviceGoodsService.selectServiceGoodsById(serviceId);
if (serviceGoods == null) {
return null;
}
@ -534,7 +534,7 @@ public class OrderUtil {
}
return null;
} catch (Exception e) {
// 记录异常信息这里可以添加日志记录
System.err.println("订单价格确认失败: " + e.getMessage());
@ -544,7 +544,7 @@ public class OrderUtil {
/**
* 处理单规格商品价格计算
*
*
* @param serviceGoods 服务商品对象
* @param type 订单类型 1=拼团 2=次卡 3=秒杀
* @return 计算后的价格
@ -564,7 +564,7 @@ public class OrderUtil {
/**
* 处理多规格商品价格计算
*
*
* @param sku SKU信息JSON字符串
* @param type 订单类型 1=拼团 2=次卡 3=秒杀null表示获取基础价格
* @return 计算后的价格
@ -577,13 +577,13 @@ public class OrderUtil {
// 解析SKU JSON
JSONObject skuJson = JSONObject.parseObject(sku);
// 如果type为null获取基础价格
if (type == null) {
String priceStr = skuJson.getString("price");
return parsePrice(priceStr);
}
switch (type) {
case 1: // 拼团
String groupPriceStr = skuJson.getString("groupprice");
@ -598,7 +598,7 @@ public class OrderUtil {
String defaultPriceStr = skuJson.getString("price");
return parsePrice(defaultPriceStr);
}
} catch (Exception e) {
System.err.println("解析多规格SKU价格失败: " + e.getMessage());
return null;
@ -607,7 +607,7 @@ public class OrderUtil {
/**
* 解析价格字符串为BigDecimal
*
*
* @param priceStr 价格字符串
* @return BigDecimal价格解析失败返回null
*/
@ -643,7 +643,7 @@ public class OrderUtil {
System.out.println("服务类型 - servicetype: " + payBefor.getServicetype());
IOrderService orderService = SpringUtils.getBean(IOrderService.class);
// 单个商品支付
if (type == 5){
GoodsOrder gorder = new GoodsOrder();
@ -668,7 +668,7 @@ public class OrderUtil {
System.out.println("查询拼团商品订单 - orderid: " + payBefor.getOrderid());
List<GoodsOrder> orderslist =goodsOrderService.selectGoodsOrderList(gorder);
System.out.println("查询到的商品订单数量: " + (orderslist != null ? orderslist.size() : 0));
if (!orderslist.isEmpty()){
System.out.println("开始更新商品订单状态");
for (GoodsOrder goodsOrder : orderslist){
@ -802,25 +802,25 @@ public class OrderUtil {
// 查询订单
Order order = orderService.selectOrderByOrderId(payBefor.getOrderid());
System.out.println("查询到的订单: " + (order != null ? order.toString() : "null"));
if (order != null) {
UserDemandQuotation userDemandQuotation=userDemandQuotationService.selectUserDemandQuotationById(payBefor.getBaojiaid());
if (userDemandQuotation!=null) {
System.out.println("找到报价记录,开始处理");
//UserDemandQuotation userDemandQuotation=userDemandQuotationList.getFirst();
System.out.println("报价记录详情: " + userDemandQuotation.toString());
System.out.println("更新报价状态为被选中");
userDemandQuotation.setStatus(2L);//被选中状态
int quotationUpdateResult = userDemandQuotationService.updateUserDemandQuotation(userDemandQuotation);
System.out.println("报价状态更新结果: " + quotationUpdateResult);
System.out.println("查询师傅信息 - workerId: " + userDemandQuotation.getWorkerid());
Users users = usersService.selectUsersById(userDemandQuotation.getWorkerid());
System.out.println("查询到的师傅信息: " + (users != null ? users.toString() : "null"));
if (users != null){
order.setStatus(2L);
@ -833,7 +833,7 @@ public class OrderUtil {
order.setWorkerId(users.getId());
int orderUpdateResult = orderService.updateOrder(order);
System.out.println("订单更新结果: " + orderUpdateResult);
// 添加订单日志
System.out.println("添加订单日志");
OrderLog orderLog = new OrderLog();
@ -944,14 +944,14 @@ public class OrderUtil {
Order order = new Order();
order.setOdertype(1); // 拼团
order.setStatus(9L); // 9=待成团
order.setPayPrice(order.getPayPrice().add(payBefor.getAllmoney()));
order.setOrderId(ptorderid);
order.setUid(payBefor.getUid());
order.setNum(payBefor.getNum()); // 默认1可根据业务调整
//order.setProductId(payBefor.getSku() != null ? Long.valueOf(payBefor.getSku()) : null); // 假设sku字段存储商品ID
order.setProductId(payBefor.getServiceid()); // 假设sku字段存储商品ID
System.out.println("商品ID: " + payBefor.getServiceid());
IServiceGoodsService serviceGoodsService = SpringUtils.getBean(IServiceGoodsService.class);
ServiceGoods serviceGoods = serviceGoodsService.selectServiceGoodsById(payBefor.getServiceid());
System.out.println("查询到的商品信息: " + (serviceGoods != null ? serviceGoods.toString() : "null"));
@ -976,7 +976,8 @@ public class OrderUtil {
order.setType(1); // 服务订单
order.setCreateType(1); // 用户自主下单
order.setUname(user.getName());
System.out.println("订单基本信息设置完成");
//20250809处理过的一个故障
order.setPayPrice(payBefor.getAllmoney());
order.setReceiveType(Long.valueOf(dispatchtype)); // 自由抢单
// 预约时间
System.out.println("预约时间: " + payBefor.getMaketime());
@ -1010,7 +1011,7 @@ public class OrderUtil {
System.out.println("开始插入订单");
int orderInsertResult = orderService.insertOrder(order);
System.out.println("订单插入结果: " + orderInsertResult + ", 订单ID: " + order.getId());
// 添加订单日志
System.out.println("添加订单日志");
IOrderLogService orderLogService = SpringUtils.getBean(IOrderLogService.class);
@ -1030,7 +1031,7 @@ public class OrderUtil {
IUserGroupBuyingService userGroupBuyingService = SpringUtils.getBean(IUserGroupBuyingService.class);
UserGroupBuying userGroupBuying = userGroupBuyingService.selectUserGroupBuyingByptorderid(ptorderid);
System.out.println("查询到的拼团记录: " + (userGroupBuying != null ? userGroupBuying.toString() : "null"));
if (userGroupBuying != null){
System.out.println("更新拼团状态");
userGroupBuying.setStatus(1L);
@ -1044,7 +1045,7 @@ public class OrderUtil {
System.out.println("=== 第三步:核验团数据 ===");
System.out.println("团订单ID: " + payBefor.getGrouporderid());
System.out.println("商品拼团人数要求: " + (serviceGoods != null ? serviceGoods.getGroupnum() : "null"));
UserGroupBuying userGroupBuyingData = new UserGroupBuying();
userGroupBuyingData.setOrderid(payBefor.getGrouporderid());
userGroupBuyingData.setPaystatus(1L);
@ -1088,7 +1089,7 @@ public class OrderUtil {
IUserUseSecondaryCardService userUseSecondaryCardService = SpringUtils.getBean(IUserUseSecondaryCardService.class);
UserUseSecondaryCard userUseSecondaryCard = userUseSecondaryCardService.selectUserUseSecondaryCardByorderId(payBefor.getOrderid());
System.out.println("查询到的次卡记录: " + (userUseSecondaryCard != null ? userUseSecondaryCard.toString() : "null"));
if (userUseSecondaryCard != null){
System.out.println("更新次卡状态");
userUseSecondaryCard.setStatus(1L);
@ -1121,7 +1122,7 @@ public class OrderUtil {
System.out.println("处理普通订单");
Order order = orderService.selectOrderByOrderId(payBefor.getOrderid());
System.out.println("查询到的订单: " + (order != null ? order.toString() : "null"));
if (order != null) {
System.out.println("添加订单日志");
OrderLog orderLog = new OrderLog();
@ -1137,7 +1138,7 @@ public class OrderUtil {
DispatchUtil.dispatchOrder(order.getId());
}
System.out.println("订单日志插入结果: " + logInsertResult);
System.out.println("更新订单状态为待派单");
order.setPayPrice(order.getPayPrice().add(payBefor.getAllmoney()));
order.setStatus(1L); // 1=待预约
@ -1216,7 +1217,7 @@ public class OrderUtil {
* 用户付款后回调中修改订单状态的辅助方法
* 逻辑如果用户付款的时候查询paynum=0没有可付款的数据
* 且订单最新状态大于等于6师傅已完成那么就修改订单为已完成状态
*
*
* @param orderid 订单ID
* @return 剩余可付款数量
*/
@ -1251,13 +1252,13 @@ public class OrderUtil {
// 5. 如果paynum=0且师傅已完成则修改订单为已完成状态
if (paynum <= 0 && isWorkerCompleted) {
System.out.println("订单 " + orderid + " 满足完成条件:无剩余付款且师傅已完成,开始修改订单状态");
// 修改订单状态为已完成
order.setStatus(4L); // 4已完成
order.setReceiveType(3L); // 3完成类型
order.setJsonStatus(9); // 9已完成
order.setLogJson("{\"type\":8}"); // 8完成状态
int updateResult = orderService.updateOrder(order);
System.out.println("订单状态更新结果: " + updateResult);
@ -1298,7 +1299,7 @@ public class OrderUtil {
// }
return paynum;
} catch (Exception e) {
System.err.println("ISTOPAYSIZE方法执行异常orderid: " + orderid + ", 错误: " + e.getMessage());
e.printStackTrace();
@ -1375,22 +1376,22 @@ public class OrderUtil {
return result;
}
}
// 智能派单 - 调用DispatchUtil进行智能派单
try {
System.out.println("【OrderUtil】开始智能派单订单ID: " + order.getId());
DispatchUtil.DispatchResult dispatchResult = DispatchUtil.dispatchOrder(order.getId());
if (dispatchResult.isSuccess()) {
Users selectedWorker = dispatchResult.getWorker();
System.out.println("【OrderUtil】智能派单成功选中师傅: " + selectedWorker.getName());
// 更新订单状态和师傅信息
order.setStatus(1L); // 待服务
order.setWorkerId(selectedWorker.getId());
order.setWorkerPhone(selectedWorker.getPhone());
orderService.updateOrder(order);
// 添加订单日志
OrderLog orderLog = new OrderLog();
orderLog.setOid(order.getId());
@ -1401,26 +1402,26 @@ public class OrderUtil {
jsonObject.put("name", "智能派单成功,师傅: " + selectedWorker.getName());
orderLog.setContent(jsonObject.toJSONString());
orderLogService.insertOrderLog(orderLog);
result.put("success", true);
result.put("msg", "智能派单成功,师傅: " + selectedWorker.getName());
result.put("workerId", selectedWorker.getId());
result.put("workerName", selectedWorker.getName());
result.put("workerPhone", selectedWorker.getPhone());
} else {
System.out.println("【OrderUtil】智能派单失败: " + dispatchResult.getMessage());
result.put("success", false);
result.put("msg", "智能派单失败: " + dispatchResult.getMessage());
}
} catch (Exception e) {
System.out.println("【OrderUtil】智能派单异常: " + e.getMessage());
e.printStackTrace();
result.put("success", false);
result.put("msg", "智能派单异常: " + e.getMessage());
}
return result;
}
@ -1502,7 +1503,7 @@ public class OrderUtil {
/**
* 库存及销量变更
* 根据订单ID更新商品/服务的销量和库存
*
*
* @param orderid 订单ID
* @param type 商品类型 1=服务类商品 2=商城类商品
* @return 处理结果成功返回true失败返回false
@ -1510,7 +1511,7 @@ public class OrderUtil {
/**
* 库存及销量变更方法
* 根据订单ID更新商品/服务的销量和库存
*
*
* @param orderid 订单ID
* @param type 商品类型1-服务类商品2-商城类商品
* @return 处理结果成功返回true失败返回false
@ -1522,12 +1523,12 @@ public class OrderUtil {
System.err.println("订单ID不能为空");
return false;
}
if (type == null || (type != 1 && type != 2)) {
System.err.println("无效的商品类型type: " + type + ", orderid: " + orderid);
return false;
}
// 1. 根据type查询订单数据
Object order = null;
if (type == 1) {
@ -1539,18 +1540,18 @@ public class OrderUtil {
IGoodsOrderService goodsOrderService = SpringUtils.getBean(IGoodsOrderService.class);
order = goodsOrderService.selectGoodsOrderByorderId(orderid);
}
if (order == null) {
System.err.println("未找到订单数据orderid: " + orderid + ", type: " + type);
return false;
}
// 2. 提取订单信息
IServiceGoodsService serviceGoodsService = SpringUtils.getBean(IServiceGoodsService.class);
Long productId = null;
Long orderNum = null;
String orderSku = null;
if (type == 1) {
// 服务类商品
Order serviceOrder = (Order) order;
@ -1564,65 +1565,65 @@ public class OrderUtil {
orderNum = goodsOrder.getNum() != null ? goodsOrder.getNum() : 1L;
orderSku = goodsOrder.getSku();
}
// 验证商品ID
if (productId == null) {
System.err.println("订单商品ID为空orderid: " + orderid + ", type: " + type);
return false;
}
// 3. 查询商品/服务数据
ServiceGoods serviceGoods = serviceGoodsService.selectServiceGoodsById(productId);
if (serviceGoods == null) {
System.err.println("未找到商品/服务数据productId: " + productId + ", type: " + type);
return false;
}
// 4. 增加销量无论是否有SKU都要增加销量
Long currentSales = serviceGoods.getSales() != null ? serviceGoods.getSales() : 0L;
serviceGoods.setSales(currentSales + orderNum);
System.out.println("销量更新商品ID: " + serviceGoods.getId() +
", 原销量: " + currentSales + ", 增加销量: " + orderNum +
System.out.println("销量更新商品ID: " + serviceGoods.getId() +
", 原销量: " + currentSales + ", 增加销量: " + orderNum +
", 新销量: " + serviceGoods.getSales());
// 5. 更新库存
updateStock(serviceGoods, orderSku, orderNum, type);
// 6. 保存更新后的商品/服务数据
int updateResult = serviceGoodsService.updateServiceGoods(serviceGoods);
if (updateResult > 0) {
System.out.println("库存及销量更新成功orderid: " + orderid +
System.out.println("库存及销量更新成功orderid: " + orderid +
", type: " + type + ", 销量增加: " + orderNum + ", 商品ID: " + serviceGoods.getId());
return true;
} else {
System.err.println("库存及销量更新失败orderid: " + orderid + ", type: " + type);
return false;
}
} catch (Exception e) {
System.err.println("库存及销量变更异常orderid: " + orderid + ", type: " + type + ", 错误: " + e.getMessage());
e.printStackTrace();
return false;
}
}
/**
* 库存及销量变更兼容旧版本
* 根据订单ID更新商品/服务的销量和库存默认为服务类商品
*
*
* @param orderid 订单ID
* @return 处理结果成功返回true失败返回false
*/
// public static boolean updateInventoryAndSales(String orderid) {
//// return updateInventoryAndSales(orderid, 1); // 默认为服务类商品
// }
/**
* 更新库存逻辑
*
*
* @param serviceGoods 商品/服务对象
* @param orderSku 订单SKU
* @param orderNum 订单数量
@ -1635,20 +1636,20 @@ public class OrderUtil {
System.err.println("商品对象为空,无法更新库存");
return;
}
if (orderNum == null || orderNum <= 0) {
System.err.println("订单数量无效orderNum: " + orderNum);
return;
}
// 如果订单的sku为空或null直接扣减ServiceGoods的库存
if (orderSku == null || orderSku.trim().isEmpty()) {
Long currentStock = serviceGoods.getStock() != null ? serviceGoods.getStock() : 0L;
Long newStock = Math.max(0, currentStock - orderNum); // 确保库存不为负数
serviceGoods.setStock(newStock);
System.out.println("直接扣减库存商品ID: " + serviceGoods.getId() +
", 原库存: " + currentStock + ", 扣减数量: " + orderNum +
System.out.println("直接扣减库存商品ID: " + serviceGoods.getId() +
", 原库存: " + currentStock + ", 扣减数量: " + orderNum +
", 剩余库存: " + newStock);
} else {
// 根据商品类型选择不同的SKU更新方法
@ -1663,14 +1664,14 @@ public class OrderUtil {
}
}
} catch (Exception e) {
System.err.println("更新库存异常商品ID: " + (serviceGoods != null ? serviceGoods.getId() : "null") +
System.err.println("更新库存异常商品ID: " + (serviceGoods != null ? serviceGoods.getId() : "null") +
", 错误: " + e.getMessage());
}
}
/**
* 更新SKU库存
*
*
* @param serviceGoods 商品/服务对象
* @param orderSku 订单SKU具体规格选择
* @param orderNum 订单数量
@ -1682,18 +1683,18 @@ public class OrderUtil {
System.err.println("商品对象为空无法更新SKU库存");
return;
}
if (orderSku == null || orderSku.trim().isEmpty()) {
System.err.println("订单SKU为空无法更新SKU库存");
return;
}
String serviceSku = serviceGoods.getSku();
if (serviceSku == null || serviceSku.trim().isEmpty()) {
System.err.println("商品SKU为空无法更新SKU库存商品ID: " + serviceGoods.getId());
return;
}
// 解析ServiceGoods的SKU JSON复杂多规格格式
JSONObject skuJson;
try {
@ -1702,20 +1703,20 @@ public class OrderUtil {
System.err.println("商品SKU JSON格式错误商品ID: " + serviceGoods.getId() + ", SKU: " + serviceSku);
return;
}
// 检查是否是复杂多规格格式
if (!skuJson.containsKey("sku") || !skuJson.containsKey("type")) {
System.err.println("商品SKU格式不是多规格格式商品ID: " + serviceGoods.getId());
return;
}
// 获取sku数组
JSONArray skuArray = skuJson.getJSONArray("sku");
if (skuArray == null || skuArray.isEmpty()) {
System.err.println("商品SKU数组为空商品ID: " + serviceGoods.getId());
return;
}
// 解析订单SKU具体规格选择
JSONObject orderSkuJson;
try {
@ -1724,33 +1725,33 @@ public class OrderUtil {
System.err.println("订单SKU JSON格式错误订单SKU: " + orderSku);
return;
}
// 在sku数组中查找匹配的规格
boolean found = false;
for (int i = 0; i < skuArray.size(); i++) {
JSONObject skuItem = skuArray.getJSONObject(i);
// 检查是否匹配订单SKU的所有属性
if (isSkuMatch(skuItem, orderSkuJson)) {
// 找到匹配的SKU更新库存
String stockStr = skuItem.getString("stock");
if (stockStr != null && !stockStr.trim().isEmpty()) {
try {
Long currentStock = Long.parseLong(stockStr);
Long newStock = Math.max(0, currentStock - orderNum); // 确保库存不为负数
skuItem.put("stock", newStock.toString());
// 更新ServiceGoods的sku字段
serviceGoods.setSku(skuJson.toJSONString());
System.out.println("SKU库存更新成功商品ID: " + serviceGoods.getId() +
", 订单SKU: " + orderSku + ", 原库存: " + currentStock +
System.out.println("SKU库存更新成功商品ID: " + serviceGoods.getId() +
", 订单SKU: " + orderSku + ", 原库存: " + currentStock +
", 扣减数量: " + orderNum + ", 剩余库存: " + newStock);
found = true;
break;
} catch (NumberFormatException e) {
System.err.println("SKU库存格式错误商品ID: " + serviceGoods.getId() +
System.err.println("SKU库存格式错误商品ID: " + serviceGoods.getId() +
", SKU: " + orderSku + ", 库存值: " + stockStr);
}
} else {
@ -1758,21 +1759,21 @@ public class OrderUtil {
}
}
}
if (!found) {
System.err.println("未找到匹配的SKU商品ID: " + serviceGoods.getId() +
System.err.println("未找到匹配的SKU商品ID: " + serviceGoods.getId() +
", 订单SKU: " + orderSku + ", 商品SKU数组大小: " + skuArray.size());
}
} catch (Exception e) {
System.err.println("更新SKU库存异常商品ID: " + (serviceGoods != null ? serviceGoods.getId() : "null") +
System.err.println("更新SKU库存异常商品ID: " + (serviceGoods != null ? serviceGoods.getId() : "null") +
", 订单SKU: " + orderSku + ", 错误: " + e.getMessage());
}
}
/**
* 更新商城商品SKU库存
*
*
* @param serviceGoods 商品/服务对象
* @param orderSku 订单SKU具体规格选择
* @param orderNum 订单数量
@ -1784,18 +1785,18 @@ public class OrderUtil {
System.err.println("商品对象为空无法更新SKU库存");
return;
}
if (orderSku == null || orderSku.trim().isEmpty()) {
System.err.println("订单SKU为空无法更新SKU库存");
return;
}
String serviceSku = serviceGoods.getSku();
if (serviceSku == null || serviceSku.trim().isEmpty()) {
System.err.println("商品SKU为空无法更新SKU库存商品ID: " + serviceGoods.getId());
return;
}
// 解析ServiceGoods的SKU JSON商城商品格式
JSONObject skuJson;
try {
@ -1804,20 +1805,20 @@ public class OrderUtil {
System.err.println("商品SKU JSON格式错误商品ID: " + serviceGoods.getId() + ", SKU: " + serviceSku);
return;
}
// 检查是否是商城商品格式
if (!skuJson.containsKey("sku") || !skuJson.containsKey("type")) {
System.err.println("商品SKU格式不是商城商品格式商品ID: " + serviceGoods.getId());
return;
}
// 获取sku数组
JSONArray skuArray = skuJson.getJSONArray("sku");
if (skuArray == null || skuArray.isEmpty()) {
System.err.println("商品SKU数组为空商品ID: " + serviceGoods.getId());
return;
}
// 解析订单SKU具体规格选择
JSONObject orderSkuJson;
try {
@ -1826,33 +1827,33 @@ public class OrderUtil {
System.err.println("订单SKU JSON格式错误订单SKU: " + orderSku);
return;
}
// 在sku数组中查找匹配的规格
boolean found = false;
for (int i = 0; i < skuArray.size(); i++) {
JSONObject skuItem = skuArray.getJSONObject(i);
// 检查是否匹配订单SKU的"价格"属性
if (isGoodsSkuMatch(skuItem, orderSkuJson)) {
// 找到匹配的SKU更新库存
String stockStr = skuItem.getString("stock");
if (stockStr != null && !stockStr.trim().isEmpty()) {
try {
Long currentStock = Long.parseLong(stockStr);
Long newStock = Math.max(0, currentStock - orderNum); // 确保库存不为负数
skuItem.put("stock", newStock.toString());
// 更新ServiceGoods的sku字段
serviceGoods.setSku(skuJson.toJSONString());
System.out.println("商城商品SKU库存更新成功商品ID: " + serviceGoods.getId() +
", 订单SKU: " + orderSku + ", 原库存: " + currentStock +
System.out.println("商城商品SKU库存更新成功商品ID: " + serviceGoods.getId() +
", 订单SKU: " + orderSku + ", 原库存: " + currentStock +
", 扣减数量: " + orderNum + ", 剩余库存: " + newStock);
found = true;
break;
} catch (NumberFormatException e) {
System.err.println("SKU库存格式错误商品ID: " + serviceGoods.getId() +
System.err.println("SKU库存格式错误商品ID: " + serviceGoods.getId() +
", SKU: " + orderSku + ", 库存值: " + stockStr);
}
} else {
@ -1860,21 +1861,21 @@ public class OrderUtil {
}
}
}
if (!found) {
System.err.println("未找到匹配的SKU商品ID: " + serviceGoods.getId() +
System.err.println("未找到匹配的SKU商品ID: " + serviceGoods.getId() +
", 订单SKU: " + orderSku + ", 商品SKU数组大小: " + skuArray.size());
}
} catch (Exception e) {
System.err.println("更新商城商品SKU库存异常商品ID: " + (serviceGoods != null ? serviceGoods.getId() : "null") +
System.err.println("更新商城商品SKU库存异常商品ID: " + (serviceGoods != null ? serviceGoods.getId() : "null") +
", 订单SKU: " + orderSku + ", 错误: " + e.getMessage());
}
}
/**
* 检查商城商品SKU是否匹配
*
*
* @param skuItem 商品SKU项
* @param orderSkuJson 订单SKU JSON
* @return 是否匹配
@ -1886,31 +1887,31 @@ public class OrderUtil {
System.err.println("商城商品SKU匹配检查参数为空");
return false;
}
// 商城商品主要根据"价格"属性进行匹配
String orderPrice = orderSkuJson.getString("价格");
String skuPrice = skuItem.getString("价格");
if (orderPrice == null || skuPrice == null) {
System.err.println("商城商品SKU价格属性为空订单价格: " + orderPrice + ", 商品价格: " + skuPrice);
return false;
}
// 如果价格属性匹配返回true
if (orderPrice.equals(skuPrice)) {
return true;
}
return false;
} catch (Exception e) {
System.err.println("商城商品SKU匹配检查异常: " + e.getMessage());
return false;
}
}
/**
* 检查SKU是否匹配
*
*
* @param skuItem 商品SKU项
* @param orderSkuJson 订单SKU JSON
* @return 是否匹配
@ -1922,36 +1923,36 @@ public class OrderUtil {
System.err.println("SKU匹配检查参数为空");
return false;
}
// 定义需要跳过的非属性字段
Set<String> skipFields = new HashSet<>(Arrays.asList(
"price", "groupprice", "seckillprice", "stock", "pic", "image"
));
// 遍历订单SKU的所有属性检查是否与商品SKU项匹配
for (String key : orderSkuJson.keySet()) {
// 跳过非属性字段
if (skipFields.contains(key)) {
continue;
}
String orderValue = orderSkuJson.getString(key);
String skuValue = skuItem.getString(key);
// 如果订单SKU中的属性在商品SKU中不存在返回false
if (skuValue == null) {
System.err.println("商品SKU中缺少属性: " + key + ", 订单SKU值: " + orderValue);
return false;
}
// 如果属性值不匹配返回false
if (!orderValue.equals(skuValue)) {
System.err.println("SKU属性不匹配属性: " + key +
System.err.println("SKU属性不匹配属性: " + key +
", 订单值: " + orderValue + ", 商品值: " + skuValue);
return false;
}
}
return true;
} catch (Exception e) {
System.err.println("SKU匹配检查异常: " + e.getMessage());

View File

@ -26,7 +26,7 @@ import java.util.*;
/**
* 微信支付工具类
*
*
* 提供微信支付相关的完整功能实现
* 主要功能
* 1. 统一下单支付
@ -71,30 +71,30 @@ public class WechatPayUtil {
private static WechatConfig wechatConfig() {
return SpringUtils.getBean(WechatConfig.class);
}
/**
* 微信支付API地址仅保留小程序相关接口
*/
private static final String WECHAT_PAY_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // 统一下单
private static final String WECHAT_QUERY_URL = "https://api.mch.weixin.qq.com/pay/orderquery"; // 订单查询
private static final String WECHAT_REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
private static final String WECHAT_REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
private static final String WECHAT_TRANSFER_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; // 企业付款
public static final String PAY_FH = "https://73bb8889.r3.cpolar.top/";
public static final String PAY_FH = "https://api.huafurenjia.cn/";
/**
* 其他配置常量
*/
private static final String TRADE_TYPE_JSAPI = "JSAPI";
private static final String TRADE_TYPE_JSAPI = "JSAPI";
private static final String CURRENCY = "CNY"; // 货币类型
private static final String SUCCESS_CODE = "SUCCESS";
private static final String FAIL_CODE = "FAIL";
private static final String SUCCESS_CODE = "SUCCESS";
private static final String FAIL_CODE = "FAIL";
/**
* RestTemplate实例配置UTF-8编码
*/
private static final RestTemplate restTemplate = createRestTemplate();
/**
* 创建配置了UTF-8编码的RestTemplate实例
*/
@ -158,36 +158,36 @@ public class WechatPayUtil {
if (attach != null && !attach.trim().isEmpty()) {
params.put("attach", attach);
}
// 3. 生成签名
String sign = generateSign(params, wechatConfig().getApikey());
params.put("sign", sign);
// 4. 发送请求
String xmlRequest = mapToXml(params);
// 设置请求头指定字符编码为UTF-8
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/xml;charset=UTF-8");
headers.set("Accept", "application/xml;charset=UTF-8");
headers.set("User-Agent", "Mozilla/5.0");
HttpEntity<String> requestEntity = new HttpEntity<>(xmlRequest, headers);
ResponseEntity<String> response = restTemplate.exchange(WECHAT_PAY_URL, HttpMethod.POST, requestEntity, String.class);
// 5. 解析响应
Map<String, String> responseMap = xmlToMap(response.getBody());
// 6. 处理响应
String returnCode = responseMap.get("return_code");
String resultCode = responseMap.get("result_code");
if (SUCCESS_CODE.equals(returnCode) && SUCCESS_CODE.equals(resultCode)) {
String prepayId = responseMap.get("prepay_id");
// 7. 构建小程序端调起支付参数
Map<String, Object> payParams = buildMiniProgramPayParams(prepayId);
result.put("success", true);
result.put("payParams", payParams);
result.put("prepayId", prepayId);
@ -196,14 +196,14 @@ public class WechatPayUtil {
String errorCode = responseMap.get("err_code");
String errorCodeDes = responseMap.get("err_code_des");
String returnMsg = responseMap.get("return_msg");
String errorMessage = "统一下单失败:" + (errorCodeDes != null ? errorCodeDes : returnMsg);
result = failResult(errorMessage);
}
} catch (Exception e) {
result = failResult("统一下单异常:" + e.getMessage());
}
return result;
}
@ -232,13 +232,13 @@ public class WechatPayUtil {
String sign = generateSign(params, wechatConfig().getApikey());
params.put("sign", sign);
String xmlRequest = mapToXml(params);
// 设置请求头指定字符编码为UTF-8解决中文乱码
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/xml;charset=UTF-8");
headers.set("Accept", "application/xml;charset=UTF-8");
headers.set("User-Agent", "Mozilla/5.0");
HttpEntity<String> requestEntity = new HttpEntity<>(xmlRequest, headers);
ResponseEntity<String> response = restTemplate.exchange(WECHAT_QUERY_URL, HttpMethod.POST, requestEntity, String.class);
Map<String, String> responseMap = xmlToMap(response.getBody());
@ -309,13 +309,13 @@ public class WechatPayUtil {
/**
* 申请退款
*
*
* @param refundInfo 退款信息
* @return 退款结果
*/
public Map<String, Object> refund(Map<String, Object> refundInfo) {
Map<String, Object> result = new HashMap<>();
try {
// 1. 参数验证
String validationError = validateRefundParams(refundInfo);
@ -324,53 +324,53 @@ public class WechatPayUtil {
result.put("message", validationError);
return result;
}
// 2. 构建退款参数
Map<String, String> params = buildRefundParams(refundInfo);
// 3. 生成签名
String sign = generateSign(params, wechatConfig().getApikey());
params.put("sign", sign);
// 4. 发送请求需要证书
String xmlRequest = mapToXml(params);
// 注意退款接口需要使用客户端证书这里简化处理
ResponseEntity<String> response = restTemplate.postForEntity(WECHAT_REFUND_URL, xmlRequest, String.class);
// 5. 解析响应
Map<String, String> responseMap = xmlToMap(response.getBody());
if (SUCCESS_CODE.equals(responseMap.get("return_code")) &&
if (SUCCESS_CODE.equals(responseMap.get("return_code")) &&
SUCCESS_CODE.equals(responseMap.get("result_code"))) {
Map<String, Object> refundResult = new HashMap<>();
refundResult.put("refundId", responseMap.get("refund_id"));
refundResult.put("outRefundNo", responseMap.get("out_refund_no"));
refundResult.put("refundFee", responseMap.get("refund_fee"));
refundResult.put("settlementRefundFee", responseMap.get("settlement_refund_fee"));
result.put("success", true);
result.put("refundInfo", refundResult);
result.put("message", "申请退款成功");
} else {
result.put("success", false);
result.put("message", "申请退款失败:" +
(responseMap.get("err_code_des") != null ? responseMap.get("err_code_des") :
result.put("message", "申请退款失败:" +
(responseMap.get("err_code_des") != null ? responseMap.get("err_code_des") :
responseMap.get("return_msg")));
}
} catch (Exception e) {
result.put("success", false);
result.put("message", "申请退款异常:" + e.getMessage());
}
return result;
}
/**
* 企业付款到零钱微信支付v3接口实现
*
*
* @param transferInfo 付款信息
* @return 付款结果
*/
@ -558,7 +558,7 @@ public class WechatPayUtil {
String nonceStr = generateNonceStr();
String packageStr = "prepay_id=" + prepayId;
String signType = "MD5";
// 构建签名参数
Map<String, String> signParams = new HashMap<>();
signParams.put("appId", wechatConfig().getAppid());
@ -566,15 +566,15 @@ public class WechatPayUtil {
signParams.put("nonceStr", nonceStr);
signParams.put("package", packageStr);
signParams.put("signType", signType);
String paySign = generateSign(signParams, wechatConfig().getApikey());
payParams.put("timeStamp", timeStamp);
payParams.put("nonceStr", nonceStr);
payParams.put("package", packageStr);
payParams.put("signType", signType);
payParams.put("paySign", paySign);
return payParams;
}
@ -594,7 +594,7 @@ public class WechatPayUtil {
}
}
stringBuilder.append("key=").append(key);
String signString = stringBuilder.toString();
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(signString.getBytes(StandardCharsets.UTF_8));
@ -602,7 +602,7 @@ public class WechatPayUtil {
for (byte b : digest) {
result.append(String.format("%02X", b));
}
return result.toString();
} catch (Exception e) {
throw new RuntimeException("生成签名失败", e);
@ -656,28 +656,28 @@ public class WechatPayUtil {
String processedXml = xml.replaceAll("<\\?xml[^>]*\\?>", "")
.replaceAll("<xml>", "")
.replaceAll("</xml>", "");
String[] elements = processedXml.split("</\\w+>");
for (String element : elements) {
if (element.trim().isEmpty()) {
continue;
}
int startTag = element.indexOf("<");
int endTag = element.indexOf(">");
if (startTag >= 0 && endTag > startTag) {
String key = element.substring(startTag + 1, endTag);
String value = element.substring(endTag + 1);
if (value.startsWith("<![CDATA[") && value.endsWith("]]>")) {
value = value.substring(9, value.length() - 3);
}
map.put(key, value);
}
}
} catch (Exception e) {
throw new RuntimeException("XML解析失败", e);
}
@ -825,14 +825,14 @@ public class WechatPayUtil {
params.put("out_refund_no", refundInfo.get("refundNo").toString());
params.put("total_fee", refundInfo.get("totalFee").toString());
params.put("refund_fee", refundInfo.get("refundFee").toString());
if (refundInfo.get("refundDesc") != null) {
params.put("refund_desc", refundInfo.get("refundDesc").toString());
}
if (refundInfo.get("notifyUrl") != null) {
params.put("notify_url", refundInfo.get("notifyUrl").toString());
}
return params;
}
@ -848,16 +848,16 @@ public class WechatPayUtil {
params.put("nonce_str", generateNonceStr());
params.put("partner_trade_no", transferInfo.get("partnerTradeNo").toString());
params.put("openid", transferInfo.get("openid").toString());
params.put("check_name", transferInfo.get("checkName") != null ?
params.put("check_name", transferInfo.get("checkName") != null ?
transferInfo.get("checkName").toString() : "NO_CHECK");
params.put("amount", transferInfo.get("amount").toString());
params.put("desc", transferInfo.get("desc").toString());
params.put("spbill_create_ip", "127.0.0.1");
if (transferInfo.get("reUserName") != null) {
params.put("re_user_name", transferInfo.get("reUserName").toString());
}
return params;
}
@ -1051,4 +1051,4 @@ public class WechatPayUtil {
return transferToUserV3(transferInfo);
}
}
}