<?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.ruoyi.ss.userCard.mapper.UserCardMapper">

    <resultMap type="UserCardVO" id="CardGetRecordsResult" autoMapping="true" />

    <sql id="selectCardGetRecordsVo">
        select
            cr.record_id,
            cr.user_id,
            u.user_name as userName,
            cr.coupon_id,
            c.name as cardName,
            cr.receive_time,
            cr.get_method,
            cr.status,
            cr.start_time,
            cr.end_time
        from ss_user_card cr
        left join ss_user u on cr.user_id = u.user_id
        left join ss_card c on cr.coupon_id = c.coupon_id
    </sql>

    <sql id="searchCondition">
        <if test="query.userId != null "> and cr.user_id = #{query.userId}</if>
        <if test="query.couponId != null "> and cr.coupon_id = #{query.couponId}</if>
        <if test="query.receiveTime != null "> and cr.receive_time = #{query.receiveTime}</if>
        <if test="query.getMethod != null  and query.getMethod != ''"> and cr.get_method = #{query.getMethod}</if>
        <if test="query.status != null  and query.status != ''"> and cr.status = #{query.status}</if>
        <if test="query.startTime != null "> and cr.start_time = #{query.startTime}</if>
        <if test="query.endTime != null "> and cr.end_time = #{query.endTime}</if>
    </sql>

    <select id="selectUserCardList" parameterType="UserCardQuery" resultMap="CardGetRecordsResult">
        <include refid="selectCardGetRecordsVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectUserCardById" parameterType="Long" resultMap="CardGetRecordsResult">
        <include refid="selectCardGetRecordsVo"/>
        where cr.record_id = #{recordId}
    </select>

    <insert id="insertUserCard" parameterType="UserCard" useGeneratedKeys="true" keyProperty="recordId">
        insert into ss_user_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="couponId != null">coupon_id,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="getMethod != null and getMethod != ''">get_method,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="couponId != null">#{couponId},</if>
            <if test="receiveTime != null">#{receiveTime},</if>
            <if test="getMethod != null and getMethod != ''">#{getMethod},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
         </trim>
    </insert>

    <update id="updateUserCard" parameterType="UserCard">
        update ss_user_card
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where record_id = #{data.recordId}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.couponId != null">coupon_id = #{data.couponId},</if>
        <if test="data.receiveTime != null">receive_time = #{data.receiveTime},</if>
        <if test="data.getMethod != null and data.getMethod != ''">get_method = #{data.getMethod},</if>
        <if test="data.status != null and data.status != ''">status = #{data.status},</if>
        <if test="data.startTime != null">start_time = #{data.startTime},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
    </sql>

    <delete id="deleteUserCardById" parameterType="Long">
        delete from ss_user_card where record_id = #{recordId}
    </delete>

    <delete id="deleteUserCardByIds" parameterType="String">
        delete from ss_user_card where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>
