<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bomaos.carmi.mapper.CardsMapper">

    <!-- 关联查询sql -->
    <sql id="relSelect">
        SELECT a.*
        FROM sys_cards a
        <where>
            <if test="page!=null and page.pageData!=null">
                <if test="page.pageData.id != null">
                    AND a.id = #{page.pageData.id}
                </if>
                <if test="page.pageData.productId != null">
                    AND a.product_id = #{page.pageData.productId}
                </if>
                <if test="page.pageData.cardInfo != null">
                    AND a.card_info LIKE CONCAT('%', #{page.pageData.cardInfo}, '%')
                </if>
                <if test="page.pageData.status != null">
                    AND a.status = #{page.pageData.status}
                </if>
                <if test="page.pageData.sellType != null">
                    AND a.sell_type = #{page.pageData.sellType}
                </if>
                <if test="page.pageData.number != null">
                    AND a.number = #{page.pageData.number}
                </if>
                <if test="page.pageData.sellNumber != null">
                    AND a.sell_number = #{page.pageData.sellNumber}
                </if>
                <if test="page.pageData.createdAt != null">
                    AND a.created_at LIKE CONCAT('%', #{page.pageData.createdAt}, '%')
                </if>
                <if test="page.pageData.updatedAt != null">
                    AND a.updated_at LIKE CONCAT('%', #{page.pageData.updatedAt}, '%')
                </if>
                <if test="page.pageData.supplierId != null">
                    AND a.supplier_id = #{page.pageData.supplierId}
                </if>
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="listPage" resultType="com.bomaos.carmi.entity.Cards">
        <include refid="relSelect"></include>
    </select>

    <!-- 查询全部 -->
    <select id="listAll" resultType="com.bomaos.carmi.entity.Cards">
        <include refid="relSelect"></include>
    </select>

    <select id="getCard" resultType="com.bomaos.carmi.entity.Cards">
        select *
        from sys_cards
        WHERE status = #{status}
          and product_id = #{productId}
        ORDER BY rand() LIMIT #{number}
    </select>

</mapper>
