运费计算优化

This commit is contained in:
Jack 2024-12-04 21:46:15 +08:00
parent eb6bec7e04
commit aa606ce880
8 changed files with 386 additions and 273 deletions

View File

@ -131,6 +131,34 @@ public class CommonUtil {
.distinct().collect(Collectors.toList()); .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 四舍五入,不保留小数点 * Double 四舍五入,不保留小数点
* @param d * @param d
@ -150,6 +178,12 @@ public class CommonUtil {
} }
/**
* 根据上一个订单状态和当前状态获取订单状态变化备注
* @param order_state_pre_id
* @param order_state_id
* @return
*/
public static String getOrderStateNote(int order_state_pre_id, int order_state_id) { public static String getOrderStateNote(int order_state_pre_id, int order_state_id) {
String rtn = ""; String rtn = "";

View File

@ -17,6 +17,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suisung.mall.common.api.*; 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.ConfigConstant;
import com.suisung.mall.common.constant.MqConstant; import com.suisung.mall.common.constant.MqConstant;
import com.suisung.mall.common.domain.UserDto; import com.suisung.mall.common.domain.UserDto;
@ -1224,7 +1225,6 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
*/ */
@Override @Override
public CommonResult addOrder() { public CommonResult addOrder() {
UserDto user = getCurrentUser(); UserDto user = getCurrentUser();
if (user == null) { if (user == null) {
throw new ApiUserException(I18nUtil._("用户信息异常!")); throw new ApiUserException(I18nUtil._("用户信息异常!"));
@ -1240,19 +1240,18 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
throw new ApiException(I18nUtil._("请选择你的收货地址!")); throw new ApiException(I18nUtil._("请选择你的收货地址!"));
} }
Integer chain_id = getParameter("chain_id", 0); Integer chain_id = getParameter("chain_id", 0);
Boolean if_chain = Convert.toBool(getParameter("if_chain"), false); Boolean if_chain = Convert.toBool(getParameter("if_chain"), false);
Integer checked_store = getParameter("checked_store", 0); Integer checked_store = getParameter("checked_store", 0);
// 从购物车走
Integer ifcart = getParameter("ifcart", 0); Integer ifcart = getParameter("ifcart", 0);
List<Map> item_rows = new ArrayList(); List<Map> item_rows = new ArrayList();
if (ifcart > 0) { if (ifcart > 0) {
// 权限判断 // 从购物车走
QueryWrapper<ShopUserCart> cartQueryWrapper = new QueryWrapper<>(); QueryWrapper<ShopUserCart> cartQueryWrapper = new QueryWrapper<>();
cartQueryWrapper.eq("chain_id", chain_id).eq("user_id", user_id).eq("cart_select", 1).gt("cart_quantity", 0); cartQueryWrapper.eq("chain_id", chain_id).eq("user_id", user_id).eq("cart_select", 1).gt("cart_quantity", 0);
if (checked_store > 0) { if (checked_store > 0) {
// 权限判断
cartQueryWrapper.eq("store_id", checked_store); cartQueryWrapper.eq("store_id", checked_store);
} }
@ -1260,6 +1259,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
item_rows = (List<Map>) cart_data.get("items"); item_rows = (List<Map>) cart_data.get("items");
} }
// 重要方法
return doQuickAddOrder(user_id, item_rows, ud_id, true, "", StateCode.ORDER_TYPE_DD, null); return doQuickAddOrder(user_id, item_rows, ud_id, true, "", StateCode.ORDER_TYPE_DD, null);
} }
@ -1304,7 +1304,8 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
List<Integer> user_voucher_ids = checkCoupon(); List<Integer> user_voucher_ids = checkCoupon();
Map checkout_row = new HashMap(); Map checkout_row = new HashMap();
Integer delivery_type_id = getParameter("delivery_type_id", StateCode.DELIVERY_TYPE_EXP); // 配送方式5-门店自提10-普通快递16-同城配送
Integer delivery_type_id = getParameter("delivery_type_id", StateCode.DELIVERY_TYPE_SAME_CITY);
if (getParameter("checkout_row") == null) { if (getParameter("checkout_row") == null) {
Date cur_date = new Date(); Date cur_date = new Date();
checkout_row.put("payment_type_id", StateCode.PAYMENT_TYPE_ONLINE); checkout_row.put("payment_type_id", StateCode.PAYMENT_TYPE_ONLINE);
@ -1318,8 +1319,9 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
checkout_row = Convert.toMap(String.class, Object.class, JSONUtil.parseObj(getParameter("checkout_row"))); checkout_row = Convert.toMap(String.class, Object.class, JSONUtil.parseObj(getParameter("checkout_row")));
} }
//配送方式 //// 配送方式5-门店自提10-普通快递16-同城配送
if (delivery_type_id.equals(StateCode.DELIVERY_TYPE_SELF_PICK_UP)) { if (delivery_type_id.equals(StateCode.DELIVERY_TYPE_SELF_PICK_UP)) {
// 5-门店自提
cal_freight = false; cal_freight = false;
} }
@ -1327,13 +1329,13 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
// 用户发票检测 // 用户发票检测
checkUserInvoice(user_id, checkout_row, user_invoice_id); checkUserInvoice(user_id, checkout_row, user_invoice_id);
// 从购物车走
List cart_rows = new ArrayList(); List cart_rows = new ArrayList();
Integer ifcart = getParameter("ifcart", 0); Integer ifcart = getParameter("ifcart", 0);
if (ifcart > 0) { if (ifcart > 0) {
// 从购物车走
QueryWrapper<ShopUserCart> cartQueryWrapper = new QueryWrapper<>(); QueryWrapper<ShopUserCart> cartQueryWrapper = new QueryWrapper<>();
cartQueryWrapper.eq("chain_id", chain_id).eq("user_id", user_id).eq("cart_select", 1).gt("cart_quantity", 0); cartQueryWrapper.eq("chain_id", chain_id).eq("user_id", user_id).eq("cart_select", 1).gt("cart_quantity", 0);
if (checked_store > 0) { if (checked_store > 0) {
cartQueryWrapper.eq("store_id", checked_store); cartQueryWrapper.eq("store_id", checked_store);
} }
@ -1478,7 +1480,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
//门店自提方式则判断是否需要分店铺下单 //门店自提方式则判断是否需要分店铺下单
if (StateCode.DELIVERY_TYPE_SELF_PICK_UP == getParameter("delivery_type_id", StateCode.DELIVERY_TYPE_EXP)) { if (StateCode.DELIVERY_TYPE_SELF_PICK_UP == getParameter("delivery_type_id", StateCode.DELIVERY_TYPE_EXP)) {
if (1 == accountBaseConfigService.getConfig("product_ziti_flag", 0)) { if (CommonConstant.Enable == accountBaseConfigService.getConfig("product_ziti_flag", CommonConstant.Disable)) {
List<Integer> store_o2o_flag_row = ((List<Map>) cart_data.get("items")).stream().map(s -> Convert.toInt(s.get("store_o2o_flag"))).distinct().collect(Collectors.toList()); List<Integer> store_o2o_flag_row = ((List<Map>) cart_data.get("items")).stream().map(s -> Convert.toInt(s.get("store_o2o_flag"))).distinct().collect(Collectors.toList());
if (store_o2o_flag_row.size() > 1) { if (store_o2o_flag_row.size() > 1) {
@ -1541,7 +1543,6 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
messageService.sendNoticeMsg(user_id, 0, message_id, args); messageService.sendNoticeMsg(user_id, 0, message_id, args);
} }
return CommonResult.success(cart_data, I18nUtil._("下单成功!")); return CommonResult.success(cart_data, I18nUtil._("下单成功!"));
} }
@ -1558,6 +1559,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
Map address_row = (Map) cart_data.get("address_row"); Map address_row = (Map) cart_data.get("address_row");
if (address_row != null) { if (address_row != null) {
Integer district_id = Convert.toInt(address_row.get("ud_city_id")); Integer district_id = Convert.toInt(address_row.get("ud_city_id"));
// 重要配送费检测和计算
shopUserCartService.calTransportFreight(cart_data, district_id); shopUserCartService.calTransportFreight(cart_data, district_id);
boolean can_delivery = (boolean) cart_data.get("can_delivery"); boolean can_delivery = (boolean) cart_data.get("can_delivery");
@ -5771,7 +5773,6 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
order_sel_money_amount = NumberUtil.add(order_sel_money_amount, order_giftbag_amount); order_sel_money_amount = NumberUtil.add(order_sel_money_amount, order_giftbag_amount);
} }
Boolean plugin_crossBorder = accountBaseConfigService.getConfig("Plugin_CrossBorder", false); Boolean plugin_crossBorder = accountBaseConfigService.getConfig("Plugin_CrossBorder", false);
Integer buyer_user_id = Convert.toInt(cart_data.get("buyer_user_id")); Integer buyer_user_id = Convert.toInt(cart_data.get("buyer_user_id"));
AccountUserInfo user_info_row = accountService.getUserInfo(buyer_user_id); AccountUserInfo user_info_row = accountService.getUserInfo(buyer_user_id);
@ -5907,7 +5908,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
is_delivery = true; is_delivery = true;
} }
// 否需要配送地址 // 虚拟商品的情况
if (is_virtual) { if (is_virtual) {
if (items.size() == 0) { if (items.size() == 0) {
is_delivery = false; is_delivery = false;

View File

@ -249,24 +249,24 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
* @param weightGram 重量单位克 * @param weightGram 重量单位克
* @param orderProductAmount 订单商品原价金额 * @param orderProductAmount 订单商品原价金额
* @param orderDiscountAmount 订单商品折扣金额订单原价减去每个商品折扣费 * @param orderDiscountAmount 订单商品折扣金额订单原价减去每个商品折扣费
* @param orderPayAmount 订单实际支付金额折扣金额-优惠券-积分扣-人工干预扣费不包含运费 * @param orderPayAmount 订单实际支付金额折扣金额-优惠券-积分扣-人工干预扣费不包含运费
* @return 同城配送费 * @return 同城配送费 (是否能配送配送费)
*/ */
@Override @Override
public Pair<Boolean, BigDecimal> computeSameCityTransportFee(Long storeId, BigDecimal orderLongitude, BigDecimal orderLatitude, 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; Boolean canDelivery = false;
if (storeId == null || storeId <= 0 || orderLongitude == null || orderLatitude == null) { if (CommonUtil.hasAnyBlank(storeId, orderLongitude, orderLatitude) || storeId <= 0) {
logger.error("同城配送费计算:缺少必要的参数"); logger.error("同城配送费计算:缺少必要的参数");
return Pair.of(canTransport, BigDecimal.ZERO); return Pair.of(canDelivery, BigDecimal.ZERO);
} }
// 获取基础运费设置记录 // 获取基础运费设置记录
ShopStoreSameCityTransportBase transportBase = getShopStoreSameCityTransportBaseById(storeId); ShopStoreSameCityTransportBase transportBase = getShopStoreSameCityTransportBaseById(storeId);
if (transportBase == null || transportBase.getStore_longitude() == null || transportBase.getStore_latitude() == null || transportBase.getDistance_base() == null || transportBase.getDelivery_base_fee() == null) { if (CommonUtil.hasAnyBlank(transportBase, transportBase.getStore_longitude(), transportBase.getStore_latitude(), transportBase.getDistance_base(), transportBase.getDelivery_base_fee())) {
logger.error("同城配送费计算:无法获取基础运费设置记录,或店铺经纬度为空"); logger.error("同城配送费计算:无法获取基础运费设置记录,或店铺经纬度为空");
return Pair.of(canTransport, BigDecimal.ZERO); return Pair.of(canDelivery, BigDecimal.ZERO);
} }
// 通过高德或百度地图api计算两点的距离如果服务不可用或无法计算使用内部算法计算距离(单位米) // 通过高德或百度地图api计算两点的距离如果服务不可用或无法计算使用内部算法计算距离(单位米)
@ -277,7 +277,8 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
// ### 基础配送费计算 // ### 基础配送费计算
BigDecimal deliveryBaseFee = transportBase.getDelivery_base_fee(); BigDecimal deliveryBaseFee = transportBase.getDelivery_base_fee();
// 每增加一个距离累加运费 // 每增加一个距离累加运费
if (transportBase.getDistance_increase_km() != null && transportBase.getDistance_increase_fee() != null && transportBase.getDistance_base() != null && distance > transportBase.getDistance_base() * 1000) { if (CommonUtil.isAllNotBlank(transportBase.getDistance_increase_km(), transportBase.getDistance_increase_fee(),transportBase.getDistance_base()) && 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 // 实际配送距离超出基础距离单位km
BigDecimal diffDistanceM = CommonUtil.DecimalRoundHalfUp(BigDecimal.valueOf(distance - transportBase.getDistance_base() * 1000).divide(BigDecimal.valueOf(1000))); BigDecimal diffDistanceM = CommonUtil.DecimalRoundHalfUp(BigDecimal.valueOf(distance - transportBase.getDistance_base() * 1000).divide(BigDecimal.valueOf(1000)));
// 累加的次数 // 累加的次数
@ -295,7 +296,8 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
} }
// 每增加一个重量累加运费重量暂时忽略,配置的时候设置0 // 每增加一个重量累加运费重量暂时忽略,配置的时候设置0
if (transportBase.getWeight_increase_kg() != null && transportBase.getWeight_increase_fee() != null && transportBase.getWeight_base() != null && weightGram > transportBase.getWeight_base() * 1000) { if (CommonUtil.isAllNotBlank(transportBase.getWeight_increase_kg(), transportBase.getWeight_increase_fee(),transportBase.getWeight_base()) && 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 // 实际配送重量超出基础重量单位kg
BigDecimal diffWeightKg = CommonUtil.DecimalRoundHalfUp(BigDecimal.valueOf(weightGram - transportBase.getWeight_base() * 1000).divide(BigDecimal.valueOf(1000))); BigDecimal diffWeightKg = CommonUtil.DecimalRoundHalfUp(BigDecimal.valueOf(weightGram - transportBase.getWeight_base() * 1000).divide(BigDecimal.valueOf(1000)));
// 累加的次数 // 累加的次数
@ -322,14 +324,14 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
if (CollUtil.isEmpty(transportList)) { if (CollUtil.isEmpty(transportList)) {
// 没有配送范围和起配金额规则的时候直接以基础配送费来配送 // 没有配送范围和起配金额规则的时候直接以基础配送费来配送
canTransport = true; canDelivery = true;
return Pair.of(canTransport, deliveryBaseFee); return Pair.of(canDelivery, deliveryBaseFee);
} }
for (ShopStoreSameCityTransport transport : transportList) { for (ShopStoreSameCityTransport transport : transportList) {
if (transport.getMax_delivery_radius() < distance) { if (transport.getMax_delivery_radius() < distance) {
// 订单距离不在配送范围内返回不送了 // 订单距离不在配送范围内返回不送了
canTransport = false; canDelivery = false;
continue; continue;
} }
@ -339,13 +341,12 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
(CommonConstant.Delivery_Amount_Comput_Type_Payment.equals(transport.getMin_delivery_amount_type()) && transport.getMin_delivery_amount().compareTo(orderPayAmount) > 0)) { (CommonConstant.Delivery_Amount_Comput_Type_Payment.equals(transport.getMin_delivery_amount_type()) && transport.getMin_delivery_amount().compareTo(orderPayAmount) > 0)) {
// 订单原价金额小于起送金额返回订单不能送达了 // 订单原价金额小于起送金额返回订单不能送达了
canTransport = false; canDelivery = false;
continue; continue;
} }
if (!canTransport) { if (!canDelivery) {
canTransport = true; canDelivery = true;
} }
// 获取优惠的配送费 // 获取优惠的配送费
@ -364,7 +365,7 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
// 配送费不能负数就是说优惠运费不能高于基础运费 // 配送费不能负数就是说优惠运费不能高于基础运费
deliveryBaseFee = deliveryBaseFee.subtract(deliveryDiscountFee); deliveryBaseFee = deliveryBaseFee.subtract(deliveryDiscountFee);
return Pair.of(canTransport, deliveryBaseFee.compareTo(BigDecimal.ZERO) >= 0 ? deliveryBaseFee : BigDecimal.ZERO); return Pair.of(canDelivery, deliveryBaseFee.compareTo(BigDecimal.ZERO) >= 0 ? deliveryBaseFee : BigDecimal.ZERO);
} }

View File

@ -28,26 +28,36 @@ public class UserDeliveryAddressController extends BaseControllerImpl {
private ShopUserDeliveryAddressService userDeliveryAddressService; private ShopUserDeliveryAddressService userDeliveryAddressService;
@ApiOperation(value = "用户地址", notes = "用户地址") @ApiOperation(value = "用户地址", notes = "用户地址")
@RequestMapping(value = "/lists", method = RequestMethod.GET) @RequestMapping(value = "/lists", method = {RequestMethod.GET, RequestMethod.POST})
public CommonResult lists(@RequestParam(name = "page", defaultValue = "1") Integer page, public CommonResult lists(@RequestParam(name = "page", defaultValue = "1") Integer page,
@RequestParam(name = "rows", defaultValue = "100") Integer rows) { @RequestParam(name = "rows", defaultValue = "100") Integer rows) {
UserDto user = getCurrentUser();
if(user==null){
throw new ApiUserException(I18nUtil._("无权限操作!"));
}
return CommonResult.success(userDeliveryAddressService.getLists(page, rows)); return CommonResult.success(userDeliveryAddressService.getLists(page, rows));
} }
@ApiOperation(value = "读取用户地址", notes = "读取用户地址") @ApiOperation(value = "读取用户地址", notes = "读取用户地址")
@RequestMapping(value = "/get", method = RequestMethod.GET) @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
public CommonResult get(@RequestParam(name = "ud_id") @ApiParam("用户地址编号") Integer ud_id) { public CommonResult get(@RequestParam(name = "ud_id") @ApiParam("用户地址编号") Integer ud_id) {
UserDto user = getCurrentUser();
if(user==null){
throw new ApiUserException(I18nUtil._("无权限操作!"));
}
return CommonResult.success(userDeliveryAddressService.getAddress()); return CommonResult.success(userDeliveryAddressService.getAddress());
} }
@ApiOperation(value = "保存用户地址", notes = "保存用户地址") @ApiOperation(value = "保存用户地址", notes = "保存用户地址")
@RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "/save", method = {RequestMethod.POST})
public CommonResult save(ShopUserDeliveryAddress deliveryAddress) { public CommonResult save(ShopUserDeliveryAddress deliveryAddress) {
return userDeliveryAddressService.saveAddress(deliveryAddress); return userDeliveryAddressService.saveAddress(deliveryAddress);
} }
@ApiOperation(value = "删除用户地址", notes = "删除用户地址") @ApiOperation(value = "删除用户地址", notes = "删除用户地址")
@RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "/remove", method = {RequestMethod.POST})
public CommonResult remove(@RequestParam(name = "ud_id") Integer ud_id) { public CommonResult remove(@RequestParam(name = "ud_id") Integer ud_id) {
UserDto user = getCurrentUser(); UserDto user = getCurrentUser();

View File

@ -138,7 +138,7 @@ public interface ShopUserCartService extends IBaseService<ShopUserCart> {
* 2配送区域问题 * 2配送区域问题
* *
* @param cart_data 最总数据 * @param cart_data 最总数据
* @param district_id 配送地区 * @param district_id 配送地区城市id city_id
*/ */
void calTransportFreight(Map cart_data, Integer district_id); void calTransportFreight(Map cart_data, Integer district_id);

View File

@ -14,7 +14,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suisung.mall.common.api.CommonResult; import com.suisung.mall.common.api.CommonResult;
import com.suisung.mall.common.api.ResultCode; import com.suisung.mall.common.api.ResultCode;
import com.suisung.mall.common.api.StateCode; import com.suisung.mall.common.api.StateCode;
import com.suisung.mall.common.constant.CommonConstant;
import com.suisung.mall.common.domain.UserDto; import com.suisung.mall.common.domain.UserDto;
import com.suisung.mall.common.exception.ApiException; import com.suisung.mall.common.exception.ApiException;
import com.suisung.mall.common.exception.ApiUserException; import com.suisung.mall.common.exception.ApiUserException;
@ -53,9 +52,11 @@ import com.suisung.mall.shop.user.service.ShopUserCartService;
import com.suisung.mall.shop.user.service.ShopUserDeliveryAddressService; import com.suisung.mall.shop.user.service.ShopUserDeliveryAddressService;
import com.suisung.mall.shop.user.service.ShopUserVoucherService; import com.suisung.mall.shop.user.service.ShopUserVoucherService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
@ -90,7 +91,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
@Autowired @Autowired
private ShopProductBaseService shopProductBaseService; private ShopProductBaseService shopProductBaseService;
@Autowired @Autowired
private ShopStoreTransportTypeService transportTypeService; private ShopStoreTransportTypeService shopStoreTransportTypeService;
@Autowired @Autowired
private ShopStoreActivityBaseService shopStoreActivityBaseService; private ShopStoreActivityBaseService shopStoreActivityBaseService;
@Autowired @Autowired
@ -140,6 +141,9 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
@Autowired @Autowired
private ShopProductImageService shopProductImageService; private ShopProductImageService shopProductImageService;
@Resource
private ShopStoreSameCityTransportBaseService shopStoreSameCityTransportBaseService;
@Override @Override
@Transactional @Transactional
public Map getLists(Integer store_id, Integer chain_id, Integer page, Integer rows) { public Map getLists(Integer store_id, Integer chain_id, Integer page, Integer rows) {
@ -534,10 +538,10 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
String cart_file = getParameter("cart_file", ""); String cart_file = getParameter("cart_file", "");
Integer ifcart = getParameter("ifcart", Integer.class); Integer ifcart = getParameter("ifcart", Integer.class);
Integer ud_id = getParameter("ud_id", Integer.class); Integer ud_id = getParameter("ud_id", Integer.class);
Integer is_delivery = getParameter("is_delivery", 1);
Integer if_virtual = getParameter("if_virtual", 1); Integer if_virtual = getParameter("if_virtual", 1);
String code = getParameter("code"); String code = getParameter("code");
// 是否需要配送
Integer is_delivery = getParameter("is_delivery", 1);
// 配送方式5-上门自提 10-普通快递 16-同城配送 // 配送方式5-上门自提 10-普通快递 16-同城配送
Integer delivery_type_id = getParameter("delivery_type_id", Integer.class); Integer delivery_type_id = getParameter("delivery_type_id", Integer.class);
if (delivery_type_id == null || delivery_type_id <= 0) { if (delivery_type_id == null || delivery_type_id <= 0) {
@ -677,7 +681,6 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
Boolean is_virtual = Convert.toBool(_item.get("is_virtual"), false); Boolean is_virtual = Convert.toBool(_item.get("is_virtual"), false);
if (_items.size() == 1 && is_virtual && product_id != null) { if (_items.size() == 1 && is_virtual && product_id != null) {
data.put("if_virtual", true); data.put("if_virtual", true);
// 修正虚拟数据 // 修正虚拟数据
@ -1138,7 +1141,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
Set transport_type_ids = tt_ids.keySet(); Set transport_type_ids = tt_ids.keySet();
for (Object transport_type_id : transport_type_ids) { for (Object transport_type_id : transport_type_ids) {
ShopStoreTransportType transport_type_row = transportTypeService.get(Convert.toInt(transport_type_id)); ShopStoreTransportType transport_type_row = shopStoreTransportTypeService.get(Convert.toInt(transport_type_id));
Boolean transport_type_free = Convert.toBool(transport_type_row.getTransport_type_free()); Boolean transport_type_free = Convert.toBool(transport_type_row.getTransport_type_free());
if (transport_type_free != null && transport_type_free) { if (transport_type_free != null && transport_type_free) {
@ -1520,7 +1523,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
} }
List<ShopStoreTransportType> transportTypes = transportTypeService.gets(transport_type_ids); List<ShopStoreTransportType> transportTypes = shopStoreTransportTypeService.gets(transport_type_ids);
for (Map store_row : store_rows) { for (Map store_row : store_rows) {
Map activitys = (Map) ObjectUtil.defaultIfNull(store_row.get("activitys"), new HashMap()); Map activitys = (Map) ObjectUtil.defaultIfNull(store_row.get("activitys"), new HashMap());
@ -2163,7 +2166,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
//店铺全局活动 //店铺全局活动
BigDecimal activityDiscountMoney = Convert.toBigDecimal(store_row.get("activityDiscountMoney")); BigDecimal activityDiscountMoney = Convert.toBigDecimal(store_row.get("activityDiscountMoney"));
//等级折扣价格 - 此价格和活动优惠取最大值 //会员等级折扣价格 - 此价格和活动优惠取最大值
BigDecimal policy_discount_amount = items.stream().map(s -> Convert.toBigDecimal(s.get("policy_discount_amount"), BigDecimal.ZERO)).reduce(BigDecimal::add).get(); BigDecimal policy_discount_amount = items.stream().map(s -> Convert.toBigDecimal(s.get("policy_discount_amount"), BigDecimal.ZERO)).reduce(BigDecimal::add).get();
store_row.put("policy_discount_amount", policy_discount_amount); store_row.put("policy_discount_amount", policy_discount_amount);
@ -2173,7 +2176,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
//店铺订单活动总额度 = 店铺全局优惠 + 单品优惠 //店铺订单活动总额度 = 店铺全局优惠 + 单品优惠
BigDecimal order_activity_discount_amount = NumberUtil.add(activityDiscountMoney, pulseActivityDiscountMoney); BigDecimal order_activity_discount_amount = NumberUtil.add(activityDiscountMoney, pulseActivityDiscountMoney);
//店铺订单总优惠额度 = 店铺全局优惠 + 单品优惠 + 价格策略折扣 //店铺订单总优惠额度 = 店铺全局优惠 + 单品优惠 + 会员等级价格策略折扣
BigDecimal order_discount_amount = NumberUtil.add(order_activity_discount_amount, policy_discount_amount); BigDecimal order_discount_amount = NumberUtil.add(order_activity_discount_amount, policy_discount_amount);
store_row.put("productMoney", 0); store_row.put("productMoney", 0);
@ -2182,27 +2185,25 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
store_row.put("productMoneyOriginGoods", store_origin_total); store_row.put("productMoneyOriginGoods", store_origin_total);
store_row.put("productPointsSel", store_points_total); store_row.put("productPointsSel", store_points_total);
store_row.put("productSpSel", store_sp_total); store_row.put("productSpSel", store_sp_total);
store_row.put("order_money_select_items", NumberUtil.sub(store_total, activityDiscountMoney)); store_row.put("order_money_select_items", NumberUtil.sub(store_total, activityDiscountMoney));
//BigDecimal item_discount_amount_sum = items.stream().map(s -> Convert.toBigDecimal(s.get("item_discount_amount"), BigDecimal.ZERO)).reduce(BigDecimal::add).get(); //BigDecimal item_discount_amount_sum = items.stream().map(s -> Convert.toBigDecimal(s.get("item_discount_amount"), BigDecimal.ZERO)).reduce(BigDecimal::add).get();
//item_discount_amount 数据实时放入 activityDiscountMoney, pulseActivityDiscountMoney 不重叠放入二选一 //item_discount_amount 数据实时放入 activityDiscountMoney, pulseActivityDiscountMoney 不重叠放入二选一
//店铺订单活动总额度 = 店铺全局优惠 + 单品优惠
store_row.put("order_activity_discount_amount", order_activity_discount_amount); store_row.put("order_activity_discount_amount", order_activity_discount_amount);
// 店铺订单总优惠额度 = 店铺全局优惠 + 单品优惠 + 价格策略折扣 // 店铺订单总优惠额度 = 店铺全局优惠 + 单品优惠 + 会员等级价格策略折扣
store_row.put("order_discount_amount", order_discount_amount); store_row.put("order_discount_amount", order_discount_amount);
store_row.put("postFree", true); // 是否免运费 store_row.put("postFree", true); // 是否免运费
store_row.put("freight", 0); // 运费 store_row.put("freight", 0); // 运费金额
store_row.put("postFreeBalance", 0); // 还需postFreeBalance元即可免邮费 store_row.put("postFreeBalance", 0); // 还需postFreeBalance元即可免邮费
store_row.put("activity_discount_amount", order_activity_discount_amount); store_row.put("activity_discount_amount", order_activity_discount_amount);
// 关键订单金额数据 // 关键订单金额数据
orderSelProductAmount = orderSelProductAmount.add(store_origin_total); orderSelProductAmount = orderSelProductAmount.add(store_origin_total);
orderSelFreightAmount = orderSelFreightAmount.add(Convert.toBigDecimal(store_row.get("freight"))); orderSelFreightAmount = orderSelFreightAmount.add(Convert.toBigDecimal(store_row.get("freight"))); // 重要订单运费金额
orderSelMoneyAmount = orderSelMoneyAmount.add(Convert.toBigDecimal(store_row.get("order_money_select_items"))); orderSelMoneyAmount = orderSelMoneyAmount.add(Convert.toBigDecimal(store_row.get("order_money_select_items")));// 重要订单折后金额订单金额-折扣金额
orderDiscountAmount = orderDiscountAmount.add(Convert.toBigDecimal(store_row.get("order_discount_amount"))); orderDiscountAmount = orderDiscountAmount.add(Convert.toBigDecimal(store_row.get("order_discount_amount"))); // 重要订单折扣金额
orderSelPointsAmount = orderSelPointsAmount.add(store_points_total); orderSelPointsAmount = orderSelPointsAmount.add(store_points_total);
orderSelSpAmount = orderSelSpAmount.add(store_sp_total); orderSelSpAmount = orderSelSpAmount.add(store_sp_total);
orderRebateAmount = orderRebateAmount.add(Convert.toBigDecimal(store_row.get("store_rebate"))); orderRebateAmount = orderRebateAmount.add(Convert.toBigDecimal(store_row.get("store_rebate")));
@ -2469,6 +2470,64 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
*/ */
@Override @Override
public void calTransportFreight(Map cart_data, Integer district_id) { public void calTransportFreight(Map cart_data, Integer district_id) {
if(cart_data==null || district_id==null){
throw new ApiException(I18nUtil._("参数有误!"));
}
// 配送方式 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) {
// 配送不到这个区域商品id
cart_data.put("transport_type_none_ids", new ArrayList());
// 配送不到这个区域商品记录
cart_data.put("delivery_item_none_row", new ArrayList());
//16-同城配送
BigDecimal orderSelProductAmount = Convert.toBigDecimal(cart_data.get("orderSelProductAmount"), BigDecimal.ZERO); //商品订单原价
BigDecimal orderSelFreightAmount = Convert.toBigDecimal(cart_data.get("orderSelFreightAmount"), BigDecimal.ZERO);//选中商品运费金额(仅运费)
BigDecimal orderSelMoneyAmount = Convert.toBigDecimal(cart_data.get("orderSelMoneyAmount"), BigDecimal.ZERO);//选中商品最终应付金额
// BigDecimal orderDiscountAmount = Convert.toBigDecimal(cart_data.get("orderDiscountAmount"), BigDecimal.ZERO);//商品订单折扣优惠金额
// BigDecimal orderSelPointsAmount = Convert.toBigDecimal(cart_data.get("orderSelPointsAmount"), BigDecimal.ZERO);//商品订单积分抵扣金额
// BigDecimal orderSelSpAmount = Convert.toBigDecimal(cart_data.get("orderSelSpAmount"), BigDecimal.ZERO);//可能是商品订单促销金额
// BigDecimal orderRebateAmount = Convert.toBigDecimal(cart_data.get("order_rebate_amount"), BigDecimal.ZERO);//可能是商品订单返利金额
// items一个或多个店铺的订单
List<Map> items = (List<Map>) cart_data.get("items");
for (Map store_row : items) {
// 一个店铺的订单
Long storeId = Convert.toLong(store_row.get("store_id"));
BigDecimal storeLng = Convert.toBigDecimal(store_row.get("store_longitude"));
BigDecimal storeLat = Convert.toBigDecimal(store_row.get("store_latitude"));
// 订单商品原价金额
BigDecimal productMoneyOriginGoods = Convert.toBigDecimal(store_row.get("productMoneyOriginGoods"));
// 订单折扣后金额
BigDecimal orderSelDiscountAmount = Convert.toBigDecimal(store_row.get("productMoneySelGoods"));
// 订单最终应付金额
BigDecimal orderPaymentAmount = Convert.toBigDecimal(store_row.get("productMoneySelGoods"));
// 同城配送运费检查和计算
Pair<Boolean, BigDecimal> pair = shopStoreSameCityTransportBaseService.computeSameCityTransportFee(storeId,storeLng,storeLat,0,productMoneyOriginGoods,orderSelDiscountAmount,orderPaymentAmount);
Boolean postFree = pair.getFirst();
store_row.put("postFree", postFree); // 是否免运费
store_row.put("freight", pair.getSecond()); // 运费金额
store_row.put("postFreeBalance", 0); // 还需postFreeBalance元即可免邮费
// 关键订单金额数据
orderSelFreightAmount = orderSelFreightAmount.add(Convert.toBigDecimal(pair.getSecond())); // 重要订单运费金额
if(postFree) {
orderSelMoneyAmount = orderSelMoneyAmount.add(Convert.toBigDecimal(store_row.get("order_money_select_items")));// 重要订单折后金额订单金额-折扣金额
}
}
cart_data.put("orderSelFreightAmount", orderSelFreightAmount);
cart_data.put("orderSelMoneyAmount", orderSelMoneyAmount);
} else {
// 普通快递运费计算和门店自取分支
// 配送不到这个区域商品id // 配送不到这个区域商品id
List transport_type_none_ids = new ArrayList(); List transport_type_none_ids = new ArrayList();
@ -2487,7 +2546,6 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
// 活动可能影响重新计算价格 // 活动可能影响重新计算价格
List<Map> items = (List<Map>) cart_data.get("items"); List<Map> items = (List<Map>) cart_data.get("items");
for (Map store_row : items) { for (Map store_row : items) {
// 不配送区域
BigDecimal freight = BigDecimal.ZERO; BigDecimal freight = BigDecimal.ZERO;
if (isgiftbag) { if (isgiftbag) {
@ -2510,6 +2568,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
} }
// todo 此处免运费传递的是transport_type设置中的值 则默认代表为订单了 未来可以优化 // todo 此处免运费传递的是transport_type设置中的值 则默认代表为订单了 未来可以优化
// 可免最大运费金额
BigDecimal post_free_max = BigDecimal.ZERO; BigDecimal post_free_max = BigDecimal.ZERO;
// 按照店铺订单计算运费 // 按照店铺订单计算运费
@ -2535,7 +2594,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
} else { } else {
// 判断运费方式如果发现同一个订单计费模式不一致报错禁止下单 // 判断运费方式如果发现同一个订单计费模式不一致报错禁止下单
Map tt_ids = (Map) store_row.get("tt_ids"); Map tt_ids = (Map) store_row.get("tt_ids");
Boolean isOnSale = (Boolean) store_row.get("is_on_sale");//活动中的商品 Boolean isOnSale = (Boolean) store_row.get("is_on_sale");//活动中的商品
List<ShopStoreTransportType> transport_type_rows = new ArrayList<>(); List<ShopStoreTransportType> transport_type_rows = new ArrayList<>();
Integer transport_type_id; Integer transport_type_id;
@ -2550,7 +2609,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
} else { } else {
if (CollUtil.isNotEmpty(tt_ids)) { if (CollUtil.isNotEmpty(tt_ids)) {
Set tt_id_row = tt_ids.keySet(); Set tt_id_row = tt_ids.keySet();
transport_type_rows = transportTypeService.gets(tt_id_row); transport_type_rows = shopStoreTransportTypeService.gets(tt_id_row);
List<Integer> transport_type_pricing_method = transport_type_rows.stream().map(s -> s.getTransport_type_pricing_method()).distinct().collect(Collectors.toList()); List<Integer> transport_type_pricing_method = transport_type_rows.stream().map(s -> s.getTransport_type_pricing_method()).distinct().collect(Collectors.toList());
if (transport_type_pricing_method.size() > 1) { if (transport_type_pricing_method.size() > 1) {
@ -2572,14 +2631,19 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
//计费规则(ENUM):1-按件数;2-按重量;3-按体积;4-按配送区域计费猜测不确定 //计费规则(ENUM):1-按件数;2-按重量;3-按体积;4-按配送区域计费猜测不确定
if (transport_type_pricing_method != null && 4 == transport_type_pricing_method) { if (transport_type_pricing_method != null && 4 == transport_type_pricing_method) {
// 按照配送区域计费 // 按照配送区域计费
Map data = transportTypeService.calFreight(transport_type_id, district_id, 1, order_money_select_items, post_free_max); Map data = shopStoreTransportTypeService.calFreight(transport_type_id, district_id, 1, order_money_select_items, post_free_max);
// 可免最大运费金额
post_free_max = Convert.toBigDecimal(data.get("post_free_max")); post_free_max = Convert.toBigDecimal(data.get("post_free_max"));
freight = Convert.toBigDecimal(data.get("freight")); freight = Convert.toBigDecimal(data.get("freight"));
} else { } else {
if (isgiftbag) { if (isgiftbag) {
// 以下是礼包的情况 // 以下是礼包的情况
Map data = transportTypeService.calFreight(transport_type_id, district_id, Convert.toInt(activity_rule.get("giftbag_quantity")), Convert.toBigDecimal(store_row.get("order_money_select_items")), post_free_max);
// 运费计算 如果一个订单都多个货物则计算方式为以最大基础运费为基础 + 每个商品递增部分
Map data = shopStoreTransportTypeService.calFreight(transport_type_id, district_id, Convert.toInt(activity_rule.get("giftbag_quantity")), Convert.toBigDecimal(store_row.get("order_money_select_items")), post_free_max);
// 是否可配送
boolean type_freight = (boolean) data.get("type_freight"); boolean type_freight = (boolean) data.get("type_freight");
if (!type_freight) { if (!type_freight) {
@ -2595,7 +2659,7 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
for (Object tt_id : _tt_ids) { for (Object tt_id : _tt_ids) {
Integer quantity = Convert.toInt(tt_ids.get(tt_id)); Integer quantity = Convert.toInt(tt_ids.get(tt_id));
Integer tt_transport_type_id = Convert.toInt(tt_id); Integer tt_transport_type_id = Convert.toInt(tt_id);
Map data = transportTypeService.calFreight(tt_transport_type_id, district_id, quantity, order_money_select_items, post_free_max); Map data = shopStoreTransportTypeService.calFreight(tt_transport_type_id, district_id, quantity, order_money_select_items, post_free_max);
boolean type_freight = (boolean) data.get("type_freight"); boolean type_freight = (boolean) data.get("type_freight");
BigDecimal _freight = Convert.toBigDecimal(data.get("freight")); BigDecimal _freight = Convert.toBigDecimal(data.get("freight"));
@ -2668,27 +2732,30 @@ public class ShopUserCartServiceImpl extends BaseServiceImpl<ShopUserCartMapper,
if (CheckUtil.isEmpty(freight)) { if (CheckUtil.isEmpty(freight)) {
// 免运费 // 免运费
store_row.put("postFree", true); // 是否免运费 store_row.put("postFree", true); // 免运费
} else { } else {
// 不免运费 // 不免运费
store_row.put("postFree", false); // 是否免运费 store_row.put("postFree", false); // 不是全免运费
BigDecimal order_money_select_items = Convert.toBigDecimal(store_row.get("order_money_select_items")); BigDecimal order_money_select_items = Convert.toBigDecimal(store_row.get("order_money_select_items"));
// 选中商品金额 - 免除运费金额 // 选中商品金额 - 免除运费金额
store_row.put("postFreeBalance", NumberUtil.sub(order_money_select_items, post_free_max).abs()); store_row.put("postFreeBalance", NumberUtil.sub(order_money_select_items, post_free_max).abs()); // 计算绝对值运费大于商品价格的时候有问题吧
} }
store_row.put("freight", freight); // 运费 store_row.put("freight", freight); // 运费
BigDecimal orderSelFreightAmount = Convert.toBigDecimal(cart_data.get("orderSelFreightAmount")); BigDecimal orderSelFreightAmount = Convert.toBigDecimal(cart_data.get("orderSelFreightAmount"));
// 订单运费总额仅运费
cart_data.put("orderSelFreightAmount", NumberUtil.add(orderSelFreightAmount, freight)); cart_data.put("orderSelFreightAmount", NumberUtil.add(orderSelFreightAmount, freight));
// 应支付金额
BigDecimal orderSelMoneyAmount = Convert.toBigDecimal(cart_data.get("orderSelMoneyAmount")); BigDecimal orderSelMoneyAmount = Convert.toBigDecimal(cart_data.get("orderSelMoneyAmount"));
cart_data.put("orderSelMoneyAmount", NumberUtil.add(orderSelMoneyAmount, freight)); cart_data.put("orderSelMoneyAmount", NumberUtil.add(orderSelMoneyAmount, freight));
} }
cart_data.put("can_delivery", transport_type_none_ids.size() == 0); cart_data.put("can_delivery", transport_type_none_ids.size() == 0);
} }
}
/** /**
* item info 基础数据 活动的通过活动修正 * item info 基础数据 活动的通过活动修正

View File

@ -6,7 +6,7 @@ spring:
mvc: mvc:
static-path-pattern: /shop/static/** static-path-pattern: /shop/static/**
datasource: datasource:
url: jdbc:mysql://@mysql.host@:@mysql.port@/@mysql.db@?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&&zeroDateTimeBehavior=convertToNull url: jdbc:mysql://@mysql.host@:@mysql.port@/@mysql.db@?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=5
username: @mysql.user@ username: @mysql.user@
password: @mysql.pwd@ password: @mysql.pwd@
driver-class-name: @mysql.driver@ driver-class-name: @mysql.driver@