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

	<resultMap id="VenueCardMap" type="com.sv.entity.VenueCard">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="card_type" property="cardType"  jdbcType="TINYINT" />
		<result column="status" property="status"  jdbcType="TINYINT" />
		<result column="price" property="price"  jdbcType="DECIMAL" />
		<result column="venue_id" property="venueId"  jdbcType="INTEGER" />
		<result column="venue_type" property="venueType"  jdbcType="TINYINT" />
		<result column="created_id" property="createdId"  jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId"  jdbcType="INTEGER" />
		<result column="created_time" property="createdTime"  jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime"  jdbcType="TIMESTAMP" />
		<result column="deleted" property="deleted"  jdbcType="TINYINT" />
		<result column="platform_id" property="platformId"  jdbcType="INTEGER" />
	</resultMap>

	<resultMap id="VenueCardOmsDtoMap" type="com.sv.dto.oms.VenueCardOmsDto" extends="VenueCardMap">
		<result column="name" property="name" />
	</resultMap>

    <resultMap id="VenueCardDtoMap" type="com.sv.dto.api.VenueCardDTO">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="card_type" property="cardType" jdbcType="TINYINT" />
		<result column="status" property="status" jdbcType="TINYINT" />
		<result column="price" property="price" jdbcType="DECIMAL" />
		<result column="venue_id" property="venueId" jdbcType="INTEGER" />
		<result column="venue_type" property="venueType" jdbcType="TINYINT" />
		<result column="created_id" property="createdId" jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId" jdbcType="INTEGER" />
		<result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP" />
		<result column="deleted" property="deleted" jdbcType="TINYINT" />
		<result column="cardName" property="cardName" />
		<result column="platform_id" property="platformId"/>
    </resultMap>
	
	<!--表名 -->
	<sql id="tableName">
		sv_venue_card
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		card_type,
		status,
		price,
		venue_id,
		venue_type,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted,
		platform_id
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{cardType, jdbcType=TINYINT},
		#{status, jdbcType=TINYINT},
		#{price, jdbcType=DECIMAL},
		#{venueId, jdbcType=INTEGER},
		#{venueType, jdbcType=TINYINT},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
		#{platformId, jdbcType=TINYINT}
	</sql>

	<!-- 查询全部记录 -->
	<select id="findAlls" resultMap="VenueCardOmsDtoMap">
		SELECT
		svc.id,
		svc.card_type,
		svc.status,
		svc.price,
		svc.venue_id,
		svc.venue_type,
		svc.created_id,
		svc.modified_id,
		svc.created_time,
		svc.modified_time,
		svc.deleted,
		svc.platform_id,
		vipc.name
		FROM
		sv_venue_card AS svc LEFT JOIN sv_vip_card AS vipc ON svc.card_type = vipc.id
		WHERE
		svc.venue_id = #{id}
		AND svc.deleted = 0
		AND vipc.deleted = 0
	</select>

	<!-- 查询全部记录 -->
	<select id="findAll" resultMap="VenueCardMap">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include>
		WHERE
		deleted = 0
	</select>

	<!-- 查询全部记录 -->
	<select id="findVenueCard" resultMap="VenueCardMap">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include>
		WHERE
		deleted = 0
	</select>
	
	<!-- 新增-->
	<insert id="insert" parameterType="com.sv.entity.VenueCard" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="cardType != null">
				card_type,
            </if>
            <if test="status != null">
				status,
            </if>
            <if test="price != null">
				price,
            </if>
            <if test="venueId != null">
				venue_id,
            </if>
            <if test="venueType != null">
				venue_type,
            </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>
            <if test="platformId != null">
				platform_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
				#{id},
            </if>
            <if test="cardType != null">
				#{cardType},
            </if>
            <if test="status != null">
				#{status},
            </if>
            <if test="price != null">
				#{price},
            </if>
            <if test="venueId != null">
				#{venueId},
            </if>
            <if test="venueType != null">
				#{venueType},
            </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>
            <if test="platformId != null">
				#{platformId},
            </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.VenueCard">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="cardType != null">
				card_type = #{cardType},
			</if>
			<if test="status != null">
				status = #{status},
			</if>
			<if test="price != null">
				price = #{price},
			</if>
			<if test="venueId != null">
				venue_id = #{venueId},
			</if>
			<if test="venueType != null">
				venue_type = #{venueType},
			</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>
			<if test="platformId != null">
				platform_id = #{platformId},
			</if>
        </set>
        WHERE id = #{id}
	</update>

    <!-- 通过编号查询 -->
    <select id="findById" parameterType="java.lang.Integer" resultMap="VenueCardMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE id = #{id}
    </select>

    <!-- 通过编号查询 -->
    <select id="findsById" parameterType="java.lang.Integer" resultMap="VenueCardOmsDtoMap">
        SELECT
        svc.id,
        svc.card_type,
        svc.status,
        svc.price,
        svc.venue_id,
        svc.venue_type,
        svc.created_id,
        svc.modified_id,
        svc.created_time,
        svc.modified_time,
        svc.deleted,
        svc.platform_id,
        vipc.name
        FROM
        sv_venue_card AS svc LEFT JOIN sv_vip_card AS vipc ON svc.card_type = vipc.id
        WHERE svc.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>

	<!-- api 根据场馆id,类型，查找场馆列表会员卡 -->
	<select id="findByVenueId" resultMap="VenueCardDtoMap">
		SELECT
		  c.id,
		  c.card_type,
	      c.price,
	      v.`name` as cardName
        FROM
            sv_venue_card as c LEFT JOIN sv_vip_card as v on c.card_type = v.id
        WHERE
            c.venue_id = #{venueId}
        AND c.venue_type = #{venueType}
        AND c.`status` = 0
        AND c.deleted = 0
        AND v.deleted = 0
		AND c.card_type != 5
	</select>

    <!-- api 根据场馆ID，会员卡类型，场馆类型查找购买会员需要的金额 -->
    <select id="findPrice" resultMap="VenueCardMap">
        SELECT
            id,
            `status`,
            price
        FROM
            sv_venue_card
        WHERE
            card_type = #{cardType}
        AND venue_type = #{type}
        AND venue_id = #{venueId}
        AND deleted = 0
    </select>

	<select id="countByVenue" resultType="java.lang.Integer">
		SELECT count(0) FROM sv_venue_card
		WHERE venue_id = #{venueId} and venue_type = #{venueType}
		and card_type = #{cardType}
		<if test="excludeId != null">
			and id != #{excludeId}
		</if>
		and deleted = 0
	</select>
</mapper>