<?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.MemberCardMapper">

	<resultMap id="MemberCardMap" type="com.sv.entity.MemberCard">

		<id column="id" jdbcType="INTEGER" property="id" />
		<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="start_time" property="startTime" />
		<result column="end_time" property="endTime" />
		<result column="status" property="status" />
		<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="name" property="name" />
    </resultMap>

	<resultMap id="MemberCardDtoMap" type="com.sv.dto.api.MemberCardDTO">
		<id column="id" jdbcType="INTEGER" property="id" />
		<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="start_time" property="startTime" />
		<result column="end_time" property="endTime" />
		<result column="status" property="status" />
		<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" />

	</resultMap>
	<!--表名 -->
	<sql id="tableName">
		sv_member_card
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		member_id,
		card_type,
		venue_type,
		venue_id,
		start_time,
		end_time,
		status,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{memberId, jdbcType=INTEGER},
		#{cardType, jdbcType=VARCHAR},
		#{venueType, jdbcType=TINYINT},
		#{venueId, jdbcType=INTEGER},
		#{startTime, jdbcType=TIMESTAMP},
		#{endTime, jdbcType=TIMESTAMP},
		#{status, jdbcType=TINYINT},
		#{platformId, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
	</sql>

	<!-- 查询全部记录 -->
	<select id="findAll" resultMap="MemberCardMap">
		SELECT
		smc.id,
		smc.member_id,
		smc.card_type,
		smc.venue_type,
		smc.venue_id,
		smc.start_time,
		smc.end_time,
		smc.status,
		smc.platform_id,
		smc.created_id,
		smc.modified_id,
		smc.created_time,
		smc.modified_time,
		smc.deleted,
		sm.nickname AS nickname,
		sv.name
		FROM
		sv_member_card AS smc
		LEFT JOIN sv_member AS sm ON smc.member_id = sm.id
		LEFT JOIN sv_venue AS sv ON smc.venue_id = sv.id
		WHERE
		smc.deleted = 0
		AND sv.deleted = 0
		<if test="status != null">
			AND smc.status = #{status}
		</if>
		<if test="veneuType != null">
			AND smc.venue_type = #{veneuType}
		</if>
		<if test="venueName != null">
			AND sv.name like '%${venueName}%'
		</if>
		ORDER BY smc.created_time DESC
	</select>

	<select id="findAlls" resultMap="MemberCardMap">
		SELECT
			smc.id,
			smc.member_id,
			smc.card_type,
			smc.venue_type,
			smc.venue_id,
			smc.start_time,
			smc.end_time,
			smc.status,
			smc.platform_id,
			smc.created_id,
			smc.modified_id,
			smc.created_time,
			smc.modified_time,
			smc.deleted,
			sm.nickname AS nickname,
			sv.name
		FROM
			sv_member_card AS smc
		LEFT JOIN sv_member AS sm ON smc.member_id = sm.id
		LEFT JOIN sv_venue AS sv ON smc.venue_id = sv.id
		WHERE
			smc.deleted = 0
	</select>
	
	<!-- 新增-->
<insert id="insert" parameterType="com.sv.entity.MemberCard" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </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="startTime != null">
				start_time,
            </if>
            <if test="endTime != null">
				end_time,
            </if>
            <if test="status != null">
				status,
            </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="memberId != null">
				#{memberId},
            </if>
            <if test="cardType != null">
				#{cardType},
            </if>
            <if test="venueType != null">
				#{venueType},
            </if>
            <if test="venueId != null">
				#{venueId},
            </if>
            <if test="startTime != null">
				#{startTime},
            </if>
            <if test="endTime != null">
				#{endTime},
            </if>
            <if test="status != null">
				#{status},
            </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.MemberCard">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<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="startTime != null">
				start_time = #{startTime},
			</if>
			<if test="endTime != null">
				end_time = #{endTime},
			</if>
			<if test="status != null">
				status = #{status},
			</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>
	
	<!-- api 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="MemberCardMap">
		SELECT
			id,
			member_id,
			card_type,
			venue_type,
			venue_id,
			start_time,
			end_time,
			status
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!-- api 查找用户是否有激活的会员卡，且在有效期内 -->
	<select id="findCard" resultMap="MemberCardMap">
		SELECT
			start_time,
			end_time,
			card_type
		FROM
			sv_member_card
		WHERE
			member_id = #{memberId}
		AND DATE(end_time) >= DATE(NOW())
		AND deleted = 0
		AND venue_type = #{venueType}
		AND venue_id = #{venueId}
		ORDER BY end_time DESC
		LIMIT 1
	</select>

	<!-- api 我的会员卡 -->
	<select id="findAllCards"  resultMap="MemberCardDtoMap">
		SELECT
			id,
			venue_type,
			venue_id,
			card_type,
			start_time,
			end_time,
			`status`
		FROM
			sv_member_card
		WHERE
			member_id = #{memberId}
		AND
		  	venue_id = #{venueId}
		AND deleted = 0
		ORDER BY
			 end_time &lt; now(),
			IF (end_time &lt; now(), 0, end_time),
			end_time ASC,id ASC
	</select>

	<!-- api 查询用户对应店的会员卡 -->
	<select id="findMemberCardByVenues" resultMap="MemberCardMap">
		SELECT
			id,
			card_type,
			venue_id,
			venue_type,
			start_time,
			end_time,
			`status`
		FROM
			sv_member_card
		WHERE
			member_id = #{memberId}
		AND venue_id = #{venueId}
		AND venue_type = #{venueType}
		AND deleted = 0
		ORDER BY
			`status` DESC,
			start_time DESC
	</select>

	<!-- api 返回用户中心会员拥有的会员卡数量 -->
	<select id="findMemberCardCount" resultType="java.lang.Integer">
		SELECT
			COUNT(0)
		FROM
			sv_member_card
		WHERE
			member_id = #{memberId}
		AND
			deleted = 0
	</select>

	<!-- api 查询激活状态的卡，同家店中 -->
	<select id="findByStatus" resultMap="MemberCardMap">
		SELECT
			id,
			`status`,
			member_id
		FROM
			sv_member_card
		WHERE
			venue_id = #{venueId}
		AND venue_type = #{venueType}
		AND member_id = #{memberId}
		AND `status` = 1
		AND deleted = 0
	</select>

    <!-- api 查询用户有没有对应篮球馆的可使用会员卡 -->
    <select id="findByMemberId" resultMap="MemberCardMap">
        SELECT
        	id,
          	member_id,
	        card_type
        FROM
          sv_member_card
        WHERE
          venue_id = #{venueId}
        AND venue_type = 1
        AND DATE(start_time) &lt;= DATE(NOW())
        AND DATE(end_time) >= DATE(NOW())
        AND member_id = #{memberId}
        AND deleted = 0
		ORDER BY card_type asc,status desc
    </select>

	<!-- api 查询用户已过期的会员卡 -->
	<select id="findAllCard" resultMap="MemberCardMap">
		SELECT
			member_id,
			id,
			`status`
		FROM
			sv_member_card
		WHERE
		DATE(end_time) &lt; DATE(NOW())
		AND `status` = 1
		AND deleted = 0
	</select>

	<!-- api 查询用户顺延的会员卡正好是今日 -->
	<select id="findAllMemberCard" resultMap="MemberCardMap">
		SELECT
			member_id,
			id,
			`status`,
			venue_type,
			venue_id
		FROM
			sv_member_card
		WHERE
			DATE(start_time) = DATE(NOW())
		AND member_id IN
		<foreach collection="cards" item="cards" index="index" open="(" close=")" separator=",">
			#{cards.memberId}
		</foreach>
		AND deleted = 0
		AND `status` = 0
	</select>

	<!-- api 批量修改用户会员卡状态 -->
	<update id="updateStatus" >
		UPDATE sv_member_card
		SET `status` = #{status}
		AND id IN
		<foreach collection="cards" item="cards" index="index" open="(" close=")" separator=",">
			#{cards.id}
		</foreach>
		AND deleted = 0
	</update>

	<!-- api  查询用户拥有对应店的会员卡 -->
	<select id="findCards" resultMap="MemberCardMap">
		SELECT
			id,
			card_type,
			venue_id,
			venue_type,
			start_time,
			end_time,
			`status`
		FROM
			sv_member_card
		WHERE
			member_id = #{memberId}
		AND venue_id = #{venueId}
		AND venue_type = #{venueType}
		AND DATE(start_time) = DATE(NOW())
		AND deleted = 0
		ORDER BY card_type DESC
		LIMIT 1
	</select>
</mapper>