同城配送费计算方法 新增

This commit is contained in:
Jack 2024-11-26 08:49:18 +08:00
parent 9a25ce2c88
commit 5b6cf0240b
6 changed files with 115 additions and 43 deletions

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.suisung.mall.common.constant;
public class CommonConstant {
public static final Integer Enable = 1; // 状态1-有效2-无效0-无效
public static final Integer Disable = 0; // 状态1-有效2-无效0-无效
public static final Integer Disable2 = 2; // 状态1-有效2-无效0-无效
// 计算同城配送费依据的金额类型1-订单原价2-订单折扣价3-订单实付金额
public static final Integer Delivery_Amount_Comput_Type_Original = 1;
public static final Integer Delivery_Amount_Comput_Type_Discounted = 2;
public static final Integer Delivery_Amount_Comput_Type_Payment = 3;
}

View File

@ -26,7 +26,5 @@ public class ConstantError {
public static final Integer NETWORK = 30; // 微信支付宝API等等接口调用
public static final Integer TASK = 40; // 计划任务相关
public static final Integer Enable = 1; // 状态1-有效2-无效0-无效
public static final Integer Disable = 0; // 状态1-有效2-无效0-无效
public static final Integer Disable2 = 2; // 状态1-有效2-无效0-无效
}

View File

@ -63,13 +63,12 @@ public interface ShopStoreSameCityTransportBaseService {
* @param storeId 店铺Id
* @param orderLongitude 订单送达地目的地经度
* @param orderLatitude 订单送达地目的地维度
* @param distanceMeter 距离单位米
* @param weightGram 重量单位克
* @param orderProductAmount 订单商品原价金额
* @param orderDiscountAmount 订单商品折扣金额订单原价减去每个商品折扣费
* @param orderPayAmount 订单实际支付金额折扣金额-优惠券-积分扣-人工干预扣费不包含运费
* @return 同城配送费
* @return 是否能配送最终同城配送费
*/
BigDecimal computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, Integer distanceMeter, Integer weightGram, BigDecimal orderProductAmount, BigDecimal orderDiscountAmount, BigDecimal orderPayAmount);
Pair<Boolean, BigDecimal> computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, Integer weightGram, BigDecimal orderProductAmount, BigDecimal orderDiscountAmount, BigDecimal orderPayAmount);
}

View File

@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suisung.mall.common.api.CommonResult;
import com.suisung.mall.common.api.StateCode;
import com.suisung.mall.common.constant.ConstantError;
import com.suisung.mall.common.constant.CommonConstant;
import com.suisung.mall.common.domain.UserDto;
import com.suisung.mall.common.modules.store.ShopStorePrinter;
import com.suisung.mall.common.modules.store.ShopStorePrinterLog;
@ -116,12 +116,12 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
record.setStore_id(Integer.parseInt(user.getStore_id()));
// 默认不向厂家绑定打印机
record.setStatus(ConstantError.Disable2);
record.setStatus(CommonConstant.Disable2);
record.setFlag(record.getStatus());
record.setCreated_by(userId);
record.setUpdated_by(userId);
// if (record.getStatus() == null) {
// record.setStatus(ConstantError.Enable);
// record.setStatus(CommonConstant.Enable);
// }
@ -132,7 +132,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
ShopStorePrinter existRecord = getOne(queryWrapper);
if (existRecord != null && existRecord.getPrinter_id() > 0) {
// 打印机已经存在的情况
// if (existRecord.getFlag() == ConstantError.Disable2) {
// if (existRecord.getFlag() == CommonConstant.Disable2) {
// // 打印机没有绑定飞鹅平台
// UpdateWrapper<ShopStorePrinter> updateWrapper = new UpdateWrapper<ShopStorePrinter>();
// updateWrapper.eq("printer_id", existRecord.getPrinter_id());
@ -143,7 +143,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
// // "922441475#r6ZXPvHH#核销柜台";
// boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", existRecord.getPrinter_sn(), existRecord.getPrinter_key(), existRecord.getPrinter_name()));
// if (success) {
// updateWrapper.set("flag", ConstantError.Enable);
// updateWrapper.set("flag", CommonConstant.Enable);
// }
//
// update(updateWrapper);
@ -167,7 +167,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
// if (!success) {
// msg = msg + ",但打印机绑定未成功,请检查打印机编号和密钥是否正确。";
// }
// record.setFlag(ConstantError.Enable);
// record.setFlag(CommonConstant.Enable);
// }
if (add(record)) {
@ -210,10 +210,10 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
updateWrapper.set("region_id", record.getRegion_id());
updateWrapper.set("paper_with", record.getPaper_with());
updateWrapper.set("website_url", record.getWebsite_url());
if (existRecord.getStatus() == null || !ConstantError.Enable.equals(existRecord.getStatus())) {
updateWrapper.set("status", ConstantError.Disable2);
if (existRecord.getStatus() == null || !CommonConstant.Enable.equals(existRecord.getStatus())) {
updateWrapper.set("status", CommonConstant.Disable2);
} else {
updateWrapper.set("status", ConstantError.Enable);
updateWrapper.set("status", CommonConstant.Enable);
}
updateWrapper.set("updated_at", new Date());
@ -223,11 +223,11 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
// if (existRecord.getPrinter_sn().equals(record.getPrinter_sn())) {
// // sn 没有变化不更新 sn
// if (ConstantError.Disable2.equals(existRecord.getFlag())) {
// if (CommonConstant.Disable2.equals(existRecord.getFlag())) {
// // 往厂商添加打印机
// boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name()));
// if (success) {
// updateWrapper.set("flag", ConstantError.Enable);
// updateWrapper.set("flag", CommonConstant.Enable);
// } else {
// msg = msg + ",但打印机绑定未成功,请检查打印机编号和密钥是否正确。";
// }
@ -244,12 +244,12 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
// 格式"922441475#r6ZXPvHH#核销柜台";
Pair<Boolean, String> retPair = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name()));
if (retPair.getFirst()) {
updateWrapper.set("flag", ConstantError.Enable);
updateWrapper.set("status", ConstantError.Enable);
updateWrapper.set("flag", CommonConstant.Enable);
updateWrapper.set("status", CommonConstant.Enable);
} else {
msg = msg + ",但打印机绑定未成功,请检查打印机编号和密钥是否填写正确。";
updateWrapper.set("flag", ConstantError.Disable2);
updateWrapper.set("status", ConstantError.Disable2);
updateWrapper.set("flag", CommonConstant.Disable2);
updateWrapper.set("status", CommonConstant.Disable2);
}
// 解绑之前的打印机
@ -285,7 +285,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
}
if (ConstantError.Enable.equals(status)) {
if (CommonConstant.Enable.equals(status)) {
// 向厂家新增打印机
// 格式"922441475#r6ZXPvHH#核销柜台";
Pair<Boolean, String> retPair = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name()));
@ -293,7 +293,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
return CommonResult.failed(retPair.getSecond());
}
} else {
status = ConstantError.Disable2;
status = CommonConstant.Disable2;
// 向厂家解绑打印机
boolean success = feieUtil.delPrinter(record.getPrinter_sn());
if (!success) {
@ -430,7 +430,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
}
QueryWrapper<ShopStorePrinter> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("store_id", storeId);
queryWrapper.eq("status", ConstantError.Enable);
queryWrapper.eq("status", CommonConstant.Enable);
queryWrapper.orderByDesc("printer_id");
return find(queryWrapper);
}

View File

@ -8,11 +8,12 @@
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.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suisung.mall.common.api.CommonResult;
import com.suisung.mall.common.constant.ConstantError;
import com.suisung.mall.common.constant.CommonConstant;
import com.suisung.mall.common.domain.UserDto;
import com.suisung.mall.common.modules.store.ShopStoreBase;
import com.suisung.mall.common.modules.store.ShopStoreSameCityTransport;
@ -140,7 +141,7 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
transportBase.setDistance_increase_fee(BigDecimal.valueOf(1));
transportBase.setWeight_increase_kg(1);
transportBase.setWeight_increase_fee(BigDecimal.valueOf(1));
transportBase.setStatus(ConstantError.Enable);
transportBase.setStatus(CommonConstant.Enable);
Date now = new Date();
transportBase.setCreated_at(now);
@ -176,7 +177,7 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
QueryWrapper<ShopStoreSameCityTransportBase> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("store_id", storeId);
queryWrapper.eq("status", ConstantError.Enable);
queryWrapper.eq("status", CommonConstant.Enable);
return getOne(queryWrapper);
}
@ -241,7 +242,6 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
* @param storeId 店铺Id
* @param orderLongitude 订单送达地目的地经度
* @param orderLatitude 订单送达地目的地维度
* @param distanceMeter 距离单位米
* @param weightGram 重量单位克
* @param orderProductAmount 订单商品原价金额
* @param orderDiscountAmount 订单商品折扣金额订单原价减去每个商品折扣费
@ -249,42 +249,96 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
* @return 同城配送费
*/
@Override
public BigDecimal computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, Integer distanceMeter, Integer weightGram, BigDecimal orderProductAmount, BigDecimal orderDiscountAmount, BigDecimal orderPayAmount) {
public Pair<Boolean, BigDecimal> computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, Integer weightGram, BigDecimal orderProductAmount, BigDecimal orderDiscountAmount, BigDecimal orderPayAmount) {
// 该订单能否配送
Boolean canTransport = false;
if (storeId == null || storeId <= 0 || orderLongitude == null || orderLatitude == null) {
logger.error("同城配送费计算:缺少必要的参数");
return BigDecimal.ZERO;
return Pair.of(canTransport, BigDecimal.ZERO);
}
// 获取基础运费设置记录
ShopStoreSameCityTransportBase transportBase = getShopStoreSameCityTransportBaseById(storeId);
if (transportBase == null || transportBase.getStore_longitude() == null || transportBase.getStore_latitude() == null || transportBase.getDistance_base() == null || transportBase.getDelivery_base_fee() == null) {
logger.error("同城配送费计算:无法获取基础运费设置记录,或店铺经纬度为空");
return BigDecimal.ZERO;
return Pair.of(canTransport, BigDecimal.ZERO);
}
// 通过高德或百度地图api计算两点的距离如果服务不可用或无法计算使用内部算法计算距离
// 通过高德或百度地图api计算两点的距离如果服务不可用或无法计算使用内部算法计算距离(单位米)
Double distanceD = PositionUtil.getDistance4(transportBase.getStore_longitude().doubleValue(), transportBase.getStore_latitude().doubleValue(), orderLongitude.doubleValue(), orderLatitude.doubleValue());
// 计算基础配送费
Integer distance = distanceD.intValue();
// ### 基础配送费计算
BigDecimal deliveryBaseFee = transportBase.getDelivery_base_fee();
// 每增加一个距离累加运费
if (distance > transportBase.getDistance_base() && transportBase.getDistance_increase_km() != null && transportBase.getDistance_increase_fee() != null) {
deliveryBaseFee.add(transportBase.getDelivery_base_fee().multiply(BigDecimal.valueOf(distance - transportBase.getDistance_base())));
if (transportBase.getDistance_increase_km() != null && transportBase.getDistance_increase_fee() != null && transportBase.getDistance_base() != null && distance > transportBase.getDistance_base() * 1000) {
Integer distanceM = transportBase.getDistance_base() * 1000;// 千米转米
deliveryBaseFee.add(transportBase.getDelivery_base_fee().multiply(BigDecimal.valueOf(distance - distanceM)));
}
// 每增加一个重量累加运费重量暂时忽略,配置的时候设置0
if (weightGram > transportBase.getWeight_base() && transportBase.getWeight_increase_kg() != null && transportBase.getWeight_increase_fee() != null) {
deliveryBaseFee.add(transportBase.getWeight_increase_fee().multiply(BigDecimal.valueOf(weightGram - transportBase.getWeight_base())));
if (transportBase.getWeight_increase_kg() != null && transportBase.getWeight_increase_fee() != null && transportBase.getWeight_base() != null && weightGram > transportBase.getWeight_base() * 1000) {
Integer weightG = transportBase.getWeight_base() * 1000; // kg g
deliveryBaseFee.add(transportBase.getWeight_increase_fee().multiply(BigDecimal.valueOf(weightGram - weightG)));
}
// 基础配送费计算完毕
// #### 基础配送费计算完毕
// 优惠的配送费重要
BigDecimal deliveryDiscountFee = BigDecimal.ZERO;
// 通过配送范围和起送金额决定使用哪个配送费优惠规则
// 获取运费配送范围和优惠信息
List<ShopStoreSameCityTransport> transportList = shopStoreSameCityTransportService.selectShopStoreSameCityTransportList(transportBase.getTransport_base_id());
// 通过配送范围和起送金额决定使用哪个配送费优惠规则
if (CollUtil.isEmpty(transportList)) {
// 没有配送范围和起配金额规则的时候直接以基础配送费来配送
canTransport = true;
return Pair.of(canTransport, deliveryBaseFee);
}
return null;
for (ShopStoreSameCityTransport transport : transportList) {
if (transport.getMax_delivery_radius() < distance) {
// 订单距离不在配送范围内返回不送了
canTransport = false;
continue;
}
// 判断订单达到起配金额没有
if ((CommonConstant.Delivery_Amount_Comput_Type_Original.equals(transport.getMin_delivery_amount_type()) && transport.getMin_delivery_amount().compareTo(orderProductAmount) > 0) ||
(CommonConstant.Delivery_Amount_Comput_Type_Discounted.equals(transport.getMin_delivery_amount_type()) && transport.getMin_delivery_amount().compareTo(orderDiscountAmount) > 0) ||
(CommonConstant.Delivery_Amount_Comput_Type_Payment.equals(transport.getMin_delivery_amount_type()) && transport.getMin_delivery_amount().compareTo(orderPayAmount) > 0)) {
// 订单原价金额小于起送金额返回订单不能送达了
canTransport = false;
continue;
}
if (!canTransport) {
canTransport = true;
}
// 获取优惠的配送费
if ((CommonConstant.Delivery_Amount_Comput_Type_Original.equals(transport.getDelivery_discount_type()) && transport.getMin_delivery_discount_amount().compareTo(orderProductAmount) <= 0) ||
(CommonConstant.Delivery_Amount_Comput_Type_Discounted.equals(transport.getDelivery_discount_type()) && transport.getMin_delivery_discount_amount().compareTo(orderDiscountAmount) <= 0) ||
(CommonConstant.Delivery_Amount_Comput_Type_Payment.equals(transport.getDelivery_discount_type()) && transport.getMin_delivery_discount_amount().compareTo(orderPayAmount) <= 0)) {
// 订单实付金额小于起送金额返回不送了
if (deliveryDiscountFee.compareTo(transport.getDelivery_discount()) < 0) {
// 采用优惠最大的一个豁免设置
deliveryDiscountFee = transport.getDelivery_discount();
}
}
}
// 配送费不能负数就是说优惠运费不能高于基础运费
deliveryBaseFee = deliveryBaseFee.subtract(deliveryDiscountFee);
return Pair.of(canTransport, deliveryBaseFee.compareTo(BigDecimal.ZERO) >= 0 ? deliveryBaseFee : BigDecimal.ZERO);
}
}

View File

@ -10,7 +10,7 @@ package com.suisung.mall.shop.store.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suisung.mall.common.constant.ConstantError;
import com.suisung.mall.common.constant.CommonConstant;
import com.suisung.mall.common.modules.store.ShopStoreSameCityTransport;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
import com.suisung.mall.shop.store.mapper.ShopStoreSameCityTransportMapper;
@ -42,7 +42,7 @@ public class ShopStoreSameCityTransportServiceImpl extends BaseServiceImpl<ShopS
QueryWrapper<ShopStoreSameCityTransport> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("transport_base_id", transportBaseId);
queryWrapper.eq("status", ConstantError.Enable);
queryWrapper.eq("status", CommonConstant.Enable);
queryWrapper.orderByAsc("transport_id");
List<ShopStoreSameCityTransport> shopStoreSameCityTransportList = list(queryWrapper);