<?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="beilv.usermembershipcard.mapper.UserMembershipCardMapper">

    <resultMap type="UserMembershipCard" id="UserMembershipCardResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="cardId"    column="card_id"    />
        <result property="totalNumber"    column="total_number"    />
        <result property="remainingNumber"    column="remaining_number"    />
        <result property="version"    column="version"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="userName"    column="username"    />
        <result property="realName"    column="real_name"    />
        <result property="mobile"    column="mobile"    />
        <result property="cardName"    column="cardName"    />
    </resultMap>

    <sql id="selectUserMembershipCardVo">
        select id, user_id, card_id, total_number, remaining_number, version, create_by, create_time, update_by, update_time from user_membership_card
    </sql>

    <select id="selectUserMembershipCardList" parameterType="UserMembershipCard" resultMap="UserMembershipCardResult">
        SELECT
            a.id,
            a.user_id,
            b.username,
            b.real_name,
            b.mobile,
            a.card_id,
            c.name cardName,
            a.total_number,
            a.remaining_number
        FROM
            user_membership_card a
            left join sys_member b on a.user_id = b.id
            left join card_information c on a.card_id = c.id
        <where>
            <if test="userId != null  and userId != ''"> and a.user_id = #{userId}</if>
            <if test="cardId != null  and cardId != ''"> and a.card_id = #{cardId}</if>
            <if test="totalNumber != null "> and a.total_number = #{totalNumber}</if>
            <if test="remainingNumber != null "> and a.remaining_number = #{remainingNumber}</if>
            <if test="version != null  and version != ''"> and a.version = #{version}</if>
            <if test="realName != null  and realName != ''"> and b.real_name like concat('%', #{realName}, '%')</if>
            <if test="mobile != null  and mobile != ''"> and b.mobile like concat('%', #{mobile}, '%')</if>
            <if test="cardName != null  and cardName != ''"> and c.name like concat('%', #{cardName}, '%')</if>
        </where>
        order by a.create_time desc
    </select>

    <select id="selectUserMembershipCardById" parameterType="Long" resultMap="UserMembershipCardResult">
        <include refid="selectUserMembershipCardVo"/>
        where id = #{id}
    </select>

    <insert id="insertUserMembershipCard" parameterType="UserMembershipCard">
        insert into user_membership_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="cardId != null">card_id,</if>
            <if test="totalNumber != null">total_number,</if>
            <if test="remainingNumber != null">remaining_number,</if>
            <if test="version != null">version,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="cardId != null">#{cardId},</if>
            <if test="totalNumber != null">#{totalNumber},</if>
            <if test="remainingNumber != null">#{remainingNumber},</if>
            <if test="version != null">#{version},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateUserMembershipCard" parameterType="UserMembershipCard">
        update user_membership_card
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="cardId != null">card_id = #{cardId},</if>
            <if test="totalNumber != null">total_number = #{totalNumber},</if>
            <if test="remainingNumber != null">remaining_number = #{remainingNumber},</if>
            <if test="version != null">version = #{version},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUserMembershipCardById" parameterType="Long">
        delete from user_membership_card where id = #{id}
    </delete>

    <delete id="deleteUserMembershipCardByIds" parameterType="String">
        delete from user_membership_card where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
