|
|
package com.yohoufo.order.service.proxy;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.core.transaction.Compensator;
|
|
|
import com.yoho.core.transaction.annoation.TxCompensatable;
|
|
|
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.yohoufo.common.ApiResponse;
|
|
|
import com.yohoufo.common.caller.UfoServiceCaller;
|
|
|
import com.yohoufo.order.model.bo.CouponBo;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
...
|
...
|
@@ -11,7 +24,13 @@ import java.util.List; |
|
|
* Created by jiexiang.wu on 2018/11/19.
|
|
|
*/
|
|
|
@Service
|
|
|
public class CouponProxyService {
|
|
|
public class CouponProxyService implements Compensator {
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(CouponProxyService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private UfoServiceCaller serviceCaller;
|
|
|
|
|
|
/**
|
|
|
* 查询用户可以使用的券
|
|
|
*
|
...
|
...
|
@@ -54,4 +73,75 @@ public class CouponProxyService { |
|
|
userCouponsBo.setEndTime(1546275661L);
|
|
|
return Lists.newArrayList(userCouponsBo);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 使用优惠券,有补偿功能
|
|
|
*
|
|
|
* @param uid
|
|
|
* @param orderCode
|
|
|
* @param coupon
|
|
|
*/
|
|
|
@TxCompensatable(value = CouponProxyService.class)
|
|
|
public void orderUseCoupon(@TxCompensateArgs("uid") int uid, @TxCompensateArgs("orderCode") long orderCode,
|
|
|
@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) {
|
|
|
logger.warn("[{}] use couponBo fail,coupons is {}", orderCode, coupon);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
|
|
logger.info("[{}] use coupon success", orderCode);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 取消券使用
|
|
|
*
|
|
|
* @param uid
|
|
|
* @param orderCode
|
|
|
* @param coupon
|
|
|
*/
|
|
|
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) {
|
|
|
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) {
|
|
|
if (resp == null) {
|
|
|
return false;
|
|
|
}
|
|
|
if (resp.getCode() != 200 || resp.getData() == null) {
|
|
|
return false;
|
|
|
}
|
|
|
return (boolean) resp.getData();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 补偿
|
|
|
*
|
|
|
* @param message json格式的字符串
|
|
|
*/
|
|
|
@Override
|
|
|
public void compensate(String message) {
|
|
|
logger.info("CouponProxyService begin to compensate : {}", message);
|
|
|
int uid = 0;
|
|
|
long orderCode = 0;
|
|
|
CouponBo couponBo = null;
|
|
|
try {
|
|
|
JSONObject json = JSON.parseObject(message);
|
|
|
uid = json.getIntValue("uid");
|
|
|
orderCode = json.getLongValue("orderCode");
|
|
|
couponBo = json.getObject("coupons", CouponBo.class);
|
|
|
} catch (Exception ex) {
|
|
|
logger.warn("parse message to json error,message is {}", message, ex);
|
|
|
}
|
|
|
orderCancelCoupon(uid, orderCode, couponBo);
|
|
|
logger.info("CouponProxyService compensate end,uid is {},orderCode is {}", uid, orderCode);
|
|
|
}
|
|
|
} |
...
|
...
|
|