|
|
@ -0,0 +1,203 @@ |
|
|
|
package org.jeecg.api.service.impl; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.jeecg.common.logistics.dto.WeChatLogisticsAddOrderRequest; |
|
|
|
import org.jeecg.common.logistics.dto.WeChatLogisticsAddOrderResponse; |
|
|
|
import org.jeecg.common.logistics.dto.WeChatLogisticsCancelOrderRequest; |
|
|
|
import org.jeecg.common.logistics.dto.WeChatLogisticsCancelOrderResponse; |
|
|
|
import org.jeecg.common.logistics.service.WeChatLogisticsService; |
|
|
|
import org.jeecg.common.logistics.util.WeChatLogisticsRequestBuilder; |
|
|
|
import org.jeecg.modules.commonOrder.entity.CommonOrder; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
|
public class LogisticsUtil { |
|
|
|
|
|
|
|
|
|
|
|
@Value("${logistics.deliveryId}") |
|
|
|
private String deliveryId; |
|
|
|
|
|
|
|
@Value("${logistics.bizId}") |
|
|
|
private String bizId; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WeChatLogisticsService weChatLogisticsService; |
|
|
|
|
|
|
|
public WeChatLogisticsAddOrderResponse addOrder(String openid, CommonOrder order){ |
|
|
|
|
|
|
|
ArrayList<String> address = getAddress(order.getAddress()); |
|
|
|
|
|
|
|
// 获取上门时间 |
|
|
|
String goTimeStr = order.getGoTime(); |
|
|
|
long goTimeStamp; |
|
|
|
try { |
|
|
|
// 将字符串时间转换为时间戳 |
|
|
|
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
|
|
java.util.Date goDate = sdf.parse(goTimeStr); |
|
|
|
java.util.Calendar calendar = java.util.Calendar.getInstance(); |
|
|
|
|
|
|
|
// 获取当前时间 |
|
|
|
java.util.Date currentDate = new java.util.Date(); |
|
|
|
|
|
|
|
// 判断上门时间是否小于当前时间 |
|
|
|
if (goDate.before(currentDate)) { |
|
|
|
// 如果小于当前时间,设置为明天的同一时刻(只改变日期,时间保持不变) |
|
|
|
calendar.setTime(goDate); |
|
|
|
calendar.add(java.util.Calendar.DAY_OF_MONTH, 1); |
|
|
|
goDate = calendar.getTime(); |
|
|
|
|
|
|
|
// 更新order中的上门时间 |
|
|
|
String newGoTimeStr = sdf.format(goDate); |
|
|
|
order.setGoTime(newGoTimeStr); |
|
|
|
log.info("上门时间小于当前时间,已调整为明天同一时刻:{}", newGoTimeStr); |
|
|
|
} |
|
|
|
|
|
|
|
// 转换为时间戳(秒) |
|
|
|
goTimeStamp = goDate.getTime() / 1000; |
|
|
|
} catch (Exception e) { |
|
|
|
// 转换异常时使用当前时间加24小时 |
|
|
|
java.util.Calendar calendar = java.util.Calendar.getInstance(); |
|
|
|
calendar.add(java.util.Calendar.DAY_OF_MONTH, 1); |
|
|
|
java.util.Date tomorrow = calendar.getTime(); |
|
|
|
|
|
|
|
// 更新order中的上门时间 |
|
|
|
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
|
|
String newGoTimeStr = sdf.format(tomorrow); |
|
|
|
order.setGoTime(newGoTimeStr); |
|
|
|
|
|
|
|
goTimeStamp = tomorrow.getTime() / 1000; |
|
|
|
log.warn("上门时间格式错误,已设置为明天:{}", newGoTimeStr); |
|
|
|
} |
|
|
|
|
|
|
|
// 使用工具类构建测试请求 |
|
|
|
WeChatLogisticsAddOrderRequest request = WeChatLogisticsRequestBuilder.createOrderRequest() |
|
|
|
.orderId(order.getId()) |
|
|
|
.openid(openid) |
|
|
|
.deliveryId(deliveryId) |
|
|
|
.bizId(bizId) |
|
|
|
.customRemark("测试订单") |
|
|
|
.addSource(0) // 小程序订单 |
|
|
|
|
|
|
|
// 发件人信息(测试数据) |
|
|
|
.sender(order.getName(), order.getPhone(), address.get(0), address.get(1), address.get(2), order.getAddressDetail()) |
|
|
|
|
|
|
|
// 收件人信息(测试数据) |
|
|
|
.receiver("李四", "13900139000", "上海市", "上海市", "浦东新区", "张江高科技园区科苑路399号") |
|
|
|
|
|
|
|
// 包裹信息 |
|
|
|
.cargo(1, 1.5, 25.0, 20.0, 15.0) |
|
|
|
.cargoDetail("测试商品", 1) |
|
|
|
|
|
|
|
// 商品信息 |
|
|
|
.shop("pages/order/detail?id=test123", "https://example.com/test.jpg", "测试商品", 1) |
|
|
|
|
|
|
|
// 保价信息(保价100元) |
|
|
|
.insured(0,0) |
|
|
|
|
|
|
|
// 服务类型(测试环境固定配置) |
|
|
|
/* |
|
|
|
1(大件快递3.60) |
|
|
|
2(特准快件) |
|
|
|
*/ |
|
|
|
.serviceWithExpectTime(1, "test_service_name", goTimeStamp) // 上门取件时间 |
|
|
|
.build(); |
|
|
|
|
|
|
|
log.info("测试下单请求:{}", request); |
|
|
|
|
|
|
|
// 调用下单服务 |
|
|
|
WeChatLogisticsAddOrderResponse response = null; |
|
|
|
try { |
|
|
|
response = weChatLogisticsService.addOrder(request); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
|
|
|
|
// 输出地址验证信息 |
|
|
|
if (response.getIsCorrectSender() != null) { |
|
|
|
log.info("发件人信息验证:{}", response.getIsCorrectSender() == 1 ? "正确" : "错误"); |
|
|
|
} |
|
|
|
if (response.getIsCorrectReceiver() != null) { |
|
|
|
log.info("收件人信息验证:{}", response.getIsCorrectReceiver() == 1 ? "正确" : "错误"); |
|
|
|
} |
|
|
|
|
|
|
|
order.setWliuNo(response.getWaybillId());//设置物流编号 |
|
|
|
|
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WeChatLogisticsCancelOrderResponse cancelOrder(String openid, CommonOrder order, String cancelMsg){ |
|
|
|
|
|
|
|
WeChatLogisticsCancelOrderRequest request = new WeChatLogisticsCancelOrderRequest(); |
|
|
|
request.setOrderId(order.getId()); |
|
|
|
request.setWaybillId(order.getWliuNo()); |
|
|
|
request.setDeliveryId(deliveryId); |
|
|
|
request.setOpenid(openid); |
|
|
|
request.setCancelMsg(cancelMsg); |
|
|
|
|
|
|
|
log.info("取消运单请求:{}", request); |
|
|
|
|
|
|
|
WeChatLogisticsCancelOrderResponse response = null; |
|
|
|
try { |
|
|
|
response = weChatLogisticsService.cancelOrder(request); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
log.info("===== 取消运单结果 ====="); |
|
|
|
log.info("订单ID:{}", response.getOrderId()); |
|
|
|
log.info("运单ID:{}", response.getWaybillId()); |
|
|
|
log.info("取消时间:{}", response.getCancelTime()); |
|
|
|
log.info("===================="); |
|
|
|
|
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
public ArrayList<String> getAddress(String address){ |
|
|
|
|
|
|
|
ArrayList<String> list = new ArrayList<>(); |
|
|
|
|
|
|
|
// 从地址中提取省市区信息 |
|
|
|
String province = ""; |
|
|
|
String city = ""; |
|
|
|
String district = ""; |
|
|
|
|
|
|
|
// 使用正则表达式解析地址字符串,提取省市区 |
|
|
|
if (address != null && !address.isEmpty()) { |
|
|
|
// 匹配省份(以省、自治区、直辖市结尾) |
|
|
|
java.util.regex.Pattern provincePattern = java.util.regex.Pattern.compile("^(.*?省|.*?自治区|北京市|天津市|上海市|重庆市)"); |
|
|
|
java.util.regex.Matcher provinceMatcher = provincePattern.matcher(address); |
|
|
|
if (provinceMatcher.find()) { |
|
|
|
province = provinceMatcher.group(1); |
|
|
|
list.add(province); |
|
|
|
} |
|
|
|
|
|
|
|
// 匹配城市(以市、自治州、地区、盟结尾) |
|
|
|
java.util.regex.Pattern cityPattern = java.util.regex.Pattern.compile("(?<=" + province + ")(.*?市|.*?自治州|.*?地区|.*?盟)"); |
|
|
|
java.util.regex.Matcher cityMatcher = cityPattern.matcher(address); |
|
|
|
if (cityMatcher.find()) { |
|
|
|
city = cityMatcher.group(1); |
|
|
|
list.add(city); |
|
|
|
} |
|
|
|
|
|
|
|
// 匹配区县(以区、县、旗结尾) |
|
|
|
java.util.regex.Pattern districtPattern = java.util.regex.Pattern.compile("(?<=" + city + ")(.*?区|.*?县|.*?旗|.*?市)"); |
|
|
|
java.util.regex.Matcher districtMatcher = districtPattern.matcher(address); |
|
|
|
if (districtMatcher.find()) { |
|
|
|
district = districtMatcher.group(1); |
|
|
|
list.add(district); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |