package biz.policy.price;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
|
import biz.policy.price.outline.DomainOnsiteResult;
|
import biz.policy.price.outline.DomainPriceResult;
|
import biz.policy.price.outline.OnsiteOutline;
|
import biz.policy.price.outline.OnsiteUpdateList;
|
import biz.policy.price.outline.OnsiteUpdateRecord;
|
import biz.policy.price.outline.PriceUpdateList;
|
import biz.policy.price.outline.PriceUpdateRecord;
|
import biz.policy.price.outline.PricesOutline;
|
import foundation.data.entity.Entity;
|
import foundation.json.JSONReader;
|
import foundation.preload.Bucket;
|
import foundation.util.ContentBuilder;
|
import foundation.util.Util;
|
|
public class PolicyBucket {
|
|
private static PolicyBucket instance;
|
public static Date Now;
|
public static Date PriceTime;
|
public static Date OnsiteTime;
|
private boolean starting;
|
private Bucket<ProductLinePolicy> productLineBucket;
|
private Bucket<ProductPolicy> productBucket;
|
private Bucket<SKUPolicy> skuBucket;
|
private Bucket<PackagePolicy> packageBucket;
|
private PriceUpdateList priceUpdateList;
|
private OnsiteUpdateList onsiteUpdateList;
|
|
static {
|
instance = new PolicyBucket();
|
Now = new Date();
|
PriceTime = new Date();
|
OnsiteTime = new Date();
|
}
|
|
private PolicyBucket() {
|
productLineBucket = new Bucket<ProductLinePolicy>();
|
productBucket = new Bucket<ProductPolicy>();
|
skuBucket = new Bucket<SKUPolicy>();
|
packageBucket = new Bucket<PackagePolicy>();
|
priceUpdateList = new PriceUpdateList();
|
starting = false;
|
}
|
|
public static PolicyBucket getInstance() {
|
Now = new Date();
|
return instance;
|
}
|
|
public void loadOnePolicy(Entity entity) {
|
//1. 产品线/BU
|
String productLineId = entity.getString("bu_id");
|
String key = productLineId;
|
ProductLinePolicy productLine = productLineBucket.get(key);
|
|
if (productLine == null) {
|
productLine = new ProductLinePolicy();
|
productLine.load(entity);
|
|
productLineBucket.loadOne(key, productLine);
|
}
|
|
//2. 产品
|
String productId = entity.getString("product_id");
|
key = createKey(key, productId);
|
ProductPolicy product = productBucket.get(key);
|
|
if (product == null) {
|
product = new ProductPolicy(productLine);
|
product.load(key, entity);
|
|
productBucket.loadOne(key, product);
|
}
|
|
//3. SKU
|
String skuId = entity.getString("id");
|
key = createKey(key, skuId);
|
SKUPolicy sku = new SKUPolicy(product);
|
sku.load(key, entity);
|
|
skuBucket.loadOne(sku.getId(), sku);
|
}
|
|
public PriceLine loadOneStandardPriceLine(Entity entity) {
|
PriceLine priceLine = null;String key;
|
String productLineId = entity.getString("bu_id");
|
String productId = entity.getString("product_id");
|
String skuId = entity.getString("sku_id");
|
|
//1.
|
if (!Util.isEmpty(skuId)) {
|
key = productLineId + "-" + productId + "-" + skuId;
|
SKUPolicy sku = skuBucket.get(key);
|
|
if (sku == null) {
|
return null;
|
}
|
|
priceLine = sku.loadOneStandardPriceLine(entity);
|
}
|
else if (!Util.isEmpty(productId)) {
|
key = productLineId + "-" + productId;
|
ProductPolicy product = productBucket.get(key);
|
|
if (product == null) {
|
return null;
|
}
|
|
priceLine = product.loadOneStandardPriceLine(entity);
|
}
|
else if (!Util.isEmpty(productLineId)) {
|
ProductLinePolicy productLine = productLineBucket.get(productLineId);
|
|
if (productLine == null) {
|
return null;
|
}
|
|
priceLine = productLine.loadOneStandardPriceLine(entity);
|
}
|
|
//2.
|
if (!starting && priceLine != null) {
|
PriceUpdateRecord record = priceUpdateList.loadOne(priceLine);
|
record.setBUId(productLineId);
|
record.setProductId(productId);
|
record.setSkuId(skuId);
|
}
|
|
return priceLine;
|
}
|
|
public PriceLine loadOnePriceListLine(Entity entity) {
|
PriceLine priceLine = null; String key;
|
|
String productLineId = entity.getString("bu_id");
|
String productId = entity.getString("product_id");
|
String skuId = entity.getString("sku_id");
|
String stateCode = entity.getString("md_prod_price_detail__state_code");
|
|
//1. SKU 价格
|
if (!Util.isEmpty(skuId)) {
|
key = productLineId + "-" + productId + "-" + skuId;
|
SKUPolicy sku = skuBucket.get(key);
|
|
if (sku == null) {
|
return null;
|
}
|
|
if (!"open".equalsIgnoreCase(stateCode)) {
|
sku.removeOnePriceListLine(entity);
|
}
|
else {
|
priceLine = sku.loadOnePriceListLine(entity);
|
}
|
|
if (!starting && priceLine != null) {
|
PriceUpdateRecord record = priceUpdateList.loadOne(priceLine);
|
record.setBUId(productLineId);
|
record.setProductId(productId);
|
record.setSkuId(skuId);
|
}
|
}
|
//2. 产品价格
|
else if (!Util.isEmpty(productId)) {
|
key = productLineId + "-" + productId;
|
ProductPolicy product = productBucket.get(key);
|
|
if (product == null) {
|
return null;
|
}
|
|
if (!"open".equalsIgnoreCase(stateCode)) {
|
product.removeOnePriceListLine(entity);
|
}
|
else {
|
priceLine = product.loadOnePriceListLine(entity);
|
}
|
|
if (!starting && priceLine != null) {
|
PriceUpdateRecord record = priceUpdateList.loadOne(priceLine);
|
record.setBUId(productLineId);
|
record.setProductId(productId);
|
}
|
}
|
//3. 产品线价格
|
else if (!Util.isEmpty(productLineId)) {
|
ProductLinePolicy productLine = productLineBucket.get(productLineId);
|
|
if (productLine == null) {
|
return null;
|
}
|
|
if (!"open".equalsIgnoreCase(stateCode)) {
|
productLine.removeOnePriceListLine(entity);
|
}
|
else {
|
priceLine = productLine.loadPrice(entity);
|
}
|
|
if (!starting && priceLine != null) {
|
PriceUpdateRecord record = priceUpdateList.loadOne(priceLine);
|
record.setBUId(productLineId);
|
}
|
}
|
|
return priceLine;
|
}
|
|
public void loadOnePricePackageDiscount(Entity entity) throws Exception {
|
String packageId = entity.getId();
|
String productLineId = entity.getString("agm_record_product__bu_id");
|
String productId = entity.getString("agm_record_product__product_id");
|
String skuId = entity.getString("agm_record_product__sku_id");
|
String stateCode = entity.getString("state_code");
|
Date dateTo = entity.getDate("date_to");
|
String key;
|
|
//失效,移除
|
if (!"open".equalsIgnoreCase(stateCode) || new Date().after(dateTo)) {
|
packageBucket.remove(packageId);
|
return ;
|
}
|
|
//1. 加载套包头
|
PackagePolicy policy = packageBucket.get(packageId);
|
|
if (policy == null) {
|
policy = new PackagePolicy(packageId);
|
policy.load(entity);
|
packageBucket.loadOne(packageId, policy);
|
}
|
|
//2. 加载套包明细
|
policy.loadOnePackageLine(entity);
|
OnsiteLine onsiteLine = null;
|
|
//3. 将政策加入sku/产品
|
if (!Util.isEmpty(skuId)){
|
key = productLineId + "-" + productId + "-" + skuId;
|
SKUPolicy sku = skuBucket.get(key);
|
onsiteLine = sku.loadOnePricePackageDiscount(entity);
|
}
|
else {
|
key = productLineId + "-" + productId;
|
ProductPolicy product = productBucket.get(key);
|
onsiteLine = product.loadOnePricePackageDiscount(entity);
|
}
|
|
if (onsiteUpdateList == null) {
|
onsiteUpdateList = new OnsiteUpdateList();
|
}
|
|
//4.
|
loadOnsiteUpdateList(onsiteLine, productLineId, productId, skuId);
|
}
|
|
public BigDecimal getOnePrice(String customerId, String key) {
|
SKUPolicy sku = skuBucket.get(key);
|
|
if (sku == null) {
|
return BigDecimal.ZERO;
|
}
|
|
return sku.getOnePrice(customerId, key);
|
}
|
|
public BigDecimal getOnePrice(PackagePolicy packagePolicy, String customerId, String key) {
|
SKUPolicy sku = skuBucket.get(key);
|
if (sku == null) {
|
return BigDecimal.ZERO;
|
}
|
|
BigDecimal price = sku.getOnePrice(null, key);
|
|
if (packagePolicy == null) {
|
return sku.getOnePrice(customerId, key);
|
}
|
|
return packagePolicy.getOnePrice(price);
|
}
|
|
public void getPriceList(String customerId, String key, PriceResult result) throws Exception {
|
SKUPolicy sku = skuBucket.get(key);
|
|
if (sku == null) {
|
return;
|
}
|
|
sku.getPriceList(customerId, result);
|
}
|
|
public void getPriceList(String customerId, PricesResult pricesResult) throws Exception {
|
for (JSONReader sku: pricesResult) {
|
String skuId = sku.getString("sku_id");
|
String productId = sku.getString("product_id");
|
String productLineId = sku.getString("bu_id");
|
String key = productLineId + "-" + productId + "-" + skuId;
|
PriceResult priceResult = pricesResult.append(skuId, sku.getString("policy_id"));
|
getPriceList(customerId, key, priceResult);
|
}
|
}
|
|
public void loadOneQtyOnsiteDiscount(Entity entity) {
|
String productLineId = entity.getString("bu_id");
|
String productId = entity.getString("product_id");
|
String skuId = entity.getString("sku_id");
|
|
if (Util.isEmpty(productLineId)) {
|
return;
|
}
|
|
ProductLinePolicy productLine = productLineBucket.get(productLineId);
|
|
if (productLine == null) {
|
return;
|
}
|
|
OnsiteLine onsiteLine = productLine.loadOneQtyOnsiteDiscount(entity);
|
|
if (onsiteUpdateList == null) {
|
onsiteUpdateList = new OnsiteUpdateList();
|
}
|
//2.
|
loadOnsiteUpdateList(onsiteLine, productLineId, productId, skuId);
|
}
|
|
public void loadOnePriceOnsiteDiscount(Entity entity) throws Exception {
|
String productLineId = entity.getString("bu_id");
|
String productId = entity.getString("product_id");
|
String skuId = entity.getString("sku_id");
|
String stateCode = entity.getString("md_prod_price_detail__state_code");
|
Date dateTo = entity.getDate("md_prod_price_detail__date_to");
|
OnsiteLine onsiteLine = null;String key;
|
|
if (!Util.isEmpty(skuId)) {
|
key = productLineId + "-" + productId + "-" + skuId;
|
SKUPolicy sku = skuBucket.get(key);
|
|
if (sku == null) {
|
return;
|
}
|
|
if (!"open".equalsIgnoreCase(stateCode) || new Date().after(dateTo)) {
|
sku.removeOnePriceListLine(entity);
|
return;
|
}
|
|
onsiteLine = sku.loadOnePriceOnsiteDiscount(entity);
|
}
|
else if (!Util.isEmpty(productId)) {
|
key = productLineId + "-" + productId;
|
ProductPolicy product = productBucket.get(key);
|
|
if (product == null) {
|
return;
|
}
|
|
if (!"open".equalsIgnoreCase(stateCode) || new Date().after(dateTo)) {
|
product.removeOnePriceListLine(entity);
|
return;
|
}
|
|
onsiteLine = product.loadOnePriceOnsiteDiscount(entity);
|
}
|
else if (!Util.isEmpty(productLineId)) {
|
ProductLinePolicy productLine = productLineBucket.get(productLineId);
|
|
if (productLine == null) {
|
return;
|
}
|
|
if (!"open".equalsIgnoreCase(stateCode) || new Date().after(dateTo)) {
|
productLine.removeOnePriceListLine(entity);
|
return;
|
}
|
|
onsiteLine = productLine.loadOnePriceOnsiteDiscount(entity);
|
}
|
|
loadOnsiteUpdateList(onsiteLine, productLineId, productId, skuId);
|
}
|
|
private void loadOnsiteUpdateList(OnsiteLine onsiteLine, String productLineId, String productId, String skuId) {
|
if (!starting && onsiteLine != null) {
|
OnsiteUpdateRecord record = onsiteUpdateList.loadOne(onsiteLine);
|
record.setBUId(productLineId);
|
record.setProductId(productId);
|
record.setSkuId(skuId);
|
}
|
}
|
|
public void getOnsiteDiscounts(String customerId, String productLineId, OnsitesResult result) {
|
ProductLinePolicy productLine = productLineBucket.get(productLineId);
|
|
if (productLine == null) {
|
return;
|
}
|
|
productLine.getOnsiteDiscounts(customerId, result);
|
}
|
|
public void getPriceOutline(PricesOutline pricesOutline) {
|
pricesOutline.setCnt_forProductLine(productLineBucket.size());
|
pricesOutline.setCnt_forProduct(productBucket.size());
|
pricesOutline.setCnt_forSKU(skuBucket.size());
|
|
pricesOutline.setUpdateList(priceUpdateList);
|
}
|
|
public void getOnsiteOutline(OnsiteOutline onsiteOutline) {
|
//1.
|
onsiteOutline.setCnt_forProductLine(productLineBucket.size());
|
onsiteOutline.setCnt_forProduct(productBucket.size());
|
onsiteOutline.setCnt_forSKU(skuBucket.size());
|
|
//2.
|
onsiteOutline.setUpdateList(onsiteUpdateList);
|
}
|
|
public void setStarting(boolean value) {
|
starting = value;
|
}
|
|
public ProductLinePolicy getProductLinePolicy(String productLineId) {
|
return productLineBucket.get(productLineId);
|
}
|
|
public ProductPolicy getProductPolicy(String productId) {
|
return productBucket.get(productId);
|
}
|
|
public PackagePolicy getPackagePolicy(String policyId) {
|
return packageBucket.get(policyId);
|
}
|
|
public SKUPolicy getSKUPolicy(String skuId) {
|
return skuBucket.get(skuId);
|
}
|
|
public DomainPriceResult getProductLinePriceList(String buId) {
|
DomainPriceResult result = new DomainPriceResult();
|
ProductLinePolicy policy = productLineBucket.get(buId);
|
|
if (policy == null) {
|
return result;
|
}
|
|
policy.getPriceList(result);
|
return result;
|
}
|
|
public DomainPriceResult getProductPriceList(String key) {
|
DomainPriceResult result = new DomainPriceResult();
|
ProductPolicy policy = productBucket.get(key);
|
|
if (policy == null) {
|
return result;
|
}
|
|
policy.getPriceList(result);
|
return result;
|
}
|
|
public DomainPriceResult getSKUPriceList(String key) {
|
DomainPriceResult result = new DomainPriceResult();
|
SKUPolicy policy = skuBucket.get(key);
|
|
if (policy == null) {
|
return result;
|
}
|
|
policy.getPriceList(result);
|
return result;
|
}
|
|
public DomainOnsiteResult getProductLineOnsiteList(String buId) {
|
DomainOnsiteResult result = new DomainOnsiteResult();
|
ProductLinePolicy policy = productLineBucket.get(buId);
|
|
if (policy == null) {
|
return result;
|
}
|
|
policy.getOnsiteList(result);
|
return result;
|
}
|
|
public DomainOnsiteResult getProductOnsiteList(String key) {
|
DomainOnsiteResult result = new DomainOnsiteResult();
|
ProductPolicy policy = productBucket.get(key);
|
|
if (policy == null) {
|
return result;
|
}
|
|
policy.getOnsiteList(result);
|
return result;
|
}
|
|
public DomainOnsiteResult getSKUOnsiteList(String skuId) {
|
DomainOnsiteResult result = new DomainOnsiteResult();
|
SKUPolicy policy = skuBucket.get(skuId);
|
|
if (policy == null) {
|
return result;
|
}
|
|
policy.getOnsiteList(result);
|
return result;
|
}
|
|
public static String createKey(String... ids) {
|
ContentBuilder buffer = new ContentBuilder("-");
|
|
for (String id : ids) {
|
buffer.append(id);
|
}
|
|
return buffer.toString();
|
}
|
}
|