<?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.sv.mapper.GiftMemberCardMapper">

<resultMap id="GiftMemberCardMap" type="com.sv.entity.GiftMemberCard">

		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="status" property="status" />
		<result column="member_id" property="memberId" />
		<result column="card_type" property="cardType" />
		<result column="venue_type" property="venueType" />
		<result column="venue_id" property="venueId" />
		<result column="platform_id" property="platformId" />
		<result column="created_id" property="createdId" />
		<result column="modified_id" property="modifiedId" />
		<result column="created_time" property="createdTime" />
		<result column="modified_time" property="modifiedTime" />
		<result column="deleted" property="deleted" />
		<result column="nickname" property="nickname" />
		<result column="mobile" property="mobile" />
		<result column="name" property="name" />
    </resultMap>
	
	<!--表名 -->
	<sql id="tableName">
		sv_gift_member_card
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		status,
		member_id,
		card_type,
		venue_type,
		venue_id,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{status, jdbcType=TINYINT},
		#{memberId, jdbcType=INTEGER},
		#{cardType, jdbcType=VARCHAR},
		#{venueType, jdbcType=TINYINT},
		#{venueId, jdbcType=INTEGER},
		#{platformId, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
	</sql>

	<!-- 查询全部记录 -->
    <select id="findAll" resultMap="GiftMemberCardMap">
        SELECT
			sgmc.id,
			sgmc.status,
			sgmc.member_id,
			sgmc.card_type,
			sgmc.venue_type,
			sgmc.venue_id,
			sgmc.platform_id,
			sgmc.created_id,
			sgmc.modified_id,
			sgmc.created_time,
			sgmc.modified_time,
			sgmc.deleted,
			sm.nickname,
			sm.mobile,
			sv.name
        FROM
        	sv_gift_member_card AS sgmc
        	LEFT JOIN sv_member AS sm ON sgmc.member_id = sm.id
        	LEFT JOIN sv_venue AS sv ON sgmc.venue_id = sv.id
		WHERE
			sgmc.deleted = 0
			AND sm.deleted = 0
			AND sv.deleted = 0
		<if test="name != ''">
			AND sv.name like '%${name}%'
		</if>
    </select>
	
	<!-- 新增-->
<insert id="insert" parameterType="com.sv.entity.GiftMemberCard" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="status != null">
				status,
            </if>
            <if test="memberId != null">
				member_id,
            </if>
            <if test="cardType != null">
				card_type,
            </if>
            <if test="venueType != null">
				venue_type,
            </if>
            <if test="venueId != null">
				venue_id,
            </if>
            <if test="platformId != null">
				platform_id,
            </if>
            <if test="createdId != null">
				created_id,
            </if>
            <if test="modifiedId != null">
				modified_id,
            </if>
            <if test="createdTime != null">
				created_time,
            </if>
            <if test="modifiedTime != null">
				modified_time,
            </if>
            <if test="deleted != null">
				deleted,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
				#{id},
            </if>
            <if test="status != null">
				#{status},
            </if>
            <if test="memberId != null">
				#{memberId},
            </if>
            <if test="cardType != null">
				#{cardType},
            </if>
            <if test="venueType != null">
				#{venueType},
            </if>
            <if test="venueId != null">
				#{venueId},
            </if>
            <if test="platformId != null">
				#{platformId},
            </if>
            <if test="createdId != null">
				#{createdId},
            </if>
            <if test="modifiedId != null">
				#{modifiedId},
            </if>
            <if test="createdTime != null">
				#{createdTime},
            </if>
            <if test="modifiedTime != null">
				#{modifiedTime},
            </if>
            <if test="deleted != null">
				#{deleted},
            </if>
        </trim>
	</insert>
	
	<!-- 删除-->
	<delete id="delete" parameterType="java.lang.Integer">
		UPDATE
			<include refid="tableName"></include>
		SET
			deleted = 1
		WHERE
			id = #{id}
	</delete>

    <!-- 批量删除 -->
    <delete id="deleteByIds">
        UPDATE
        	<include refid="tableName"></include>
		SET
			deleted = 1
        WHERE
			id IN
			<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
				#{item}
			</foreach>
    </delete>
	
	<!-- 更新记录 -->
<update id="update" parameterType="com.sv.entity.GiftMemberCard">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="status != null">
				status = #{status},
			</if>
			<if test="memberId != null">
				member_id = #{memberId},
			</if>
			<if test="cardType != null">
				card_type = #{cardType},
			</if>
			<if test="venueType != null">
				venue_type = #{venueType},
			</if>
			<if test="venueId != null">
				venue_id = #{venueId},
			</if>
			<if test="platformId != null">
				platform_id = #{platformId},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="createdTime != null">
				created_time = #{createdTime},
			</if>
			<if test="modifiedTime != null">
				modified_time = #{modifiedTime},
			</if>
			<if test="deleted != null">
				deleted = #{deleted},
			</if>
        </set>
        WHERE id = #{id}
	</update>
	
	<!-- 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="GiftMemberCardMap">
		SELECT
			<include refid="Field"></include>
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

    <!--更新送会员卡记录状态-->
    <update id="updateStatus">
        UPDATE
        	<include refid="tableName"></include>
        SET
        	status = #{status, jdbcType=TINYINT}
        WHERE
        	id
        IN
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
			#{id}
        </foreach>
    </update>

</mapper>