package com.yc.ship.module.marketing.dal.mysql.couponCode;

import cn.hutool.core.date.DateUtil;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.yc.ship.module.marketing.controller.admin.couponCode.vo.CouponCodePageReqVO;
import com.yc.ship.module.marketing.dal.dataobject.couponCode.CouponCodeDO;
import com.yc.ship.module.marketing.dal.dataobject.couponCodeUseRecord.CouponCodeUseRecordDO;
import com.yc.ship.module.marketing.enums.coupon.CouponCodeUseStateEnum;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

/**
 * 优惠券码 Mapper
 *
 * @author lfy
 */
@Mapper
public interface CouponCodeMapper extends BaseMapperX<CouponCodeDO> {


    default List<CouponCodeDO> selectGetAndUsedNumList(Collection<Long> couponIdList) {
        return selectJoinList(CouponCodeDO.class, new MPJLambdaWrapperX<CouponCodeDO>()
                .select(CouponCodeDO::getCouponId)
                .selectCount(CouponCodeDO::getId,"get_num")
                .selectCount(CouponCodeUseRecordDO::getId, "used_num")
                .leftJoin(CouponCodeUseRecordDO.class, CouponCodeUseRecordDO::getCouponCodeId, CouponCodeDO::getId)
                .in(CouponCodeDO::getCouponId, couponIdList)
                .groupBy(CouponCodeDO::getCouponId)
        );
    }

    default PageResult<CouponCodeDO> selectPage(CouponCodePageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<CouponCodeDO>()
                .eq(CouponCodeDO::getCouponId, pageReqVO.getCouponId())
                .orderByDesc(CouponCodeDO::getCreateTime)
                .orderByDesc(CouponCodeDO::getId)
        );
    }

    /**
     * 获取今日已领取数量
     * @param couponId
     * @return
     */
    default Long selectCountToday(Long couponId) {
        return selectCount(new LambdaQueryWrapperX<CouponCodeDO>()
                .eq(CouponCodeDO::getCouponId, couponId)
                .between(CouponCodeDO::getCreateTime, DateUtil.beginOfDay(DateUtil.date()), DateUtil.endOfDay(DateUtil.date()))
        );
    }

    /**
     * 获取当前券用户已领取数量
     * @param memberId
     * @param couponId
     * @return
     */
    default Long selectCountByMemberIdAndCouponId(Long memberId, Long couponId) {
        return selectCount(new LambdaQueryWrapperX<CouponCodeDO>()
                .eq(CouponCodeDO::getMemberId, memberId)
                .eq(CouponCodeDO::getCouponId, couponId)
        );
    }

    /**
     * 根据用户ID查询未使用的优惠券码
     * @param memberId
     * @return
     */
    default List<CouponCodeDO> selectListByMemberId(Long memberId) {
        return selectList(new LambdaQueryWrapperX<CouponCodeDO>()
                .eq(CouponCodeDO::getMemberId, memberId)
                .eq(CouponCodeDO::getState, CouponCodeUseStateEnum.UNUSED.getType())
        );
    }

    /**
     * 获取优惠券已领取数量
     * @param couponId
     * @return
     */
    default Long selectCountByCouponId(Long couponId) {
        return selectCount(new LambdaQueryWrapperX<CouponCodeDO>()
                .eq(CouponCodeDO::getCouponId, couponId)
        );
    }

    /**
     * 根据核销码查询优惠券码
     * @param newCode
     * @return
     */
    default CouponCodeDO selectByCode(String newCode) {
        return selectOne(CouponCodeDO::getCode, newCode);
    }

    /**
     * 获取未使用的优惠券码列表
     * @param couponIds 优惠券ID列表
     * @return
     */
    default List<CouponCodeDO> selectNotUsedCouponCodeList(Long memberId, List<Long> couponIds) {
        return selectList(new LambdaQueryWrapperX<CouponCodeDO>()
                .eq(CouponCodeDO::getMemberId, memberId)
                .in(CouponCodeDO::getCouponId, couponIds)
                .eq(CouponCodeDO::getState, CouponCodeUseStateEnum.UNUSED.getType())
        );
    }
}