...
|
...
|
@@ -9,16 +9,19 @@ import com.yoho.core.transaction.annoation.TxCompensateArgs; |
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
|
|
|
import com.yohobuy.ufo.model.promotion.UserCouponsListBo;
|
|
|
import com.yohoufo.common.ApiResponse;
|
|
|
import com.yohoufo.common.caller.UfoServiceCaller;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.order.model.bo.CouponBo;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by jiexiang.wu on 2018/11/19.
|
...
|
...
|
@@ -38,40 +41,27 @@ public class CouponProxyService implements Compensator { |
|
|
* @return
|
|
|
*/
|
|
|
public List<UserCouponsBo> getUserAvailableCoupons(int uid) {
|
|
|
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(1514739661);
|
|
|
userCouponsBo.setEndTime(1546275661);
|
|
|
|
|
|
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(1514739661);
|
|
|
userCouponsBo1.setEndTime(1546275661);
|
|
|
return Lists.newArrayList(userCouponsBo, userCouponsBo1);
|
|
|
logger.info("[{}] getUserAvailableCoupons", uid);
|
|
|
//未使用的优惠券,包括还未生效的券
|
|
|
ApiResponse resp = serviceCaller.call("app.coupons.listNoUsed", uid);
|
|
|
UserCouponsListBo result = getResultFromApiResponse(resp);
|
|
|
if (Objects.isNull(result) || CollectionUtils.isEmpty(result.getCoupons())) {
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
//过滤未生效的券
|
|
|
int currentTime = DateUtil.getCurrentTimeSecond();
|
|
|
return result.getCoupons().stream().filter(e -> e.getStartTime() < currentTime && e.getEndTime() > currentTime).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
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.setProductIdInclude(Lists.newArrayList(88888900));
|
|
|
userCouponsBo.setStartTime(1514739661);
|
|
|
userCouponsBo.setEndTime(1546275661);
|
|
|
return Lists.newArrayList(userCouponsBo);
|
|
|
public List<UserCouponsBo> checkAndGetCoupons(int uid, List<String> couponCodes) {
|
|
|
logger.info("[{}] checkAndGetCoupons:{}", uid, couponCodes);
|
|
|
//未使用的优惠券,包括还未生效的券
|
|
|
ApiResponse resp = serviceCaller.call("app.coupons.checkAndGet", uid, couponCodes);
|
|
|
UserCouponsListBo result = getResultFromApiResponse(resp);
|
|
|
if (Objects.isNull(result) || CollectionUtils.isEmpty(result.getCoupons())) {
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
return result.getCoupons();
|
|
|
}
|
|
|
|
|
|
|
...
|
...
|
@@ -87,7 +77,8 @@ public class CouponProxyService implements Compensator { |
|
|
@TxCompensateArgs("couponBo") CouponBo coupon) {
|
|
|
logger.info("[{}] request to use coupon,couponBo is {}", orderCode, coupon);
|
|
|
ApiResponse resp = serviceCaller.call("app.coupons.use", uid, orderCode, Lists.newArrayList(coupon.getCouponCode()));
|
|
|
if (getResultFromApiResponse(resp) == false) {
|
|
|
Boolean result = getResultFromApiResponse(resp);
|
|
|
if (result == null || result.booleanValue() == false) {
|
|
|
logger.warn("[{}] use couponBo fail,coupons is {}", orderCode, coupon);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
...
|
...
|
@@ -105,21 +96,22 @@ public class CouponProxyService implements Compensator { |
|
|
public void orderCancelCoupon(int uid, long orderCode, CouponBo coupon) {
|
|
|
logger.info("[{}] request to cancel coupon,couponBo is {}", orderCode, coupon);
|
|
|
ApiResponse resp = serviceCaller.call("app.coupons.cancel", uid, orderCode, Lists.newArrayList(coupon.getCouponCode()));
|
|
|
if (getResultFromApiResponse(resp) == false) {
|
|
|
Boolean result = getResultFromApiResponse(resp);
|
|
|
if (result == null || result.booleanValue() == false) {
|
|
|
logger.warn("[{}] cancel couponBo fail,coupons is {}", orderCode, coupon);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
|
|
logger.info("[{}] cancel coupon success", orderCode);
|
|
|
}
|
|
|
|
|
|
private boolean getResultFromApiResponse(ApiResponse resp) {
|
|
|
private <T> T getResultFromApiResponse(ApiResponse resp) {
|
|
|
if (resp == null) {
|
|
|
return false;
|
|
|
return null;
|
|
|
}
|
|
|
if (resp.getCode() != 200 || resp.getData() == null) {
|
|
|
return false;
|
|
|
return null;
|
|
|
}
|
|
|
return (boolean) resp.getData();
|
|
|
return (T) resp.getData();
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -137,7 +129,7 @@ public class CouponProxyService implements Compensator { |
|
|
JSONObject json = JSON.parseObject(message);
|
|
|
uid = json.getIntValue("uid");
|
|
|
orderCode = json.getLongValue("orderCode");
|
|
|
couponBo = json.getObject("coupons", CouponBo.class);
|
|
|
couponBo = json.getObject("couponBo", CouponBo.class);
|
|
|
} catch (Exception ex) {
|
|
|
logger.warn("parse message to json error,message is {}", message, ex);
|
|
|
}
|
...
|
...
|
|