同城配送费计算方法
This commit is contained in:
parent
1d6c4bdf5b
commit
9a25ce2c88
@ -13,6 +13,8 @@ import com.suisung.mall.common.modules.store.ShopStoreSameCityTransportBase;
|
|||||||
import com.suisung.mall.common.pojo.dto.ShopStoreSameCityTransportBaseDTO;
|
import com.suisung.mall.common.pojo.dto.ShopStoreSameCityTransportBaseDTO;
|
||||||
import org.springframework.data.util.Pair;
|
import org.springframework.data.util.Pair;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
public interface ShopStoreSameCityTransportBaseService {
|
public interface ShopStoreSameCityTransportBaseService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,8 +38,15 @@ public interface ShopStoreSameCityTransportBaseService {
|
|||||||
* @param storeId
|
* @param storeId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ShopStoreSameCityTransportBaseDTO getDetailById(Long storeId);
|
ShopStoreSameCityTransportBaseDTO getShopStoreSameCityTransportBaseDTOById(Long storeId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据店铺Id获取同城配送基础运费记录
|
||||||
|
*
|
||||||
|
* @param storeId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
ShopStoreSameCityTransportBase getShopStoreSameCityTransportBaseById(Long storeId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存同城配送基础设置(存在更新,不存在新增)
|
* 保存同城配送基础设置(存在更新,不存在新增)
|
||||||
@ -46,4 +55,21 @@ public interface ShopStoreSameCityTransportBaseService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Pair<Long, String> saveOrUpdateShopStoreSameCityTransportBase(ShopStoreSameCityTransportBase shopStoreSameCityTransportBase);
|
Pair<Long, String> saveOrUpdateShopStoreSameCityTransportBase(ShopStoreSameCityTransportBase shopStoreSameCityTransportBase);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算同城订单配送费
|
||||||
|
*
|
||||||
|
* @param storeId 店铺Id
|
||||||
|
* @param orderLongitude (订单送达地)目的地经度
|
||||||
|
* @param orderLatitude (订单送达地)目的地维度
|
||||||
|
* @param distanceMeter 距离(单位米)
|
||||||
|
* @param weightGram 重量(单位克)
|
||||||
|
* @param orderProductAmount 订单商品原价金额
|
||||||
|
* @param orderDiscountAmount 订单商品折扣金额(订单原价减去每个商品折扣费)
|
||||||
|
* @param orderPayAmount 订单实际支付金额(折扣金额-优惠券-积分扣-人工干预扣费),不包含运费
|
||||||
|
* @return 同城配送费
|
||||||
|
*/
|
||||||
|
BigDecimal computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, Integer distanceMeter, Integer weightGram, BigDecimal orderProductAmount, BigDecimal orderDiscountAmount, BigDecimal orderPayAmount);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -18,13 +18,15 @@ import com.suisung.mall.common.modules.store.ShopStoreBase;
|
|||||||
import com.suisung.mall.common.modules.store.ShopStoreSameCityTransport;
|
import com.suisung.mall.common.modules.store.ShopStoreSameCityTransport;
|
||||||
import com.suisung.mall.common.modules.store.ShopStoreSameCityTransportBase;
|
import com.suisung.mall.common.modules.store.ShopStoreSameCityTransportBase;
|
||||||
import com.suisung.mall.common.pojo.dto.ShopStoreSameCityTransportBaseDTO;
|
import com.suisung.mall.common.pojo.dto.ShopStoreSameCityTransportBaseDTO;
|
||||||
|
import com.suisung.mall.common.utils.PositionUtil;
|
||||||
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
|
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
|
||||||
import com.suisung.mall.shop.chain.controller.admin.ShopChainUserController;
|
import com.suisung.mall.shop.chain.controller.admin.ShopChainUserController;
|
||||||
import com.suisung.mall.shop.store.mapper.ShopStoreSameCityTransportBaseMapper;
|
import com.suisung.mall.shop.store.mapper.ShopStoreSameCityTransportBaseMapper;
|
||||||
import com.suisung.mall.shop.store.service.ShopStoreBaseService;
|
import com.suisung.mall.shop.store.service.ShopStoreBaseService;
|
||||||
import com.suisung.mall.shop.store.service.ShopStoreSameCityTransportBaseService;
|
import com.suisung.mall.shop.store.service.ShopStoreSameCityTransportBaseService;
|
||||||
import com.suisung.mall.shop.store.service.ShopStoreSameCityTransportService;
|
import com.suisung.mall.shop.store.service.ShopStoreSameCityTransportService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.data.util.Pair;
|
import org.springframework.data.util.Pair;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -38,12 +40,12 @@ import static com.suisung.mall.common.utils.ContextUtil.getCurrentUser;
|
|||||||
@Service
|
@Service
|
||||||
public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<ShopStoreSameCityTransportBaseMapper, ShopStoreSameCityTransportBase> implements ShopStoreSameCityTransportBaseService {
|
public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<ShopStoreSameCityTransportBaseMapper, ShopStoreSameCityTransportBase> implements ShopStoreSameCityTransportBaseService {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ShopStoreSameCityTransportBaseServiceImpl.class);
|
||||||
@Resource
|
@Resource
|
||||||
private ShopStoreSameCityTransportService shopStoreSameCityTransportService;
|
private ShopStoreSameCityTransportService shopStoreSameCityTransportService;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ShopStoreBaseService shopStoreBaseService;
|
private ShopStoreBaseService shopStoreBaseService;
|
||||||
@Autowired
|
@Resource
|
||||||
private ShopChainUserController user;
|
private ShopChainUserController user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,13 +56,13 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
@Override
|
@Override
|
||||||
public CommonResult ShopStoreSameCityTransportBaseDetail() {
|
public CommonResult ShopStoreSameCityTransportBaseDetail() {
|
||||||
// 判断有没有权限
|
// 判断有没有权限
|
||||||
// UserDto user = getCurrentUser();
|
UserDto user = getCurrentUser();
|
||||||
// if (user == null || !user.isStore()) {
|
if (user == null || !user.isStore()) {
|
||||||
// return CommonResult.failed("无权限操作!");
|
return CommonResult.failed("无权限操作!");
|
||||||
// }
|
}
|
||||||
|
|
||||||
Long storeId = 2L; //Convert.toLong(user.getStore_id());
|
Long storeId = Convert.toLong(user.getStore_id());
|
||||||
ShopStoreSameCityTransportBaseDTO retDTO = getDetailById(storeId);
|
ShopStoreSameCityTransportBaseDTO retDTO = getShopStoreSameCityTransportBaseDTOById(storeId);
|
||||||
if (retDTO == null) {
|
if (retDTO == null) {
|
||||||
return CommonResult.failed("商家未设置店铺地址,请先设置店铺地址!");
|
return CommonResult.failed("商家未设置店铺地址,请先设置店铺地址!");
|
||||||
}
|
}
|
||||||
@ -77,12 +79,12 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
@Override
|
@Override
|
||||||
public CommonResult saveOrUpdateSameCityTransport(ShopStoreSameCityTransportBaseDTO shopStoreSameCityTransportBaseDTO) {
|
public CommonResult saveOrUpdateSameCityTransport(ShopStoreSameCityTransportBaseDTO shopStoreSameCityTransportBaseDTO) {
|
||||||
// 判断有没有权限
|
// 判断有没有权限
|
||||||
// UserDto user = getCurrentUser();
|
UserDto user = getCurrentUser();
|
||||||
// if (user == null || !user.isStore()) {
|
if (user == null || !user.isStore()) {
|
||||||
// return CommonResult.failed("无权限操作!");
|
return CommonResult.failed("无权限操作!");
|
||||||
// }
|
}
|
||||||
|
|
||||||
Integer userId = 3;//user.getId();
|
Integer userId = user.getId();
|
||||||
|
|
||||||
if (shopStoreSameCityTransportBaseDTO == null || shopStoreSameCityTransportBaseDTO.getTransportBase() == null) {
|
if (shopStoreSameCityTransportBaseDTO == null || shopStoreSameCityTransportBaseDTO.getTransportBase() == null) {
|
||||||
return CommonResult.failed("参数有误");
|
return CommonResult.failed("参数有误");
|
||||||
@ -91,14 +93,14 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
ShopStoreSameCityTransportBase transportBase = shopStoreSameCityTransportBaseDTO.getTransportBase();
|
ShopStoreSameCityTransportBase transportBase = shopStoreSameCityTransportBaseDTO.getTransportBase();
|
||||||
transportBase.setUpdated_by(userId);
|
transportBase.setUpdated_by(userId);
|
||||||
// 新增或更新同城配送基础设置
|
// 新增或更新同城配送基础设置
|
||||||
Pair<Long,String> pair = saveOrUpdateShopStoreSameCityTransportBase(transportBase);
|
Pair<Long, String> pair = saveOrUpdateShopStoreSameCityTransportBase(transportBase);
|
||||||
|
|
||||||
// 新增或更新同城配送扩展设置列表
|
// 新增或更新同城配送扩展设置列表
|
||||||
shopStoreSameCityTransportBaseDTO.setTransportBase(transportBase);
|
shopStoreSameCityTransportBaseDTO.setTransportBase(transportBase);
|
||||||
shopStoreSameCityTransportBaseDTO.rebuildTransportList();
|
shopStoreSameCityTransportBaseDTO.rebuildTransportList();
|
||||||
shopStoreSameCityTransportService.saveOrUpdateShopStoreSameCityTransportList(shopStoreSameCityTransportBaseDTO.getTransportList());
|
shopStoreSameCityTransportService.saveOrUpdateShopStoreSameCityTransportList(shopStoreSameCityTransportBaseDTO.getTransportList());
|
||||||
|
|
||||||
if (pair.getFirst()>0) {
|
if (pair.getFirst() > 0) {
|
||||||
return CommonResult.success(null, pair.getSecond());
|
return CommonResult.success(null, pair.getSecond());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +114,7 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ShopStoreSameCityTransportBaseDTO getDetailById(Long storeId) {
|
public ShopStoreSameCityTransportBaseDTO getShopStoreSameCityTransportBaseDTOById(Long storeId) {
|
||||||
if (storeId == null || storeId <= 0) {
|
if (storeId == null || storeId <= 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -123,10 +125,7 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryWrapper<ShopStoreSameCityTransportBase> queryWrapper = new QueryWrapper<>();
|
ShopStoreSameCityTransportBase transportBase = getShopStoreSameCityTransportBaseById(storeId);
|
||||||
queryWrapper.eq("store_id", storeId);
|
|
||||||
queryWrapper.orderByAsc("transport_base_id");
|
|
||||||
ShopStoreSameCityTransportBase transportBase = getOne(queryWrapper);
|
|
||||||
if (transportBase == null) {
|
if (transportBase == null) {
|
||||||
transportBase = new ShopStoreSameCityTransportBase();
|
transportBase = new ShopStoreSameCityTransportBase();
|
||||||
transportBase.setStore_id(storeId);
|
transportBase.setStore_id(storeId);
|
||||||
@ -163,7 +162,26 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
return shopStoreSameCityTransportBaseDTO;
|
return shopStoreSameCityTransportBaseDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<Long,String> saveOrUpdateShopStoreSameCityTransportBase(ShopStoreSameCityTransportBase transportBase) {
|
/**
|
||||||
|
* 根据店铺Id获取同城配送基础运费记录
|
||||||
|
*
|
||||||
|
* @param storeId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ShopStoreSameCityTransportBase getShopStoreSameCityTransportBaseById(Long storeId) {
|
||||||
|
if (storeId == null || storeId <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
QueryWrapper<ShopStoreSameCityTransportBase> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("store_id", storeId);
|
||||||
|
queryWrapper.eq("status", ConstantError.Enable);
|
||||||
|
|
||||||
|
return getOne(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pair<Long, String> saveOrUpdateShopStoreSameCityTransportBase(ShopStoreSameCityTransportBase transportBase) {
|
||||||
if (transportBase == null || transportBase.getStore_id() == null || transportBase.getStore_id() <= 0) {
|
if (transportBase == null || transportBase.getStore_id() == null || transportBase.getStore_id() <= 0) {
|
||||||
return Pair.of(0L, "缺少店铺Id必要参数!");
|
return Pair.of(0L, "缺少店铺Id必要参数!");
|
||||||
}
|
}
|
||||||
@ -209,4 +227,64 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
|
|||||||
return Pair.of(0L, "更新失败!");
|
return Pair.of(0L, "更新失败!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 计算订单同城配送费,
|
||||||
|
// 1.先获取订单的经纬度,计算配送距离,获取配送范围,确定是否能配送,如果能计算基础运费?
|
||||||
|
// 2.如果能配送,再订单总重量,订单原价金额,订单折后金额,订单实付金额
|
||||||
|
// 3.根据两点经纬度,计算配送距离,结合订单总重量,计算基础运费。
|
||||||
|
// 4.查看是否有运费优惠设置,如果有,就直接从基础运费中扣除优惠运费,得出最终的订单配送费。
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算同城订单配送费
|
||||||
|
*
|
||||||
|
* @param storeId 店铺Id
|
||||||
|
* @param orderLongitude (订单送达地)目的地经度
|
||||||
|
* @param orderLatitude (订单送达地)目的地维度
|
||||||
|
* @param distanceMeter 距离(单位米)
|
||||||
|
* @param weightGram 重量(单位克)
|
||||||
|
* @param orderProductAmount 订单商品原价金额
|
||||||
|
* @param orderDiscountAmount 订单商品折扣金额(订单原价减去每个商品折扣费)
|
||||||
|
* @param orderPayAmount 订单实际支付金额(折扣金额-优惠券-积分扣-人工干预扣费),不包含运费
|
||||||
|
* @return 同城配送费
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BigDecimal computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, Integer distanceMeter, Integer weightGram, BigDecimal orderProductAmount, BigDecimal orderDiscountAmount, BigDecimal orderPayAmount) {
|
||||||
|
if (storeId == null || storeId <= 0 || orderLongitude == null || orderLatitude == null) {
|
||||||
|
logger.error("同城配送费计算:缺少必要的参数");
|
||||||
|
return 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过高德或百度地图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())));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 每增加一个重量累加运费(重量暂时忽略,配置的时候设置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())));
|
||||||
|
}
|
||||||
|
// 基础配送费计算完毕
|
||||||
|
|
||||||
|
|
||||||
|
// 获取运费配送范围和优惠信息
|
||||||
|
List<ShopStoreSameCityTransport> transportList = shopStoreSameCityTransportService.selectShopStoreSameCityTransportList(transportBase.getTransport_base_id());
|
||||||
|
|
||||||
|
// 通过配送范围和起送金额,决定使用哪个配送费优惠规则
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,7 +68,7 @@ public class ShopStoreSameCityTransportServiceImpl extends BaseServiceImpl<ShopS
|
|||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (ShopStoreSameCityTransport transport : transportList) {
|
for (ShopStoreSameCityTransport transport : transportList) {
|
||||||
if(transport.getTransport_base_id()==null||transport.getTransport_base_id()<=0){
|
if (transport.getTransport_base_id() == null || transport.getTransport_base_id() <= 0) {
|
||||||
logger.error("缺少 transport_base_id 必要参数!");
|
logger.error("缺少 transport_base_id 必要参数!");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user