package biz.policy.price;
|
|
import java.math.BigDecimal;
|
|
import foundation.util.MapList;
|
|
public class PriceSelector {
|
|
private int priorityNo;
|
private MapList<String, PriceLine> priceList;
|
private BigDecimal basePrice;
|
|
public PriceSelector() {
|
priceList = new MapList<String, PriceLine>();
|
priorityNo = -1;
|
}
|
|
public void loadOne(PriceLine priceLine) {
|
if (priceLine == null) {
|
return;
|
}
|
|
if (PriceLine.Type_Standard.equals(priceLine.getTypeCode())) {
|
basePrice = priceLine.getPrice();
|
}
|
|
int linePriorityNo = priceLine.getPriorityNo();
|
|
//1. 如果行优先级低于当前优先级, 直接退出
|
if (linePriorityNo < priorityNo) {
|
return;
|
}
|
|
//2. 如果行优先级高于当前优先级,需要清空当前列表,添加行
|
if (linePriorityNo > priorityNo) {
|
priorityNo = linePriorityNo;
|
priceList.clear();
|
}
|
|
//3. 如果价格已存在,则不加入
|
String lineId = priceLine.getId();
|
if (priceList.contains(lineId)) {
|
return ;
|
}
|
|
BigDecimal price = priceLine.getPrice();
|
|
if (price != null) {
|
priceList.add(lineId, priceLine);
|
return ;
|
}
|
|
try {
|
PriceLine newLine = priceLine.copy();
|
newLine.setPrice(newLine.getDiscountRate().multiply(basePrice));
|
priceList.add(lineId, newLine);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public MapList<String, PriceLine> getPriceList() {
|
return priceList;
|
}
|
|
public BigDecimal getLowerPrice() {
|
int size = priceList.size();
|
|
if (size == 0) {
|
return null;
|
}
|
|
if (size == 1) {
|
BigDecimal result = priceList.get(0).getPrice();
|
return new BigDecimal(result.toString());
|
}
|
else {
|
BigDecimal result = priceList.get(0).getPrice();
|
BigDecimal current = null;
|
|
for (PriceLine line: priceList) {
|
current = line.getPrice();
|
|
if (result.compareTo(current) > 0) {
|
result = current;
|
}
|
}
|
|
return new BigDecimal(result.toString());
|
}
|
}
|
|
}
|