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.StorePlatformBenefits;
import shop.alien.entity.store.vo.StorePlatformBenefitsVo;

import java.util.List;

/**
 * 福利表 Mapper 接口
 *
 * @author ssk
 * @since 2025-12-18
 */
public interface StorePlatformBenefitsMapper extends BaseMapper<StorePlatformBenefits> {

    /**
     * 分页查询用户举报信息
     *
     * @param page         分页对象
     * @param queryWrapper 查询条件包装器
     * @return 分页结果
     */
    @Select("<script>" +
            " select * from ( " +
            " select\n" +
            "\tspb.id,\n" +
            "\tspb.type,\n" +
            "\tspb.business_id,\n" +
            "\tspb.usage_conditions,\n" +
            "\tldc.name,\n" +
            " spb.created_time," +
            " 0 quantity \n" +
            "from\n" +
            "\tstore_platform_benefits spb\n" +
            "inner join life_discount_coupon ldc on\n" +
            "\tspb.business_id = ldc.id and spb.delete_flag = 0 and spb.type = 1 " +
            " union all " +
            " select spb.id, spb.type, spb.business_id, spb.usage_conditions, svc.name, spb.created_time,svc.quantity  " +
            " from store_platform_benefits spb " +
            " inner join store_virtual_currency svc on spb.business_id = svc.id  and spb.delete_flag = 0 and spb.type = 2 " +
            " ) a " +
            " ${ew.customSqlSegment}" +
            "</script>")
    IPage<StorePlatformBenefitsVo> getPlatformBenefitsPage(
            IPage<StorePlatformBenefitsVo> page,
            @Param(Constants.WRAPPER) QueryWrapper<StorePlatformBenefitsVo> queryWrapper
    );
    /**
     * 查询所有福利信息，包含优惠券信息和U币信息
     *
     * @return 福利信息列表
     */
    @Select("select * from ( " +
            " select " +
            " spb.id, " +
            " spb.type, " +
            " spb.business_id, " +
            " spb.usage_conditions, " +
            " spb.status, " +
            " spb.delete_flag, " +
            " spb.created_time, " +
            " spb.created_user_id, " +
            " spb.updated_time, " +
            " spb.updated_user_id, " +
            " ldc.name couponName" +
            " from store_platform_benefits spb " +
            " inner join life_discount_coupon ldc on spb.business_id = ldc.id and spb.delete_flag = 0 and spb.type = 1 " +
            " union all " +
            " select " +
            " spb.id, " +
            " spb.type, " +
            " spb.business_id, " +
            " spb.usage_conditions, " +
            " spb.status, " +
            " spb.delete_flag, " +
            " spb.created_time, " +
            " spb.created_user_id, " +
            " spb.updated_time, " +
            " spb.updated_user_id, " +
            " svc.name couponName" +
            " from store_platform_benefits spb " +
            " inner join store_virtual_currency svc on spb.business_id = svc.id and spb.delete_flag = 0 and spb.type = 2 " +
            " ) a " +
            " order by created_time desc")
    List<StorePlatformBenefitsVo> selectListAndCoupon();
}

