|
|
package com.yohoufo.common.helper;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yohobuy.ufo.model.order.bo.ShoppingProductInfo;
|
|
|
import com.yohobuy.ufo.model.promotion.constant.CouponProductLimitTypeEnum;
|
|
|
import com.yohobuy.ufo.model.promotion.request.ProductLimitValueBo;
|
|
|
import com.yohobuy.ufo.model.promotion.response.ProductLimitInfo;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class ProductLimitCollector {
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(ProductLimitCollector.class);
|
|
|
|
|
|
private static List<LimitValueHandler> LIMIT_VALUES_LIST = Lists.newArrayList();
|
|
|
|
|
|
static{
|
|
|
LIMIT_VALUES_LIST.add(new NoLimitValueHandler());
|
|
|
LIMIT_VALUES_LIST.add(new BrandLimitValueHandler());
|
|
|
LIMIT_VALUES_LIST.add(new CategoryLimitValueHandler());
|
|
|
LIMIT_VALUES_LIST.add(new SeriesLimitValueHandler());
|
|
|
LIMIT_VALUES_LIST.add(new ProductIncludeLimitValueHandler());
|
|
|
LIMIT_VALUES_LIST.add(new ProductExcludeLimitValueHandler());
|
|
|
}
|
|
|
|
|
|
|
|
|
public static ProductLimitInfo getProductLimitInfo(List<ProductLimitValueBo> productLimitValueBo, Integer productLimitType, String productLimitValue ){
|
|
|
|
|
|
ProductLimitInfo productLimitInfo = new ProductLimitInfo();
|
|
|
|
|
|
// 处理 排除&指定的商品
|
|
|
if (CollectionUtils.isNotEmpty(productLimitValueBo)){
|
|
|
|
|
|
for (ProductLimitValueBo item : productLimitValueBo){
|
|
|
Optional<LimitValueHandler> handler = LIMIT_VALUES_LIST.stream().filter(x->Integer.valueOf(x.getLimitValue().getLimitType()) == item.getLimitType()).findFirst();
|
|
|
if (handler.isPresent()){
|
|
|
handler.get().setProductLimitValue(productLimitInfo, item.getProductIds(), productLimitValue);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Optional<LimitValueHandler> limitValueHandler = LIMIT_VALUES_LIST.stream().filter(x->Integer.valueOf(x.getLimitValue().getLimitType()) == productLimitType).findFirst();
|
|
|
limitValueHandler.ifPresent(x->x.setProductLimitValue(productLimitInfo, null, productLimitValue));
|
|
|
|
|
|
return productLimitInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param shoppingProductInfo
|
|
|
* @param productLimitInfo
|
|
|
* @return
|
|
|
*/
|
|
|
public static boolean match(String couponCode,
|
|
|
ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo){
|
|
|
for (LimitValueHandler limitValueHandler : LIMIT_VALUES_LIST){
|
|
|
|
|
|
if (!limitValueHandler.isMatch(shoppingProductInfo, productLimitInfo)){
|
|
|
logger.info("[{}] ProductLimitRuleMatcher Condition <<< {},{}; Result <<< [{}] not matched",
|
|
|
couponCode, shoppingProductInfo, productLimitInfo, limitValueHandler.getLimitValue());
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
logger.info("[{}] ProductLimitRuleMatcher Condition <<< {},{}; Result <<< matched", couponCode, shoppingProductInfo, productLimitInfo);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
private static List<Integer> covertProductLimitValueList(String productLimitValue) {
|
|
|
List<Integer> limitValues = Lists.newArrayList();
|
|
|
try{
|
|
|
limitValues = Arrays.stream(productLimitValue.split(",")).map(Integer::valueOf).collect(Collectors.toList());
|
|
|
}catch (Exception e){
|
|
|
logger.warn("error happened. productLimitValue {}", productLimitValue);
|
|
|
}
|
|
|
|
|
|
return limitValues;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
public interface LimitValueHandler {
|
|
|
/**
|
|
|
* 商品限制类型
|
|
|
* @return
|
|
|
*/
|
|
|
CouponProductLimitTypeEnum getLimitValue();
|
|
|
|
|
|
/**
|
|
|
* 商品限制的值
|
|
|
* @param productIds
|
|
|
* @param productLimitValue
|
|
|
*/
|
|
|
void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue);
|
|
|
|
|
|
/**
|
|
|
* 是否满足使用条件
|
|
|
* @param productLimitInfo
|
|
|
* @return
|
|
|
*/
|
|
|
boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo);
|
|
|
}
|
|
|
|
|
|
private static class NoLimitValueHandler implements LimitValueHandler {
|
|
|
|
|
|
@Override
|
|
|
public CouponProductLimitTypeEnum getLimitValue() {
|
|
|
return CouponProductLimitTypeEnum.NON;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue) {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class BrandLimitValueHandler implements LimitValueHandler {
|
|
|
|
|
|
@Override
|
|
|
public CouponProductLimitTypeEnum getLimitValue() {
|
|
|
return CouponProductLimitTypeEnum.SPECIFIC_BRAND;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue) {
|
|
|
productLimitInfo.setBrandInclude(covertProductLimitValueList(productLimitValue));
|
|
|
}
|
|
|
|
|
|
public boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo) {
|
|
|
|
|
|
return CollectionUtils.isEmpty(productLimitInfo.getBrandInclude()) || (CollectionUtils.isNotEmpty(productLimitInfo.getBrandInclude())
|
|
|
&& productLimitInfo.getBrandInclude().contains(shoppingProductInfo.getBrandId()));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static class CategoryLimitValueHandler implements LimitValueHandler {
|
|
|
|
|
|
@Override
|
|
|
public CouponProductLimitTypeEnum getLimitValue() {
|
|
|
return CouponProductLimitTypeEnum.SPECIFIC_CATEGORY;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue) {
|
|
|
productLimitInfo.setCategoryInclude(covertProductLimitValueList(productLimitValue));
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo) {
|
|
|
|
|
|
return CollectionUtils.isEmpty(productLimitInfo.getCategoryInclude()) ||
|
|
|
(CollectionUtils.isNotEmpty(productLimitInfo.getCategoryInclude())
|
|
|
&& productLimitInfo.getCategoryInclude().contains(shoppingProductInfo.getCategoryId()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static class SeriesLimitValueHandler implements LimitValueHandler {
|
|
|
|
|
|
@Override
|
|
|
public CouponProductLimitTypeEnum getLimitValue() {
|
|
|
return CouponProductLimitTypeEnum.SPECIFIC_SERIES;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue) {
|
|
|
productLimitInfo.setSeriesInclude((covertProductLimitValueList(productLimitValue)));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo) {
|
|
|
return CollectionUtils.isEmpty(productLimitInfo.getSeriesInclude())
|
|
|
|| (CollectionUtils.isNotEmpty(productLimitInfo.getSeriesInclude())
|
|
|
&& productLimitInfo.getSeriesInclude().contains(shoppingProductInfo.getSeriesId()));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static class ProductIncludeLimitValueHandler implements LimitValueHandler {
|
|
|
|
|
|
@Override
|
|
|
public CouponProductLimitTypeEnum getLimitValue() {
|
|
|
return CouponProductLimitTypeEnum.SPECIFIC_PRODUCT;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue) {
|
|
|
productLimitInfo.setProductIdInclude(productIds);
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo) {
|
|
|
|
|
|
//包含商品
|
|
|
return CollectionUtils.isEmpty(productLimitInfo.getProductIdInclude())
|
|
|
|| (CollectionUtils.isNotEmpty(productLimitInfo.getProductIdInclude())
|
|
|
&& productLimitInfo.getProductIdInclude().contains(shoppingProductInfo.getProductId()));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static class ProductExcludeLimitValueHandler implements LimitValueHandler {
|
|
|
|
|
|
@Override
|
|
|
public CouponProductLimitTypeEnum getLimitValue() {
|
|
|
return CouponProductLimitTypeEnum.EXCLUDE_PRODUCT;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setProductLimitValue(ProductLimitInfo productLimitInfo, List<Integer> productIds, String productLimitValue) {
|
|
|
productLimitInfo.setProductIdExclude(productIds);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isMatch(ShoppingProductInfo shoppingProductInfo, ProductLimitInfo productLimitInfo) {
|
|
|
//排除商品
|
|
|
return CollectionUtils.isEmpty(productLimitInfo.getProductIdExclude())
|
|
|
|| (CollectionUtils.isNotEmpty(productLimitInfo.getProductIdExclude())
|
|
|
&& !productLimitInfo.getProductIdExclude().contains(shoppingProductInfo.getProductId()));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} |