package com.kmall.api.service;

import com.google.common.collect.Maps;
import com.kmall.api.dao.mk.ApiMkStoreTicketDiscountMapper;
import com.kmall.api.entity.mk.MkStoreTicketDiscountVo;
import com.kmall.common.constant.Dict;
import com.kmall.api.dao.ApiCouponMapper;
import com.kmall.api.dao.ApiUserCouponMapper;
import com.kmall.api.entity.CouponVo;
import com.kmall.api.entity.UserCouponVo;
import com.kmall.common.utils.CharUtil;
import com.kmall.common.utils.Constant;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ApiCouponService {
    @Autowired
    private ApiCouponMapper apiCouponMapper;
    @Autowired
    private ApiMkStoreTicketDiscountMapper mkStoreTicketDiscountMapper;

    /**
     * 获取优惠信息提示邮费 还差多少邮费
     *
     * @param userId
     * @param goodsTotalPrice
     * @return
     */
    /*
    public CouponVo matchShippingSign(Long userId, BigDecimal goodsTotalPrice) {
        CouponVo result = new CouponVo();
        //
        Map couponParam = Maps.newHashMap();
        couponParam.put("enabled", true);
        Integer[] send_types = new Integer[]{7};
        couponParam.put("send_types", send_types);
        List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
        if (null != couponVos && couponVos.size() > 0) {
            for (CouponVo couponVo : couponVos) {
                BigDecimal difDec = couponVo.getMin_goods_amount().subtract(goodsTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                // 是否最低优惠券
                boolean optimal = false;
                if (null != result && null != result.getId()) {
                    // 都是凑单,取小的
                    if (difDec.doubleValue() > 0.0 && result.getDifDec().doubleValue() > 0.0
                            && difDec.compareTo(result.getDifDec()) < 0) {
                        optimal = true;
                    }
                    // 一个凑单，一个满足，取凑单
                    else if (difDec.doubleValue() >= 0.0 && result.getDifDec().doubleValue() <= 0.0) {
                        optimal = true;
                    } // 都满足，取type_money小的
                    else if (difDec.doubleValue() < 0.0 && result.getDifDec().doubleValue() < 0.0
                            && result.getMoney().compareTo(couponVo.getType_money()) > 0) {
                        optimal = true;
                    }
                } else {
                    optimal = true;
                }
                if (!optimal) {
                    continue;
                }
                if (difDec.doubleValue() > 0.0 && couponVo.getType_money().compareTo(Constant.ZERO) == 0) {
                    couponVo.setMsg("满￥" + couponVo.getMin_goods_amount() + "元免配送费，还差" + difDec + "元");
                    couponVo.setType(1);
                } else if (couponVo.getType_money().doubleValue() > 0.0) {
                    couponVo.setMsg("满￥" + couponVo.getMin_goods_amount() + "元，配送费" + couponVo.getType_money() + "元，还差" + difDec + "元");
                    couponVo.setType(1);
                } else {
                    couponVo.setMsg("满￥" + couponVo.getMin_goods_amount() + "元免配送费");
                    couponVo.setType(0);
                }
                couponVo.setDifDec(difDec);
                couponVo.setMoney(couponVo.getType_money());
                result = couponVo;
            }
        }
        return result;
    }*/

    /**
     * 获取可使用的优惠 邮费
     *
     * @param userId
     * @param goodsTotalPrice
     * @return
     */
    /*
    public BigDecimal matchShipping(Long userId, BigDecimal goodsTotalPrice) {
        BigDecimal result = new BigDecimal(10);
        //
        Map couponParam = Maps.newHashMap();
        couponParam.put("enabled", true);
        Integer[] send_types = new Integer[]{7};
        couponParam.put("send_types", send_types);
        List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
        if (null != couponVos && couponVos.size() > 0) {
            for (CouponVo couponVo : couponVos) {
                // 是否免运费
                if (couponVo.getMin_goods_amount().compareTo(goodsTotalPrice) <= 0
                        && result.compareTo(couponVo.getType_money()) > 0) {
                    result = couponVo.getType_money();
                }
            }
        }
        return result;
    }*/

    /**
     * 获取可用的满减券
     *
     * @param userId
     * @param goodsTotalPrice
     * @return
     */
    /*
    public CouponVo matchFullSub(Long userId, BigDecimal goodsTotalPrice) {
        CouponVo result = new CouponVo();
        //
        Map couponParam = Maps.newHashMap();
        couponParam.put("enabled", true);
        // 获取优惠信息提示 满减
        Integer[] send_types = new Integer[]{0};
//        param.put("unUsed", true);
        couponParam.put("send_types", send_types);
        List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
        if (null != couponVos && couponVos.size() > 0) {
            CouponVo fullCutVo = new CouponVo();
            BigDecimal fullCutDec = Constant.ZERO;
            for (CouponVo couponVo : couponVos) {
                BigDecimal difDec = couponVo.getMin_goods_amount().subtract(goodsTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                if (difDec.doubleValue() < 0.0 && fullCutDec.compareTo(couponVo.getType_money()) < 0) {
                    fullCutDec = couponVo.getType_money();
                    fullCutVo = couponVo;
                    fullCutVo.setType(0);
                    fullCutVo.setMsg("可使用满减券" + couponVo.getName());
                }
                if (!StringUtils.isNullOrEmpty(fullCutVo.getMsg())) {
                    result = fullCutVo;
                }
            }
        }
        return result;
    }*/

    /**
     * 获取优惠信息提示满减券 还差多少满减
     *
     * @param userId
     * @param goodsTotalPrice
     * @return
     */
    /*
    public CouponVo matchFullSubSign(Long userId, BigDecimal goodsTotalPrice, long storeId) {
        CouponVo result = new CouponVo();
        //
        Map couponParam = Maps.newHashMap();
        couponParam.put("enabled", true);
        // 获取优惠信息提示 满减
        couponParam.put("storeId", storeId);
//        List<CouponVo> couponVos = apiCouponMapper.queryList(couponParam);
        List<MkStoreTicketDiscountVo> discountVoList = mkStoreTicketDiscountMapper.queryList(couponParam);
        if (null != discountVoList && discountVoList.size() > 0) {
            CouponVo fullCutVo = new CouponVo();
            BigDecimal fullCutDec = Constant.ZERO;
            BigDecimal minAmount = new BigDecimal(100000);
            for (MkStoreTicketDiscountVo discountVo : discountVoList) {
                BigDecimal cond = Constant.ZERO;
                BigDecimal money = Constant.ZERO;
                if(discountVo.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_00.getItem())){
                    cond = discountVo.getVoucherCond();
                    money = discountVo.getVoucherMoney();
                }
                if(discountVo.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_01.getItem())){
                    cond = discountVo.getDiscCond();
                    money = discountVo.getDiscRatio();//满多少打几折
                }
                if(discountVo.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_02.getItem())){
                    cond = discountVo.getExchCond();
                    money = discountVo.getExchCond();
                }
                BigDecimal difDec = cond.subtract(goodsTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                if (difDec.doubleValue() > 0.0
                        && minAmount.compareTo(discountVo.getVoucherCond()) > 0) {
                    fullCutDec = money;
                    fullCutVo.setType(1);
                    fullCutVo.setMsg(discountVo.getName() + "，还差" + difDec + "元");
                } else if (difDec.doubleValue() < 0.0 && fullCutDec.compareTo(money) < 0) {
                    fullCutVo.setType(0);
                    fullCutVo.setMsg("可使用满减券" + discountVo.getName());
                }
                if (!StringUtils.isNullOrEmpty(fullCutVo.getMsg())) {
                    result = fullCutVo;
                }
            }
        }
        return result;
    }*/
}
