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

    <resultMap type="FsUserCourse" id="FsUserCourseResult">
        <result property="courseId"    column="course_id"    />
        <result property="cateId"    column="cate_id"    />
        <result property="subCateId"    column="sub_cate_id"    />
        <result property="courseName"    column="course_name"    />
        <result property="title"    column="title"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
        <result property="isVip"    column="is_vip"    />
        <result property="isHot"    column="is_hot"    />
        <result property="isShow"    column="is_show"    />
        <result property="views"    column="views"    />
        <result property="duration"    column="duration"    />
        <result property="description"    column="description"    />
        <result property="hotRanking"    column="hot_ranking"    />
        <result property="integral"    column="integral"    />
        <result property="price"    column="price"    />
        <result property="sellPrice"    column="sell_price"    />
        <result property="project"    column="project"    />
        <result property="tags"    column="tags"    />
        <result property="likes"    column="likes"    />
        <result property="favoriteNum"    column="favorite_num"    />
        <result property="shares"    column="shares"    />
        <result property="isAutoPlay"    column="is_auto_play"    />
        <result property="isFast"    column="is_fast"    />
        <result property="isBest"    column="is_best"    />
        <result property="isTui"    column="is_tui"    />
        <result property="hotNum"    column="hot_num"    />
        <result property="isIntegral"    column="is_integral"    />
        <result property="courseType"    column="course_type"    />
        <result property="talentId"    column="talent_id"    />
        <result property="isDel"    column="is_del"    />
        <result property="isNext"    column="is_next"    />
        <result property="isPrivate"    column="is_private"    />
        <result property="secondImg"    column="second_img"    />
        <result property="companyIds"    column="company_ids"    />

    </resultMap>

    <sql id="selectFsUserCourseVo">
        select course_id,is_private,company_ids,is_next,talent_id,second_img,is_del, cate_id,sub_cate_id, course_name, title, img_url, sort, create_time, update_time, status, is_vip, is_hot, is_show, views, duration, description, hot_ranking, integral, price, sell_price, project, tags, likes, favorite_num, shares, is_auto_play, is_fast, is_best, is_tui, hot_num, is_integral, course_type from fs_user_course
    </sql>

    <select id="selectFsUserCourseList" parameterType="FsUserCourse" resultMap="FsUserCourseResult">
        <include refid="selectFsUserCourseVo"/>
        <where>
            <if test="cateId != null "> and cate_id = #{cateId}</if>
            <if test="subCateId != null "> and sub_cate_id = #{subCateId}</if>
            <if test="courseName != null  and courseName != ''"> and course_name like concat('%', #{courseName}, '%')</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and img_url = #{imgUrl}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isVip != null "> and is_vip = #{isVip}</if>
            <if test="isHot != null "> and is_hot = #{isHot}</if>
            <if test="isShow != null "> and is_show = #{isShow}</if>
            <if test="views != null "> and views = #{views}</if>
            <if test="duration != null "> and duration = #{duration}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="hotRanking != null "> and hot_ranking = #{hotRanking}</if>
            <if test="integral != null "> and integral = #{integral}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="sellPrice != null "> and sell_price = #{sellPrice}</if>
            <if test="project != null "> and project = #{project}</if>
            <if test="tags != null  and tags != ''"> and tags = #{tags}</if>
            <if test="likes != null "> and likes = #{likes}</if>
            <if test="favoriteNum != null "> and favorite_num = #{favoriteNum}</if>
            <if test="shares != null "> and shares = #{shares}</if>
            <if test="isAutoPlay != null "> and is_auto_play = #{isAutoPlay}</if>
            <if test="isFast != null "> and is_fast = #{isFast}</if>
            <if test="isBest != null "> and is_best = #{isBest}</if>
            <if test="isTui != null "> and is_tui = #{isTui}</if>
            <if test="hotNum != null "> and hot_num = #{hotNum}</if>
            <if test="isIntegral != null "> and is_integral = #{isIntegral}</if>
            <if test="courseType != null "> and course_type = #{courseType}</if>
            <if test="talentId != null "> and talent_id = #{talentId}</if>
            <if test="isNext != null "> and is_next = #{isNext}</if>
            <if test="isPrivate != null "> and is_private = #{isPrivate}</if>
        </where>
    </select>

    <select id="selectFsUserCourseByCourseId" parameterType="Long" resultMap="FsUserCourseResult">
        <include refid="selectFsUserCourseVo"/>
        where course_id = #{courseId}
    </select>

    <insert id="insertFsUserCourse" parameterType="FsUserCourse" useGeneratedKeys="true" keyProperty="courseId">
        insert into fs_user_course
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cateId != null">cate_id,</if>
            <if test="subCateId != null">sub_cate_id,</if>
            <if test="courseName != null">course_name,</if>
            <if test="title != null">title,</if>
            <if test="imgUrl != null">img_url,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
            <if test="isVip != null">is_vip,</if>
            <if test="isHot != null">is_hot,</if>
            <if test="isShow != null">is_show,</if>
            <if test="views != null">views,</if>
            <if test="duration != null">duration,</if>
            <if test="description != null">description,</if>
            <if test="hotRanking != null">hot_ranking,</if>
            <if test="integral != null">integral,</if>
            <if test="price != null">price,</if>
            <if test="sellPrice != null">sell_price,</if>
            <if test="project != null">project,</if>
            <if test="tags != null">tags,</if>
            <if test="likes != null">likes,</if>
            <if test="favoriteNum != null">favorite_num,</if>
            <if test="shares != null">shares,</if>
            <if test="isAutoPlay != null">is_auto_play,</if>
            <if test="isFast != null">is_fast,</if>
            <if test="isBest != null">is_best,</if>
            <if test="isTui != null">is_tui,</if>
            <if test="hotNum != null">hot_num,</if>
            <if test="isIntegral != null">is_integral,</if>
            <if test="courseType != null">course_type,</if>
            <if test="talentId != null">talent_id,</if>
            <if test="isDel != null">is_del,</if>
            <if test="isNext != null">is_next,</if>
            <if test="isPrivate != null">is_private,</if>
            <if test="secondImg != null">second_img,</if>
            <if test="companyIds != null">company_ids,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cateId != null">#{cateId},</if>
            <if test="subCateId != null">#{subCateId},</if>
            <if test="courseName != null">#{courseName},</if>
            <if test="title != null">#{title},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            <if test="isVip != null">#{isVip},</if>
            <if test="isHot != null">#{isHot},</if>
            <if test="isShow != null">#{isShow},</if>
            <if test="views != null">#{views},</if>
            <if test="duration != null">#{duration},</if>
            <if test="description != null">#{description},</if>
            <if test="hotRanking != null">#{hotRanking},</if>
            <if test="integral != null">#{integral},</if>
            <if test="price != null">#{price},</if>
            <if test="sellPrice != null">#{sellPrice},</if>
            <if test="project != null">#{project},</if>
            <if test="tags != null">#{tags},</if>
            <if test="likes != null">#{likes},</if>
            <if test="favoriteNum != null">#{favoriteNum},</if>
            <if test="shares != null">#{shares},</if>
            <if test="isAutoPlay != null">#{isAutoPlay},</if>
            <if test="isFast != null">#{isFast},</if>
            <if test="isBest != null">#{isBest},</if>
            <if test="isTui != null">#{isTui},</if>
            <if test="hotNum != null">#{hotNum},</if>
            <if test="isIntegral != null">#{isIntegral},</if>
            <if test="courseType != null">#{courseType},</if>
            <if test="talentId != null">#{talentId},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="isNext != null">#{isNext},</if>
            <if test="isPrivate != null">#{isPrivate},</if>
            <if test="secondImg != null">#{secondImg},</if>
            <if test="companyIds != null">#{companyIds},</if>
         </trim>
    </insert>

    <update id="updateFsUserCourse" parameterType="FsUserCourse">
        update fs_user_course
        <trim prefix="SET" suffixOverrides=",">
            <if test="cateId != null">cate_id = #{cateId},</if>
            <if test="subCateId != null">sub_cate_id = #{subCateId},</if>
            <if test="courseName != null">course_name = #{courseName},</if>
            <if test="title != null">title = #{title},</if>
            <if test="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isVip != null">is_vip = #{isVip},</if>
            <if test="isHot != null">is_hot = #{isHot},</if>
            <if test="isShow != null">is_show = #{isShow},</if>
            <if test="views != null">views = #{views},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="description != null">description = #{description},</if>
            <if test="hotRanking != null">hot_ranking = #{hotRanking},</if>
            <if test="integral != null">integral = #{integral},</if>
            <if test="price != null">price = #{price},</if>
            <if test="sellPrice != null">sell_price = #{sellPrice},</if>
            <if test="project != null">project = #{project},</if>
            <if test="tags != null">tags = #{tags},</if>
            <if test="likes != null">likes = #{likes},</if>
            <if test="favoriteNum != null">favorite_num = #{favoriteNum},</if>
            <if test="shares != null">shares = #{shares},</if>
            <if test="isAutoPlay != null">is_auto_play = #{isAutoPlay},</if>
            <if test="isFast != null">is_fast = #{isFast},</if>
            <if test="isBest != null">is_best = #{isBest},</if>
            <if test="isTui != null">is_tui = #{isTui},</if>
            <if test="hotNum != null">hot_num = #{hotNum},</if>
            <if test="isIntegral != null">is_integral = #{isIntegral},</if>
            <if test="courseType != null">course_type = #{courseType},</if>
            <if test="talentId != null">talent_id = #{talentId},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="isNext != null">is_next = #{isNext},</if>
            <if test="isPrivate != null">is_private = #{isPrivate},</if>
            <if test="secondImg != null">second_img = #{secondImg},</if>
            <if test="companyIds != null">company_ids = #{companyIds},</if>
        </trim>
        where course_id = #{courseId}
    </update>

    <update id="deleteFsUserCourseByCourseId" parameterType="Long">
        update fs_user_course set is_del = 1 where course_id = #{courseId}
    </update>

    <update id="deleteFsUserCourseByCourseIds" parameterType="String">
        update fs_user_course set is_del = 1 where course_id in
        <foreach item="courseId" collection="array" open="(" separator="," close=")">
            #{courseId}
        </foreach>
    </update>

    <select id="getFsUserCourseList" resultType="FsUserCourseListVO">
        SELECT DISTINCT
        fcp.period_id,
        fcp.period_name
        FROM
        fs_user_course_period fcp
        LEFT JOIN fs_user_course_period_days fcpd ON fcpd.period_id = fcp.period_id
        LEFT JOIN fs_user_course c ON c.course_id = fcpd.course_id
        WHERE
        c.is_del = 0
        AND FIND_IN_SET(#{companyId}, fcp.company_id)
        <if test="keyword != null and keyword !='' ">
            AND fcp.period_name LIKE concat('%',#{keyword},'%'
            )
        </if>
        ORDER BY
        fcp.period_id
    </select>

    <!-- 查询用户参与记录 -->
    <select id="getParticipationRecordByMap" resultType="com.fs.course.vo.FsUserCourseParticipationRecordVO">
        select
            fu.user_id,
            fucv.video_id,
            fu.nickname as nickName,
            fu.username as userName,
            fu.avatar,
            fu.phone as phoneNumber,
            fu.create_time,
            max(fcwl.create_time) as watchDate
        from fs_user_course_video fucv
        inner join fs_course_watch_log fcwl on fcwl.video_id = fucv.video_id
        inner join fs_user fu on fu.user_id = fcwl.user_id
        where fucv.video_id = #{params.videoId}
        <if test="params.keyword != null and params.keyword != ''">
            and (
                fu.user_id = #{params.keyword}
                or fu.nickname like concat('%', #{params.keyword}, '%')
                or fu.username like concat('%', #{params.keyword}, '%')
                or fu.phone = #{params.keyword}
            )
        </if>
        <choose>
            <when test="params.type == 0">
                and (
                    select count(log_id) as count
                    from fs_course_answer_logs
                    where user_id = fu.user_id and fcwl.video_id = fucv.video_id and is_right = 1
                ) > 0
            </when>
            <when test="params.type == 1">
                and fcwl.log_type = 2
            </when>
            <when test="params.type == 2">
                and fcwl.log_type in (1,4)
            </when>
            <otherwise>
                and fcwl.log_type = 3
            </otherwise>
        </choose>
        group by fu.user_id, fucv.video_id, fu.nickname, fu.username, fu.avatar, fu.phone, fu.create_time
    </select>
    <select id="selectFsUserCourseListCompanyPVO" resultType="com.fs.course.vo.FsUserCourseListPVO">
        select c.*,cc.cate_name,ucc.cate_name as sub_cate_name from fs_user_course  c
        left join fs_user_course_category cc on c.cate_id=cc.cate_id
        left join fs_user_course_category ucc on  ucc.cate_id = c.sub_cate_id
        where c.is_del = 0 and
        FIND_IN_SET(#{maps.companyId}, company_ids)
        <if test = ' maps.cateId !=null '>
            and (cc.cate_id =#{maps.cateId} or cc.pid=#{maps.cateId} )
        </if>
        <if test = ' maps.courseName!=null and maps.courseName != "" '>
            and c.course_name like concat('%', #{maps.courseName}, '%')
        </if>
        <if test = ' maps.isPrivate !=null '>
            and c.is_private = #{maps.isPrivate}
        </if>
        <if test = ' maps.isShow !=null '>
            and c.is_show = #{maps.isShow}
        </if>
        <if test="maps.project != null">
            and c.project = #{maps.project}
        </if>
        order by c.course_id
    </select>
</mapper>
