Authored by tanling

优惠券使用退还

1 package com.yohoufo.dal.promotion; 1 package com.yohoufo.dal.promotion;
2 2
3 3
  4 +import com.yohoufo.dal.promotion.model.CouponCancelUseDO;
  5 +import com.yohoufo.dal.promotion.model.CouponUseDO;
4 import com.yohoufo.dal.promotion.model.UserCoupon; 6 import com.yohoufo.dal.promotion.model.UserCoupon;
5 import org.apache.ibatis.annotations.Param; 7 import org.apache.ibatis.annotations.Param;
6 8
@@ -17,6 +19,16 @@ public interface UserCouponMapper { @@ -17,6 +19,16 @@ public interface UserCouponMapper {
17 19
18 UserCoupon selectByPrimaryKey(Integer id); 20 UserCoupon selectByPrimaryKey(Integer id);
19 21
  22 +
  23 + int updateCoupon2Use(CouponUseDO couponUseDO);
  24 +
  25 +
  26 + int updateCouponNotUse(CouponCancelUseDO cancelUseDO);
  27 +
  28 +
  29 + List<UserCoupon> selectByUidAndCouponCodes(@Param("uid") Integer uid, @Param("couponCodes") List<String> couponCode);
  30 +
  31 +
20 List<UserCoupon> selectUsableCouponByUid(@Param("uid") Integer uid, @Param("now") int now); 32 List<UserCoupon> selectUsableCouponByUid(@Param("uid") Integer uid, @Param("now") int now);
21 33
22 int selectCntUsableCouponByUid(@Param("uid") Integer uid, @Param("now") int now); 34 int selectCntUsableCouponByUid(@Param("uid") Integer uid, @Param("now") int now);
  1 +package com.yohoufo.dal.promotion.model;
  2 +
  3 +import java.util.List;
  4 +
  5 +public class CouponCancelUseDO {
  6 +
  7 + private int uid;
  8 +
  9 + private List<String> couponCodes;
  10 +
  11 + private long orderCode;
  12 +
  13 + public CouponCancelUseDO(int uid, List<String> couponCodes, long orderCode) {
  14 + this.uid = uid;
  15 + this.couponCodes = couponCodes;
  16 + this.orderCode = orderCode;
  17 + }
  18 +
  19 + public int getUid() {
  20 + return uid;
  21 + }
  22 +
  23 + public void setUid(int uid) {
  24 + this.uid = uid;
  25 + }
  26 +
  27 + public List<String> getCouponCodes() {
  28 + return couponCodes;
  29 + }
  30 +
  31 + public void setCouponCodes(List<String> couponCodes) {
  32 + this.couponCodes = couponCodes;
  33 + }
  34 +
  35 + public long getOrderCode() {
  36 + return orderCode;
  37 + }
  38 +
  39 + public void setOrderCode(long orderCode) {
  40 + this.orderCode = orderCode;
  41 + }
  42 +}
  1 +package com.yohoufo.dal.promotion.model;
  2 +
  3 +import java.util.List;
  4 +
  5 +public class CouponUseDO {
  6 +
  7 + private int uid;
  8 +
  9 + private List<String> couponCodes;
  10 +
  11 + private long orderCode;
  12 +
  13 + private int useTime;
  14 +
  15 + private CouponUseDO(Builder builder) {
  16 + setUid(builder.uid);
  17 + setCouponCodes(builder.couponCodes);
  18 + setOrderCode(builder.orderCode);
  19 + setUseTime(builder.useTime);
  20 + }
  21 +
  22 + public static Builder newBuilder() {
  23 + return new Builder();
  24 + }
  25 +
  26 + public int getUid() {
  27 + return uid;
  28 + }
  29 +
  30 + public void setUid(int uid) {
  31 + this.uid = uid;
  32 + }
  33 +
  34 + public List<String> getCouponCodes() {
  35 + return couponCodes;
  36 + }
  37 +
  38 + public void setCouponCodes(List<String> couponCodes) {
  39 + this.couponCodes = couponCodes;
  40 + }
  41 +
  42 + public long getOrderCode() {
  43 + return orderCode;
  44 + }
  45 +
  46 + public void setOrderCode(long orderCode) {
  47 + this.orderCode = orderCode;
  48 + }
  49 +
  50 + public int getUseTime() {
  51 + return useTime;
  52 + }
  53 +
  54 + public void setUseTime(int useTime) {
  55 + this.useTime = useTime;
  56 + }
  57 +
  58 +
  59 + public static final class Builder {
  60 + private int uid;
  61 + private List<String> couponCodes;
  62 + private long orderCode;
  63 + private int useTime;
  64 +
  65 + private Builder() {
  66 + }
  67 +
  68 + public Builder uid(int val) {
  69 + uid = val;
  70 + return this;
  71 + }
  72 +
  73 + public Builder couponCodes(List<String> val) {
  74 + couponCodes = val;
  75 + return this;
  76 + }
  77 +
  78 + public Builder orderCode(long val) {
  79 + orderCode = val;
  80 + return this;
  81 + }
  82 +
  83 + public Builder useTime(int val) {
  84 + useTime = val;
  85 + return this;
  86 + }
  87 +
  88 + public CouponUseDO build() {
  89 + return new CouponUseDO(this);
  90 + }
  91 + }
  92 +}
@@ -28,6 +28,20 @@ @@ -28,6 +28,20 @@
28 </select> 28 </select>
29 29
30 30
  31 +
  32 + <select id="selectByUidAndCouponCodes" resultMap="BaseResultMap">
  33 + select
  34 + <include refid="Base_Column_List"/>
  35 + from user_coupon
  36 + where uid = #{uid}
  37 + AND coupon_code IN
  38 + <foreach collection="couponCodes" item="couponCode" open="(" separator="," close=")">
  39 + #{couponCode,jdbcType=VARCHAR}
  40 + </foreach>
  41 + </select>
  42 +
  43 +
  44 +
31 <select id="selectByUidAndToken" resultMap="BaseResultMap" > 45 <select id="selectByUidAndToken" resultMap="BaseResultMap" >
32 select 46 select
33 <include refid="Base_Column_List" /> 47 <include refid="Base_Column_List" />
@@ -200,4 +214,42 @@ @@ -200,4 +214,42 @@
200 create_time = #{createTime,jdbcType=INTEGER} 214 create_time = #{createTime,jdbcType=INTEGER}
201 where id = #{id,jdbcType=INTEGER} 215 where id = #{id,jdbcType=INTEGER}
202 </update> 216 </update>
  217 +
  218 +
  219 + <update id="updateCouponNotUse" parameterType="com.yohoufo.dal.promotion.model.CouponCancelUseDO">
  220 + update user_coupon
  221 + <set>
  222 + status = 0,use_time = 0, order_code =0
  223 + </set>
  224 + where uid= #{uid}
  225 + and order_code= #{orderCode}
  226 + and status = 1
  227 + and coupon_code in
  228 + <foreach collection="couponCodes" index="index" item="couponCode"
  229 + open="(" separator="," close=")">
  230 + #{couponCode}
  231 + </foreach>
  232 + </update>
  233 +
  234 + <update id="updateCoupon2Use" parameterType="com.yohoufo.dal.promotion.model.CouponUseDO">
  235 + update user_coupon
  236 + <set>
  237 + <if test="orderCode != null">
  238 + order_code= #{orderCode},
  239 + </if>
  240 + <if test="useTime != null">
  241 + use_time= #{useTime},
  242 + </if>
  243 + status=1
  244 + </set>
  245 + where uid= #{uid}
  246 + and coupon_code in
  247 + <foreach collection="couponCodes" index="index" item="couponCode"
  248 + open="(" separator="," close=")">
  249 + #{couponCode}
  250 + </foreach>
  251 + and status = 0
  252 + and order_code= 0
  253 + </update>
  254 +
203 </mapper> 255 </mapper>
  1 +package com.yohoufo.promotion.common;
  2 +
  3 +public enum CouponUseStatusEnum {
  4 +
  5 + NOT_USED(0, "未使用"),
  6 +
  7 + USED(1, "已使用"),
  8 +
  9 + /**
  10 + * 已作废
  11 + *
  12 + * @使用场景 1、生日礼包最多使用一张其它会作废
  13 + * 2、新客礼包最多使用一张它会作废
  14 + */
  15 + INVALID(2, "已作废");
  16 +
  17 + /**
  18 + * 编码
  19 + */
  20 + private int code;
  21 +
  22 + /**
  23 + * 描述
  24 + */
  25 + private String desc;
  26 +
  27 + CouponUseStatusEnum(int code, String desc) {
  28 + this.code = code;
  29 + this.desc = desc;
  30 + }
  31 +
  32 + public int getCode() {
  33 + return code;
  34 + }
  35 +
  36 + public String getDesc() {
  37 + return desc;
  38 + }
  39 +
  40 +}
  1 +package com.yohoufo.promotion.controller;
  2 +
  3 +
  4 +import com.yohoufo.common.ApiResponse;
  5 +import com.yohoufo.promotion.model.response.CouponInfo;
  6 +import com.yohoufo.promotion.service.ICouponService;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.web.bind.annotation.RequestMapping;
  11 +import org.springframework.web.bind.annotation.RequestParam;
  12 +import org.springframework.web.bind.annotation.ResponseBody;
  13 +import org.springframework.web.bind.annotation.RestController;
  14 +
  15 +import java.util.List;
  16 +
  17 +@RestController
  18 +@RequestMapping(value = "/orderCoupon")
  19 +public class OrderCouponController {
  20 +
  21 +
  22 + private final Logger logger = LoggerFactory.getLogger(getClass());
  23 +
  24 + @Autowired
  25 + ICouponService couponService;
  26 +
  27 + /**
  28 + * 使用优惠券(订单使用)
  29 + * @param uid
  30 + * @param couponCodes
  31 + * @param orderCode
  32 + * @return
  33 + */
  34 + @RequestMapping(params = "method=app.coupons.use")
  35 + @ResponseBody
  36 + public ApiResponse useCoupon(@RequestParam(value = "uid") Integer uid,
  37 + @RequestParam(value = "couponCodes") List<String> couponCodes,
  38 + @RequestParam(value = "orderCode") long orderCode) {
  39 +
  40 + logger.info("use coupons, uid: {},couponCodes: {}, orderCode :{}", uid, couponCodes, orderCode);
  41 + boolean useSuccess = couponService.useCoupon(uid, couponCodes, orderCode);
  42 + logger.info("use coupons, uid: {},couponCodes: {}, orderCode :{},result: {}", uid, couponCodes, orderCode, useSuccess);
  43 + return new ApiResponse.ApiResponseBuilder().code(200).data(useSuccess).build();
  44 + }
  45 +
  46 +
  47 + /**
  48 + * 使用优惠券(订单退还)
  49 + * @param uid
  50 + * @param couponCodes
  51 + * @param orderCode
  52 + * @return
  53 + */
  54 + @RequestMapping(params = "method=app.coupons.cancel")
  55 + @ResponseBody
  56 + public ApiResponse cancelCoupon(@RequestParam(value = "uid") Integer uid,
  57 + @RequestParam(value = "couponCodes") List<String> couponCodes,
  58 + @RequestParam(value = "orderCode") long orderCode) {
  59 +
  60 + logger.info("cancel coupons, uid: {},couponCodes: {}, orderCode :{}", uid, couponCodes, orderCode);
  61 + boolean cancelSuccess = couponService.cancelCoupon(uid, couponCodes, orderCode);
  62 + logger.info("cancel coupons, uid: {},couponCodes: {}, orderCode :{},result: {}", uid, couponCodes, orderCode, cancelSuccess);
  63 + return new ApiResponse.ApiResponseBuilder().code(200).data(cancelSuccess).build();
  64 + }
  65 +
  66 +
  67 +}
1 package com.yohoufo.promotion.controller; 1 package com.yohoufo.promotion.controller;
2 2
3 import com.yoho.core.rest.annotation.ServiceDesc; 3 import com.yoho.core.rest.annotation.ServiceDesc;
4 -import com.yoho.error.ServiceError;  
5 -import com.yoho.error.exception.ServiceException;  
6 -import com.yohobuy.ufo.model.promotion.request.CouponCancelUseReq;  
7 -import com.yohobuy.ufo.model.promotion.request.CouponUseReq;  
8 -import com.yohobuy.ufo.model.promotion.request.UserCouponListReq;  
9 -import com.yohobuy.ufo.model.promotion.UserCouponsListBo;  
10 -import org.apache.commons.collections.CollectionUtils;  
11 import org.slf4j.Logger; 4 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory; 5 import org.slf4j.LoggerFactory;
13 import org.springframework.stereotype.Controller; 6 import org.springframework.stereotype.Controller;
14 -import org.springframework.web.bind.annotation.RequestBody;  
15 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.ResponseBody; 8 import org.springframework.web.bind.annotation.ResponseBody;
17 9
18 -import java.util.List;  
19 -  
20 /** 10 /**
21 * Created by jiexiang.wu on 2018/11/19. 11 * Created by jiexiang.wu on 2018/11/19.
22 * 内部接口 12 * 内部接口
@@ -28,88 +18,79 @@ public class UserCouponController { @@ -28,88 +18,79 @@ public class UserCouponController {
28 18
29 private static Logger logger = LoggerFactory.getLogger(UserCouponController.class); 19 private static Logger logger = LoggerFactory.getLogger(UserCouponController.class);
30 20
31 - /**  
32 - * 新获取可用的优惠券接口,包括基本信息,以及优惠规则。(供购物车使用)  
33 - *  
34 - * @param req 包含用户id的请求  
35 - * @return  
36 - * @see [类、类#方法、类#成员]  
37 - */  
38 - @RequestMapping(value = "/getUserAvailableCoupons")  
39 - @ResponseBody  
40 - public UserCouponsListBo getUserAvailableCoupons(@RequestBody UserCouponListReq req) {  
41 - logger.info("queryUserNoUsedCoupons request is {}", req);  
42 -  
43 - if (req == null || (req.getUid() <= 0)) {  
44 - logger.warn("queryUserNoUsedCoupons RequestBody is wrong , the request is {} ", req);  
45 - throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);  
46 - }  
47 -  
48 - //todo  
49 - return null;  
50 - }  
51 -  
52 - /**  
53 - * 查询并校验优惠券是否能用  
54 - * 提供给order模块调用判断优惠券是否可以使用  
55 - * 订单使用前调用校验优惠券是否可用  
56 - *  
57 - * @param req  
58 - * @return  
59 - */  
60 - @RequestMapping(value = "/checkUseCouponsAndGet")  
61 - @ResponseBody  
62 - public UserCouponsListBo checkUseCouponsAndGet(@RequestBody UserCouponListReq req) {  
63 - logger.info("checkUseCouponsAndGet req is:{}", req);  
64 - if (req == null || req.getUid() <= 0 || CollectionUtils.isEmpty(req.getCouponCodes())) {  
65 - throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);  
66 - }  
67 - //todo  
68 - return null;  
69 - } 21 +// /**
  22 +// * 新获取可用的优惠券接口,包括基本信息,以及优惠规则。(供购物车使用)
  23 +// *
  24 +// * @param req 包含用户id的请求
  25 +// * @return
  26 +// * @see [类、类#方法、类#成员]
  27 +// */
  28 +// @RequestMapping(value = "/getUserAvailableCoupons")
  29 +// @ResponseBody
  30 +// public UserCouponsListBo getUserAvailableCoupons(@RequestBody UserCouponListReq req) {
  31 +// logger.info("queryUserNoUsedCoupons request is {}", req);
  32 +//
  33 +// if (req == null || (req.getUid() <= 0)) {
  34 +// logger.warn("queryUserNoUsedCoupons RequestBody is wrong , the request is {} ", req);
  35 +// throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
  36 +// }
  37 +//
  38 +// //todo
  39 +// return null;
  40 +// }
  41 +//
  42 +// /**
  43 +// * 查询并校验优惠券是否能用
  44 +// * 提供给order模块调用判断优惠券是否可以使用
  45 +// * 订单使用前调用校验优惠券是否可用
  46 +// *
  47 +// * @param req
  48 +// * @return
  49 +// */
  50 +// @RequestMapping(value = "/checkUseCouponsAndGet")
  51 +// @ResponseBody
  52 +// public UserCouponsListBo checkUseCouponsAndGet(@RequestBody UserCouponListReq req) {
  53 +// logger.info("checkUseCouponsAndGet req is:{}", req);
  54 +// if (req == null || req.getUid() <= 0 || CollectionUtils.isEmpty(req.getCouponCodes())) {
  55 +// throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
  56 +// }
  57 +// //todo
  58 +// return null;
  59 +// }
70 60
71 /** 61 /**
72 * 订单使用优惠券(订单中心调用) 62 * 订单使用优惠券(订单中心调用)
73 * 63 *
74 - * @param useReq  
75 * @return 64 * @return
76 */ 65 */
77 @RequestMapping(value = "/orderUseCoupon") 66 @RequestMapping(value = "/orderUseCoupon")
78 @ResponseBody 67 @ResponseBody
79 - public Boolean orderUseCoupon(@RequestBody CouponUseReq useReq) {  
80 - logger.info("use coupon request is {}", useReq);  
81 - if (useReq.getUid() <= 0 || CollectionUtils.isEmpty(useReq.getCouponCodes()) || useReq.getOrderCode() <= 0) {  
82 - logger.info("use coupon request params error {}", useReq);  
83 - throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);  
84 - }  
85 -// UseCouponBo useCouponBo = new UseCouponBo(useReq.getUid(), useReq.getOrderCode(), useReq.getCouponCodes());  
86 -// return iCouponService.useCoupons(useCouponBo);  
87 - //todo 68 + public Boolean orderUseCoupon() {
88 return null; 69 return null;
89 } 70 }
90 71
91 72
92 - /**  
93 - * 取消订单优惠券的使用  
94 - *  
95 - * @param couponOrderCancelUseReq  
96 - * @return  
97 - */  
98 - @RequestMapping(value = "/cancelCouponUse")  
99 - @ResponseBody  
100 - public boolean cancelOrderUseCoupon(@RequestBody CouponCancelUseReq couponOrderCancelUseReq) {  
101 -  
102 - int uid = couponOrderCancelUseReq.getUid();  
103 - long orderCode = couponOrderCancelUseReq.getOrderCode();  
104 - List<String> couponCodes = couponOrderCancelUseReq.getCouponCodes();  
105 - if (uid <= 0 || orderCode <= 0 || CollectionUtils.isEmpty(couponCodes)) {  
106 - logger.warn("cancel order use coupons req param err:{}", couponOrderCancelUseReq);  
107 - throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);  
108 - }  
109 -  
110 -// CancelUseCouponBo cancelUseCouponBo = new CancelUseCouponBo(uid, orderCode, couponCodes);  
111 -// return iCouponService.cancelOrderUseCoupon(cancelUseCouponBo);  
112 - //todo  
113 - return false;  
114 - } 73 +// /**
  74 +// * 取消订单优惠券的使用
  75 +// *
  76 +// * @param couponOrderCancelUseReq
  77 +// * @return
  78 +// */
  79 +// @RequestMapping(value = "/cancelCouponUse")
  80 +// @ResponseBody
  81 +// public boolean cancelOrderUseCoupon(@RequestBody CouponCancelUseReq couponOrderCancelUseReq) {
  82 +//
  83 +// int uid = couponOrderCancelUseReq.getUid();
  84 +// long orderCode = couponOrderCancelUseReq.getOrderCode();
  85 +// List<String> couponCodes = couponOrderCancelUseReq.getCouponCodes();
  86 +// if (uid <= 0 || orderCode <= 0 || CollectionUtils.isEmpty(couponCodes)) {
  87 +// logger.warn("cancel order use coupons req param err:{}", couponOrderCancelUseReq);
  88 +// throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
  89 +// }
  90 +//
  91 +//// CancelUseCouponBo cancelUseCouponBo = new CancelUseCouponBo(uid, orderCode, couponCodes);
  92 +//// return iCouponService.cancelOrderUseCoupon(cancelUseCouponBo);
  93 +// //todo
  94 +// return false;
  95 +// }
115 } 96 }
@@ -81,6 +81,7 @@ public class CouponConvert { @@ -81,6 +81,7 @@ public class CouponConvert {
81 // TODO 81 // TODO
82 userCoupon.setEndTime(couponAndType.getCoupon().getEndTime()); 82 userCoupon.setEndTime(couponAndType.getCoupon().getEndTime());
83 userCoupon.setCreateTime(now); 83 userCoupon.setCreateTime(now);
  84 + userCoupon.setOrderCode(0l);
84 userCoupon.setStatus(UserCouponsStatusEnum.NO_USE.getCode()); 85 userCoupon.setStatus(UserCouponsStatusEnum.NO_USE.getCode());
85 userCoupon.setCouponToken(couponAndType.getCoupon().getCouponToken()); 86 userCoupon.setCouponToken(couponAndType.getCoupon().getCouponToken());
86 return userCoupon; 87 return userCoupon;
@@ -20,4 +20,24 @@ public interface ICouponService { @@ -20,4 +20,24 @@ public interface ICouponService {
20 * @return 20 * @return
21 */ 21 */
22 public String senCoupon(Integer uid, String couponToken); 22 public String senCoupon(Integer uid, String couponToken);
  23 +
  24 + /**
  25 + * 使用优惠券
  26 + * @param uid
  27 + * @param couponCodes
  28 + * @param orderCode
  29 + * @return
  30 + */
  31 + public boolean useCoupon(Integer uid, List<String> couponCodes, long orderCode);
  32 +
  33 + /**
  34 + * 退还优惠券
  35 + * @param uid
  36 + * @param couponCodes
  37 + * @param orderCode
  38 + * @return
  39 + */
  40 + public boolean cancelCoupon(Integer uid, List<String> couponCodes, long orderCode);
  41 +
  42 +
23 } 43 }
@@ -6,15 +6,14 @@ import com.yoho.error.exception.ServiceException; @@ -6,15 +6,14 @@ import com.yoho.error.exception.ServiceException;
6 import com.yohoufo.common.utils.DateUtil; 6 import com.yohoufo.common.utils.DateUtil;
7 import com.yohoufo.dal.promotion.CouponMapper; 7 import com.yohoufo.dal.promotion.CouponMapper;
8 import com.yohoufo.dal.promotion.UserCouponMapper; 8 import com.yohoufo.dal.promotion.UserCouponMapper;
9 -import com.yohoufo.dal.promotion.model.Coupon;  
10 -import com.yohoufo.dal.promotion.model.CouponAndType;  
11 -import com.yohoufo.dal.promotion.model.CouponType;  
12 -import com.yohoufo.dal.promotion.model.UserCoupon; 9 +import com.yohoufo.dal.promotion.model.*;
  10 +import com.yohoufo.promotion.common.CouponUseStatusEnum;
13 import com.yohoufo.promotion.common.CouponsStatusEnum; 11 import com.yohoufo.promotion.common.CouponsStatusEnum;
14 import com.yohoufo.promotion.convert.CouponConvert; 12 import com.yohoufo.promotion.convert.CouponConvert;
15 import com.yohoufo.promotion.model.response.CouponInfo; 13 import com.yohoufo.promotion.model.response.CouponInfo;
16 import com.yohoufo.promotion.service.*; 14 import com.yohoufo.promotion.service.*;
17 import org.apache.commons.collections.CollectionUtils; 15 import org.apache.commons.collections.CollectionUtils;
  16 +import org.apache.commons.lang3.StringUtils;
18 import org.slf4j.Logger; 17 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory; 18 import org.slf4j.LoggerFactory;
20 import org.springframework.beans.factory.annotation.Autowired; 19 import org.springframework.beans.factory.annotation.Autowired;
@@ -88,6 +87,75 @@ public class CouponServiceImpl implements ICouponService { @@ -88,6 +87,75 @@ public class CouponServiceImpl implements ICouponService {
88 return couponCode; 87 return couponCode;
89 } 88 }
90 89
  90 + @Override
  91 + public boolean useCoupon(Integer uid, List<String> couponCodes, long orderCode) {
  92 +
  93 + if (uid == null || uid.intValue() <=0
  94 + || CollectionUtils.isEmpty(couponCodes)
  95 + || orderCode < 0){
  96 + logger.warn("useCoupon param invalidate");
  97 + throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
  98 + }
  99 +
  100 + if (checkUserCoupon(uid, couponCodes, orderCode)){
  101 + return false;
  102 + }
  103 +
  104 + CouponUseDO couponUseDO = CouponUseDO.newBuilder()
  105 + .uid(uid)
  106 + .orderCode(orderCode)
  107 + .useTime(DateUtil.getCurrentTimeSecond())
  108 + .couponCodes(couponCodes)
  109 + .build();
  110 + int updateRow = userCouponMapper.updateCoupon2Use(couponUseDO);
  111 +
  112 + if (updateRow >= couponCodes.size()) {
  113 + logger.info("order use coupons success:{},{},{}",uid, couponCodes, orderCode);
  114 + return true;
  115 + } else {
  116 + logger.warn("order use coupons fail:{},{},{}", uid, couponCodes, orderCode);
  117 + return false;
  118 + }
  119 + }
  120 +
  121 + @Override
  122 + public boolean cancelCoupon(Integer uid, List<String> couponCodes, long orderCode) {
  123 +
  124 + if (uid == null || uid.intValue() <=0
  125 + || CollectionUtils.isEmpty(couponCodes)
  126 + || orderCode < 0){
  127 + logger.warn("cancelCoupon param invalidate");
  128 + throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
  129 + }
  130 +
  131 + CouponCancelUseDO cancelUseDO = new CouponCancelUseDO(uid, couponCodes, orderCode);
  132 + int row = userCouponMapper.updateCouponNotUse(cancelUseDO);
  133 + if (row >= couponCodes.size()) {
  134 + logger.info("cancel order use coupon succ {},{},{}:{}", uid, couponCodes, orderCode, row);
  135 + return true;
  136 + } else {
  137 + logger.info("cancel order use coupon fail {},{},{}:{}", uid, couponCodes, orderCode, row);
  138 + return false;
  139 + }
  140 +
  141 + }
  142 +
  143 + private boolean checkUserCoupon(Integer uid, List<String> couponCodes, long orderCode) {
  144 + List<UserCoupon> userCoupons = userCouponMapper.selectByUidAndCouponCodes(uid, couponCodes);
  145 + if (CollectionUtils.isEmpty(userCoupons) || userCoupons.size() != couponCodes.size()){
  146 + logger.warn("user {},{} coupon not enough {},{} ", uid,orderCode, couponCodes.size(), userCoupons.size());
  147 + return true;
  148 + }
  149 +
  150 + return userCoupons.stream().anyMatch(userCoupon -> {
  151 + if (userCoupon.getStatus().intValue() != CouponUseStatusEnum.NOT_USED.getCode()){
  152 + return true;
  153 + }else{
  154 + return false;
  155 + }
  156 + });
  157 + }
  158 +
91 159
92 /** 160 /**
93 * 优惠券数量校验 161 * 优惠券数量校验