<?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.fs.course.mapper.FsUserCoursePeriodMapper">

    <resultMap type="FsUserCoursePeriod" id="FsUserCoursePeriodResult">
        <result property="periodId"    column="period_id"    />
        <result property="periodName"    column="period_name"    />
        <result property="companyId"    column="company_id"    />
        <result property="trainingCampId"    column="training_camp_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="courseStyle"    column="course_style"    />
        <result property="liveRoomStyle"    column="live_room_style"    />
        <result property="redPacketGrantMethod"    column="red_packet_grant_method"    />
        <result property="periodType"    column="period_type"    />
        <result property="periodStartingTime"    column="period_starting_time"    />
        <result property="periodEndTime"    column="period_end_time"    />
    </resultMap>

    <sql id="selectFsUserCoursePeriodVo">
        select * from fs_user_course_period
    </sql>

    <select id="selectFsUserCoursePeriodList" parameterType="FsUserCoursePeriod" resultMap="FsUserCoursePeriodResult">
        <include refid="selectFsUserCoursePeriodVo"/>
        <where>
            <if test="periodName != null  and periodName != ''"> and period_name like concat('%', #{periodName}, '%')</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="trainingCampId != null "> and training_camp_id = #{trainingCampId}</if>
            <if test="courseStyle != null  and courseStyle != ''"> and course_style = #{courseStyle}</if>
            <if test="liveRoomStyle != null  and liveRoomStyle != ''"> and live_room_style = #{liveRoomStyle}</if>
            <if test="redPacketGrantMethod != null "> and red_packet_grant_method = #{redPacketGrantMethod}</if>
            <if test="periodType != null "> and period_type = #{periodType}</if>
            <if test="periodStartingTime != null "> and period_starting_time = #{periodStartingTime}</if>
            <if test="periodEndTime != null "> and period_end_time = #{periodEndTime}</if>
        </where>
    </select>

    <select id="selectFsUserCoursePeriodPage" parameterType="FsUserCoursePeriod" resultType="FsUserCoursePeriodVO">
        SELECT
        fs_user_course_period.period_id,
        fs_user_course_period.period_name,
        fs_user_course_period.company_id,
        fs_user_course_period.training_camp_id,
        fs_user_course_period.create_time,
        fs_user_course_period.update_time,
        fs_user_course_period.period_status,
        course_style,
        live_room_style,
        red_packet_grant_method,
        period_type,
        period_end_time,
        period_starting_time,
        fctc.training_camp_name AS trainingCampName,
        GROUP_CONCAT( company.company_name ) AS companyName
        FROM
        fs_user_course_period
        left join fs_user_course_training_camp fctc on fctc.training_camp_id = fs_user_course_period.training_camp_id
        left join company on FIND_IN_SET(company.company_id, fs_user_course_period.company_id) > 0
        <where>
            <if test="trainingCampId != null"> and fs_user_course_period.training_camp_id = #{trainingCampId}</if>
            <if test="periodName != null  and periodName != ''"> and period_name like concat('%', #{periodName}, '%')</if>
            <if test="companyIdList != null and companyIdList.size() > 0 ">
                and fs_user_course_period.company_id in
                <foreach item="companyId" index="index" collection="companyIdList"
                         open="(" separator="," close=")">
                    #{companyId}
                </foreach>
            </if>
            <if test="periodStartingTime != null "> and period_starting_time &gt;= #{periodStartingTime}</if>
            <if test="periodEndTime != null "> and period_end_time &lt;= #{periodEndTime}</if>
        </where>
        group by fs_user_course_period.period_id
        order by create_time desc
    </select>

    <select id="selectFsUserCoursePeriodById" parameterType="Long" resultMap="FsUserCoursePeriodResult">
        <include refid="selectFsUserCoursePeriodVo"/>
        where period_id = #{periodId}
    </select>

    <insert id="insertFsUserCoursePeriod" parameterType="FsUserCoursePeriod" useGeneratedKeys="true" keyProperty="periodId">
        insert into fs_user_course_period
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="periodId != null">period_id,</if>
            <if test="periodName != null">period_name,</if>
            <if test="companyId != null">company_id,</if>
            <if test="trainingCampId != null">training_camp_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="courseStyle != null">course_style,</if>
            <if test="liveRoomStyle != null">live_room_style,</if>
            <if test="redPacketGrantMethod != null">red_packet_grant_method,</if>
            <if test="periodType != null">period_type,</if>
            <if test="periodStartingTime != null">period_starting_time,</if>
            <if test="periodEndTime != null">period_end_time,</if>
            <if test="viewStartTime != null">view_start_time,</if>
            <if test="viewEndTime != null">view_end_time,</if>
            <if test="lastJoinTime != null">last_join_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="periodId != null">#{periodId},</if>
            <if test="periodName != null">#{periodName},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="trainingCampId != null">#{trainingCampId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="courseStyle != null">#{courseStyle},</if>
            <if test="liveRoomStyle != null">#{liveRoomStyle},</if>
            <if test="redPacketGrantMethod != null">#{redPacketGrantMethod},</if>
            <if test="periodType != null">#{periodType},</if>
            <if test="periodStartingTime != null">#{periodStartingTime},</if>
            <if test="periodEndTime != null">#{periodEndTime},</if>
            <if test="viewStartTime != null">#{viewStartTime},</if>
            <if test="viewEndTime != null">#{viewEndTime},</if>
            <if test="lastJoinTime != null">#{lastJoinTime},</if>
        </trim>
    </insert>

    <update id="updateFsUserCoursePeriod" parameterType="FsUserCoursePeriod">
        update fs_user_course_period
        <trim prefix="SET" suffixOverrides=",">
            <if test="periodName != null">period_name = #{periodName},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="trainingCampId != null">training_camp_id = #{trainingCampId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="courseStyle != null">course_style = #{courseStyle},</if>
            <if test="liveRoomStyle != null">live_room_style = #{liveRoomStyle},</if>
            <if test="redPacketGrantMethod != null">red_packet_grant_method = #{redPacketGrantMethod},</if>
            <if test="periodType != null">period_type = #{periodType},</if>
            <if test="periodStartingTime != null">period_starting_time = #{periodStartingTime},</if>
            <if test="periodEndTime != null">period_end_time = #{periodEndTime},</if>
            <if test="viewStartTime != null">view_start_time = #{viewStartTime},</if>
            <if test="viewEndTime != null">view_end_time = #{viewEndTime},</if>
            <if test="lastJoinTime != null">last_join_time = #{lastJoinTime},</if>
        </trim>
        where period_id = #{periodId}
    </update>

    <delete id="deleteFsUserCoursePeriodById" parameterType="Long">
        delete from fs_user_course_period where period_id = #{periodId}
    </delete>

    <delete id="deleteFsUserCoursePeriodByIds" parameterType="String">
        delete from fs_user_course_period where period_id in
        <foreach item="periodId" collection="array" open="(" separator="," close=")">
            #{periodId}
        </foreach>
    </delete>

    <select id="selectCompanyByPeriodVideo" resultType="com.fs.course.param.CompanyRedPacketParam">
        SELECT
        company.company_id,
        company.company_name,
        ifnull ( fvrp.red_packet_money, 0 ) AS amount
        FROM
        company
        LEFT JOIN fs_user_course_video_red_package fvrp ON fvrp.company_id = company.company_id
        AND fvrp.period_id = #{periodId}
        <if test="videoId != null and videoId!='' ">
            and fvrp.video_id = #{videoId}
        </if>
        WHERE
        company.company_id IN (
        SELECT
        company.company_id
        FROM
        company
        LEFT JOIN fs_user_course_period fcp ON FIND_IN_SET ( company.company_id, fcp.company_id ) > 0
        WHERE
        fcp.period_id = #{periodId}
        )
    </select>

    <select id="selectCompanyByPeriod" resultType="com.fs.course.param.CompanyRedPacketParam">
        SELECT
            company.company_id,
            company.company_name
        FROM
            company
                LEFT JOIN fs_user_course_period fcp ON FIND_IN_SET ( company.company_id, fcp.company_id ) > 0
        where fcp.period_id = #{periodId}
    </select>

    <select id="selectPeriodRedPacket" resultType="com.fs.course.vo.PeriodRedPacketVO">
        SELECT
            DISTINCT a.*,
                     c.course_name,
                     cv.title videoName,
                     ifnull ( fvrp.red_packet_money, 0 ) AS amount
        FROM
            fs_user_course_period_days a
                LEFT JOIN fs_user_course_video cv ON a.video_id = cv.video_id
                LEFT JOIN fs_user_course c ON c.course_id = cv.course_id
                LEFT JOIN fs_user_course_video_red_package fvrp ON fvrp.period_id = a.period_id
                AND fvrp.video_id = a.video_id
                AND fvrp.period_id = #{periodId}
                AND fvrp.company_id = #{companyId}
                AND fvrp.data_type = 2
        WHERE
            a.video_id IN (
                SELECT
                    a.video_id
                FROM
                    fs_user_course_period_days a
                        LEFT JOIN fs_user_course_video cv ON a.video_id = cv.video_id
                        LEFT JOIN fs_user_course c ON c.course_id = cv.course_id
                WHERE
                    a.period_id = #{periodId}
            )
          and a.period_id = #{periodId}
    </select>

    <select id="selectFsUserCoursePeriodsByIds" resultType="FsUserCoursePeriod">
        select * from fs_user_course_period
        where period_id in
        <foreach collection="periodIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="countPeriodByCampIds" resultType="java.lang.Integer">
        select count(ucp.period_id)
        from fs_user_course_period ucp
        inner join fs_user_course_training_camp uctc on uctc.training_camp_id = ucp.training_camp_id
        where uctc.training_camp_id in
        <foreach collection="params.ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        <![CDATA[
        and ucp.period_starting_time <= #{params.date}
        and ucp.period_end_time >= #{params.date}
        ]]>
    </select>
</mapper>
