feat: better logistic channel price comparison

pull/8040/head
Gauthier LO 2024-08-01 11:57:12 +02:00
parent 450e9f0846
commit 8b144a43d4
6 changed files with 85 additions and 12 deletions

View File

@ -24,6 +24,9 @@ public class CostTrialCalculation {
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private final Date effectiveDate; private final Date effectiveDate;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private final Date previousEffectiveDate;
private final BigDecimal unitPrice; private final BigDecimal unitPrice;
private final BigDecimal shippingCost; private final BigDecimal shippingCost;
@ -44,7 +47,7 @@ public class CostTrialCalculation {
private CostTrialCalculation(String countryCode, String logisticsChannelName, String logisticChannelCode, private CostTrialCalculation(String countryCode, String logisticsChannelName, String logisticChannelCode,
BigDecimal unitPrice, BigDecimal shippingCost, BigDecimal registrationCost, BigDecimal additionalCost, Date effectiveDate, BigDecimal unitPrice, BigDecimal shippingCost, BigDecimal registrationCost, BigDecimal additionalCost, Date effectiveDate,
BigDecimal previousUnitPrice,BigDecimal previousShippingCost, BigDecimal previousRegistrationCost, BigDecimal previousAdditionalCost) { BigDecimal previousUnitPrice,BigDecimal previousShippingCost, BigDecimal previousRegistrationCost, BigDecimal previousAdditionalCost, Date previousEffectiveDate) {
this.countryCode = countryCode; this.countryCode = countryCode;
this.logisticsChannelName = logisticsChannelName; this.logisticsChannelName = logisticsChannelName;
this.logisticChannelCode = logisticChannelCode; this.logisticChannelCode = logisticChannelCode;
@ -57,12 +60,13 @@ public class CostTrialCalculation {
this.previousShippingCost = previousShippingCost; this.previousShippingCost = previousShippingCost;
this.previousRegistrationCost = previousRegistrationCost; this.previousRegistrationCost = previousRegistrationCost;
this.previousAdditionalCost = previousAdditionalCost; this.previousAdditionalCost = previousAdditionalCost;
this.previousEffectiveDate = previousEffectiveDate;
} }
public CostTrialCalculation(LogisticChannelPrice price, LogisticChannelPrice previousPrice,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)), 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() previousPrice.getCalUnitPrice(), previousPrice.calculateShippingPrice(BigDecimal.valueOf(weight)), previousPrice.getRegistrationFee(), previousPrice.getAdditionalCost(), previousPrice.getEffectiveDate()
); );
} }

View File

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

View File

@ -33,8 +33,65 @@
AND effective_date &lt;= #{date} AND effective_date &lt;= #{date}
AND active = 1 AND active = 1
ORDER BY effective_date DESC ORDER BY effective_date DESC
LIMIT 2 </select> LIMIT 1
</select>
<select id="findPrevious" resultType="org.jeecg.modules.business.entity.LogisticChannelPrice">
WITH current_price AS
(
SELECT lcp.*
FROM logistic_channel_price lcp
JOIN logistic_channel lc ON lc.id = lcp.channel_id
WHERE zh_name = #{channelName}
AND weight_range_start &lt;= #{trueWeight}
AND weight_range_end &gt;= #{trueWeight}
AND effective_country IN
<foreach collection="countryList"
separator=","
open="("
close=")"
index="index"
item="country"
>
#{country}
</foreach>
AND effective_date &lt;= #{date}
AND active = 1
ORDER BY effective_date DESC
LIMIT 1
)
SELECT lcp.*
FROM logistic_channel_price lcp
JOIN logistic_channel lc ON lc.id = lcp.channel_id
WHERE lc.zh_name = #{channelName}
AND weight_range_start &lt;= #{trueWeight}
AND weight_range_end &gt;= #{trueWeight}
AND effective_country IN
<foreach collection="countryList"
separator=","
open="("
close=")"
index="index"
item="country"
>#{country}
</foreach>
AND effective_date &lt;= #{date}
AND active = 1
AND (
lcp.cal_unit_price != (
SELECT cal_unit_price
FROM current_price
WHERE effective_country = lcp.effective_country
)
OR
lcp.registration_fee != (
SELECT registration_fee
FROM current_price
WHERE effective_country = lcp.effective_country
)
)
ORDER BY effective_date DESC
LIMIT 1;
</select>
<select id="findPricesBy" resultType="org.jeecg.modules.business.entity.LogisticChannelPrice"> <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, SELECT lcp.id, lcp.create_by, lcp.create_time, lcp.update_by, lcp.update_time,
lc.id AS channel_id, effective_date, effective_country, lc.id AS channel_id, effective_date, effective_country,

View File

@ -7,7 +7,7 @@
FROM client c FROM client c
JOIN shop s ON c.id = s.owner_id JOIN shop s ON c.id = s.owner_id
JOIN platform_order po on s.id = po.shop_id JOIN platform_order po on s.id = po.shop_id
WHERE po.shipping_invoice_number = #{invoiceNumber} WHERE po.shipping_invoice_number = #{invoiceNumber} OR po.purchase_invoice_number = #{invoiceNumber};
</select> </select>
<select id="fetchShippingInvoiceId" resultType="java.lang.String"> <select id="fetchShippingInvoiceId" resultType="java.lang.String">
SELECT id SELECT id

View File

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

View File

@ -79,7 +79,16 @@ public class LogisticChannelServiceImpl extends ServiceImpl<LogisticChannelMappe
@Override @Override
public List<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); List<LogisticChannelPrice> priceList = new ArrayList<>();
LogisticChannelPrice currentPrice = logisticChannelPriceMapper.findBy(channelName, new java.util.Date(), BigDecimal.valueOf(trueWeight), countryList);
if(currentPrice != null) {
priceList.add(currentPrice);
LogisticChannelPrice previousPrice = logisticChannelPriceMapper.findPrevious(channelName, new java.util.Date(), BigDecimal.valueOf(trueWeight), countryList);
if(previousPrice != null) {
priceList.add(previousPrice);
}
}
return priceList;
} }
@Override @Override
@ -99,6 +108,8 @@ public class LogisticChannelServiceImpl extends ServiceImpl<LogisticChannelMappe
trueWeight = weight; trueWeight = weight;
} }
List<LogisticChannelPrice> priceList = findLogisticsChannelPrice(channelName, new Date(), trueWeight, countryList); List<LogisticChannelPrice> priceList = findLogisticsChannelPrice(channelName, new Date(), trueWeight, countryList);
System.out.println("Price list for " + channelName + " is : ");
System.out.println(priceList);
LogisticChannelPrice price = priceList.stream() LogisticChannelPrice price = priceList.stream()
.max(Comparator.comparing(LogisticChannelPrice::getEffectiveDate)) .max(Comparator.comparing(LogisticChannelPrice::getEffectiveDate))
.orElse(null); .orElse(null);