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

    <resultMap id="VenueLessonTicketMap" type="com.sv.entity.VenueLessonTicket">

        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="venue_type" property="venueType"/>
        <result column="venue_id" property="venueId"/>
        <result column="ticket" property="ticket"/>
        <result column="lesson_id" property="lessonId"/>
        <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_venue_lesson_ticket
	</sql>

    <!-- 字段 -->
    <sql id="Field">
		id,
		venue_type,
		venue_id,
		ticket,
		lesson_id,
		status,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{veneuType, jdbcType=TINYINT},
		#{venueId, jdbcType=INTEGER},
		#{ticket, jdbcType=VARCHAR},
		#{lessonId, jdbcType=INTEGER},
		#{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="VenueLessonTicketMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE
        deleted = 0
    </select>

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

        INSERT INTO
        <include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="veneuType != null">
                venue_type,
            </if>
            <if test="venueId != null">
                venue_id,
            </if>
            <if test="ticket != null">
                ticket,
            </if>
            <if test="lessonId != null">
                lesson_id,
            </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="veneuType != null">
                #{veneuType},
            </if>
            <if test="venueId != null">
                #{venueId},
            </if>
            <if test="ticket != null">
                #{ticket},
            </if>
            <if test="lessonId != null">
                #{lessonId},
            </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.VenueLessonTicket">
        UPDATE
        <include refid="tableName"></include>
        <set>
            <if test="veneuType != null">
                venue_type = #{veneuType},
            </if>
            <if test="venueId != null">
                venue_id = #{venueId},
            </if>
            <if test="ticket != null">
                ticket = #{ticket},
            </if>
            <if test="lessonId != null">
                lesson_id = #{lessonId},
            </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>

    <!-- 通过编号查询 -->
    <select id="findById" parameterType="java.lang.Integer" resultMap="VenueLessonTicketMap">
        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>

    <insert id="insertTickets">
        INSERT INTO sv_venue_lesson_ticket
        (venue_type,venue_id,ticket,lesson_id,status,platform_id)
        VALUES
        <foreach item="item" index="index" collection="tickets"  separator=",">
            (#{venueType},#{venueId},#{item},#{lessonId},0,1)
        </foreach>
    </insert>

    <select id="countByTicketAndLesson" resultType="java.lang.Integer">
        SELECT COUNT(0) from sv_venue_lesson_ticket
        WHERE venue_type = #{venueType} and venue_id = #{venueId}
        and ticket = #{ticket} and lesson_id = #{lessonId}
        and deleted = 0
    </select>

    <select id="findNoUsedTicket" resultMap="VenueLessonTicketMap">
        SELECT * from sv_venue_lesson_ticket
        WHERE lesson_id = #{lessonId} and status = 0 and deleted = 0 for update
    </select>

    <delete id="deletedTickets">
        DELETE FROM sv_venue_lesson_ticket
        WHERE
        status = 0
        and id IN
        <foreach item="item" index="index" collection="tickets" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </delete>

    <!-- api 预约课程，查询场馆课程票号总条数 -->
    <select id="findCount" resultType="java.lang.Integer">
        SELECT
	      COUNT(0)
        FROM
            sv_venue_lesson_ticket
        WHERE
            lesson_id = #{lessonId}
        AND deleted = 0
        AND `status` = 0
    </select>

    <!-- api 修改场馆课程票号状态为已购买 -->
    <update id="updateLessonTicketStatus">
        UPDATE
          sv_venue_lesson_ticket
        SET
          `status` = #{status}
        WHERE
         id
        IN
        <foreach collection="tickets" item="tickets" index="index" open="(" close=")" separator=",">
            #{tickets.id}
        </foreach>
        AND
          deleted = 0
        AND  `status` = 0
    </update>

    <!-- api随机取票 -->
    <select id="findRandom" resultMap="VenueLessonTicketMap">
        select
            id,
            venue_type,
            venue_id,
            ticket,
            lesson_id,
            status
        from
          <include refid="tableName"></include>
        where
        lesson_id = #{lessonId}
        AND
        deleted = 0
        AND
        `status` = 0
        ORDER BY id ASC
        LIMIT #{limitSum} for update
    </select>

    <!-- api 修改票据为未购买 -->
    <update id="updateTicketStatus">
        UPDATE
          sv_venue_lesson_ticket
        SET
          `status` = #{status}
        WHERE
          id
        IN
        <foreach collection="memberLessonTickets" item="memberLessonTickets" index="index" open="(" close=")" separator=",">
            #{memberLessonTickets.ticketId}
        </foreach>
        AND
          deleted = 0
    </update>


    <!-- api -->
    <select id="findNum" resultType="java.lang.Integer">
        SELECT
	      COUNT(0)
        FROM
            sv_member_lesson_ticket
        WHERE
            lesson_id = #{venueId}
        AND deleted = 0
    </select>
</mapper>