package com.gree.mall.miniapp.logic.coupon;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.coupon.*;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.CoustomUserCouponMapper;
import com.gree.mall.miniapp.commonmapper.CustomCoupouMapper;
import com.gree.mall.miniapp.enums.CouponActiveTypeEnum;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.enums.CouponTypeEnum;
import com.gree.mall.miniapp.enums.QrCodeEnum;
import com.gree.mall.miniapp.enums.UserTypeEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.logic.common.outside.WechatLogic;
import com.gree.mall.miniapp.logic.user.UserLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.*;
import com.gree.mall.miniapp.utils.CommonUtils;
import com.gree.mall.miniapp.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;

@Slf4j
@Component
public class CouponLogic {

    @Autowired
    UserCouponService userCouponService;
    @Autowired
    CustomCoupouMapper customCoupouMapper;
    @Autowired
    CouponService couponService;
    @Autowired
    CouponGoodsService couponGoodsService;
    @Autowired
    RedisLockRegistry redisLockRegistry;
    @Autowired
    private UserService userService;
    @Autowired
    private UserLogic userLogic;
    @Autowired
    private CoustomUserCouponMapper coustomUserCouponMapper;
    @Autowired
    private WechatLogic wechatLogic;
    @Autowired
    CouponLogic couponLogic;
    @Autowired
    CommonLogic commonLogic;


    /**
     * 统计 未使用 ，已使用，已过期优惠券
     *
     * @param userId
     * @return
     */
    public UserCouponCountBean count(String userId) {
        UserCouponCountBean userCouponCountBean = new UserCouponCountBean();
        List<UserCoupon> list = userCouponService.lambdaQuery().eq(UserCoupon::getUserId, userId).list();
        if (list.size() == 0) {
            return userCouponCountBean;
        }
        //未使用数量,已使用数量，已过期数量
        int wsy = 0, ysy = 0, ygq = 0;
        for (UserCoupon userCoupon : list) {

            if (userCoupon.getStatus()) {
                ysy++;
            }else {
                if (userCoupon.getActiveEndTime().getTime() > new Date().getTime()) {
                    wsy++;
                } else {
                    ygq++;
                }
            }
        }
        userCouponCountBean.setWsy(wsy);
        userCouponCountBean.setYsy(ysy);
        userCouponCountBean.setYgq(ygq);
        return userCouponCountBean;
    }

    /**
     * 我的优惠券
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<UserCoupon> page(String userId, Integer type, Integer pageNum, Integer pageSize) {

        Date date = new Date();

        LambdaQueryWrapper<UserCoupon> qw = new LambdaQueryWrapper<>();
        qw.eq(UserCoupon::getUserId, userId);

        //未使用
        if (type != null && type == 0) {
            qw.eq(UserCoupon::getStatus, type).ge(UserCoupon::getActiveEndTime,date);
        }
        //已使用
        if (type != null && type == 1) {
            qw.eq(UserCoupon::getStatus, type);
        }
        //已过期
        if (type != null && type == 2) {
            qw.eq(UserCoupon::getStatus,0);
            qw.lt(UserCoupon::getActiveEndTime, date);
        }
        qw.orderByDesc(UserCoupon::getReceiveTime);
        IPage<UserCoupon> page = userCouponService.page(new Page<>(pageNum, pageSize), qw);
        return page;
    }


    public List<CouponObtainBean> allObtainCoupou(String userId,HttpServletRequest request) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        List<CouponObtainBean> couponObtainBeans = customCoupouMapper.listObtainCoupou(userId,currentCompanyWechat.getCompanyWechatId());
        //发券
        try {
            List<CouponObtainBean> obtions = couponObtainBeans.stream().filter(v -> v.getObtainType() == 1).collect(Collectors.toList());
            if (obtions.size() > 0) {
                List<String> couponIds = obtions.stream().map(CouponObtainBean::getCouponId).collect(Collectors.toList());
                for(String couponId : couponIds) {
                    Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_COUPON + couponId);
                    if(!obtain.tryLock(10,TimeUnit.SECONDS)){
                        log.error("系统繁忙，领券失败,userId:{},couponId:{}",userId,couponId);
                        continue;
                    }
                    try {
                        couponLogic.obtainCoupou(request, userId, couponId);
                    }finally {
                        obtain.unlock();
                    }
                }
            }
        }catch(Exception e){
            log.info("【优惠券发放失败】",e);
        }
        return couponObtainBeans;
    }


    public List<UserCoupon> expiring(String userId) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 1);
        return userCouponService.lambdaQuery()
                .eq(UserCoupon::getUserId, userId)
                .le(UserCoupon::getActiveEndTime, cal.getTime())
                .list();
    }

    private void distributeCoupon(String userId, String serviceId , String companyWechatId ,String companyName) {
        String couponId = "1460852089443127298";
        //customerTransfer(userId, serviceId, couponId ,companyWechatId , companyName);
    }

    /**
     * 领取优惠券
     *
     * @param userId
     * @throws RemoteServiceException
     */
    public void obtainCoupou(HttpServletRequest request, String userId, String couponId) throws RemoteServiceException, InterruptedException {

//        if (StringUtils.isBlank(userId) && StringUtils.isBlank(couponId)) {
//            String obtainUserId = CommonUtils.getUserId(request);
//            User obtainUser = userService.getById(obtainUserId);
//            String serviceId = obtainUser.getServiceId();
//            if (StringUtils.isNotBlank(serviceId)) {
//                User service = userService.getById(serviceId);
//                if (service != null && service.getCustomerServiceFlag()) {
//                    distributeCoupon(obtainUserId, serviceId , obtainUser.getCompanyWechatId() ,obtainUser.getCompanyName());
//                }
//            }
//            return;
//        }
            Coupon coupon = couponService.getById(couponId);
            //限领张数
            Integer receiveLimitCount = coupon.getReceiveLimitCount();
            //剩余可领取数量
            Integer leftAmount = coupon.getLeftAmount();
            //已领取数量
            Integer receiveAmount = coupon.getReceiveAmount() == null ? 0 : coupon.getReceiveAmount();
            if (leftAmount <= 0) {
                throw new RemoteServiceException("领取优惠券失败,优惠券已被领取完");
            }
            Integer count = userCouponService.lambdaQuery()
                    .eq(UserCoupon::getUserId, userId)
                    .eq(UserCoupon::getCouponId, couponId).count();
            if (receiveLimitCount != 0 && count >= receiveLimitCount) {
                throw new RemoteServiceException("领取失败,你已领取过券");
            }
            //实际领取张数
            int num = leftAmount <= receiveLimitCount  ? leftAmount : leftAmount - receiveLimitCount;
            //维护券的剩余数量和领取数量
            coupon.setLeftAmount(leftAmount - num);
            coupon.setReceiveAmount(receiveAmount + num);
            coupon.updateById();
            User user = userService.getById(userId);
            Boolean transferType = user.getType().equals(UserTypeEnum.SERVICE.toString());
            //新增我的优惠券
            List<UserCoupon> list = new ArrayList<>();
            Date now = new Date();
            for (int i = 0; i < receiveLimitCount; i++) {
                UserCoupon userCoupon = new UserCoupon();
                userCoupon.setCouponId(couponId);
                userCoupon.setUserId(userId);
                userCoupon.setReceiveTime(now);
                userCoupon.setCouponName(coupon.getCouponName());
                userCoupon.setCouponType(coupon.getCouponType());
                userCoupon.setOrderAmount(coupon.getOrderAmount());
                userCoupon.setCouponValue(coupon.getCouponValue());
                userCoupon.setStatus(false);
                //userCoupon.setTransferType(transferType);
                if (coupon.getActiveDay() == null) {
                    coupon.setActiveDay(0);
                }
                if (coupon.getActiveType() == CouponActiveTypeEnum.COMMON.getActiveType()) {
                    userCoupon.setActiveStartTime(coupon.getActiveStartTime());
                    userCoupon.setActiveEndTime(coupon.getActiveEndTime());
                }
                //当日起多少天
                if (coupon.getActiveType() == CouponActiveTypeEnum.TODAY.getActiveType()) {
                    userCoupon.setActiveStartTime(DateUtils.getStartTime());
                    userCoupon.setActiveEndTime(DateUtils.getStartTimeByDay(coupon.getActiveDay()));
                }
                //次日起多少天
                if (coupon.getActiveType() == CouponActiveTypeEnum.NEXTDAY.getActiveType()) {
                    userCoupon.setActiveStartTime(DateUtils.getStartTimeByDay(1));
                    userCoupon.setActiveEndTime(DateUtils.getStartTimeByDay(coupon.getActiveDay() + 1));
                }
                userCoupon.setState(now.before(coupon.getActiveEndTime()) && now.after(coupon.getActiveStartTime()));
                userCoupon.setCompanyWechatId(user.getCompanyWechatId());
                userCoupon.setCompanyName(user.getCompanyName());
                list.add(userCoupon);
            }
            userCouponService.saveBatch(list);
    }


    public List<CustomCoupouBean> useableCoupou(String userId, BigDecimal orderAmount, List<String> goodsSpecIds) {

        List<CustomCoupouBean> customCoupouBeans = customCoupouMapper.listCoupou(userId, orderAmount, goodsSpecIds);
        return customCoupouBeans;
    }


    /**
     * 券详情
     *
     * @return
     */
    public UserCoupon detail(String userCouponId) {

        return userCouponService.getById(userCouponId);
    }

    public CouponQrcodeBean qrcode(String userCouponId) throws RemoteServiceException {

        UserCoupon userCoupon = userCouponService.lambdaQuery()
                .eq(UserCoupon::getId, userCouponId)
                .eq(UserCoupon::getStatus, false)
                .eq(UserCoupon::getTransferType, true)
                .ge(UserCoupon::getActiveEndTime, new Date())
                .one();
        if (userCoupon == null) {
            throw new RemoteServiceException("优惠券不可转赠");
        }
        String qrcode = userCoupon.getTransferQrcode();
        if (StringUtils.isBlank(qrcode)) {
            qrcode = wechatLogic.getQrcode(QrCodeEnum.COUPON.toString().toLowerCase(Locale.ROOT), userCoupon.getId(), userCoupon.getUserId(),userCoupon.getCompanyWechatId());
            if (StringUtils.isNotBlank(qrcode)) {
                userCoupon.setTransferQrcode(qrcode);
                userCouponService.updateById(userCoupon);
            }
        }
        Coupon coupon = couponService.getById(userCoupon.getCouponId());
        CouponQrcodeBean couponQrcodeBean = new CouponQrcodeBean();
        couponQrcodeBean.setQrcode(qrcode);
        couponQrcodeBean.setLeftShareTimes(userCoupon.getLeftShareTimes());
        couponQrcodeBean.setShareTimes(coupon.getShareTimes());
        return couponQrcodeBean;
    }

    public String transferCoupon(HttpServletRequest request, String userCouponId) throws RemoteServiceException {
        UserCoupon userCoupon = userCouponService.lambdaQuery()
                .eq(UserCoupon::getId, userCouponId)
                .eq(UserCoupon::getStatus, false)
                .eq(UserCoupon::getTransferType, true)
                .ge(UserCoupon::getActiveEndTime, new Date())
                .one();
        if (userCoupon == null || userCoupon.getLeftShareTimes() < 1) {
            throw new RemoteServiceException("优惠券已被领完");
        }
        String userId = CommonUtils.getUserId(request);
        if (userId.equals(userCoupon.getUserId())) {
            throw new RemoteServiceException("不能转赠自己");
        }
        User user = userService.lambdaQuery()
                .eq(User::getUserId, userId)
                .eq(User::getStatus,true)
                .one();
        if (user == null) {
            throw new RemoteServiceException("用户不存在");
        }
        if (user.getType().equals(UserTypeEnum.SERVICE.toString())) {
            throw new RemoteServiceException("业务员不能领取");
        }

        Integer count = userCouponService.lambdaQuery()
                .eq(UserCoupon::getUserId, userId)
                .eq(UserCoupon::getCouponId, userCoupon.getCouponId())
                .count();

        if (count > 0) {
            throw new RemoteServiceException("领取失败,领取优惠券超过限制数量");
        }


        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_COUPON_TRANSFER + userCoupon.getId());
        String transferCouponId;
        try {
            if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            UserCoupon reObtainCoupon = this.detail(userCouponId);
            if (reObtainCoupon.getLeftShareTimes() < 1) {
                throw new RemoteServiceException("优惠已被用户使用");
            }

            transferCouponId = transfer(user.getUserId(), userCouponId , user.getCompanyWechatId(), user.getCompanyName());
        } catch (InterruptedException e) {
            log.error("", e);
            throw new RemoteServiceException("系统繁忙，请稍后再试");
        } finally {
            obtain.unlock();
        }
        return transferCouponId;
    }

    @Transactional
    protected String transfer(String userId, String userCouponId , String companyWechatId , String companyName) {
        UserCoupon userCoupon = userCouponService.lambdaQuery()
                .eq(UserCoupon::getId, userCouponId)
                .eq(UserCoupon::getStatus, false)
                .eq(UserCoupon::getTransferType, true)
                .ge(UserCoupon::getActiveEndTime, new Date())
                .one();
        String transferCouponId = null;
        if (userCoupon != null && userCoupon.getLeftShareTimes() > 0) {
            userLogic.bind(userId, userCoupon.getUserId(),"");
            coustomUserCouponMapper.decreaseLeftTimes(userCoupon.getId());
            UserCoupon transferUserCoupon = new UserCoupon();
            BeanUtils.copyProperties(userCoupon, transferUserCoupon, "id");
            transferUserCoupon.setId(null);
            transferUserCoupon.setTransferType(false);
            transferUserCoupon.setReceiveTime(new Date());
            transferUserCoupon.setTransferQrcode(null);
            transferUserCoupon.setTransferor(userCoupon.getId());
            transferUserCoupon.setLeftShareTimes(0);
            transferUserCoupon.setUserId(userId);
            transferUserCoupon.setCompanyWechatId(companyWechatId);
            transferUserCoupon.setCompanyName(companyName);
            userCouponService.save(transferUserCoupon);
            transferCouponId = transferUserCoupon.getId();

            userCoupon.setLeftShareTimes(userCoupon.getLeftShareTimes() - 1);
            userCoupon.setStatus(userCoupon.getLeftShareTimes() == 0);
            userCoupon.updateById();
        }
        return transferCouponId;
    }


//    public void customerTransfer(String userId, String serviceId, String couponId ,String companyWechatId,String companyName) {
//        UserCoupon userCoupon = userCouponService.lambdaQuery()
//                .eq(UserCoupon::getUserId, serviceId)
//                .eq(UserCoupon::getCouponId, couponId)
//                .eq(UserCoupon::getTransferType, true)
//                .one();
//        //券不存在
//        if (userCoupon == null) {
//            return;
//        }
//        Integer count = userCouponService.lambdaQuery()
//                .eq(UserCoupon::getUserId, userId)
//                .eq(UserCoupon::getCouponId, couponId)
//                .eq(UserCoupon::getTransferType, false)
//                .count();
//        //已领券
//        if (count > 0) {
//            return;
//        }
//
//        UserCoupon transferUserCoupon = new UserCoupon();
//        BeanUtils.copyProperties(userCoupon, transferUserCoupon, "id");
//        transferUserCoupon.setId(null);
//        transferUserCoupon.setTransferType(false);
//        transferUserCoupon.setReceiveTime(new Date());
//        transferUserCoupon.setTransferQrcode(null);
//        transferUserCoupon.setTransferor(userCoupon.getId());
//        transferUserCoupon.setLeftShareTimes(0);
//        transferUserCoupon.setUserId(userId);
//        transferUserCoupon.setCompanyWechatId(companyWechatId);
//        transferUserCoupon.setCompanyName(companyName);
//        userCouponService.save(transferUserCoupon);
//    }


    /**
     * 核销优惠券,可转增的券全部是可使用状态
     *
     * @param userCouponId
     */
    @Transactional
    public UserCoupon cancelCoupon(String userCouponId,Boolean use) {

        UserCoupon userCoupon = userCouponService.getById(userCouponId);
        UserCoupon userCouponCopy = null;
        if (userCoupon != null && userCoupon.getTransferType() != null && userCoupon.getTransferType() && userCoupon.getLeftShareTimes() > 0) {
            //数量减一,增加一条新的记录
            if(use) {
                userCoupon.setLeftShareTimes(userCoupon.getLeftShareTimes() - 1);
                userCoupon.setStatus(userCoupon.getLeftShareTimes() == 0);
                userCoupon.updateById();
                //coustomUserCouponMapper.decreaseLeftTimes(userCoupon.getId());
            }
//            userCouponCopy = new UserCoupon();
//            userCouponCopy.setUserId(userCoupon.getUserId());
//            userCouponCopy.setCouponId(userCoupon.getCouponId());
//            userCouponCopy.setCouponName(userCoupon.getCouponName());
//            userCouponCopy.setCouponValue(userCoupon.getCouponValue());
//            userCouponCopy.setCouponType(userCoupon.getCouponType());
//            userCouponCopy.setTransferor(userCoupon.getId());
//            userCouponCopy.setOrderAmount(userCoupon.getOrderAmount());
//            userCouponCopy.setReceiveTime(new Date());
//            userCouponCopy.setUsedTime(new Date());
//            userCouponCopy.setTransferType(false);
//            userCouponCopy.setStatus(use);
//            userCouponCopy.setState(true);
//            userCouponCopy.setActiveStartTime(userCoupon.getActiveStartTime());
//            userCouponCopy.setActiveEndTime(userCoupon.getActiveEndTime());
//            userCouponCopy.setSelfUse(true);
//            userCouponService.save(userCouponCopy);
        } else {
            userCouponService.lambdaUpdate()
                    .set(UserCoupon::getStatus, use)
                    .set(UserCoupon::getUsedTime, new Date())
                    .eq(UserCoupon::getId, userCouponId)
                    .update();
        }
        return userCouponCopy == null ? userCoupon : userCouponCopy;
    }


    /**
     * 订单取消,恢复优惠券
     *
     * @param userCouponIds 优惠券id数组
     */
    @Transactional
    public void updateStatus(List<String> userCouponIds) {
        userCouponService.lambdaUpdate()
                .set(UserCoupon::getStatus, false)
                .set(UserCoupon::getUsedTime, null)
                .in(UserCoupon::getId, userCouponIds)
                .update();
        for (String userCouponId : userCouponIds) {
            if (StringUtils.isBlank(userCouponId)) {
                continue;
            }
            log.info("订单取消,恢复用户优惠券id{}", userCouponId);
            UserCoupon userCoupon = userCouponService.getById(userCouponId);
            if(userCoupon == null)
                continue;
            if(userCoupon.getTransferType() == null || !userCoupon.getTransferType())
                continue;

            coustomUserCouponMapper.increaseLeftTimes(userCouponId);
        }
    }


    /**
     * 优惠券过期提醒
     */
    public Boolean timeout(String userId) {
        Date timeByDay = DateUtils.getTimeByDay(new Date(), -3);
        Integer count = userCouponService.lambdaQuery().eq(UserCoupon::getUserId, userId)
                .between(UserCoupon::getActiveEndTime, timeByDay, new Date()).count();
        return count > 0;
    }

    /**
     * 发放新人券
     */
    public void addUserCoupon(String userId) {
//        Coupon coupon = couponService.lambdaQuery().eq(Coupon::getCouponName, "新人券").one();
//        if(coupon == null){
//            return;
//        }
//        UserCoupon userCoupon = new UserCoupon();
//        userCoupon.setUserId(userId);
//        userCoupon.setCouponId(coupon.getCouponId());
//        userCoupon.setCouponName(coupon.getCouponName());
//        userCoupon.setCouponValue(coupon.getCouponValue());
//        userCoupon.setCouponType(coupon.getCouponType());
//        userCoupon.setOrderAmount(coupon.getOrderAmount());
//        userCoupon.setActiveStartTime(coupon.getActiveStartTime());
//        userCoupon.setActiveEndTime(coupon.getActiveEndTime());
//        userCoupon.setReceiveTime(new Date());
//        userCoupon.setReceivePlatform("SYSTEM");
//        userCoupon.insert();
    }

    /**
     * 检查优惠券是否可用
     */
    public UserCouponBean check(String userCouponId, BigDecimal payment, List<String> goodsIds,Boolean use) throws
            RemoteServiceException, InterruptedException {

        if(StringUtils.isBlank(userCouponId)) {
            return null;
        }
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_COUPON_CHECK + userCouponId);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            //使用了优惠券
            if (StringUtils.isNotBlank(userCouponId)) {
                UserCoupon userCoupon = this.detail(userCouponId);

                UserCouponBean userCouponBean = new UserCouponBean();

                if (!userCoupon.getState()) {
                    throw new RemoteServiceException("下单失败,优惠券不可用");
                }
                if (userCoupon.getStatus()) {
                    throw new RemoteServiceException("优惠券已使用");
                }
                if (userCoupon.getTransferType() && userCoupon.getLeftShareTimes() < 1) {
                    throw new RemoteServiceException("优惠已被用户使用");
                }


                long curTime = new Date().getTime();
                if (userCoupon.getActiveStartTime().getTime() > curTime || userCoupon.getActiveEndTime().getTime() < curTime) {
                    throw new RemoteServiceException("优惠券使用时间范围不符合要求");
                }
                if (userCoupon.getCouponType().equals(CouponTypeEnum.SATISFY.toString())) {
                    log.info("【试用优惠券条件】券试用条件:{},订单金额：{}", userCoupon.getOrderAmount().doubleValue(), payment.doubleValue());
                    if (userCoupon.getOrderAmount().doubleValue() != 0 && userCoupon.getOrderAmount().doubleValue() > payment.doubleValue()) {
                        throw new RemoteServiceException("优惠券不符合使用要求");
                    }
                } else if (userCoupon.getCouponType().equals(CouponTypeEnum.GOODS.toString())) {

                    List<CouponGoods> couponGoodsList = couponGoodsService.lambdaQuery()
                            .eq(CouponGoods::getCouponId, userCoupon.getCouponId())
                            .in(CouponGoods::getGoodsId, goodsIds)
                            .list();
                    if (couponGoodsList.size() == 0) {
                        throw new RemoteServiceException("优惠券不符合使用要求");
                    }
                    userCouponBean.setGoodsId(couponGoodsList.get(0).getGoodsId());
                }

                if (userCoupon.getTransferType()) {
                    if (userCoupon.getLeftShareTimes() < 1) {
                        throw new RemoteServiceException("优惠已被用户使用");
                    }
                }
                //将券改为已使用状态
                UserCoupon userCouponRet = this.cancelCoupon(userCoupon.getId(),use);
                BeanUtils.copyProperties(userCouponRet, userCouponBean, "goodsId");

                return userCouponBean;
            }
        }finally {
            obtain.unlock();
        }
        return null;
    }


    /**
     * 可用的优惠券
     * @param userId
     * @return
     */
    public List<UserCoupon> myCoupon(String userId){
        Date date = new Date();
        return userCouponService.lambdaQuery().eq(UserCoupon::getUserId,userId)
                .eq(UserCoupon::getStatus,0).ge(UserCoupon::getActiveEndTime,date).list();
    }



}
