Authored by wujiexiang

优惠券结算

@@ -99,9 +99,10 @@ public class CouponRuleMatcher { @@ -99,9 +99,10 @@ public class CouponRuleMatcher {
99 public boolean match(ChargeGoods chargeGoods, UserCouponsBo couponsBo) { 99 public boolean match(ChargeGoods chargeGoods, UserCouponsBo couponsBo) {
100 final CouponProductLimitTypeEnum limitTypeEnum = CouponProductLimitTypeEnum.of(couponsBo.getProductLimitType()).orElse(null); 100 final CouponProductLimitTypeEnum limitTypeEnum = CouponProductLimitTypeEnum.of(couponsBo.getProductLimitType()).orElse(null);
101 if (limitTypeEnum == CouponProductLimitTypeEnum.SPECIFIC_PRODUCT) { 101 if (limitTypeEnum == CouponProductLimitTypeEnum.SPECIFIC_PRODUCT) {
102 - return couponsBo.getSknInclude().contains(chargeGoods.getProductId()); 102 + return couponsBo.getProductIdInclude().contains(chargeGoods.getProductId());
103 } else { 103 } else {
104 - return true; 104 + //目前不存在
  105 + return false;
105 } 106 }
106 } 107 }
107 } 108 }
@@ -4,6 +4,7 @@ import com.google.common.collect.Lists; @@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
4 import com.yoho.core.rest.client.ServiceCaller; 4 import com.yoho.core.rest.client.ServiceCaller;
5 import com.yoho.error.ServiceError; 5 import com.yoho.error.ServiceError;
6 import com.yoho.error.exception.ServiceException; 6 import com.yoho.error.exception.ServiceException;
  7 +import com.yohobuy.ufo.model.promotion.UserCouponsBo;
7 import com.yohoufo.common.alarm.EventBusPublisher; 8 import com.yohoufo.common.alarm.EventBusPublisher;
8 import com.yohoufo.common.utils.AddressUtil; 9 import com.yohoufo.common.utils.AddressUtil;
9 import com.yohoufo.dal.order.SellerOrderGoodsMapper; 10 import com.yohoufo.dal.order.SellerOrderGoodsMapper;
@@ -18,6 +19,7 @@ import com.yohoufo.order.charge.CouponRuleMatcher; @@ -18,6 +19,7 @@ import com.yohoufo.order.charge.CouponRuleMatcher;
18 import com.yohoufo.order.charge.model.ChargeGoods; 19 import com.yohoufo.order.charge.model.ChargeGoods;
19 import com.yohoufo.order.charge.model.ChargeParam; 20 import com.yohoufo.order.charge.model.ChargeParam;
20 import com.yohoufo.order.charge.model.ChargeResult; 21 import com.yohoufo.order.charge.model.ChargeResult;
  22 +import com.yohoufo.order.charge.model.CouponMatchResult;
21 import com.yohoufo.order.event.BuyerCancelEvent; 23 import com.yohoufo.order.event.BuyerCancelEvent;
22 import com.yohoufo.order.event.NotPaidNoticeEvent; 24 import com.yohoufo.order.event.NotPaidNoticeEvent;
23 import com.yohoufo.order.model.AddressInfo; 25 import com.yohoufo.order.model.AddressInfo;
@@ -31,16 +33,17 @@ import com.yohoufo.order.service.cache.CacheCleaner; @@ -31,16 +33,17 @@ import com.yohoufo.order.service.cache.CacheCleaner;
31 import com.yohoufo.order.service.proxy.CouponProxyService; 33 import com.yohoufo.order.service.proxy.CouponProxyService;
32 import com.yohoufo.order.service.proxy.InBoxFacade; 34 import com.yohoufo.order.service.proxy.InBoxFacade;
33 import com.yohoufo.order.service.proxy.UserProxyService; 35 import com.yohoufo.order.service.proxy.UserProxyService;
  36 +import com.yohoufo.order.service.support.CouponSupport;
34 import com.yohoufo.order.service.support.ShoppingSupport; 37 import com.yohoufo.order.service.support.ShoppingSupport;
35 import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator; 38 import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
36 import com.yohoufo.order.utils.AddressHelper; 39 import com.yohoufo.order.utils.AddressHelper;
37 import com.yohoufo.order.utils.MathUtils; 40 import com.yohoufo.order.utils.MathUtils;
38 import org.apache.commons.lang3.StringUtils; 41 import org.apache.commons.lang3.StringUtils;
  42 +import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
39 import org.slf4j.Logger; 43 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory; 44 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired; 45 import org.springframework.beans.factory.annotation.Autowired;
42 import java.math.BigDecimal; 46 import java.math.BigDecimal;
43 -import java.util.Arrays;  
44 import java.util.List; 47 import java.util.List;
45 import org.springframework.stereotype.Service; 48 import org.springframework.stereotype.Service;
46 49
@@ -89,6 +92,9 @@ public class ShoppingServiceImpl implements IShoppingService { @@ -89,6 +92,9 @@ public class ShoppingServiceImpl implements IShoppingService {
89 @Autowired 92 @Autowired
90 private CouponRuleMatcher couponRuleMatcher; 93 private CouponRuleMatcher couponRuleMatcher;
91 94
  95 + @Autowired
  96 + private CouponSupport couponSupport;
  97 +
92 /** 98 /**
93 * 结算页数据 99 * 结算页数据
94 * @param request 100 * @param request
@@ -151,99 +157,63 @@ public class ShoppingServiceImpl implements IShoppingService { @@ -151,99 +157,63 @@ public class ShoppingServiceImpl implements IShoppingService {
151 157
152 @Override 158 @Override
153 public ShoppingCouponListResp selectCoupon(ShoppingRequest request) { 159 public ShoppingCouponListResp selectCoupon(ShoppingRequest request) {
154 - //todo  
155 - ShoppingCoupon coupon1 = new ShoppingCoupon();  
156 - coupon1.setCouponValueStr("100");  
157 - coupon1.setCouponCode("a123456");  
158 - coupon1.setCouponName("test");  
159 - coupon1.setCouponValidity("2018.1.1-2019.1.1");  
160 - coupon1.setSelected("N");  
161 - coupon1.setIsSelectedSupport("Y");  
162 - coupon1.setUseRuleStr("满499可用");  
163 -  
164 - ShoppingCoupon coupon2 = new ShoppingCoupon();  
165 - coupon2.setCouponValueStr("50");  
166 - coupon2.setCouponCode("b987654");  
167 - coupon2.setCouponName("全场通用");  
168 - coupon2.setCouponValidity("2018.1.1-2019.1.1");  
169 - coupon2.setSelected("N");  
170 - coupon2.setIsSelectedSupport("Y");  
171 - coupon2.setUseRuleStr("满200可用");  
172 - ShoppingCouponListResp resp = new ShoppingCouponListResp();  
173 - resp.setCoupons(Lists.newArrayList(coupon1, coupon2));  
174 - return resp;  
175 160
176 -// if ((request.getSkup()) < 0 || request.getUid() < 0) {  
177 -// logger.warn("compute uid or skup is null");  
178 -// throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);  
179 -// }  
180 -//  
181 -// // 检查 商品sku是否可售  
182 -// SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());  
183 -//  
184 -// //构建算费  
185 -// ChargeContext chargeContext = buildChargeContext(request, skupGood);  
186 -//  
187 -// List<UserCouponsBo> userCouponsBos = couponProxyService.getUserAvailableCoupons(chargeContext.getChargeParam().getUid());  
188 -// List<CouponMatchResult> couponMatchResults = couponRuleMatcher.match(userCouponsBos,chargeContext); 161 + if ((request.getSkup()) < 0 || request.getUid() < 0) {
  162 + logger.warn("compute uid or skup is null");
  163 + throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
  164 + }
189 165
190 - //todo  
191 - } 166 + // 检查 商品sku是否可售
  167 + SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
192 168
193 - @Override  
194 - public ComputeResponse compute(ShoppingRequest request) {  
195 - CouponInfo result = CouponInfo.builder().couponCode("a123456").couponAmountStr("¥50.00").couponTitle("test").couponCount(1).build(); 169 + //构建算费
  170 + ChargeContext chargeContext = buildChargeContext(request, skupGood);
196 171
  172 + //查询用户可用的优惠券
  173 + List<UserCouponsBo> userCouponsBos = couponProxyService.getUserAvailableCoupons(chargeContext.getChargeParam().getUid());
197 174
198 - PromotionFormula formula1 = new PromotionFormula();  
199 - formula1.setPromotion(OrderConstant.GOODS_PRICE);  
200 - formula1.setPromotionAmount(OrderConstant.MONEY_SIGN + "500"); 175 + //进行券匹配
  176 + List<CouponMatchResult> couponMatchResults = couponRuleMatcher.match(userCouponsBos,chargeContext);
201 177
202 - PromotionFormula formula2 = new PromotionFormula();  
203 - formula2.setPromotion(OrderConstant.DELIVERY_DESC);  
204 - formula2.setPromotionAmount(OrderConstant.PLUS_SIGN + OrderConstant.MONEY_SIGN + 25); 178 + //满足当前商品的优惠券
  179 + List<ShoppingCoupon> usableCoupons = couponSupport.filterUsableCoupons(couponMatchResults);
205 180
  181 + //券选中
  182 + usableCoupons.forEach(shoppingCoupon -> {
  183 + if (request.getCouponCodes().contains(shoppingCoupon.getCouponCode())) {
  184 + shoppingCoupon.setSelected(OrderConstant.Y_STR);
  185 + }
  186 + });
206 187
207 - PromotionFormula formula3 = new PromotionFormula();  
208 - formula3.setPromotion(OrderConstant.GOODS_REAL_PRICE);  
209 - formula3.setPromotionAmount(OrderConstant.MONEY_SIGN + "425"); 188 + ShoppingCouponListResp resp = new ShoppingCouponListResp();
  189 + resp.setCoupons(usableCoupons);
  190 + return resp;
  191 + }
210 192
  193 + @Override
  194 + public ComputeResponse compute(ShoppingRequest request) {
211 195
212 - PromotionFormula formula4 = new PromotionFormula();  
213 - formula4.setPromotion("优惠券");  
214 - formula4.setPromotionAmount("-"+ OrderConstant.MONEY_SIGN +"100"); 196 + // 入口参数检查
  197 + if ((request.getSkup()) < 0 || request.getUid() < 0) {
  198 + logger.warn("compute uid or skup is null");
  199 + throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
  200 + }
215 201
216 - List<PromotionFormula> formulaList = Arrays.asList(formula1, formula2, formula4, formula3); 202 + // 检查 商品sku是否可售
  203 + SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
  204 + //构建算费
  205 + ChargeContext chargeContext = buildChargeContext(request, skupGood);
  206 + //算费
  207 + chargeService.charge(chargeContext);
  208 + //算费结果
  209 + ChargeResult chargeResult = chargeContext.getChargeResult();
217 210
218 ComputeResponse response = new ComputeResponse(); 211 ComputeResponse response = new ComputeResponse();
219 -  
220 - response.setCouponInfo(result);  
221 - response.setPromotionFormulaList(formulaList);  
222 - response.setAmount("425.00"); 212 + response.setCouponInfo(shoppingSupport.getCouponInfo(chargeResult));
  213 + response.setPromotionFormulaList(shoppingSupport.getPromotionFormula(chargeResult));
  214 + response.setAmount(MathUtils.formatStr(chargeResult.getFinalAmount()));
223 215
224 return response; 216 return response;
225 -  
226 - // 入口参数检查  
227 -// if ((request.getSkup()) < 0 || request.getUid() < 0) {  
228 -// logger.warn("compute uid or skup is null");  
229 -// throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);  
230 -// }  
231 -//  
232 -// // 检查 商品sku是否可售  
233 -// SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());  
234 -// //构建算费  
235 -// ChargeContext chargeContext = buildChargeContext(request, skupGood);  
236 -// //算费  
237 -// chargeService.charge(chargeContext);  
238 -// //算费结果  
239 -// ChargeResult chargeResult = chargeContext.getChargeResult();  
240 -//  
241 -// ComputeResponse response = new ComputeResponse();  
242 -// response.setCouponInfo(shoppingSupport.getCouponInfo(chargeResult));  
243 -// response.setPromotionFormulaList(shoppingSupport.getPromotionFormula(chargeResult));  
244 -// response.setAmount(MathUtils.formatStr(chargeResult.getFinalAmount()));  
245 -//  
246 -// return response;  
247 } 217 }
248 218
249 /** 219 /**
@@ -19,17 +19,39 @@ public class CouponProxyService { @@ -19,17 +19,39 @@ public class CouponProxyService {
19 * @return 19 * @return
20 */ 20 */
21 public List<UserCouponsBo> getUserAvailableCoupons(int uid) { 21 public List<UserCouponsBo> getUserAvailableCoupons(int uid) {
22 - //todo  
23 - return Lists.newArrayList(); 22 + UserCouponsBo userCouponsBo = new UserCouponsBo();
  23 + userCouponsBo.setCouponCode("a100");
  24 + userCouponsBo.setCouponAmount(BigDecimal.valueOf(20));
  25 + userCouponsBo.setUseLimitType("2");
  26 + userCouponsBo.setUseLimitValue("299");
  27 + userCouponsBo.setProductLimitType("1");
  28 + userCouponsBo.setProductIdInclude(Lists.newArrayList(88888900));
  29 + userCouponsBo.setUseRule("满299可用");
  30 + userCouponsBo.setStartTime(1514739661L);
  31 + userCouponsBo.setEndTime(1546275661L);
  32 +
  33 + UserCouponsBo userCouponsBo1 = new UserCouponsBo();
  34 + userCouponsBo1.setCouponCode("a200");
  35 + userCouponsBo1.setCouponAmount(BigDecimal.valueOf(100));
  36 + userCouponsBo1.setUseLimitType("0");
  37 + userCouponsBo1.setUseLimitValue("");
  38 + userCouponsBo1.setProductLimitType("1");
  39 + userCouponsBo1.setProductIdInclude(Lists.newArrayList(88888900));
  40 + userCouponsBo1.setStartTime(1514739661L);
  41 + userCouponsBo1.setEndTime(1546275661L);
  42 + return Lists.newArrayList(userCouponsBo, userCouponsBo1);
24 } 43 }
25 44
26 public List<UserCouponsBo> checkUseCouponsAndGet(int uid, List<String> couponCodes) { 45 public List<UserCouponsBo> checkUseCouponsAndGet(int uid, List<String> couponCodes) {
27 UserCouponsBo userCouponsBo = new UserCouponsBo(); 46 UserCouponsBo userCouponsBo = new UserCouponsBo();
  47 + userCouponsBo.setCouponCode("a100");
28 userCouponsBo.setCouponAmount(BigDecimal.valueOf(20)); 48 userCouponsBo.setCouponAmount(BigDecimal.valueOf(20));
29 userCouponsBo.setUseLimitType("2"); 49 userCouponsBo.setUseLimitType("2");
30 userCouponsBo.setUseLimitValue("299"); 50 userCouponsBo.setUseLimitValue("299");
31 userCouponsBo.setProductLimitType("1"); 51 userCouponsBo.setProductLimitType("1");
32 - userCouponsBo.setSknInclude(Lists.newArrayList(88888900)); 52 + userCouponsBo.setProductIdInclude(Lists.newArrayList(88888900));
  53 + userCouponsBo.setStartTime(1514739661L);
  54 + userCouponsBo.setEndTime(1546275661L);
33 return Lists.newArrayList(userCouponsBo); 55 return Lists.newArrayList(userCouponsBo);
34 } 56 }
35 } 57 }
1 package com.yohoufo.order.service.support; 1 package com.yohoufo.order.service.support;
2 2
  3 +import com.google.common.collect.Lists;
  4 +import com.yohobuy.ufo.model.order.constants.OrderConstant;
3 import com.yohobuy.ufo.model.promotion.UserCouponsBo; 5 import com.yohobuy.ufo.model.promotion.UserCouponsBo;
4 import com.yohoufo.order.charge.model.CouponMatchResult; 6 import com.yohoufo.order.charge.model.CouponMatchResult;
5 import com.yohoufo.order.model.response.ShoppingCoupon; 7 import com.yohoufo.order.model.response.ShoppingCoupon;
  8 +import com.yohoufo.order.utils.DateUtil;
  9 +import com.yohoufo.order.utils.MathUtils;
  10 +import org.apache.commons.collections.CollectionUtils;
  11 +import org.springframework.stereotype.Component;
6 12
7 import java.util.List; 13 import java.util.List;
8 import java.util.stream.Collectors; 14 import java.util.stream.Collectors;
@@ -10,13 +16,36 @@ import java.util.stream.Collectors; @@ -10,13 +16,36 @@ import java.util.stream.Collectors;
10 /** 16 /**
11 * Created by jiexiang.wu on 2018/11/19. 17 * Created by jiexiang.wu on 2018/11/19.
12 */ 18 */
  19 +@Component
13 public class CouponSupport { 20 public class CouponSupport {
14 21
15 - public List<ShoppingCoupon> getUsableCoupons(List<CouponMatchResult> couponMatchResults) { 22 + public List<ShoppingCoupon> filterUsableCoupons(List<CouponMatchResult> couponMatchResults) {
  23 + if (CollectionUtils.isEmpty(couponMatchResults)) {
  24 + return Lists.newArrayList();
  25 + }
16 //找到可用的券 26 //找到可用的券
17 List<UserCouponsBo> usableCoupons = couponMatchResults.stream().filter(result -> result.isUsable()).map(result -> result.getUserCouponsBo()).collect(Collectors.toList()); 27 List<UserCouponsBo> usableCoupons = couponMatchResults.stream().filter(result -> result.isUsable()).map(result -> result.getUserCouponsBo()).collect(Collectors.toList());
18 28
19 - //todo  
20 - return null; 29 + if (CollectionUtils.isEmpty(usableCoupons)) {
  30 + return Lists.newArrayList();
  31 + }
  32 + List<ShoppingCoupon> result = Lists.newLinkedList();
  33 +
  34 + usableCoupons.forEach(userCouponsBo -> {
  35 + ShoppingCoupon shoppingCoupon = new ShoppingCoupon();
  36 + shoppingCoupon.setCouponCode(userCouponsBo.getCouponCode());
  37 + shoppingCoupon.setCouponName(userCouponsBo.getCouponName());
  38 + shoppingCoupon.setCouponValueStr(userCouponsBo.getCouponAmount().intValue() + "");
  39 + shoppingCoupon.setCouponValidity(DateUtil.format(DateUtil.getDateFromLong(userCouponsBo.getStartTime()), "yyyy.MM.dd") + "-" +
  40 + DateUtil.format(DateUtil.getDateFromLong(userCouponsBo.getEndTime()), "yyyy.MM.dd"));
  41 + shoppingCoupon.setUseRuleStr(userCouponsBo.getUseRule());
  42 +
  43 + shoppingCoupon.setSelected(OrderConstant.N_STR);
  44 + shoppingCoupon.setIsSelectedSupport(OrderConstant.Y_STR);
  45 +
  46 + result.add(shoppingCoupon);
  47 + });
  48 +
  49 + return result;
21 } 50 }
22 } 51 }
  1 +package com.yohoufo.order.utils;
  2 +
  3 +import java.text.ParseException;
  4 +import java.text.SimpleDateFormat;
  5 +import java.util.Date;
  6 +
  7 +/**
  8 + * Created by jiexiang.wu on 2018/11/20.
  9 + */
  10 +public class DateUtil {
  11 +
  12 + public static final String yyyy_MM_dd_HH_mm_SS = "yyyy-MM-dd HH:mm:ss";
  13 +
  14 + public static final String yyyy$MM$dd = "yyyy.MM.dd";
  15 +
  16 +
  17 + /**
  18 + * 将Date类型转换为字符串
  19 + *
  20 + * @param date 日期类型
  21 + * @param pattern 字符串格式
  22 + * @return 日期字符串
  23 + */
  24 + public static String format(Date date, String pattern) {
  25 + if (date == null) {
  26 + return "null";
  27 + }
  28 + if (pattern == null || pattern.equals("") || pattern.equals("null")) {
  29 + pattern = yyyy$MM$dd;
  30 + }
  31 + return new SimpleDateFormat(pattern).format(date);
  32 + }
  33 +
  34 +
  35 + /**
  36 + * @param time
  37 + * @return java.lang.String
  38 + */
  39 + public static Date getDateFromLong(long time) {
  40 + Long timestamp = time*1000;
  41 +
  42 + String date = new java.text.SimpleDateFormat(yyyy_MM_dd_HH_mm_SS).format(new java.util.Date(timestamp));
  43 + return format(date);
  44 + }
  45 +
  46 + /**
  47 + * 将字符串转换为Date类型
  48 + *
  49 + * @param date 字符串类型
  50 + * @return 日期类型
  51 + */
  52 + public static Date format(String date) {
  53 + return format(date, null);
  54 + }
  55 +
  56 + /**
  57 + * 将字符串转换为Date类型
  58 + *
  59 + * @param date 字符串类型
  60 + * @param pattern 格式
  61 + * @return 日期类型
  62 + */
  63 + public static Date format(String date, String pattern) {
  64 + if (pattern == null || pattern.equals("") || pattern.equals("null")) {
  65 + pattern = yyyy_MM_dd_HH_mm_SS;
  66 + }
  67 + if (date == null || date.equals("") || date.equals("null")) {
  68 + return new Date();
  69 + }
  70 + Date d = null;
  71 + try {
  72 + d = new SimpleDateFormat(pattern).parse(date);
  73 + } catch (ParseException pe) {
  74 + }
  75 + return d;
  76 + }
  77 +
  78 +}