<?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.gxwebsoft.shop.mapper.ShopCouponMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,
               COALESCE((SELECT COUNT(*) FROM shop_user_coupon suc WHERE suc.coupon_id = a.id AND suc.deleted = 0), 0) AS receive_num
        FROM shop_coupon a
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.name != null">
                AND a.name LIKE CONCAT('%', #{param.name}, '%')
            </if>
            <if test="param.description != null">
                AND a.description LIKE CONCAT('%', #{param.description}, '%')
            </if>
            <if test="param.type != null">
                AND a.type = #{param.type}
            </if>
            <if test="param.reducePrice != null">
                AND a.reduce_price = #{param.reducePrice}
            </if>
            <if test="param.discount != null">
                AND a.discount = #{param.discount}
            </if>
            <if test="param.minPrice != null">
                AND a.min_price = #{param.minPrice}
            </if>
            <if test="param.expireType != null">
                AND a.expire_type = #{param.expireType}
            </if>
            <if test="param.expireDay != null">
                AND a.expire_day = #{param.expireDay}
            </if>
            <if test="param.startTime != null">
                AND a.start_time LIKE CONCAT('%', #{param.startTime}, '%')
            </if>
            <if test="param.endTime != null">
                AND a.end_time LIKE CONCAT('%', #{param.endTime}, '%')
            </if>
            <if test="param.applyRange != null">
                AND a.apply_range = #{param.applyRange}
            </if>
            <if test="param.applyRangeConfig != null">
                AND a.apply_range_config LIKE CONCAT('%', #{param.applyRangeConfig}, '%')
            </if>
            <if test="param.isExpire != null">
                AND a.is_expire = #{param.isExpire}
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.totalCount != null">
                AND a.total_count = #{param.totalCount}
            </if>
            <if test="param.issuedCount != null">
                AND a.issued_count = #{param.issuedCount}
            </if>
            <if test="param.limitPerUser != null">
                AND a.limit_per_user = #{param.limitPerUser}
            </if>
            <if test="param.enabled != null">
                AND a.enabled = #{param.enabled}
            </if>
            <if test="param.keywords != null">
                AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
                )
              </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.ShopCoupon">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.shop.entity.ShopCoupon">
        <include refid="selectSql"></include>
    </select>

</mapper>
