package biz.policy.price;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
|
public class PriceResult implements IJSONProvider {
|
|
private String skuId;
|
private String selectId;
|
private boolean includeNotAlive;
|
private PriceLine baseLine;
|
private List<PriceLine> selecteds;
|
private List<PriceLine> domainLines;
|
|
public PriceResult(String skuId, String selectId, boolean includeNotAlive) {
|
this.skuId = skuId;
|
this.selectId = selectId;
|
this.includeNotAlive = includeNotAlive;
|
domainLines = new ArrayList<PriceLine>();
|
selecteds = new ArrayList<PriceLine>();
|
}
|
|
public void setBasePrice(PriceLine baseLine) {
|
this.baseLine = baseLine;
|
}
|
|
public void addPriceList(List<PriceLine> prices) throws Exception {
|
domainLines.addAll(prices);
|
}
|
|
public void setSelectedLine(List<OnsiteLine> lines) {
|
if (lines == null) {
|
return ;
|
}
|
|
for (OnsiteLine line : lines) {
|
selecteds.add(line.transToPrice(baseLine.getPrice()));
|
}
|
}
|
|
public String getSelectId() {
|
return selectId;
|
}
|
|
private PriceLine getPriorSelected() {
|
PriceSelector selector = new PriceSelector();
|
|
if (selecteds == null) {
|
return null;
|
}
|
|
//1.
|
for (PriceLine line: selecteds) {
|
if (!line.isAlive()) {
|
continue;
|
}
|
|
selector.loadOne(line);
|
}
|
|
List<PriceLine> result = selector.getPriceList().getItemList();
|
|
if (result.size() >= 1) {
|
return result.get(0);
|
}
|
//3.
|
return null;
|
}
|
|
private List<PriceLine> getActives() {
|
PriceSelector selector = new PriceSelector();
|
|
//1.
|
selector.loadOne(baseLine);
|
|
//2.
|
for (PriceLine line: domainLines) {
|
if (!line.isAlive()) {
|
continue;
|
}
|
|
selector.loadOne(line);
|
}
|
|
//3.
|
return selector.getPriceList().getItemList();
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginObject(skuId);
|
|
//1.
|
List<PriceLine> topActives = getActives();
|
writer.beginArray("actives");
|
|
for (PriceLine line: topActives) {
|
line.writeJSON(writer);
|
}
|
|
writer.endArray();
|
|
//2.
|
writer.beginArray("lines");
|
|
if (baseLine != null) {
|
baseLine.writeJSON(writer);
|
}
|
|
for (PriceLine line: domainLines) {
|
if (!line.isAlive() && !includeNotAlive) {
|
continue;
|
}
|
|
line.writeJSON(writer);
|
}
|
|
writer.endArray();
|
|
//3.
|
|
PriceLine selected = getPriorSelected();
|
if (selected != null) {
|
writer.beginObject(selectId);
|
|
selected.writeJSONBody(writer);
|
|
writer.endObject();
|
}
|
|
writer.endObject();
|
}
|
}
|