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

    <resultMap id="VenueLessonMap" type="com.sv.entity.VenueLesson">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="venue_type" property="venueType"/>
        <result column="venue_id" property="venueId"/>
        <result column="name" property="name"/>
        <result column="date" property="date"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="num" property="num"/>
        <result column="sale_num" property="saleNum"/>
        <result column="coach_id" property="coachId"/>
        <result column="qr_code" property="qrCode"/>
        <result column="description" property="description"/>
        <result column="price" property="price"/>
        <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="note" property="note"/>
        <result column="type" property="type"></result>
    </resultMap>
    <resultMap id="VenueLessonOrderDtoMap" type="com.sv.dto.api.VenueLessonDTO" extends="VenueLessonMap">
        <result column="address" property="address"/>
    </resultMap>
    <resultMap id="VenueLessonDtoMap" type="com.sv.dto.api.VenueLessonDTO" extends="VenueLessonMap">
        <association property="coach" select="com.sv.mapper.CoachMapper.findById" column="coach_id"></association>
        <association property="venueLessonTagConfigs" select="com.sv.mapper.VenueLessonTagMapper.findByLessonId" column="id"></association>
    </resultMap>
    <resultMap id="LessonDtoMap" type="com.sv.dto.api.VenueLessonDTO" extends="VenueLessonMap">
        <association property="coach" select="com.sv.mapper.CoachMapper.findById" column="coach_id"></association>
        <association property="venueLessonImages" select="com.sv.mapper.VenueLessonImageMapper.findByLessonId" column="id"></association>
    </resultMap>

    <resultMap id="VenueLessonOmsDTOMap" type="com.sv.dto.oms.VenueLessonOmsDTO" extends="VenueLessonMap">
        <result column="coach_name" property="coachName"></result>
        <collection property="lessonImage" select="findImagesById" column="id"></collection>
        <collection property="tags" select="findTagsById" column="id"></collection>
    </resultMap>
    <!--表名 -->
    <sql id="tableName">
		sv_venue_lesson
	</sql>

    <!-- 字段 -->
    <sql id="Field">
		id,
		venue_type,
		venue_id,
		name,
		date,
		start_time,
		end_time,
		num,
		sale_num,
		coach_id,
		qr_code,
		description,
		price,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted,
		note,
		status,
        type
	</sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{venueType, jdbcType=TINYINT},
		#{venueId, jdbcType=INTEGER},
		#{name, jdbcType=VARCHAR},
		#{date, jdbcType=DATE},
		#{startTime, jdbcType=TIME},
		#{endTime, jdbcType=TIME},
		#{num, jdbcType=INTEGER},
		#{saleNum, jdbcType=INTEGER},
		#{coachId, jdbcType=INTEGER},
		#{qrCode, jdbcType=INTEGER},
		#{description, jdbcType=LONGTEXT},
		#{price, jdbcType=DECIMAL},
		#{platformId, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT},
		#{note, jdbcType=VARCHAR},
		#{status, jdbcType=INTEGER}
	</sql>

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

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

        INSERT INTO
        <include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="venueType != null">
                venue_type,
            </if>
            <if test="venueId != null">
                venue_id,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="date != null">
                date,
            </if>
            <if test="startTime != null">
                start_time,
            </if>
            <if test="endTime != null">
                end_time,
            </if>
            <if test="num != null">
                num,
            </if>
            <if test="saleNum != null">
                sale_num,
            </if>
            <if test="coachId != null">
                coach_id,
            </if>
            <if test="qrCode != null">
                qr_code,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="price != null">
                price,
            </if>
            <if test="platformId != null">
                platform_id,
            </if>
            <if test="note != null">
                note,
            </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="venueType != null">
                #{venueType},
            </if>
            <if test="venueId != null">
                #{venueId},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="date != null">
                #{date},
            </if>
            <if test="startTime != null">
                #{startTime},
            </if>
            <if test="endTime != null">
                #{endTime},
            </if>
            <if test="num != null">
                #{num},
            </if>
            <if test="saleNum != null">
                #{saleNum},
            </if>
            <if test="coachId != null">
                #{coachId},
            </if>
            <if test="qrCode != null">
                #{qrCode},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="price != null">
                #{price},
            </if>
            <if test="platformId != null">
                #{platformId},
            </if>
            <if test="note != null">
                #{note},
            </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.VenueLesson">
        UPDATE
        <include refid="tableName"></include>
        <set>
            <if test="venueType != null">
                venue_type = #{venueType},
            </if>
            <if test="venueId != null">
                venue_id = #{venueId},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="date != null">
                date = #{date},
            </if>
            <if test="startTime != null">
                start_time = #{startTime},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
            <if test="num != null">
                num = #{num},
            </if>
            <if test="saleNum != null">
                sale_num = #{saleNum},
            </if>
            <if test="coachId != null">
                coach_id = #{coachId},
            </if>
            <if test="qrCode != null">
                qr_code = #{qrCode},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="price != null">
                price = #{price},
            </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>
            <if test="note != null">
                note = #{note},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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


    <select id="findByIdForOms" parameterType="java.lang.Integer" resultMap="VenueLessonOmsDTOMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE id = #{id}
    </select>

    <!-- 查询全部记录 -->
    <select id="findAllForOms" resultMap="VenueLessonOmsDTOMap">
        SELECT
        v.*,c.nickname as coach_name
        FROM
        <include refid="tableName"></include> as v
        LEFT JOIN sv_coach as c
        on c.id = v.coach_id
        WHERE
        v.deleted = 0
        <if test="rightTime != null">
            and v.start_time >= #{leftTime}
            and v.end_time &lt;= #{rightTime}
        </if>
        order by v.`date` desc,v.start_time asc
    </select>

    <select id="findImagesById" resultType="java.lang.String">
        select url from sv_venue_lesson_image where lesson_id = #{id} and deleted = 0
    </select>

    <select id="findTagsById" resultType="java.lang.String">
        select name from sv_venue_lesson_tag
        WHERE  lesson_id = #{id} and deleted = 0
    </select>

    <update id="deleteImagesById">
        UPDATE sv_venue_lesson_image SET deleted = 1
        where lesson_id = #{id}
    </update>

    <insert id="insertImages">
        INSERT INTO sv_venue_lesson_image
        (lesson_id,url,platform_id)
        VALUES
        <foreach item="item" index="index" collection="urls"  separator=",">
            (#{lessonId},#{item},1)
        </foreach>
    </insert>

    <select id="countByTime" resultType="java.lang.Integer">
      SELECT count(0) FROM sv_venue_lesson
      WHERE
        (
            (<![CDATA[ start_time <= #{startTime}]]>  and end_time >= #{startTime})
            or
            (<![CDATA[ start_time <= #{endTime}]]> and end_time >= #{endTime})
        )
        and deleted = 0 and venue_id = #{venueId} and venue_type = #{venueType} and `date` = #{date}
        <if test="lessonId != null">
            and id != #{lessonId}
        </if>
    </select>

    <select id="countByTimeAndCoach" resultType="java.lang.Integer">
        SELECT count(0) FROM sv_venue_lesson
        WHERE
        (
        (<![CDATA[ start_time <= #{startTime}]]>  and end_time >= #{startTime})
        or
        (<![CDATA[ start_time <= #{endTime}]]> and end_time >= #{endTime})
        )
        and deleted = 0 and coach_id = #{coachId} and `date` = #{date}
        <if test="lessonId != null">
            and id != #{lessonId}
        </if>
    </select>

    <!-- api 查询对应场馆课程的上周，这周下2周数据，从周一开始，周日结束 -->
    <select id="findShow" resultMap="VenueLessonMap">
        SELECT
          date,
          venue_id,
          venue_type
        FROM
          sv_venue_lesson
        WHERE
          date >= #{starTime}
        AND
          date &lt;= #{endTime}
        AND
          venue_id = #{venueId}
        AND
          venue_type = #{type}
        AND `status` = 1
        AND deleted = 0
        GROUP BY date
    </select>

    <!-- api 查询预约课程上课时间对应的课程信息 -->
    <select id="lessonShow" resultMap="VenueLessonDtoMap">
        SELECT
            id,
            date,
            start_time,
            end_time,
            price,
            sale_num,
            num,
            coach_id,
            name
        FROM
            sv_venue_lesson
        WHERE
            date = #{date}
        AND venue_id = #{venueId}
        AND venue_type = #{type}
        AND `status` = 1
        AND deleted = 0
        ORDER BY start_time ASC
    </select>

    <!-- api 查询课程详情 -->
    <select id="findByLessonId" resultMap="LessonDtoMap">
        SELECT
            le.id,
            le.venue_id,
            le.venue_type,
            le.`name`,
            le.date,
            le.start_time,
            le.end_time,
            ve.address as address,
            le.description,
            le.sale_num,
            le.price,
            le.coach_id,
            le.num,
            le.note,
            ve.latitude,
            ve.longitude,
            le.type,
            ve.`name` AS venueName
        FROM
            sv_venue_lesson AS le
        LEFT JOIN sv_venue AS ve ON le.venue_id = ve.id
        WHERE
            le.id = #{lessonId}
        AND le.deleted = 0
        AND le.`status` = 1
    </select>

    <!-- api 显示预约订单 -->
    <select id="findByLessonAndMemberCard" resultMap="VenueLessonDtoMap">
        SELECT
            vl.`name`,
            vl.date,
            vl.start_time,
            vl.end_time,
            ue.address as address,
            vl.price,
            vl.num,
            vl.sale_num,
            vl.note
        FROM
            sv_venue_lesson AS vl
        LEFT JOIN sv_venue AS ue ON vl.venue_id = ue.id
        WHERE
            venue_id = #{venueId}
        AND venue_type = #{venueType}
        AND vl.id = #{lessonId}
    </select>

    <!-- api 修改课程销售数量 -->
    <update id="updateSaleNumById" >
        UPDATE
          sv_venue_lesson
        SET
          sale_num = sale_num + #{num}
        WHERE
          id = #{lessonId}
    </update>

    <!-- api 查找用户预约对应的课程信息 -->
    <select id="findMemberBillById" resultMap="VenueLessonMap">
        SELECT
	      `name`,
	      date,
	      start_time,
	      end_time
        FROM
            sv_venue_lesson
        WHERE
	        id = #{id}
    </select>

    <!-- api 签到查询对应的课程 -->
    <select id="findLessonByTime" resultMap="VenueLessonMap">
        SELECT
            id,
            venue_id,
            venue_type,
            date,
            start_time,
            end_time,
            coach_id
        FROM
          sv_venue_lesson
        WHERE
          coach_id = #{coachId}
        AND date = #{nowDate}
        AND start_time &lt;= #{nowTime}
        AND end_time >= #{nowTime}
        AND deleted = 0
    </select>

    <select id="countByCoachId" resultType="java.lang.Integer">
        SELECT count(0) from sv_venue_lesson
        WHERE coach_id= #{coachId} and deleted = 0
    </select>

    <!-- api 查询当前日期，是否可预约 -->
    <select id="findBook" resultMap="VenueLessonMap">
        SELECT
	     id,sale_num,num
        FROM
            sv_venue_lesson
        WHERE
            venue_id = #{venueId}
        AND venue_type = #{type}
        AND `date` = #{date}
        AND deleted = 0
        AND `status` = 1
    </select>
    <select id="findBooks" resultType="java.lang.Integer">
        SELECT
        COUNT(0)
        FROM
        sv_venue_lesson
        WHERE
        venue_id = #{venueId}
        AND venue_type = #{type}
        AND DATE(date) = #{date}
        AND DATE(NOW()) &lt;= #{date}
        AND deleted = 0
        AND `status` = 1
    </select>

    <!-- 修改库存 -->
    <update id="updateSaleNumBy">
        UPDATE
          sv_venue_lesson
        SET
          sale_num = sale_num - #{num}
        WHERE
        id = #{lessonId}
    </update>
</mapper>