Authored by wujiexiang

优惠券结算

... ... @@ -99,9 +99,10 @@ public class CouponRuleMatcher {
public boolean match(ChargeGoods chargeGoods, UserCouponsBo couponsBo) {
final CouponProductLimitTypeEnum limitTypeEnum = CouponProductLimitTypeEnum.of(couponsBo.getProductLimitType()).orElse(null);
if (limitTypeEnum == CouponProductLimitTypeEnum.SPECIFIC_PRODUCT) {
return couponsBo.getSknInclude().contains(chargeGoods.getProductId());
return couponsBo.getProductIdInclude().contains(chargeGoods.getProductId());
} else {
return true;
//目前不存在
return false;
}
}
}
... ...
... ... @@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.common.utils.AddressUtil;
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
... ... @@ -18,6 +19,7 @@ import com.yohoufo.order.charge.CouponRuleMatcher;
import com.yohoufo.order.charge.model.ChargeGoods;
import com.yohoufo.order.charge.model.ChargeParam;
import com.yohoufo.order.charge.model.ChargeResult;
import com.yohoufo.order.charge.model.CouponMatchResult;
import com.yohoufo.order.event.BuyerCancelEvent;
import com.yohoufo.order.event.NotPaidNoticeEvent;
import com.yohoufo.order.model.AddressInfo;
... ... @@ -31,16 +33,17 @@ import com.yohoufo.order.service.cache.CacheCleaner;
import com.yohoufo.order.service.proxy.CouponProxyService;
import com.yohoufo.order.service.proxy.InBoxFacade;
import com.yohoufo.order.service.proxy.UserProxyService;
import com.yohoufo.order.service.support.CouponSupport;
import com.yohoufo.order.service.support.ShoppingSupport;
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
import com.yohoufo.order.utils.AddressHelper;
import com.yohoufo.order.utils.MathUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;
... ... @@ -89,6 +92,9 @@ public class ShoppingServiceImpl implements IShoppingService {
@Autowired
private CouponRuleMatcher couponRuleMatcher;
@Autowired
private CouponSupport couponSupport;
/**
* 结算页数据
* @param request
... ... @@ -151,99 +157,63 @@ public class ShoppingServiceImpl implements IShoppingService {
@Override
public ShoppingCouponListResp selectCoupon(ShoppingRequest request) {
//todo
ShoppingCoupon coupon1 = new ShoppingCoupon();
coupon1.setCouponValueStr("100");
coupon1.setCouponCode("a123456");
coupon1.setCouponName("test");
coupon1.setCouponValidity("2018.1.1-2019.1.1");
coupon1.setSelected("N");
coupon1.setIsSelectedSupport("Y");
coupon1.setUseRuleStr("满499可用");
ShoppingCoupon coupon2 = new ShoppingCoupon();
coupon2.setCouponValueStr("50");
coupon2.setCouponCode("b987654");
coupon2.setCouponName("全场通用");
coupon2.setCouponValidity("2018.1.1-2019.1.1");
coupon2.setSelected("N");
coupon2.setIsSelectedSupport("Y");
coupon2.setUseRuleStr("满200可用");
ShoppingCouponListResp resp = new ShoppingCouponListResp();
resp.setCoupons(Lists.newArrayList(coupon1, coupon2));
return resp;
// if ((request.getSkup()) < 0 || request.getUid() < 0) {
// logger.warn("compute uid or skup is null");
// throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
// }
//
// // 检查 商品sku是否可售
// SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
//
// //构建算费
// ChargeContext chargeContext = buildChargeContext(request, skupGood);
//
// List<UserCouponsBo> userCouponsBos = couponProxyService.getUserAvailableCoupons(chargeContext.getChargeParam().getUid());
// List<CouponMatchResult> couponMatchResults = couponRuleMatcher.match(userCouponsBos,chargeContext);
//todo
if ((request.getSkup()) < 0 || request.getUid() < 0) {
logger.warn("compute uid or skup is null");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
@Override
public ComputeResponse compute(ShoppingRequest request) {
CouponInfo result = CouponInfo.builder().couponCode("a123456").couponAmountStr("¥50.00").couponTitle("test").couponCount(1).build();
// 检查 商品sku是否可售
SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
//构建算费
ChargeContext chargeContext = buildChargeContext(request, skupGood);
PromotionFormula formula1 = new PromotionFormula();
formula1.setPromotion(OrderConstant.GOODS_PRICE);
formula1.setPromotionAmount(OrderConstant.MONEY_SIGN + "500");
//查询用户可用的优惠券
List<UserCouponsBo> userCouponsBos = couponProxyService.getUserAvailableCoupons(chargeContext.getChargeParam().getUid());
PromotionFormula formula2 = new PromotionFormula();
formula2.setPromotion(OrderConstant.DELIVERY_DESC);
formula2.setPromotionAmount(OrderConstant.PLUS_SIGN + OrderConstant.MONEY_SIGN + 25);
//进行券匹配
List<CouponMatchResult> couponMatchResults = couponRuleMatcher.match(userCouponsBos,chargeContext);
//满足当前商品的优惠券
List<ShoppingCoupon> usableCoupons = couponSupport.filterUsableCoupons(couponMatchResults);
PromotionFormula formula3 = new PromotionFormula();
formula3.setPromotion(OrderConstant.GOODS_REAL_PRICE);
formula3.setPromotionAmount(OrderConstant.MONEY_SIGN + "425");
//券选中
usableCoupons.forEach(shoppingCoupon -> {
if (request.getCouponCodes().contains(shoppingCoupon.getCouponCode())) {
shoppingCoupon.setSelected(OrderConstant.Y_STR);
}
});
ShoppingCouponListResp resp = new ShoppingCouponListResp();
resp.setCoupons(usableCoupons);
return resp;
}
PromotionFormula formula4 = new PromotionFormula();
formula4.setPromotion("优惠券");
formula4.setPromotionAmount("-"+ OrderConstant.MONEY_SIGN +"100");
@Override
public ComputeResponse compute(ShoppingRequest request) {
List<PromotionFormula> formulaList = Arrays.asList(formula1, formula2, formula4, formula3);
// 入口参数检查
if ((request.getSkup()) < 0 || request.getUid() < 0) {
logger.warn("compute uid or skup is null");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
ComputeResponse response = new ComputeResponse();
// 检查 商品sku是否可售
SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
//构建算费
ChargeContext chargeContext = buildChargeContext(request, skupGood);
//算费
chargeService.charge(chargeContext);
//算费结果
ChargeResult chargeResult = chargeContext.getChargeResult();
response.setCouponInfo(result);
response.setPromotionFormulaList(formulaList);
response.setAmount("425.00");
ComputeResponse response = new ComputeResponse();
response.setCouponInfo(shoppingSupport.getCouponInfo(chargeResult));
response.setPromotionFormulaList(shoppingSupport.getPromotionFormula(chargeResult));
response.setAmount(MathUtils.formatStr(chargeResult.getFinalAmount()));
return response;
// 入口参数检查
// if ((request.getSkup()) < 0 || request.getUid() < 0) {
// logger.warn("compute uid or skup is null");
// throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
// }
//
// // 检查 商品sku是否可售
// SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
// //构建算费
// ChargeContext chargeContext = buildChargeContext(request, skupGood);
// //算费
// chargeService.charge(chargeContext);
// //算费结果
// ChargeResult chargeResult = chargeContext.getChargeResult();
//
// ComputeResponse response = new ComputeResponse();
// response.setCouponInfo(shoppingSupport.getCouponInfo(chargeResult));
// response.setPromotionFormulaList(shoppingSupport.getPromotionFormula(chargeResult));
// response.setAmount(MathUtils.formatStr(chargeResult.getFinalAmount()));
//
// return response;
}
/**
... ...
... ... @@ -19,17 +19,39 @@ public class CouponProxyService {
* @return
*/
public List<UserCouponsBo> getUserAvailableCoupons(int uid) {
//todo
return Lists.newArrayList();
UserCouponsBo userCouponsBo = new UserCouponsBo();
userCouponsBo.setCouponCode("a100");
userCouponsBo.setCouponAmount(BigDecimal.valueOf(20));
userCouponsBo.setUseLimitType("2");
userCouponsBo.setUseLimitValue("299");
userCouponsBo.setProductLimitType("1");
userCouponsBo.setProductIdInclude(Lists.newArrayList(88888900));
userCouponsBo.setUseRule("满299可用");
userCouponsBo.setStartTime(1514739661L);
userCouponsBo.setEndTime(1546275661L);
UserCouponsBo userCouponsBo1 = new UserCouponsBo();
userCouponsBo1.setCouponCode("a200");
userCouponsBo1.setCouponAmount(BigDecimal.valueOf(100));
userCouponsBo1.setUseLimitType("0");
userCouponsBo1.setUseLimitValue("");
userCouponsBo1.setProductLimitType("1");
userCouponsBo1.setProductIdInclude(Lists.newArrayList(88888900));
userCouponsBo1.setStartTime(1514739661L);
userCouponsBo1.setEndTime(1546275661L);
return Lists.newArrayList(userCouponsBo, userCouponsBo1);
}
public List<UserCouponsBo> checkUseCouponsAndGet(int uid, List<String> couponCodes) {
UserCouponsBo userCouponsBo = new UserCouponsBo();
userCouponsBo.setCouponCode("a100");
userCouponsBo.setCouponAmount(BigDecimal.valueOf(20));
userCouponsBo.setUseLimitType("2");
userCouponsBo.setUseLimitValue("299");
userCouponsBo.setProductLimitType("1");
userCouponsBo.setSknInclude(Lists.newArrayList(88888900));
userCouponsBo.setProductIdInclude(Lists.newArrayList(88888900));
userCouponsBo.setStartTime(1514739661L);
userCouponsBo.setEndTime(1546275661L);
return Lists.newArrayList(userCouponsBo);
}
}
... ...
package com.yohoufo.order.service.support;
import com.google.common.collect.Lists;
import com.yohobuy.ufo.model.order.constants.OrderConstant;
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
import com.yohoufo.order.charge.model.CouponMatchResult;
import com.yohoufo.order.model.response.ShoppingCoupon;
import com.yohoufo.order.utils.DateUtil;
import com.yohoufo.order.utils.MathUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
... ... @@ -10,13 +16,36 @@ import java.util.stream.Collectors;
/**
* Created by jiexiang.wu on 2018/11/19.
*/
@Component
public class CouponSupport {
public List<ShoppingCoupon> getUsableCoupons(List<CouponMatchResult> couponMatchResults) {
public List<ShoppingCoupon> filterUsableCoupons(List<CouponMatchResult> couponMatchResults) {
if (CollectionUtils.isEmpty(couponMatchResults)) {
return Lists.newArrayList();
}
//找到可用的券
List<UserCouponsBo> usableCoupons = couponMatchResults.stream().filter(result -> result.isUsable()).map(result -> result.getUserCouponsBo()).collect(Collectors.toList());
//todo
return null;
if (CollectionUtils.isEmpty(usableCoupons)) {
return Lists.newArrayList();
}
List<ShoppingCoupon> result = Lists.newLinkedList();
usableCoupons.forEach(userCouponsBo -> {
ShoppingCoupon shoppingCoupon = new ShoppingCoupon();
shoppingCoupon.setCouponCode(userCouponsBo.getCouponCode());
shoppingCoupon.setCouponName(userCouponsBo.getCouponName());
shoppingCoupon.setCouponValueStr(userCouponsBo.getCouponAmount().intValue() + "");
shoppingCoupon.setCouponValidity(DateUtil.format(DateUtil.getDateFromLong(userCouponsBo.getStartTime()), "yyyy.MM.dd") + "-" +
DateUtil.format(DateUtil.getDateFromLong(userCouponsBo.getEndTime()), "yyyy.MM.dd"));
shoppingCoupon.setUseRuleStr(userCouponsBo.getUseRule());
shoppingCoupon.setSelected(OrderConstant.N_STR);
shoppingCoupon.setIsSelectedSupport(OrderConstant.Y_STR);
result.add(shoppingCoupon);
});
return result;
}
}
... ...
package com.yohoufo.order.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by jiexiang.wu on 2018/11/20.
*/
public class DateUtil {
public static final String yyyy_MM_dd_HH_mm_SS = "yyyy-MM-dd HH:mm:ss";
public static final String yyyy$MM$dd = "yyyy.MM.dd";
/**
* 将Date类型转换为字符串
*
* @param date 日期类型
* @param pattern 字符串格式
* @return 日期字符串
*/
public static String format(Date date, String pattern) {
if (date == null) {
return "null";
}
if (pattern == null || pattern.equals("") || pattern.equals("null")) {
pattern = yyyy$MM$dd;
}
return new SimpleDateFormat(pattern).format(date);
}
/**
* @param time
* @return java.lang.String
*/
public static Date getDateFromLong(long time) {
Long timestamp = time*1000;
String date = new java.text.SimpleDateFormat(yyyy_MM_dd_HH_mm_SS).format(new java.util.Date(timestamp));
return format(date);
}
/**
* 将字符串转换为Date类型
*
* @param date 字符串类型
* @return 日期类型
*/
public static Date format(String date) {
return format(date, null);
}
/**
* 将字符串转换为Date类型
*
* @param date 字符串类型
* @param pattern 格式
* @return 日期类型
*/
public static Date format(String date, String pattern) {
if (pattern == null || pattern.equals("") || pattern.equals("null")) {
pattern = yyyy_MM_dd_HH_mm_SS;
}
if (date == null || date.equals("") || date.equals("null")) {
return new Date();
}
Date d = null;
try {
d = new SimpleDateFormat(pattern).parse(date);
} catch (ParseException pe) {
}
return d;
}
}
... ...