Authored by wujiexiang

优惠券结算

@@ -8,10 +8,10 @@ @@ -8,10 +8,10 @@
8 <result column="skup" jdbcType="INTEGER" property="skup" /> 8 <result column="skup" jdbcType="INTEGER" property="skup" />
9 <result column="goods_price" jdbcType="DECIMAL" property="goodsPrice" /> 9 <result column="goods_price" jdbcType="DECIMAL" property="goodsPrice" />
10 <result column="goods_amount" jdbcType="DECIMAL" property="goodsAmount" /> 10 <result column="goods_amount" jdbcType="DECIMAL" property="goodsAmount" />
11 - <result column="coupons_cut_amount" jdbcType="DECIMAL" property="couponCutAmount" /> 11 + <result column="coupon_cut_amount" jdbcType="DECIMAL" property="couponCutAmount" />
12 </resultMap> 12 </resultMap>
13 <sql id="Base_Column_List"> 13 <sql id="Base_Column_List">
14 - id, order_code, skup, goods_price, goods_amount, uid 14 + id, order_code, skup, goods_price, goods_amount, uid,coupon_cut_amount
15 </sql> 15 </sql>
16 16
17 <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 17 <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
@@ -85,7 +85,7 @@ @@ -85,7 +85,7 @@
85 uid, 85 uid,
86 </if> 86 </if>
87 <if test="couponCutAmount != null"> 87 <if test="couponCutAmount != null">
88 - coupons_cut_amount, 88 + coupon_cut_amount,
89 </if> 89 </if>
90 </trim> 90 </trim>
91 <trim prefix="values (" suffix=")" suffixOverrides=","> 91 <trim prefix="values (" suffix=")" suffixOverrides=",">
@@ -127,7 +127,7 @@ public class ChargeService { @@ -127,7 +127,7 @@ public class ChargeService {
127 chargeResult.setFinalAmount(newFinalAmount); 127 chargeResult.setFinalAmount(newFinalAmount);
128 128
129 CouponPayResult couponPayResult = CouponPayResult.builder().couponCode(userCouponsBo.getCouponCode()) 129 CouponPayResult couponPayResult = CouponPayResult.builder().couponCode(userCouponsBo.getCouponCode())
130 - .couponAmount(couponPayAmount).couponTitle(userCouponsBo.getCouponName()).build(); 130 + .couponAmount(couponPayAmount).couponTitle(userCouponsBo.getCouponName()).couponCount(1).build();
131 131
132 chargeResult.setCouponPayResult(couponPayResult); 132 chargeResult.setCouponPayResult(couponPayResult);
133 133
@@ -139,7 +139,7 @@ public class ChargeService { @@ -139,7 +139,7 @@ public class ChargeService {
139 139
140 private UserCouponsBo getOneUsableUserCouponBo(ChargeContext chargeContext) { 140 private UserCouponsBo getOneUsableUserCouponBo(ChargeContext chargeContext) {
141 ChargeParam chargeParam = chargeContext.getChargeParam(); 141 ChargeParam chargeParam = chargeContext.getChargeParam();
142 - List<UserCouponsBo> couponsBos = couponProxyService.checkUseCouponsAndGet(chargeParam.getUid(), chargeParam.getCouponCodes()); 142 + List<UserCouponsBo> couponsBos = couponProxyService.checkAndGetCoupons(chargeParam.getUid(), chargeParam.getCouponCodes());
143 if (CollectionUtils.isEmpty(couponsBos)) { 143 if (CollectionUtils.isEmpty(couponsBos)) {
144 logger.info("[{}] not find any coupons by couponCodes:{}", chargeParam.getUid(), chargeParam.getCouponCodes()); 144 logger.info("[{}] not find any coupons by couponCodes:{}", chargeParam.getUid(), chargeParam.getCouponCodes());
145 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD); 145 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
@@ -150,6 +150,7 @@ public class ChargeService { @@ -150,6 +150,7 @@ public class ChargeService {
150 //找到可用的券 150 //找到可用的券
151 List<UserCouponsBo> usableCoupons = couponMatchResults.stream().filter(result -> result.isUsable()).map(result -> result.getUserCouponsBo()).collect(Collectors.toList()); 151 List<UserCouponsBo> usableCoupons = couponMatchResults.stream().filter(result -> result.isUsable()).map(result -> result.getUserCouponsBo()).collect(Collectors.toList());
152 152
  153 + //当前只有使用一张券
153 if (CollectionUtils.isEmpty(usableCoupons) || usableCoupons.size() != 1) { 154 if (CollectionUtils.isEmpty(usableCoupons) || usableCoupons.size() != 1) {
154 logger.info("[{}] get coupons by couponCodes:{} is invalid", chargeParam.getUid(), chargeParam.getCouponCodes()); 155 logger.info("[{}] get coupons by couponCodes:{} is invalid", chargeParam.getUid(), chargeParam.getCouponCodes());
155 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD); 156 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
@@ -3,6 +3,7 @@ package com.yohoufo.order.service.impl; @@ -3,6 +3,7 @@ package com.yohoufo.order.service.impl;
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
4 import com.yoho.core.common.utils.DateUtil; 4 import com.yoho.core.common.utils.DateUtil;
5 import com.yoho.core.dal.datasource.annotation.Database; 5 import com.yoho.core.dal.datasource.annotation.Database;
  6 +import com.yoho.core.transaction.YHTxCoordinator;
6 import com.yoho.core.transaction.annoation.YHTransaction; 7 import com.yoho.core.transaction.annoation.YHTransaction;
7 import com.yoho.error.ServiceError; 8 import com.yoho.error.ServiceError;
8 import com.yoho.error.exception.ServiceException; 9 import com.yoho.error.exception.ServiceException;
@@ -39,6 +40,9 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService { @@ -39,6 +40,9 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService {
39 @Autowired 40 @Autowired
40 private CouponProxyService couponProxyService; 41 private CouponProxyService couponProxyService;
41 42
  43 + @Autowired
  44 + YHTxCoordinator tx;
  45 +
42 46
43 @YHTransaction 47 @YHTransaction
44 public BuyerOrderSubmitResult doSumbitOrder(OrderBuilder orderBuilder){ 48 public BuyerOrderSubmitResult doSumbitOrder(OrderBuilder orderBuilder){
@@ -56,6 +60,8 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService { @@ -56,6 +60,8 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService {
56 } 60 }
57 }catch (Exception ex){ 61 }catch (Exception ex){
58 62
  63 + tx.rollback();
  64 +
59 try{ 65 try{
60 boolean returnStorageFlag = productProxyService.returnStorage(orderBuilder.getSkup()); 66 boolean returnStorageFlag = productProxyService.returnStorage(orderBuilder.getSkup());
61 67
@@ -9,16 +9,19 @@ import com.yoho.core.transaction.annoation.TxCompensateArgs; @@ -9,16 +9,19 @@ import com.yoho.core.transaction.annoation.TxCompensateArgs;
9 import com.yoho.error.ServiceError; 9 import com.yoho.error.ServiceError;
10 import com.yoho.error.exception.ServiceException; 10 import com.yoho.error.exception.ServiceException;
11 import com.yohobuy.ufo.model.promotion.UserCouponsBo; 11 import com.yohobuy.ufo.model.promotion.UserCouponsBo;
  12 +import com.yohobuy.ufo.model.promotion.UserCouponsListBo;
12 import com.yohoufo.common.ApiResponse; 13 import com.yohoufo.common.ApiResponse;
13 import com.yohoufo.common.caller.UfoServiceCaller; 14 import com.yohoufo.common.caller.UfoServiceCaller;
  15 +import com.yohoufo.common.utils.DateUtil;
14 import com.yohoufo.order.model.bo.CouponBo; 16 import com.yohoufo.order.model.bo.CouponBo;
  17 +import org.apache.commons.collections.CollectionUtils;
15 import org.slf4j.Logger; 18 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory; 19 import org.slf4j.LoggerFactory;
17 import org.springframework.beans.factory.annotation.Autowired; 20 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.stereotype.Service; 21 import org.springframework.stereotype.Service;
19 -  
20 -import java.math.BigDecimal;  
21 import java.util.List; 22 import java.util.List;
  23 +import java.util.Objects;
  24 +import java.util.stream.Collectors;
22 25
23 /** 26 /**
24 * Created by jiexiang.wu on 2018/11/19. 27 * Created by jiexiang.wu on 2018/11/19.
@@ -38,40 +41,27 @@ public class CouponProxyService implements Compensator { @@ -38,40 +41,27 @@ public class CouponProxyService implements Compensator {
38 * @return 41 * @return
39 */ 42 */
40 public List<UserCouponsBo> getUserAvailableCoupons(int uid) { 43 public List<UserCouponsBo> getUserAvailableCoupons(int uid) {
41 - UserCouponsBo userCouponsBo = new UserCouponsBo();  
42 - userCouponsBo.setCouponCode("a100");  
43 - userCouponsBo.setCouponAmount(BigDecimal.valueOf(20));  
44 - userCouponsBo.setUseLimitType("2");  
45 - userCouponsBo.setUseLimitValue("299");  
46 - userCouponsBo.setProductLimitType("1");  
47 - userCouponsBo.setProductIdInclude(Lists.newArrayList(88888900));  
48 - userCouponsBo.setUseRule("满299可用");  
49 - userCouponsBo.setStartTime(1514739661);  
50 - userCouponsBo.setEndTime(1546275661);  
51 -  
52 - UserCouponsBo userCouponsBo1 = new UserCouponsBo();  
53 - userCouponsBo1.setCouponCode("a200");  
54 - userCouponsBo1.setCouponAmount(BigDecimal.valueOf(100));  
55 - userCouponsBo1.setUseLimitType("0");  
56 - userCouponsBo1.setUseLimitValue("");  
57 - userCouponsBo1.setProductLimitType("1");  
58 - userCouponsBo1.setProductIdInclude(Lists.newArrayList(88888900));  
59 - userCouponsBo1.setStartTime(1514739661);  
60 - userCouponsBo1.setEndTime(1546275661);  
61 - return Lists.newArrayList(userCouponsBo, userCouponsBo1); 44 + logger.info("[{}] getUserAvailableCoupons", uid);
  45 + //未使用的优惠券,包括还未生效的券
  46 + ApiResponse resp = serviceCaller.call("app.coupons.listNoUsed", uid);
  47 + UserCouponsListBo result = getResultFromApiResponse(resp);
  48 + if (Objects.isNull(result) || CollectionUtils.isEmpty(result.getCoupons())) {
  49 + return Lists.newArrayList();
  50 + }
  51 + //过滤未生效的券
  52 + int currentTime = DateUtil.getCurrentTimeSecond();
  53 + return result.getCoupons().stream().filter(e -> e.getStartTime() < currentTime && e.getEndTime() > currentTime).collect(Collectors.toList());
62 } 54 }
63 55
64 - public List<UserCouponsBo> checkUseCouponsAndGet(int uid, List<String> couponCodes) {  
65 - UserCouponsBo userCouponsBo = new UserCouponsBo();  
66 - userCouponsBo.setCouponCode("a100");  
67 - userCouponsBo.setCouponAmount(BigDecimal.valueOf(20));  
68 - userCouponsBo.setUseLimitType("2");  
69 - userCouponsBo.setUseLimitValue("299");  
70 - userCouponsBo.setProductLimitType("1");  
71 - userCouponsBo.setProductIdInclude(Lists.newArrayList(88888900));  
72 - userCouponsBo.setStartTime(1514739661);  
73 - userCouponsBo.setEndTime(1546275661);  
74 - return Lists.newArrayList(userCouponsBo); 56 + public List<UserCouponsBo> checkAndGetCoupons(int uid, List<String> couponCodes) {
  57 + logger.info("[{}] checkAndGetCoupons:{}", uid, couponCodes);
  58 + //未使用的优惠券,包括还未生效的券
  59 + ApiResponse resp = serviceCaller.call("app.coupons.checkAndGet", uid, couponCodes);
  60 + UserCouponsListBo result = getResultFromApiResponse(resp);
  61 + if (Objects.isNull(result) || CollectionUtils.isEmpty(result.getCoupons())) {
  62 + return Lists.newArrayList();
  63 + }
  64 + return result.getCoupons();
75 } 65 }
76 66
77 67
@@ -87,7 +77,8 @@ public class CouponProxyService implements Compensator { @@ -87,7 +77,8 @@ public class CouponProxyService implements Compensator {
87 @TxCompensateArgs("couponBo") CouponBo coupon) { 77 @TxCompensateArgs("couponBo") CouponBo coupon) {
88 logger.info("[{}] request to use coupon,couponBo is {}", orderCode, coupon); 78 logger.info("[{}] request to use coupon,couponBo is {}", orderCode, coupon);
89 ApiResponse resp = serviceCaller.call("app.coupons.use", uid, orderCode, Lists.newArrayList(coupon.getCouponCode())); 79 ApiResponse resp = serviceCaller.call("app.coupons.use", uid, orderCode, Lists.newArrayList(coupon.getCouponCode()));
90 - if (getResultFromApiResponse(resp) == false) { 80 + Boolean result = getResultFromApiResponse(resp);
  81 + if (result == null || result.booleanValue() == false) {
91 logger.warn("[{}] use couponBo fail,coupons is {}", orderCode, coupon); 82 logger.warn("[{}] use couponBo fail,coupons is {}", orderCode, coupon);
92 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD); 83 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
93 } 84 }
@@ -105,21 +96,22 @@ public class CouponProxyService implements Compensator { @@ -105,21 +96,22 @@ public class CouponProxyService implements Compensator {
105 public void orderCancelCoupon(int uid, long orderCode, CouponBo coupon) { 96 public void orderCancelCoupon(int uid, long orderCode, CouponBo coupon) {
106 logger.info("[{}] request to cancel coupon,couponBo is {}", orderCode, coupon); 97 logger.info("[{}] request to cancel coupon,couponBo is {}", orderCode, coupon);
107 ApiResponse resp = serviceCaller.call("app.coupons.cancel", uid, orderCode, Lists.newArrayList(coupon.getCouponCode())); 98 ApiResponse resp = serviceCaller.call("app.coupons.cancel", uid, orderCode, Lists.newArrayList(coupon.getCouponCode()));
108 - if (getResultFromApiResponse(resp) == false) { 99 + Boolean result = getResultFromApiResponse(resp);
  100 + if (result == null || result.booleanValue() == false) {
109 logger.warn("[{}] cancel couponBo fail,coupons is {}", orderCode, coupon); 101 logger.warn("[{}] cancel couponBo fail,coupons is {}", orderCode, coupon);
110 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD); 102 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
111 } 103 }
112 logger.info("[{}] cancel coupon success", orderCode); 104 logger.info("[{}] cancel coupon success", orderCode);
113 } 105 }
114 106
115 - private boolean getResultFromApiResponse(ApiResponse resp) { 107 + private <T> T getResultFromApiResponse(ApiResponse resp) {
116 if (resp == null) { 108 if (resp == null) {
117 - return false; 109 + return null;
118 } 110 }
119 if (resp.getCode() != 200 || resp.getData() == null) { 111 if (resp.getCode() != 200 || resp.getData() == null) {
120 - return false; 112 + return null;
121 } 113 }
122 - return (boolean) resp.getData(); 114 + return (T) resp.getData();
123 } 115 }
124 116
125 /** 117 /**
@@ -137,7 +129,7 @@ public class CouponProxyService implements Compensator { @@ -137,7 +129,7 @@ public class CouponProxyService implements Compensator {
137 JSONObject json = JSON.parseObject(message); 129 JSONObject json = JSON.parseObject(message);
138 uid = json.getIntValue("uid"); 130 uid = json.getIntValue("uid");
139 orderCode = json.getLongValue("orderCode"); 131 orderCode = json.getLongValue("orderCode");
140 - couponBo = json.getObject("coupons", CouponBo.class); 132 + couponBo = json.getObject("couponBo", CouponBo.class);
141 } catch (Exception ex) { 133 } catch (Exception ex) {
142 logger.warn("parse message to json error,message is {}", message, ex); 134 logger.warn("parse message to json error,message is {}", message, ex);
143 } 135 }
@@ -3,12 +3,12 @@ package com.yohoufo.order.service.support; @@ -3,12 +3,12 @@ package com.yohoufo.order.service.support;
3 import com.google.common.collect.Lists; 3 import com.google.common.collect.Lists;
4 import com.yohobuy.ufo.model.order.constants.OrderConstant; 4 import com.yohobuy.ufo.model.order.constants.OrderConstant;
5 import com.yohobuy.ufo.model.promotion.UserCouponsBo; 5 import com.yohobuy.ufo.model.promotion.UserCouponsBo;
  6 +import com.yohoufo.common.utils.DateUtil;
6 import com.yohoufo.order.charge.model.ChargeResult; 7 import com.yohoufo.order.charge.model.ChargeResult;
7 import com.yohoufo.order.charge.model.CouponMatchResult; 8 import com.yohoufo.order.charge.model.CouponMatchResult;
8 import com.yohoufo.order.charge.model.CouponPayResult; 9 import com.yohoufo.order.charge.model.CouponPayResult;
9 import com.yohoufo.order.model.response.CouponInfo; 10 import com.yohoufo.order.model.response.CouponInfo;
10 import com.yohoufo.order.model.response.ShoppingCoupon; 11 import com.yohoufo.order.model.response.ShoppingCoupon;
11 -import com.yohoufo.order.utils.DateUtil;  
12 import com.yohoufo.order.utils.MathUtils; 12 import com.yohoufo.order.utils.MathUtils;
13 import org.apache.commons.collections.CollectionUtils; 13 import org.apache.commons.collections.CollectionUtils;
14 import org.springframework.stereotype.Component; 14 import org.springframework.stereotype.Component;
@@ -39,8 +39,8 @@ public class CouponSupport { @@ -39,8 +39,8 @@ public class CouponSupport {
39 shoppingCoupon.setCouponCode(userCouponsBo.getCouponCode()); 39 shoppingCoupon.setCouponCode(userCouponsBo.getCouponCode());
40 shoppingCoupon.setCouponName(userCouponsBo.getCouponName()); 40 shoppingCoupon.setCouponName(userCouponsBo.getCouponName());
41 shoppingCoupon.setCouponValueStr(userCouponsBo.getCouponAmount().intValue() + ""); 41 shoppingCoupon.setCouponValueStr(userCouponsBo.getCouponAmount().intValue() + "");
42 - shoppingCoupon.setCouponValidity(DateUtil.format(DateUtil.getDateFromLong(userCouponsBo.getStartTime()), "yyyy.MM.dd") + "-" +  
43 - DateUtil.format(DateUtil.getDateFromLong(userCouponsBo.getEndTime()), "yyyy.MM.dd")); 42 + shoppingCoupon.setCouponValidity(DateUtil.getDateFormat(userCouponsBo.getStartTime()) + "-" +
  43 + DateUtil.getDateFormat(userCouponsBo.getEndTime()));
44 shoppingCoupon.setUseRuleStr(userCouponsBo.getUseRule()); 44 shoppingCoupon.setUseRuleStr(userCouponsBo.getUseRule());
45 45
46 shoppingCoupon.setSelected(OrderConstant.N_STR); 46 shoppingCoupon.setSelected(OrderConstant.N_STR);
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 -}  
1 package com.yohoufo.promotion.controller; 1 package com.yohoufo.promotion.controller;
2 2
3 3
  4 +import com.yoho.error.ServiceError;
  5 +import com.yoho.error.exception.ServiceException;
4 import com.yohobuy.ufo.model.promotion.UserCouponsListBo; 6 import com.yohobuy.ufo.model.promotion.UserCouponsListBo;
5 import com.yohoufo.common.ApiResponse; 7 import com.yohoufo.common.ApiResponse;
6 import com.yohoufo.promotion.service.ICouponService; 8 import com.yohoufo.promotion.service.ICouponService;
  9 +import org.apache.commons.collections.CollectionUtils;
7 import org.slf4j.Logger; 10 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory; 11 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
@@ -72,7 +75,7 @@ public class OrderCouponController { @@ -72,7 +75,7 @@ public class OrderCouponController {
72 * @param uid 75 * @param uid
73 * @return 76 * @return
74 */ 77 */
75 - @RequestMapping(params = "method=app.coupons.list") 78 + @RequestMapping(params = "method=app.coupons.listNoUsed")
76 @ResponseBody 79 @ResponseBody
77 public ApiResponse queryUserNoUsedCoupons(@RequestParam(value = "uid") Integer uid) { 80 public ApiResponse queryUserNoUsedCoupons(@RequestParam(value = "uid") Integer uid) {
78 81
@@ -83,4 +86,23 @@ public class OrderCouponController { @@ -83,4 +86,23 @@ public class OrderCouponController {
83 } 86 }
84 87
85 88
  89 + /**
  90 + * 优惠券列表(订单使用)
  91 + *
  92 + * @param uid
  93 + * @return
  94 + */
  95 + @RequestMapping(params = "method=app.coupons.checkAndGet")
  96 + @ResponseBody
  97 + public ApiResponse checkAndGetCoupons(@RequestParam(value = "uid") int uid,
  98 + @RequestParam(value = "couponCodes") List<String> couponCodes) {
  99 +
  100 + logger.info("checkAndGet coupons, uid: {},couponCodes:{}", uid, couponCodes);
  101 + if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
  102 + throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
  103 + }
  104 + UserCouponsListBo couponBoList = couponService.checkAndGetCoupons(uid, couponCodes);
  105 + logger.info("checkAndGet coupons, uid: {},couponCodes:{},success", uid, couponCodes);
  106 + return new ApiResponse.ApiResponseBuilder().code(200).data(couponBoList).build();
  107 + }
86 } 108 }
@@ -47,5 +47,13 @@ public interface ICouponService { @@ -47,5 +47,13 @@ public interface ICouponService {
47 */ 47 */
48 public UserCouponsListBo queryUserNoUsedCoupons(Integer uid); 48 public UserCouponsListBo queryUserNoUsedCoupons(Integer uid);
49 49
  50 + /**
  51 + * 校验券并返回券信息
  52 + *
  53 + * @param uid
  54 + * @param couponCodes
  55 + * @return
  56 + */
  57 + UserCouponsListBo checkAndGetCoupons(int uid, List<String> couponCodes);
50 58
51 } 59 }
@@ -224,9 +224,9 @@ public class CouponServiceImpl implements ICouponService { @@ -224,9 +224,9 @@ public class CouponServiceImpl implements ICouponService {
224 List<String> couponIdList = list.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList()); 224 List<String> couponIdList = list.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
225 List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponIdList); 225 List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponIdList);
226 Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> { 226 Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
227 - if (coupon.getStatus()!=null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()){ 227 + if (coupon.getStatus() != null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()) {
228 return true; 228 return true;
229 - }else{ 229 + } else {
230 return false; 230 return false;
231 } 231 }
232 }).collect(Collectors.toMap(Coupon::getId, Function.identity())); 232 }).collect(Collectors.toMap(Coupon::getId, Function.identity()));
@@ -234,7 +234,9 @@ public class CouponServiceImpl implements ICouponService { @@ -234,7 +234,9 @@ public class CouponServiceImpl implements ICouponService {
234 // 优惠券商品限制 key=couponId, value=ProductId集合 234 // 优惠券商品限制 key=couponId, value=ProductId集合
235 Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(couponList); 235 Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(couponList);
236 236
237 - List<UserCouponsBo> couponBoList = list.stream().map(userCoupon -> { 237 + List<UserCouponsBo> couponBoList = list.stream().filter(userCoupon -> {
  238 + return couponMap.containsKey(userCoupon.getCouponId());
  239 + }).map(userCoupon -> {
238 240
239 Coupon coupon = couponMap.get(userCoupon.getCouponId()); 241 Coupon coupon = couponMap.get(userCoupon.getCouponId());
240 List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId()); 242 List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId());
@@ -291,12 +293,14 @@ public class CouponServiceImpl implements ICouponService { @@ -291,12 +293,14 @@ public class CouponServiceImpl implements ICouponService {
291 } 293 }
292 }).collect(Collectors.toMap(Coupon::getId, Function.identity())); 294 }).collect(Collectors.toMap(Coupon::getId, Function.identity()));
293 295
294 - List<CouponInfo> couponInfoList = list.stream().map(userCoupon -> { 296 + List<CouponInfo> couponInfoList = list.stream().filter(userCoupon -> {
  297 + return couponMap.containsKey(userCoupon.getCouponId());
  298 + }).map(userCoupon -> {
295 299
296 Coupon coupon = couponMap.get(userCoupon.getCouponId()); 300 Coupon coupon = couponMap.get(userCoupon.getCouponId());
297 301
298 // 单个获取couponType 302 // 单个获取couponType
299 - CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType()!=null ? userCoupon.getCouponType().intValue() : null); 303 + CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
300 304
301 return CouponConvert.convertCouponInfo(userCoupon, coupon, couponType); 305 return CouponConvert.convertCouponInfo(userCoupon, coupon, couponType);
302 306
@@ -314,5 +318,62 @@ public class CouponServiceImpl implements ICouponService { @@ -314,5 +318,62 @@ public class CouponServiceImpl implements ICouponService {
314 318
315 int cnt = userCouponMapper.selectCntUsableCouponByUid(uid, DateUtil.getCurrentTimeSecond()); 319 int cnt = userCouponMapper.selectCntUsableCouponByUid(uid, DateUtil.getCurrentTimeSecond());
316 return cnt; 320 return cnt;
  321 +
  322 + }
  323 +
  324 + @Override
  325 + public UserCouponsListBo checkAndGetCoupons(int uid, List<String> couponCodes) {
  326 + //数据校验
  327 + if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
  328 + logger.warn("check coupon use param error:{}, {}", uid, couponCodes);
  329 + throw new ServiceException(ServiceError.PROMOTION_PARAM_IS_ERROR);
  330 + }
  331 +
  332 + // 获取用户券记录
  333 + List<UserCoupon> userCoupons = userCouponMapper.selectByUidAndCouponCodes(uid, couponCodes);
  334 +
  335 + int time = DateUtil.getCurrentTimeSecond();
  336 +
  337 + List<UserCouponsBo> couponBoList = couponCodes.stream().map(couponCode -> {
  338 +
  339 + UserCoupon userCoupon = userCoupons.stream().filter(e -> couponCode.equals(e.getCouponCode())).findAny().orElse(null);
  340 + logger.info("user coupon {},{},{}", uid, couponCode, userCoupon);
  341 + if (userCoupon == null) {
  342 + logger.warn("not find user coupon by{},{}", uid, couponCode);
  343 + throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_YOUS);
  344 + }
  345 + //判断券状态
  346 + if (userCoupon.getStatus().intValue() != CouponUseStatusEnum.NOT_USED.getCode()) {
  347 + logger.warn("user this coupon is used:{},{}", userCoupon.getUid(), userCoupon.getCouponCode());
  348 + throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_USE_DOUBLE);
  349 + }
  350 +
  351 + //判断生失效时间
  352 + if (userCoupon.getStartTime() > time || userCoupon.getEndTime() < time) {
  353 + logger.warn("coupon has expire or not arrive time:{},{},{}", userCoupon.getStartTime(), userCoupon.getEndTime(), time);
  354 + throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ALLOW_OR_EXPIRE);
  355 + }
  356 +
  357 + //券模板的状态
  358 + Coupon coupon = couponCacheService.getCouponWithCache(userCoupon.getCouponToken());
  359 + if (coupon == null) {
  360 + logger.warn("coupons is null");
  361 + throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_EXISTS);
  362 + }
  363 + if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()) {
  364 + logger.warn("coupon status can't be use:{},{}", coupon.getId(), coupon.getStatus());
  365 + throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
  366 + }
  367 + //所有的校验都通过了,生成bo
  368 + Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(Lists.newArrayList(coupon));
  369 + List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId());
  370 +
  371 + UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon);
  372 + couponBo.setProductIdInclude(productIds);
  373 +
  374 + return couponBo;
  375 +
  376 + }).collect(Collectors.toList());
  377 + return UserCouponsListBo.builder().coupons(couponBoList).build();
317 } 378 }
318 } 379 }