Feat : shipping fee difference

pull/8040/head
Gauthier LO 2024-07-29 11:56:01 +02:00
parent 7a773aa3dc
commit cbb066032e
6 changed files with 44 additions and 12 deletions

View File

@ -32,9 +32,19 @@ public class CostTrialCalculation {
private final BigDecimal additionalCost;
private final BigDecimal previousUnitPrice;
private CostTrialCalculation(String countryCode, String logisticsChannelName, String logisticChannelCode, BigDecimal unitPrice, BigDecimal shippingCost,
BigDecimal registrationCost, BigDecimal additionalCost, Date effectiveDate) {
private final BigDecimal previousShippingCost;
private final BigDecimal previousRegistrationCost;
private final BigDecimal previousAdditionalCost;
private CostTrialCalculation(String countryCode, String logisticsChannelName, String logisticChannelCode,
BigDecimal unitPrice, BigDecimal shippingCost, BigDecimal registrationCost, BigDecimal additionalCost, Date effectiveDate,
BigDecimal previousUnitPrice,BigDecimal previousShippingCost, BigDecimal previousRegistrationCost, BigDecimal previousAdditionalCost) {
this.countryCode = countryCode;
this.logisticsChannelName = logisticsChannelName;
this.logisticChannelCode = logisticChannelCode;
@ -43,15 +53,28 @@ public class CostTrialCalculation {
this.registrationCost = registrationCost;
this.additionalCost = additionalCost;
this.effectiveDate = effectiveDate;
this.previousUnitPrice = previousUnitPrice;
this.previousShippingCost = previousShippingCost;
this.previousRegistrationCost = previousRegistrationCost;
this.previousAdditionalCost = previousAdditionalCost;
}
public CostTrialCalculation(LogisticChannelPrice price, int weight, String logisticsChannelName, String code) {
public CostTrialCalculation(LogisticChannelPrice price, LogisticChannelPrice previousPrice,int weight, String logisticsChannelName, String code) {
this(price.getEffectiveCountry(), logisticsChannelName, code, price.getCalUnitPrice(), price.calculateShippingPrice(BigDecimal.valueOf(weight)),
price.getRegistrationFee(), price.getAdditionalCost(), price.getEffectiveDate());
price.getRegistrationFee(), price.getAdditionalCost(), price.getEffectiveDate(),
previousPrice.getCalUnitPrice(), previousPrice.calculateShippingPrice(BigDecimal.valueOf(weight)), previousPrice.getRegistrationFee(), previousPrice.getAdditionalCost()
);
}
@JsonProperty("TotalCost")
public double getTotalCost() {
return shippingCost.add(registrationCost).add(additionalCost).setScale(2, RoundingMode.CEILING).doubleValue();
}
@JsonProperty("CostDifference")
public double getCostDifference() {
double previousCost = previousShippingCost.add(previousRegistrationCost).add(previousAdditionalCost).setScale(2, RoundingMode.CEILING).doubleValue();
BigDecimal diff = BigDecimal.valueOf((getTotalCost() - previousCost) / previousCost * 100);
return diff.setScale(2, RoundingMode.CEILING).doubleValue();
}
}

View File

@ -34,7 +34,7 @@ public interface LogisticChannelPriceMapper extends BaseMapper<LogisticChannelPr
* @param countryList the country, represented by 2 letters code
* @return one propre price
*/
LogisticChannelPrice findBy(
List<LogisticChannelPrice> findBy(
@Param("channelName") String channelName,
@Param("date") Date shippingTime,
@Param("trueWeight") BigDecimal weight,

View File

@ -33,7 +33,7 @@
AND effective_date &lt;= #{date}
AND active = 1
ORDER BY effective_date DESC
LIMIT 1 </select>
LIMIT 2 </select>
<select id="findPricesBy" resultType="org.jeecg.modules.business.entity.LogisticChannelPrice">
SELECT lcp.id, lcp.create_by, lcp.create_time, lcp.update_by, lcp.update_time,

View File

@ -47,7 +47,7 @@ public interface ILogisticChannelService extends IService<LogisticChannel> {
* @param country 2 letters code of the destination country
* @return one suitable logistic channel price
*/
LogisticChannelPrice findLogisticsChannelPrice(String channelName, Date date, int trueWeight, List<String> country);
List<LogisticChannelPrice> findLogisticsChannelPrice(String channelName, Date date, int trueWeight, List<String> country);
List<CostTrialCalculation> logisticChannelTrial(int weight, int volume, List<String> countryList);

View File

@ -90,13 +90,16 @@ public class LogisticChannelPriceServiceImpl extends ServiceImpl<LogisticChannel
String countryCode = countryService.findByEnName(order.getCountry()).getCode();
LogisticChannelPrice price = logisticChannelPriceMapper.findBy(
List<LogisticChannelPrice> priceList = logisticChannelPriceMapper.findBy(
logisticChannelName,
order.getShippingTime(),
weight,
Collections.singletonList(countryCode)
);
// find the one with latest effective date
LogisticChannelPrice price = priceList.stream()
.max(Comparator.comparing(LogisticChannelPrice::getEffectiveDate))
.orElse(null);
if (price == null) {
throw new UserException("Can't find price for channel {}, shipped at {}, weight {}, country {}",
logisticChannelName,

View File

@ -78,7 +78,7 @@ public class LogisticChannelServiceImpl extends ServiceImpl<LogisticChannelMappe
}
@Override
public LogisticChannelPrice findLogisticsChannelPrice(String channelName, Date date, int trueWeight, List<String> countryList) {
public List<LogisticChannelPrice> findLogisticsChannelPrice(String channelName, Date date, int trueWeight, List<String> countryList) {
return logisticChannelPriceMapper.findBy(channelName, new java.util.Date(), BigDecimal.valueOf(trueWeight), countryList);
}
@ -98,9 +98,15 @@ public class LogisticChannelServiceImpl extends ServiceImpl<LogisticChannelMappe
} else {
trueWeight = weight;
}
LogisticChannelPrice price = findLogisticsChannelPrice(channelName, new Date(), trueWeight, countryList);
List<LogisticChannelPrice> priceList = findLogisticsChannelPrice(channelName, new Date(), trueWeight, countryList);
LogisticChannelPrice price = priceList.stream()
.max(Comparator.comparing(LogisticChannelPrice::getEffectiveDate))
.orElse(null);
LogisticChannelPrice previousPrice = priceList.stream()
.min(Comparator.comparing(LogisticChannelPrice::getEffectiveDate))
.orElse(null);
if (price != null) {
return new CostTrialCalculation(price, trueWeight, internalName, code);
return new CostTrialCalculation(price, previousPrice, trueWeight, internalName, code);
} else {
return null;
}