<?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.zsElectric.boot.business.mapper.CouponMapper">

    <!-- 获取优惠劵分页列表 -->
    <select id="getCouponPage" resultType="com.zsElectric.boot.business.model.vo.CouponVO">
        SELECT
        id,
        template_id,
        name,
        coupon_code,
        status,
        description,
        user_id,
        take_type,
        take_time,
        expire_time,
        use_order_id,
        use_time,
        create_by,
        create_time,
        update_by,
        update_time,
        is_deleted
        FROM
        c_coupon
        <where>
        is_deleted = 0
        </where>
        ORDER BY create_time DESC
    </select>
    <select id="getUserCouponPage" resultType="com.zsElectric.boot.business.model.vo.CouponVO">
        SELECT
        id,
        template_id,
        name,
        coupon_code,
        status,
        description,
        user_id,
        take_type,
        take_time,
        expire_time,
        use_order_id,
        use_time,
        create_by,
        create_time,
        update_by,
        update_time,
        is_deleted
        FROM
        c_coupon
        <where>
            is_deleted = 0
            <if test="queryParams.userId != null">
                and user_id = #{queryParams.userId}
            </if>
            <if test="queryParams.status != null">
                and status = #{queryParams.status}
            </if>
        </where>
    </select>
    <select id="getCouponStatusNum" resultType="com.zsElectric.boot.business.model.vo.applet.AppCouponStatusNumVO">
        SELECT
            COUNT(CASE WHEN STATUS = 1 THEN 1 END) AS unusedNum,
            COUNT(CASE WHEN STATUS = 2 THEN 1 END) AS usedNum,
            COUNT(CASE WHEN STATUS = 3 THEN 1 END) AS expiredNum
        FROM c_coupon
        WHERE is_deleted = 0
          AND user_id = #{userId}
    </select>

</mapper>
