package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeDiscountCouponUser;
import shop.alien.entity.store.vo.LifeDiscountCouponUserWebVo;

import java.time.LocalDate;

/**
 * <p>
 * 优惠券用户表 Mapper 接口
 * </p>
 *
 * @author ssk
 * @since 2025-02-08
 */
public interface LifeDiscountCouponUserMapper extends BaseMapper<LifeDiscountCouponUser> {

    /**
     * 分页查询已发放的优惠券记录
     *
     * @param iPage        分页
     * @param queryWrapper 查询条件
     * @return IPage<LifeDiscountCouponVo>
     */
    @Select("select a.*, b.user_name, b.real_name, b.user_phone from life_discount_coupon_user a left join life_user b on a.user_id = b.id ${ew.customSqlSegment}")
    IPage<LifeDiscountCouponUserWebVo> selectPageList(IPage<LifeDiscountCouponUserWebVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponUserWebVo> queryWrapper);

    /**
     * 用户券列表：先按模板（含逻辑删除模板）与用户 tab 筛选，再分页；与内存过滤后再分页语义一致。
     */
    IPage<LifeDiscountCouponUser> selectPageAfterTemplateFilter(IPage<LifeDiscountCouponUser> page,
                                                                  @Param("userId") int userId,
                                                                  @Param("tabType") String tabType,
                                                                  @Param("today") LocalDate today,
                                                                  @Param("sevenDaysLater") LocalDate sevenDaysLater,
                                                                  @Param("sevenDaysAgo") LocalDate sevenDaysAgo,
                                                                  @Param("couponType") Integer couponType,
                                                                  @Param("filterStoreId") String filterStoreId,
                                                                  @Param("storeKeyword") String storeKeyword);

}
