Authored by chenchao

ship fee calculate by buyer address

package com.yohoufo.dal.order.model;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
/**
* Created by li.ma on 2018/9/28.
... ... @@ -12,6 +16,9 @@ public class AppraiseAddress {
// 地址信息的四级编码
private Integer areaCode;
@Getter@Setter
private BigDecimal shipFee;
// 0 北京鉴定中心 1 南京鉴定中心
private Integer type;
... ...
... ... @@ -8,9 +8,10 @@
<result column="address_name" jdbcType="VARCHAR" property="addressName" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="ship_fee" javaType="DECIMAL" property="shipFee" />
</resultMap>
<sql id="Base_Column_List">
id, type, area_code, address_name, mobile, address
id, `type`, area_code,ship_fee, address_name, mobile, address
</sql>
<select id="selectAll" resultMap="BaseResultMap">
... ...
package com.yohoufo.order.charge;
import com.yohobuy.ufo.model.order.vo.AddressInfo;
import com.yohoufo.order.charge.model.ChargeGoods;
import com.yohoufo.order.charge.model.ChargeParam;
import com.yohoufo.order.charge.model.ChargeResult;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Builder;
import lombok.Builder;
import org.apache.commons.lang3.tuple.Pair;
/**
* Created by jiexiang.wu on 2018/11/19.
... ... @@ -18,6 +20,8 @@ public class ChargeContext {
//--------------input ------------//
private ChargeParam chargeParam;
private ChargeGoods chargeGoods;
private boolean shipFeeRequird;
private Pair<AddressInfo, AddressInfo> userAddressPair;
//--------------output ------------//
private ChargeResult chargeResult;
... ...
... ... @@ -83,17 +83,26 @@ public class ChargeService {
chargeContext.getChargeResult().getFinalAmount());
}
/**
* 计算运费
*
* @param chargeContext
*/
private void calculateShippingAmount(ChargeContext chargeContext) {
double deliveryWayCost = 0D;
//如果是门店自提,则免运费
double deliveryWayCost = shoppingSupport.isDeliveryWayByStore(chargeContext.getChargeParam().getDeliveryWay()) ?
0 : deliveryWayCostSupport.getCostOfSf();
if (shoppingSupport.isDeliveryWayByStore(chargeContext.getChargeParam().getDeliveryWay())){
deliveryWayCost = 0D;
}else if(chargeContext.getUserAddressPair() != null){
String areaCode = chargeContext.getUserAddressPair().getLeft().getAreaCode();
deliveryWayCost = deliveryWayCostSupport.getCostOfSfByAreaCode(areaCode);
}else{
//运费的最终保证
if (chargeContext.isShipFeeRequird()){
deliveryWayCost = deliveryWayCostSupport.getCostOfSf();
}
}
//运费
chargeContext.getChargeResult().setDeliveryWay(chargeContext.getChargeParam().getDeliveryWay());
chargeContext.getChargeResult().setShippingAmount(deliveryWayCost);
... ...
package com.yohoufo.order.charge.model;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
... ...
package com.yohoufo.order.charge.model;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Builder;
import java.util.Set;
/**
* Created by jiexiang.wu on 2018/11/19.
... ...
... ... @@ -73,9 +73,14 @@ public class ShoppingController {
@RequestMapping(params = "method=ufo.order.compute")
public ApiResponse compute(@RequestParam(name = "uid") int uid,
@RequestParam(name = "skup") int skup,
@RequestParam(name = "coupon_code",required = false) String couponCode) {
ShoppingRequest req = ShoppingRequest.builder().uid(uid).skup(skup).couponCodes(CouponCodeUtils.asList(couponCode)).build();
@RequestParam(name = "coupon_code",required = false) String couponCode,
@RequestParam(name = "addressId", required = false) String addressId) {
ShoppingRequest req = ShoppingRequest.builder().uid(uid)
.skup(skup)
.couponCodes(CouponCodeUtils.asList(couponCode))
.addressId(addressId)
.build();
logger.info("in ufo.order.compute, req {}", req);
ComputeResponse resp = buyerOrderService.compute(req);
return new ApiResponse.ApiResponseBuilder().code(200).data(resp).message("ok").build();
... ...
... ... @@ -47,10 +47,7 @@ public class AppraiseAddressService {
return queryInitAddress();
}
String provinceCode = areaCode.substring(0, 2);
Map<Integer, AppraiseAddress> map = buildCodeAppraiseAddressMap();
AppraiseAddress address = map.get(Integer.valueOf(provinceCode));
AppraiseAddress address = queryAppraiseAddressByAreaCode(areaCode);
if (Objects.nonNull(address)){
return do2Resp(address);
}
... ... @@ -58,6 +55,22 @@ public class AppraiseAddressService {
return queryInitAddress();
}
/**
*
* 结果结构:数据库模型 DO
* @param areaCode
* @return
*/
public AppraiseAddress queryAppraiseAddressByAreaCode(String areaCode) {
if (StringUtils.isEmpty(areaCode) || areaCode.length() < 2) {
return null;
}
String provinceCode = areaCode.substring(0, 2);
Map<Integer, AppraiseAddress> map = buildCodeAppraiseAddressMap();
AppraiseAddress address = map.get(Integer.valueOf(provinceCode));
return address;
}
private static final String DEFAULT_MOBILE = "13073457202";
// 根据默认鉴定中心地址
... ...
... ... @@ -237,6 +237,10 @@ public class ShoppingServiceImpl implements IShoppingService {
checkSkupIsChangePrice(uid, skup);
//构建算费
ChargeContext chargeContext = buildChargeContext(request, skupGood);
if (StringUtils.isNoneBlank(request.getAddressId())){
Pair<AddressInfo, AddressInfo> addressInfoPair = getAndCheckAddressInfo(request);
chargeContext.setUserAddressPair(addressInfoPair);
}
//算费
chargeService.charge(chargeContext);
//算费结果
... ... @@ -275,6 +279,8 @@ public class ShoppingServiceImpl implements IShoppingService {
checkSkupIsChangePrice(uid, skup);
//算费
ChargeContext chargeContext = buildChargeContext(shoppingRequest,psog);
chargeContext.setShipFeeRequird(true);
chargeContext.setUserAddressPair(userAddressPair);
chargeService.charge(chargeContext);
//算费结果
ChargeResult chargeResult = chargeContext.getChargeResult();
... ...
package com.yohoufo.order.service.support;
import com.google.common.base.Throwables;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.dal.order.model.AppraiseAddress;
import com.yohoufo.order.service.impl.AppraiseAddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Objects;
/**
* 运费
*
... ... @@ -20,6 +26,9 @@ public class DeliveryWayCostSupport {
@Autowired
private UfoServiceCaller ufoServiceCaller;
@Autowired
private AppraiseAddressService appraiseAddressService;
public double getCostOfSf() {
String key = "buyer.delivery.way.cost.sf";
double defaultValue = 25;
... ... @@ -41,4 +50,26 @@ public class DeliveryWayCostSupport {
}
}
public double getCostOfSfByAreaCode(String areaCode) {
double fee = 25D;
try {
AppraiseAddress appraiseAddress = appraiseAddressService.queryAppraiseAddressByAreaCode(areaCode);
BigDecimal shipFeeFromDB;
if (appraiseAddress != null && Objects.nonNull(shipFeeFromDB = appraiseAddress.getShipFee())){
fee = shipFeeFromDB.doubleValue();
}else{
log.warn("in getCostOfSfByAreaCode fail, appraiseAddress {} areaCode {}", appraiseAddress, areaCode);
fee = getCostOfSf();
}
} catch (Exception e) {
fee = getCostOfSf();
log.warn("request cost of sf fail exception {}", Throwables.getStackTraceAsString(e));
}finally {
return fee;
}
}
}
... ...