|
|
package com.yoho.search.consumer.service.logic.productIndex;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import com.yoho.search.base.utils.MathUtils;
|
|
|
import com.yoho.search.consumer.service.base.ProductPriceService;
|
|
|
import com.yoho.search.consumer.service.bo.ProductPriceBO;
|
|
|
import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
|
|
|
import com.yoho.search.consumer.service.logic.VipPriceLogicService;
|
|
|
import com.yoho.search.dal.model.ProductPrice;
|
|
|
import com.yoho.search.dal.model.ProductPriceHistory;
|
|
|
|
|
|
@Service
|
|
|
public class ProductPriceBOLogicService {
|
|
|
|
|
|
@Autowired
|
|
|
private ProductPriceService productPriceService;
|
|
|
@Autowired
|
|
|
private SpecialDealLogicService specialDealLogicService;
|
|
|
@Autowired
|
|
|
private VipPriceLogicService vipPriceLogicService;
|
|
|
|
|
|
/**
|
|
|
* 构造skn的价格相关的bo
|
|
|
*
|
|
|
* @param productSkns
|
|
|
* @return
|
|
|
*/
|
|
|
public List<ProductPriceBO> buildProductPriceBO(List<Integer> productSkns) {
|
|
|
List<ProductPrice> productPrices = productPriceService.getProductPrices(productSkns);
|
|
|
List<ProductPriceHistory> productPriceHistorys = productPriceService.selectProductPriceHistorys(productSkns);
|
|
|
Map<Integer, ProductPriceHistory> productPriceHistoryMap = productPriceHistorys.stream().collect(Collectors.toMap(ProductPriceHistory::getProductSkn, (p) -> p));
|
|
|
List<ProductPriceBO> results = new ArrayList<ProductPriceBO>();
|
|
|
for (ProductPrice productPrice : productPrices) {
|
|
|
// 取价格的历史记录
|
|
|
ProductPriceHistory productPriceHistory = productPriceHistoryMap.get(productPrice.getProductSkn());
|
|
|
// 构造ProductPriceBO
|
|
|
ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
|
|
|
// 构造ProductIndexBO
|
|
|
this.buildProductPriceBO(productPriceBO, productPrice, productPriceHistory);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private void buildProductPriceBO(ProductPriceBO productPriceBO, ProductPrice productPrice, ProductPriceHistory productPriceHistory) {
|
|
|
// 1、计算并重设vip相关价格
|
|
|
vipPriceLogicService.fillProductPriceVipPrice(productPriceBO);
|
|
|
// 2、计算折扣相关
|
|
|
double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
|
|
|
double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");
|
|
|
double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
if (specialOffer <= 0.5) {
|
|
|
productPriceBO.setSpecialoffer("Y");
|
|
|
} else {
|
|
|
productPriceBO.setSpecialoffer("N");
|
|
|
}
|
|
|
double promotionDiscountInt = this.divide(1, productPriceBO.getSalesPrice().multiply(new BigDecimal(10)), productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setPromotionDiscountInt((long) promotionDiscountInt);
|
|
|
double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));
|
|
|
// 3、设置学生价相关属性
|
|
|
BigDecimal studentPrice = productPriceBO.getStudentPrice();
|
|
|
productPriceBO.setIsStudentPrice(studentPrice == null || studentPrice.compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
|
|
|
productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N": "Y");
|
|
|
// 4、设置最新降价
|
|
|
productPriceBO.setIsLatestReducePrice(this.isLatestReducePrice(productPrice, productPriceHistory));
|
|
|
// TODO fill new
|
|
|
}
|
|
|
|
|
|
private void fillIsNew(Integer firstShelveTime, ProductPriceBO productPriceBO) {
|
|
|
BigDecimal marketPrice = productPriceBO.getMarketPrice();
|
|
|
BigDecimal salesPrice = productPriceBO.getSalesPrice();
|
|
|
boolean isNew = this.isNew(firstShelveTime, marketPrice, salesPrice);
|
|
|
productPriceBO.setIsnew(isNew?"Y":"N");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*@计算isNew
|
|
|
*@首次上架7天内展示且折扣率大于等于88折)
|
|
|
*/
|
|
|
private boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
|
|
|
try {
|
|
|
if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {
|
|
|
return false;
|
|
|
}
|
|
|
long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());
|
|
|
if (dateCount > 7) {
|
|
|
return false;
|
|
|
}
|
|
|
// 计算折扣
|
|
|
Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);
|
|
|
if (discount >= 0.88) {
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
} catch (Exception e) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private String isLatestReducePrice(ProductPrice productPrice, ProductPriceHistory productPriceHistory) {
|
|
|
BigDecimal newPrice = productPrice.getCurrentPrice();
|
|
|
if (newPrice == null || productPriceHistory == null) {
|
|
|
return "N";
|
|
|
}
|
|
|
if (newPrice.compareTo(productPriceHistory.getCurrentSalesPrice()) == 0) {
|
|
|
return newPrice.compareTo(productPriceHistory.getHistorySalesPrice()) < 0 ? "Y" : "N";
|
|
|
}
|
|
|
return newPrice.compareTo(productPriceHistory.getCurrentSalesPrice()) < 0 ? "Y" : "N";
|
|
|
}
|
|
|
|
|
|
private double divide(int scale, BigDecimal a, BigDecimal b) {
|
|
|
if (b == null || b.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
return 0;
|
|
|
}
|
|
|
StringBuilder sb = new StringBuilder("0.0");
|
|
|
for (int i = 1; i < scale; i++) {
|
|
|
sb.append('0');
|
|
|
}
|
|
|
BigDecimal divideResult = a.divide(b, scale, RoundingMode.DOWN);
|
|
|
DecimalFormat decimalFormat = new DecimalFormat(sb.toString());
|
|
|
String result = decimalFormat.format(divideResult);
|
|
|
return Double.parseDouble(result);
|
|
|
}
|
|
|
|
|
|
private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
|
|
|
if (bigDecimal == null) {
|
|
|
return 0d;
|
|
|
}
|
|
|
return bigDecimal.doubleValue();
|
|
|
}
|
|
|
|
|
|
private BigDecimal getBigDecimalFromDouble(Double d) {
|
|
|
if (d == null) {
|
|
|
return new BigDecimal(0);
|
|
|
}
|
|
|
return new BigDecimal(d);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|