<?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="cn.iocoder.mall.promotion.biz.dao.CouponCardMapper">

    <sql id="FIELDS">
        id, template_id, title, status, user_id, take_type,
        price_available, valid_start_time, valid_end_time, preferential_type, percent_off, price_off,
        discount_price_limit, used_order_id,  used_price, used_time,
        create_time
    </sql>

<!--    <select id="selectListByPidAndStatusOrderBySort" resultType="CouponCardDO">-->
<!--        SELECT-->
<!--            <include refid="FIELDS" />-->
<!--        FROM coupon_card-->
<!--        WHERE pid = #{pid}-->
<!--        AND status = #{status}-->
<!--        AND deleted = 0-->
<!--        ORDER BY sort ASC-->
<!--    </select>-->

<!--    <select id="selectList" resultType="CouponCardDO">-->
<!--        SELECT-->
<!--          <include refid="FIELDS" />-->
<!--        FROM coupon_card-->
<!--        WHERE deleted = 0-->
<!--    </select>-->

    <select id="selectById" parameterType="Integer" resultType="CouponCardDO">
        SELECT
          <include refid="FIELDS" />
        FROM coupon_card
        WHERE id = #{id}
    </select>

    <select id="selectListByUserIdAndStatus" resultType="CouponCardDO">
        SELECT
            <include refid="FIELDS" />
        FROM coupon_card
        <where>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </select>

    <select id="selectListByPage" resultType="CouponCardDO">
        SELECT
          <include refid="FIELDS" />
        FROM coupon_card
        <where>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
        LIMIT #{offset}, #{limit}
    </select>

    <select id="selectCountByPage" resultType="Integer">
        SELECT
          COUNT(1)
        FROM coupon_card
        <where>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </select>

    <select id="selectCountByUserIdAndTemplateId" resultType="Integer">
        SELECT
            COUNT(1)
        FROM coupon_card
        <where>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="templateId != null">
                AND template_id = #{templateId}
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="CouponCardDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO coupon_card (
          template_id, title, status, user_id, take_type,
          price_available, valid_start_time, valid_end_time, preferential_type, percent_off, price_off,
          discount_price_limit, used_order_id,  used_price, used_time,
          create_time
        ) VALUES (
          #{templateId}, #{title}, #{status}, #{userId}, #{takeType},
          #{priceAvailable}, #{validStartTime}, #{validEndTime}, #{preferentialType}, #{percentOff}, #{priceOff},
          #{discountPriceLimit}, #{usedOrderId}, #{usedPrice}, #{usedTime},
          #{createTime}
        )
    </insert>

    <update id="update" parameterType="CouponCardDO">
        UPDATE coupon_card
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="usedOrderId != null">
                used_order_id = #{usedOrderId},
            </if>
            <if test="usedPrice != null">
                used_price = #{usedPrice},
            </if>
            <if test="usedTime != null">
                used_time = #{usedTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

</mapper>