CouponConvert.java 10.9 KB
package com.yohoufo.promotion.convert;

import com.google.common.collect.Lists;
import com.yoho.service.model.promotion.ProductLimitValueBo;
import com.yohobuy.ufo.model.promotion.CouponSimpleBo;
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
import com.yohobuy.ufo.model.promotion.constant.*;
import com.yohobuy.ufo.model.promotion.response.CouponInfo;
import com.yohobuy.ufo.model.promotion.response.ProductLimitInfo;
import com.yohoufo.common.helper.ProductLimitCollector;
import com.yohoufo.common.utils.BigDecimalHelper;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.promotion.model.Coupon;
import com.yohoufo.dal.promotion.model.CouponAndType;
import com.yohoufo.dal.promotion.model.CouponType;
import com.yohoufo.dal.promotion.model.UserCoupon;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class CouponConvert {
    //3天的秒数
    private static final int THREE_DAY_SECOND = 3 * 24 * 60 * 60;


    private final static Logger logger = LoggerFactory.getLogger(CouponConvert.class);

    /**
     * 订单默认使用
     * @param userCoupon
     * @param coupon
     * @param couponType
     * @param productLimitValueBo
     * @return
     */
    public static UserCouponsBo covertCouponBo(UserCoupon userCoupon, Coupon coupon, CouponType couponType, List<ProductLimitValueBo> productLimitValueBo){

        UserCouponsBo couponBo = new UserCouponsBo();

        couponBo.setCouponCode(userCoupon.getCouponCode());

        couponBo.setCouponAmount(coupon.getCouponAmount());
        couponBo.setCouponName(coupon.getCouponName());
        couponBo.setProductLimitType(coupon.getProductLimitType()!=null ? String.valueOf(coupon.getProductLimitType()):"");
        couponBo.setUseLimitType(coupon.getUseLimitType()!=null ? String.valueOf(coupon.getUseLimitType()) : "");
        couponBo.setUseLimitValue(coupon.getUseLimitValue()!=null ? String.valueOf(coupon.getUseLimitValue()) : "");

        couponBo.setStartTime(userCoupon.getStartTime());
        couponBo.setEndTime(userCoupon.getEndTime());
        couponBo.setStatus(userCoupon.getStatus());

        couponBo.setSkupForbidType(coupon.getSkupForbidType());
        couponBo.setSkupAllowType(coupon.getSkupAllowType());

        // use_limit_type use_limit_value 满多少元可用
        if (coupon.getUseLimitType()!=null && CouponUseLimitTypeEnum.AMOUNT_LIMIT.getLimitType().equals(String.valueOf(coupon.getUseLimitType()))){
            couponBo.setUseRule(MessageFormat.format(CouponUseLimitTypeEnum.AMOUNT_LIMIT.getText(),coupon.getUseLimitValue().intValue()));
        }

        //优惠券说明
        couponBo.setNotes(Lists.newArrayList(coupon.getRemark()));

        //即将过期
        couponBo.setIsExpiredSoon(isExpiredSoon(userCoupon) ? Constants.YES : Constants.NO);

        if (couponType != null){
            couponBo.setCouponType(couponType.getId());
            couponBo.setCouponTypeName(couponType.getCaption());
        }


        ProductLimitInfo productLimitInfo = ProductLimitCollector.getProductLimitInfo(productLimitValueBo, coupon.getProductLimitType(), coupon.getProductLimitValue());
        couponBo.setProductLimitInfo(productLimitInfo);

        couponBo.setBusinessClient(coupon.getBusinessClient());

        return couponBo;
    }




    /**
     * 用户优惠券信息 用户中心展示
     * @param userCoupon 用户优惠券
     * @param coupon 优惠券
     * @param couponType 优惠券类型
     * @return
     */
    public static CouponInfo convertCouponInfo(UserCoupon userCoupon, Coupon coupon, CouponType couponType) {
        CouponInfo couponInfo = new CouponInfo();

        couponInfo.setCoupon_code(userCoupon.getCouponCode());

        StringBuilder coupon_validity = new StringBuilder();
        coupon_validity.append(DateUtil.getDateFormat(userCoupon.getStartTime()));
        coupon_validity.append("-");
        coupon_validity.append(DateUtil.getDateFormat(userCoupon.getEndTime()));

        couponInfo.setCoupon_validity(coupon_validity.toString());

        //即将过期
        couponInfo.setIs_expired_soon(isExpiredSoon(userCoupon) ? Constants.YES : Constants.NO);

        //券的用户类型
        couponInfo.setUser_type(userCoupon.getUserType());

        // 优惠券信息
        if (coupon != null){
            couponInfo.setCoupon_name(coupon.getCouponName());
            couponInfo.setCoupon_token(coupon.getCouponToken());
            couponInfo.setCoupon_value(coupon.getCouponAmount());

            // use_limit_type use_limit_value 满多少元可用
            if (coupon.getUseLimitType()!=null && CouponUseLimitTypeEnum.AMOUNT_LIMIT.getLimitType().equals(String.valueOf(coupon.getUseLimitType()))){
                couponInfo.setUse_rule(MessageFormat.format(CouponUseLimitTypeEnum.AMOUNT_LIMIT.getText(),coupon.getUseLimitValue().intValue()));
            }

            if (StringUtils.isNotEmpty(coupon.getRemark())){
                couponInfo.setNotes(Arrays.asList(coupon.getRemark()));
            }else
            // product_limit_type product_limit_value 特定商品
            if (coupon.getProductLimitType()!=null && CouponProductLimitTypeEnum.SPECIFIC_PRODUCT.getLimitType().equals(String.valueOf(coupon.getProductLimitType()))){
                couponInfo.setNotes(Arrays.asList(CouponProductLimitTypeEnum.SPECIFIC_PRODUCT.getText()));
            }
        }

        // 优惠券类型信息

        if (couponType != null){
            couponInfo.setCoupon_type(couponType.getId());
            couponInfo.setCoupon_type_name(couponType.getCaption());
        }
        return couponInfo;
    }


    /**
     * 领取优惠券时构造信息
     * @param uid
     * @param couponAndType
     * @param couponCode
     * @return
     */
    public static UserCoupon convertUserCoupon(Integer uid,String couponCode, CouponAndType couponAndType) {
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setUid(uid);
        userCoupon.setCouponId(couponAndType.getCoupon().getId());
        userCoupon.setUserType(couponAndType.getCoupon().getUserType());
        userCoupon.setCouponType(couponAndType.getCouponType().getId());
        userCoupon.setCouponCode(couponCode);
        userCoupon.setUseTime(0);
        userCoupon.setStartTime(couponAndType.getCoupon().getStartTime());
        userCoupon.setEndTime(couponAndType.getCoupon().getEndTime());
        userCoupon.setCreateTime(DateUtil.getCurrentTimeSecond());
        userCoupon.setOrderCode(0l);
        userCoupon.setStatus(UserCouponsStatusEnum.NO_USE.getCode());
        userCoupon.setCouponToken(couponAndType.getCoupon().getCouponToken());
        return userCoupon;
    }


    /**
     * 即将过期
     *
     * @param userCoupon
     * @return
     */
    public static boolean isExpiredSoon(UserCoupon userCoupon) {
        //未使用状态 且 过期时间在三天内
        return userCoupon.getStatus() == CouponUseStatusEnum.NOT_USED.getCode()
                && DateUtil.between(userCoupon.getEndTime() - DateUtil.getCurrentTimeSecond(), 0, THREE_DAY_SECOND);
    }

    /**
     * 展示原有的券名
     * @param coupon
     * @param userCoupon
     * @return
     */
    public static CouponSimpleBo do2CouponSimpleBo(Coupon coupon, UserCoupon userCoupon){
        CouponSimpleBo couponSimpleBo = new CouponSimpleBo();
        couponSimpleBo.setCouponId(coupon.getId());
        String couponName = coupon.getCouponName();
        couponSimpleBo.setCouponName(couponName);
        couponSimpleBo.setCouponAmount(coupon.getCouponAmount());
        couponSimpleBo.setUseLimitType(coupon.getUseLimitType().intValue());
        couponSimpleBo.setUseLimitValue(String.valueOf(coupon.getUseLimitValue()));
        couponSimpleBo.setCheckRequired(coupon.getCheckRequired());
        couponSimpleBo.setCouponToken(coupon.getCouponToken());
        couponSimpleBo.setCouponType(coupon.getCouponType());
        couponSimpleBo.setStartTime(DateUtil.formatDate(coupon.getStartTime(), DateUtil.YYYY_MM_DD_DOT));
        couponSimpleBo.setEndTime(DateUtil.formatDate(coupon.getEndTime(), DateUtil.YYYY_MM_DD_DOT));
        Integer receiveStartTime;
        if (Objects.nonNull(receiveStartTime=coupon.getReceiveStartTime())){
            couponSimpleBo.setReceiveStartTime(DateUtil.formatDate(receiveStartTime, DateUtil.YYYY_MM_DD_DOT));
        }
        Integer receiveEndTime;
        if (Objects.nonNull(receiveEndTime=coupon.getReceiveEndTime())){
            couponSimpleBo.setReceiveEndTime(DateUtil.formatDate(receiveEndTime, DateUtil.YYYY_MM_DD_DOT));
        }
        //是否可以领取
        if (Objects.nonNull(userCoupon)){
            couponSimpleBo.setReceive("N");
        }else{
            couponSimpleBo.setReceive("Y");
        }
        return couponSimpleBo;
    }

    /**
     * 重命名券名
     * @param coupon
     * @return
     */
    public static CouponSimpleBo do2CouponSimpleBo(Coupon coupon){
        CouponSimpleBo couponSimpleBo = new CouponSimpleBo();
        couponSimpleBo.setCouponId(coupon.getId());
        String newName = buildCouponNameByType(coupon);
        couponSimpleBo.setCouponName(newName);
        couponSimpleBo.setCouponAmount(coupon.getCouponAmount());
        couponSimpleBo.setUseLimitType(coupon.getUseLimitType().intValue());
        String useLimitValueStr;
        couponSimpleBo.setUseLimitValue(useLimitValueStr=String.valueOf(coupon.getUseLimitValue()));
        couponSimpleBo.setCheckRequired(coupon.getCheckRequired());
        couponSimpleBo.setCouponToken(coupon.getCouponToken());
        Integer couponType;
        couponSimpleBo.setCouponType(couponType=coupon.getCouponType());

        couponSimpleBo.setStartTime(DateUtil.formatDate(coupon.getStartTime(), DateUtil.YYYY_MM_DD_DOT));
        couponSimpleBo.setEndTime(DateUtil.formatDate(coupon.getEndTime(), DateUtil.YYYY_MM_DD_DOT));
        return couponSimpleBo;
    }


    static String buildCouponNameByType(Coupon coupon){
        String couponAmountSkipDecimal = BigDecimalHelper.getStripTrailingZeros(coupon.getCouponAmount());
        Integer couponType = coupon.getCouponType();
        String useLimitValueStr=String.valueOf(coupon.getUseLimitValue());
        String couponDesc = coupon.getCouponName();
        if (Integer.valueOf(CouponTypeEnum.ACTIVITY_COUPON.getCode()).equals(couponType)) {
            String useLimitTypeStr = coupon.getUseLimitType().toString();
            if (CouponUseLimitTypeEnum.NON_LIMIT.getLimitType().equals(useLimitTypeStr)) {
                couponDesc = "直降" + couponAmountSkipDecimal;
            }
            if (CouponUseLimitTypeEnum.AMOUNT_LIMIT.getLimitType().equals(useLimitTypeStr)){
                couponDesc =  "满" + useLimitValueStr + "减" + couponAmountSkipDecimal;
            }
        }
        if (Integer.valueOf(CouponTypeEnum.SHIPPING_COUPON.getCode()).equals(couponType)){
            couponDesc = "包邮";
        }
        return couponDesc;
    }
}