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

<resultMap id="VenuePriceMap" type="com.sv.entity.VenuePrice">

		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="price" property="price" />
		<result column="venue_id" property="venueId" />
		<result column="venue_tye" property="venueTye" />
		<result column="star_time" property="starTime" />
		<result column="end_time" property="endTime" />
		<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_venue_price
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		price,
		venue_id,
		venue_tye,
		star_time,
		end_time,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{price, jdbcType=DECIMAL},
		#{venueId, jdbcType=INTEGER},
		#{venueTye, jdbcType=TINYINT},
		#{starTime, jdbcType=TIME},
		#{endTime, jdbcType=TIME},
		#{platformId, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
	</sql>

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

    <!-- 查询场馆对应的价格 -->
    <select id="findPrice" resultMap="VenuePriceMap">
        SELECT
		price,star_time,end_time
        FROM
        	<include refid="tableName"></include>
		WHERE
			deleted = 0
		and venue_id = #{venueId}
		and venue_tye = 1
    </select>

	<!-- 新增-->
<insert id="insert" parameterType="com.sv.entity.VenuePrice" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="price != null">
				price,
            </if>
            <if test="venueId != null">
				venue_id,
            </if>
            <if test="venueTye != null">
				venue_tye,
            </if>
            <if test="starTime != null">
				star_time,
            </if>
            <if test="endTime != null">
				end_time,
            </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="price != null">
				#{price},
            </if>
            <if test="venueId != null">
				#{venueId},
            </if>
            <if test="venueTye != null">
				#{venueTye},
            </if>
            <if test="starTime != null">
				#{starTime},
            </if>
            <if test="endTime != null">
				#{endTime},
            </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">
		delete from
			<include refid="tableName"></include>
		WHERE
			venue_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.VenuePrice">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="price != null">
				price = #{price},
			</if>
			<if test="venueId != null">
				venue_id = #{venueId},
			</if>
			<if test="venueTye != null">
				venue_tye = #{venueTye},
			</if>
			<if test="starTime != null">
				star_time = #{starTime},
			</if>
			<if test="endTime != null">
				end_time = #{endTime},
			</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="VenuePriceMap">
		SELECT
			<include refid="Field"></include>
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!-- 判断时间是否有冲突-->
	<select id="checkTime" resultType="java.lang.Integer">
		SELECT
		COUNT(0)
		FROM
		sv_venue_price
		WHERE
		 venue_tye = 1
		and
		 (
            (<![CDATA[ star_time <= #{starTime}]]>  and end_time >= #{starTime})
            or
            (<![CDATA[ star_time <= #{endTime}]]> and end_time >= #{endTime})
        )
		AND venue_id = #{venueId}
		AND deleted = 0
	</select>

    <!-- 根据场馆Id查询对应的时间段的价格 -->
    <select id="findByVenueId" resultMap="VenuePriceMap">
        SELECT
	      price,star_time,end_time
        FROM
            sv_venue_price
        WHERE
            venue_id = #{venueId}
        AND venue_tye = 1
        AND deleted = 0
    </select>

	<!-- api -->
	<select id="findPriceByVenueId" resultMap="VenuePriceMap">
		SELECT
		price
		FROM
		sv_venue_price
		WHERE
		venue_id = #{venueId}
		AND star_time &lt;= #{time}
		AND end_time > #{time}
		AND deleted = 0
		AND venue_tye = 1
	</select>
</mapper>