From e56f8e46c52ed9bef438d18fe1c2c68f00309842 Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Fri, 6 Dec 2024 00:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=BA=E4=B8=B0=E5=90=8C=E5=9F=8E=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../suisung/mall/common/utils/CommonUtil.java | 28 --------- mall-shop/pom.xml | 9 ++- .../suisung/mall/shop/config/BaiduUtil.java | 40 +++++++++++++ .../impl/ShopOrderBaseServiceImpl.java | 41 ++++++++----- .../admin/ShopStorePrinterController.java | 2 +- ...StoreSameCityTransportBaseServiceImpl.java | 50 ++++++++-------- .../user/service/ShopUserCartService.java | 12 ++++ .../service/impl/ShopUserCartServiceImpl.java | 59 ++++++++++++++----- 8 files changed, 153 insertions(+), 88 deletions(-) diff --git a/mall-common/src/main/java/com/suisung/mall/common/utils/CommonUtil.java b/mall-common/src/main/java/com/suisung/mall/common/utils/CommonUtil.java index ef29724b..e5b1f8dd 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/utils/CommonUtil.java +++ b/mall-common/src/main/java/com/suisung/mall/common/utils/CommonUtil.java @@ -131,34 +131,6 @@ public class CommonUtil { .distinct().collect(Collectors.toList()); } - /** - * 判断任何一个对象是否有空值? - * @param objs - * @return - */ - public static boolean hasAnyBlank(Object... objs) { - if (objs == null || objs.length == 0) { - return true; - } - - for (Object o : objs) { - if (o == null) { - return true; - } - } - - return false; - } - - /** - * 判断全部对象是否都不为空 - * @param objs - * @return - */ - public static boolean isAllNotBlank(Object... objs) { - return !hasAnyBlank(objs); - } - /** * Double 四舍五入,不保留小数点 * @param d diff --git a/mall-shop/pom.xml b/mall-shop/pom.xml index 7672add2..e9518400 100644 --- a/mall-shop/pom.xml +++ b/mall-shop/pom.xml @@ -185,12 +185,18 @@ 2.7.0 + + + org.gavaghan + geodesy + 1.1.3 + + org.apache.httpcomponents httpclient - 4.5.2 @@ -218,7 +224,6 @@ org.apache.httpcomponents httpcore - 4.4.4 diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/config/BaiduUtil.java b/mall-shop/src/main/java/com/suisung/mall/shop/config/BaiduUtil.java index 7c3182f1..a9ce1592 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/config/BaiduUtil.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/config/BaiduUtil.java @@ -7,6 +7,11 @@ import com.alibaba.fastjson.JSONObject; import com.suisung.mall.common.utils.PropertiesUtil; import com.suisung.mall.shop.entity.LocationBean; +import org.gavaghan.geodesy.Ellipsoid; +import org.gavaghan.geodesy.GeodeticCalculator; +import org.gavaghan.geodesy.GeodeticCurve; +import org.gavaghan.geodesy.GlobalCoordinates; + import java.util.Map; public class BaiduUtil { @@ -15,6 +20,12 @@ public class BaiduUtil { final static String ADDRESS_TO_LONGITUDEA_URL = "https://api.map.baidu.com/geocoding/v3/?address="; final static String BAIDU_GET_ADDRESS_URL = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location="; + /** + * 通过地址获取经纬度 + * + * @param address + * @return + */ public static LocationBean getLatAndLngByAddress(String address) { if (StrUtil.isBlank(address)) { return null; @@ -31,6 +42,13 @@ public class BaiduUtil { return null; } + /** + * 通过经纬度获取地址 + * + * @param lng + * @param lat + * @return + */ public static String getAddressByLocation(String lng, String lat) { String url = BAIDU_GET_ADDRESS_URL + lat + "," + lng + "&output=json&pois=1&ak=" + AK; String strResult = HttpUtil.get(url); @@ -46,4 +64,26 @@ public class BaiduUtil { } return null; } + + /** + * 根据两点获取距离 + * 参考:https://www.cnblogs.com/xuzhw/p/10143626.html + * 对比百度地图,计算结果和Sphere坐标系计算结果一致,表明计算结果正确,WGS84坐标系的计算结果存在几十米的误差。 + * 不同的坐标系精度不同,计算结果不一样。大家根据实际情况自己选择 + * + * @param longitudeFrom + * @param latitudeFrom + * @param longitudeTo + * @param latitudeTo + * @return + */ + public static Double getDistance(Double longitudeFrom, Double latitudeFrom, Double longitudeTo, Double latitudeTo) { + if (longitudeFrom == null || latitudeFrom == null || longitudeTo == null || latitudeTo == null) { + return null; + } + + GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom); + GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo); + return new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target).getEllipsoidalDistance(); + } } \ No newline at end of file diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java index dc94a9d6..e3bf25da 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java @@ -19,6 +19,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.suisung.mall.common.api.*; import com.suisung.mall.common.constant.CommonConstant; import com.suisung.mall.common.constant.ConfigConstant; +import com.suisung.mall.common.constant.ConstantError; import com.suisung.mall.common.constant.MqConstant; import com.suisung.mall.common.domain.UserDto; import com.suisung.mall.common.exception.ApiException; @@ -1425,6 +1426,11 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl queryWrapper = new QueryWrapper<>(); queryWrapper.eq("order_id", orderId); List shopOrderItemList = shopOrderItemService.list(queryWrapper); if (shopOrderBase == null || CollUtil.isEmpty(shopOrderItemList)) { + logger.error("无法获取订单信息!"); + return null; + } + + ShopStoreBase shopStoreBase = shopStoreBaseService.get(shopOrderBase.getStore_id()); + ShopStoreInfo shopStoreInfo = shopStoreInfoService.get(shopOrderBase.getStore_id()); + ShopOrderDeliveryAddress shopOrderDeliveryAddress = shopOrderDeliveryAddressService.selectByOrderId(orderId); + if (shopStoreBase == null || shopStoreInfo == null || shopOrderDeliveryAddress == null) { + logger.error("无法获取店铺信息!"); return null; } // 7位数取单号,位数不够向左补0 String orderPickupNumStr = String.format("%07d", orderPickupNum); - + // TODO 顺丰同城业务员给的店铺id + String shopId = "3243279847393"; SFCreateOrderReq sfCreateOrderReq = new SFCreateOrderReq(); sfCreateOrderReq.setDev_id(devId); - sfCreateOrderReq.setShop_id(shopStoreBase.getStore_id().toString()); + sfCreateOrderReq.setShop_id(shopId); // TODO 顺丰同城业务员给的店铺id sfCreateOrderReq.setPush_time(DateUtil.currentSeconds()); sfCreateOrderReq.setShop_order_id(orderId); sfCreateOrderReq.setOrder_time(shopOrderBase.getOrder_time().getTime() / 1000); @@ -8373,7 +8384,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl pairReceiveAddr = shopOrderDeliveryAddressService.checkAddress(shopOrderDeliveryAddress); SFOrderReceiveReq receive = new SFOrderReceiveReq(); receive.setUser_name(shopOrderDeliveryAddress.getDa_name()); - receive.setUser_phone(shopOrderDeliveryAddress.getDa_telephone()); + receive.setUser_phone(shopOrderDeliveryAddress.getDa_mobile()); receive.setUser_address(pairReceiveAddr.getSecond().getFullAddress()); receive.setUser_lng(pairReceiveAddr.getSecond().getLongitude()); receive.setUser_lat(pairReceiveAddr.getSecond().getLatitude()); diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java index 8143913f..bf76c649 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java @@ -33,7 +33,7 @@ public class ShopStorePrinterController { @ApiOperation(value = "内部测试案例", notes = "内部测试案例") @RequestMapping(value = "/testcase", method = {RequestMethod.GET}) public CommonResult TestCase() { - Object data = shopStoreSameCityTransportBaseService.computeSameCityTransportFee(2L, BigDecimal.valueOf(120.085), BigDecimal.valueOf(23.37), 20000, BigDecimal.valueOf(102), BigDecimal.valueOf(96), BigDecimal.valueOf(94),true); + Object data = shopStoreSameCityTransportBaseService.computeSameCityTransportFee(3L, BigDecimal.valueOf(110.07123874241765), BigDecimal.valueOf(23.366250981849255), 100, BigDecimal.valueOf(0.5), BigDecimal.valueOf(5.5), BigDecimal.valueOf(5.5),true); return CommonResult.success(data); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreSameCityTransportBaseServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreSameCityTransportBaseServiceImpl.java index 6a10e9f3..7c164b85 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreSameCityTransportBaseServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStoreSameCityTransportBaseServiceImpl.java @@ -10,6 +10,7 @@ package com.suisung.mall.shop.store.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.suisung.mall.common.api.CommonResult; @@ -37,6 +38,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; +import java.text.DecimalFormat; import java.util.Date; import java.util.List; @@ -237,8 +239,6 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl transportBase.getDistance_base() * 1000) { -// if (transportBase.getDistance_increase_km() != null && transportBase.getDistance_increase_fee() != null && transportBase.getDistance_base() != null && distance > transportBase.getDistance_base() * 1000) { + if (transportBase.getDistance_increase_km() != null && transportBase.getDistance_increase_fee() != null && transportBase.getDistance_base() != null && distance > transportBase.getDistance_base() * 1000) { // 实际配送距离超出基础距离,单位km BigDecimal diffDistanceM = CommonUtil.DecimalRoundHalfUp(BigDecimal.valueOf(distance - transportBase.getDistance_base() * 1000).divide(BigDecimal.valueOf(1000))); // 累加的次数 @@ -308,8 +304,7 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl transportBase.getWeight_base() * 1000) { - // if (transportBase.getWeight_increase_kg() != null && transportBase.getWeight_increase_fee() != null && transportBase.getWeight_base() != null && weightGram > transportBase.getWeight_base() * 1000) { + if (transportBase.getWeight_increase_kg() != null && transportBase.getWeight_increase_fee() != null && transportBase.getWeight_base() != null && weightGram > transportBase.getWeight_base() * 1000) { // 实际配送重量超出基础重量,单位kg BigDecimal diffWeightKg = CommonUtil.DecimalRoundHalfUp(BigDecimal.valueOf(weightGram - transportBase.getWeight_base() * 1000).divide(BigDecimal.valueOf(1000))); // 累加的次数 @@ -339,10 +334,14 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl 0) || (CommonConstant.Delivery_Amount_Comput_Type_Payment.equals(transport.getMin_delivery_amount_type()) && transport.getMin_delivery_amount().compareTo(orderPayAmount) > 0)) { // 订单原价金额小于起送金额,返回订单不能送达了 - canDelivery = false; + // canNotDeliveryReason ="有订单未满足起送金额,请检查您的订单金额!"; //String.format("有订单未满足%.2f元起送金额!",transport.getMin_delivery_amount()); continue; } - if (!canDelivery) { - canDelivery = true; - } + canDeliveryAreaCnt+=1; + canNotDeliveryReason=""; // 获取优惠的配送费 if ((CommonConstant.Delivery_Amount_Comput_Type_Original.equals(transport.getDelivery_discount_type()) && transport.getMin_delivery_discount_amount().compareTo(orderProductAmount) <= 0) || @@ -371,17 +369,17 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl { */ Map formatCartRows(List cart_rows, Map activity_rows, ProductVo productVo, FixOrderVo fixOrderVo); + /** + * 尝试去操作:配送区域判断及运费, 并修正最终数据 + *

+ * 1、单品在一个订单中,运费规则计算 + * 2、配送区域问题 + * + * @param cart_data 最终数据 + * @param district_id 配送地区(城市id) + * @param canThrow 关键数据缺失,抛出 api 异常错误 + */ + void tryCalTransportFreight(Map cart_data, Integer district_id, boolean canThrow); + /** * 配送区域判断及运费, 并修正最终数据 *

diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/user/service/impl/ShopUserCartServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/user/service/impl/ShopUserCartServiceImpl.java index 97100a5c..8efac7ab 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/user/service/impl/ShopUserCartServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/user/service/impl/ShopUserCartServiceImpl.java @@ -53,7 +53,6 @@ import com.suisung.mall.shop.user.service.ShopUserCartService; import com.suisung.mall.shop.user.service.ShopUserDeliveryAddressService; import com.suisung.mall.shop.user.service.ShopUserVoucherService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -143,7 +142,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl * 1、单品在一个订单中,运费规则计算 * 2、配送区域问题 * * @param cart_data 最终数据 * @param district_id 配送地区(城市id) + * @param canThrow 关键数据缺失,抛出 api 异常错误 */ @Override - public void calTransportFreight(Map cart_data, Integer district_id) { - if(cart_data==null || district_id==null){ + public void tryCalTransportFreight(Map cart_data, Integer district_id, boolean canThrow) { + if (cart_data == null || district_id == null) { throw new ApiException(I18nUtil._("参数有误!")); } boolean can_delivery = true; // 配送方式 5-门店自取;10-普通快递;16-同城配送; Integer delivery_type_id = Convert.toInt(cart_data.get("delivery_type_id"), StateCode.DELIVERY_TYPE_SAME_CITY); + // 同城配送运费计算分支 if (delivery_type_id == StateCode.DELIVERY_TYPE_SAME_CITY) { + ShopUserDeliveryAddress uAddress = Convert.convert(ShopUserDeliveryAddress.class, cart_data.get("delivery_address_row")); + if (uAddress == null && canThrow) { + throw new ApiException(I18nUtil._("至少选择一个配送地址!")); + } + // 配送不到这个区域商品id cart_data.put("transport_type_none_ids", new ArrayList()); // 配送不到这个区域商品记录 @@ -2492,6 +2498,13 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl items = (List) cart_data.get("items"); for (Map store_row : items) { + + // 检查店铺城市和收货地址是否同一城市? + List storeDistrictId = Convert.toList(Integer.class, store_row.get("store_district_id")); + if (canThrow && uAddress!=null && storeDistrictId != null && storeDistrictId.size() > 0 && !CollUtil.contains(storeDistrictId, uAddress.getUd_city_id())) { + throw new ApiException(I18nUtil._("订单不在同一城市,请检查您下单所在店铺的具体位置!")); + } + // 一个店铺的订单 Long storeId = Convert.toLong(store_row.get("store_id")); BigDecimal storeLng = Convert.toBigDecimal(store_row.get("store_longitude")); @@ -2504,27 +2517,27 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl + * 1、单品在一个订单中,运费规则计算 + * 2、配送区域问题 + * + * @param cart_data 最终数据 + * @param district_id 配送地区(城市id) + */ + @Override + public void calTransportFreight(Map cart_data, Integer district_id) { + tryCalTransportFreight(cart_data, district_id, false); + } + /** * item info 基础数据, 活动的通过活动修正。 *